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 an Deterministic AEAD Service.
18 #include "deterministic_aead_impl.h"
19
20 #include <memory>
21 #include <string>
22 #include <utility>
23
24 #include "tink/deterministic_aead.h"
25 #include "create.h"
26
27 namespace tink_testing_api {
28
29 using ::crypto::tink::util::StatusOr;
30
Create(grpc::ServerContext * context,const CreationRequest * request,CreationResponse * response)31 grpc::Status DeterministicAeadImpl::Create(grpc::ServerContext* context,
32 const CreationRequest* request,
33 CreationResponse* response) {
34 return CreatePrimitiveForRpc<crypto::tink::DeterministicAead>(request,
35 response);
36 }
37
EncryptDeterministically(grpc::ServerContext * context,const DeterministicAeadEncryptRequest * request,DeterministicAeadEncryptResponse * response)38 grpc::Status DeterministicAeadImpl::EncryptDeterministically(
39 grpc::ServerContext* context,
40 const DeterministicAeadEncryptRequest* request,
41 DeterministicAeadEncryptResponse* response) {
42 StatusOr<std::unique_ptr<crypto::tink::DeterministicAead>> daead =
43 PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::DeterministicAead>(
44 request->annotated_keyset());
45 if (!daead.ok()) {
46 return grpc::Status(
47 grpc::StatusCode::FAILED_PRECONDITION,
48 absl::StrCat("Creating primitive failed: ", daead.status().message()));
49 }
50
51 StatusOr<std::string> ciphertext = (*daead)->EncryptDeterministically(
52 request->plaintext(), request->associated_data());
53 if (!ciphertext.ok()) {
54 response->set_err(std::string(ciphertext.status().message()));
55 return grpc::Status::OK;
56 }
57 response->set_ciphertext(*ciphertext);
58 return grpc::Status::OK;
59 }
60
DecryptDeterministically(grpc::ServerContext * context,const DeterministicAeadDecryptRequest * request,DeterministicAeadDecryptResponse * response)61 grpc::Status DeterministicAeadImpl::DecryptDeterministically(
62 grpc::ServerContext* context,
63 const DeterministicAeadDecryptRequest* request,
64 DeterministicAeadDecryptResponse* response) {
65 StatusOr<std::unique_ptr<crypto::tink::DeterministicAead>> daead =
66 PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::DeterministicAead>(
67 request->annotated_keyset());
68 if (!daead.ok()) {
69 return grpc::Status(
70 grpc::StatusCode::FAILED_PRECONDITION,
71 absl::StrCat("Creating primitive failed: ", daead.status().message()));
72 }
73 StatusOr<std::string> plaintext = (*daead)->DecryptDeterministically(
74 request->ciphertext(), request->associated_data());
75 if (!plaintext.ok()) {
76 response->set_err(std::string(plaintext.status().message()));
77 return grpc::Status::OK;
78 }
79 response->set_plaintext(*plaintext);
80 return grpc::Status::OK;
81 }
82
83 } // namespace tink_testing_api
84