xref: /aosp_15_r20/external/tink/testing/cc/mac_impl.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2020 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 //
15 ///////////////////////////////////////////////////////////////////////////////
16 
17 // Implementation of a MAC Service.
18 #include "mac_impl.h"
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "tink/mac.h"
25 #include "create.h"
26 #include "proto/testing_api.grpc.pb.h"
27 
28 namespace tink_testing_api {
29 
30 using ::crypto::tink::util::StatusOr;
31 
Create(grpc::ServerContext * context,const CreationRequest * request,CreationResponse * response)32 ::grpc::Status MacImpl::Create(grpc::ServerContext* context,
33                                const CreationRequest* request,
34                                CreationResponse* response) {
35   return CreatePrimitiveForRpc<crypto::tink::Mac>(request, response);
36 }
37 
38 // Computes a MAC
ComputeMac(grpc::ServerContext * context,const ComputeMacRequest * request,ComputeMacResponse * response)39 ::grpc::Status MacImpl::ComputeMac(grpc::ServerContext* context,
40                                    const ComputeMacRequest* request,
41                                    ComputeMacResponse* response) {
42   StatusOr<std::unique_ptr<crypto::tink::Mac>> mac_result =
43       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Mac>(
44           request->annotated_keyset());
45   if (!mac_result.ok()) {
46     response->set_err(std::string(mac_result.status().message()));
47     return ::grpc::Status::OK;
48   }
49   auto compute_result = mac_result.value()->ComputeMac(request->data());
50   if (!compute_result.ok()) {
51     response->set_err(std::string(compute_result.status().message()));
52     return ::grpc::Status::OK;
53   }
54   response->set_mac_value(compute_result.value());
55   return ::grpc::Status::OK;
56 }
57 
58 // Verifies a MAC
VerifyMac(grpc::ServerContext * context,const VerifyMacRequest * request,VerifyMacResponse * response)59 ::grpc::Status MacImpl::VerifyMac(grpc::ServerContext* context,
60                                   const VerifyMacRequest* request,
61                                   VerifyMacResponse* response) {
62   StatusOr<std::unique_ptr<crypto::tink::Mac>> mac_result =
63       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Mac>(
64           request->annotated_keyset());
65   if (!mac_result.ok()) {
66     response->set_err(std::string(mac_result.status().message()));
67     return ::grpc::Status::OK;
68   }
69   auto status =
70       mac_result.value()->VerifyMac(request->mac_value(), request->data());
71   if (!status.ok()) {
72     response->set_err(std::string(status.message()));
73     return ::grpc::Status::OK;
74   }
75   return ::grpc::Status::OK;
76 }
77 
78 }  // namespace tink_testing_api
79