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 PrfSet Service.
18 #include "prf_set_impl.h"
19
20 #include <memory>
21 #include <string>
22 #include <utility>
23
24 #include "tink/prf/prf_set.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 using ::grpc::ServerContext;
32
Create(grpc::ServerContext * context,const CreationRequest * request,CreationResponse * response)33 ::grpc::Status PrfSetImpl::Create(grpc::ServerContext* context,
34 const CreationRequest* request,
35 CreationResponse* response) {
36 return CreatePrimitiveForRpc<crypto::tink::PrfSet>(request, response);
37 }
38
39 // Returns the Key Ids of the Keyset.
KeyIds(ServerContext * context,const PrfSetKeyIdsRequest * request,PrfSetKeyIdsResponse * response)40 ::grpc::Status PrfSetImpl::KeyIds(ServerContext* context,
41 const PrfSetKeyIdsRequest* request,
42 PrfSetKeyIdsResponse* response) {
43 StatusOr<std::unique_ptr<crypto::tink::PrfSet>> prf_set_result =
44 PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::PrfSet>(
45 request->annotated_keyset());
46 if (!prf_set_result.ok()) {
47 response->set_err(std::string(prf_set_result.status().message()));
48 return ::grpc::Status::OK;
49 }
50 auto* output = response->mutable_output();
51 output->set_primary_key_id(prf_set_result.value()->GetPrimaryId());
52 for (auto const& item : prf_set_result.value()->GetPrfs()) {
53 output->add_key_id(item.first);
54 }
55 return ::grpc::Status::OK;
56 }
57
58 // Computes the output of one PRF.
Compute(ServerContext * context,const PrfSetComputeRequest * request,PrfSetComputeResponse * response)59 ::grpc::Status PrfSetImpl::Compute(ServerContext* context,
60 const PrfSetComputeRequest* request,
61 PrfSetComputeResponse* response) {
62 StatusOr<std::unique_ptr<crypto::tink::PrfSet>> prf_set_result =
63 PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::PrfSet>(
64 request->annotated_keyset());
65 if (!prf_set_result.ok()) {
66 response->set_err(std::string(prf_set_result.status().message()));
67 return ::grpc::Status::OK;
68 }
69 auto prfs = prf_set_result.value()->GetPrfs();
70 auto prf_it = prfs.find(request->key_id());
71 if (prf_it == prfs.end()) {
72 response->set_err("Unknown key ID.");
73 return ::grpc::Status::OK;
74 }
75 auto compute_result =
76 prf_it->second->Compute(request->input_data(), request->output_length());
77 if (!compute_result.ok()) {
78 response->set_err(std::string(compute_result.status().message()));
79 return ::grpc::Status::OK;
80 }
81 response->set_output(compute_result.value());
82 return ::grpc::Status::OK;
83 }
84
85 } // namespace tink_testing_api
86