• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 AEAD Service.
18 #include "aead_impl.h"
19 
20 #include <memory>
21 #include <string>
22 #include <utility>
23 
24 #include "tink/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 AeadImpl::Create(grpc::ServerContext* context,
32                                 const CreationRequest* request,
33                                 CreationResponse* response) {
34   return CreatePrimitiveForRpc<crypto::tink::Aead>(request, response);
35 }
36 
Encrypt(grpc::ServerContext * context,const AeadEncryptRequest * request,AeadEncryptResponse * response)37 ::grpc::Status AeadImpl::Encrypt(grpc::ServerContext* context,
38                                  const AeadEncryptRequest* request,
39                                  AeadEncryptResponse* response) {
40   StatusOr<std::unique_ptr<crypto::tink::Aead>> aead =
41       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Aead>(
42           request->annotated_keyset());
43   if (!aead.ok()) {
44     return grpc::Status(
45         grpc::StatusCode::FAILED_PRECONDITION,
46         absl::StrCat("Creating primitive failed: ", aead.status().message()));
47   }
48 
49   StatusOr<std::string> ciphertext =
50       (*aead)->Encrypt(request->plaintext(), request->associated_data());
51   if (!ciphertext.ok()) {
52     response->set_err(std::string(ciphertext.status().message()));
53     return grpc::Status::OK;
54   }
55   response->set_ciphertext(*ciphertext);
56   return grpc::Status::OK;
57 }
58 
Decrypt(grpc::ServerContext * context,const AeadDecryptRequest * request,AeadDecryptResponse * response)59 grpc::Status AeadImpl::Decrypt(grpc::ServerContext* context,
60                                  const AeadDecryptRequest* request,
61                                  AeadDecryptResponse* response) {
62   StatusOr<std::unique_ptr<crypto::tink::Aead>> aead =
63       PrimitiveFromSerializedBinaryProtoKeyset<crypto::tink::Aead>(
64           request->annotated_keyset());
65   if (!aead.ok()) {
66     return grpc::Status(
67         grpc::StatusCode::FAILED_PRECONDITION,
68         absl::StrCat("Creating primitive failed: ", aead.status().message()));
69   }
70 
71   StatusOr<std::string> plaintext =
72       (*aead)->Decrypt(request->ciphertext(), request->associated_data());
73   if (!plaintext.ok()) {
74     response->set_err(std::string(plaintext.status().message()));
75     return grpc::Status::OK;
76   }
77   response->set_plaintext(*plaintext);
78   return grpc::Status::OK;
79 }
80 
81 }  // namespace tink_testing_api
82