xref: /aosp_15_r20/external/tink/testing/cc/streaming_aead_impl_test.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 #include "streaming_aead_impl.h"
18 
19 #include <ostream>
20 #include <sstream>
21 #include <string>
22 
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "tink/binary_keyset_writer.h"
26 #include "tink/cleartext_keyset_handle.h"
27 #include "tink/streamingaead/streaming_aead_config.h"
28 #include "tink/streamingaead/streaming_aead_key_templates.h"
29 #include "proto/testing_api.grpc.pb.h"
30 
31 namespace crypto {
32 namespace tink {
33 namespace {
34 
35 using ::crypto::tink::StreamingAeadKeyTemplates;
36 using ::crypto::tink::BinaryKeysetWriter;
37 using ::crypto::tink::CleartextKeysetHandle;
38 
39 using ::testing::Eq;
40 using ::testing::IsEmpty;
41 using ::tink_testing_api::CreationRequest;
42 using ::tink_testing_api::CreationResponse;
43 using ::tink_testing_api::StreamingAeadDecryptRequest;
44 using ::tink_testing_api::StreamingAeadDecryptResponse;
45 using ::tink_testing_api::StreamingAeadEncryptRequest;
46 using ::tink_testing_api::StreamingAeadEncryptResponse;
47 
48 using crypto::tink::KeysetHandle;
49 using google::crypto::tink::KeyTemplate;
50 
ValidKeyset()51 std::string ValidKeyset() {
52   const KeyTemplate& key_template =
53       StreamingAeadKeyTemplates::Aes128GcmHkdf4KB();
54   auto handle_result = KeysetHandle::GenerateNew(key_template);
55   EXPECT_TRUE(handle_result.ok());
56   std::stringbuf keyset;
57   auto writer_result =
58       BinaryKeysetWriter::New(absl::make_unique<std::ostream>(&keyset));
59   EXPECT_TRUE(writer_result.ok());
60 
61   auto status = CleartextKeysetHandle::Write(writer_result.value().get(),
62                                              *handle_result.value());
63   EXPECT_TRUE(status.ok());
64   return keyset.str();
65 }
66 
67 class StreamingAeadImplTest : public ::testing::Test {
68  protected:
SetUpTestSuite()69   static void SetUpTestSuite() {
70     ASSERT_TRUE(StreamingAeadConfig::Register().ok());
71   }
72 };
73 
TEST_F(StreamingAeadImplTest,CreateSuccess)74 TEST_F(StreamingAeadImplTest, CreateSuccess) {
75   tink_testing_api::StreamingAeadImpl streaming_aead;
76   std::string keyset = ValidKeyset();
77   CreationRequest request;
78   request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
79   CreationResponse response;
80 
81   EXPECT_TRUE(streaming_aead.Create(nullptr, &request, &response).ok());
82   EXPECT_THAT(response.err(), IsEmpty());
83 }
84 
TEST_F(StreamingAeadImplTest,CreateFails)85 TEST_F(StreamingAeadImplTest, CreateFails) {
86   tink_testing_api::StreamingAeadImpl streaming_aead;
87   CreationRequest request;
88   request.mutable_annotated_keyset()->set_serialized_keyset("bad keyset");
89   CreationResponse response;
90 
91   EXPECT_TRUE(streaming_aead.Create(nullptr, &request, &response).ok());
92   EXPECT_THAT(response.err(), Not(IsEmpty()));
93 }
94 
95 
TEST_F(StreamingAeadImplTest,EncryptDecryptSuccess)96 TEST_F(StreamingAeadImplTest, EncryptDecryptSuccess) {
97   tink_testing_api::StreamingAeadImpl streaming_aead;
98   std::string keyset = ValidKeyset();
99   StreamingAeadEncryptRequest enc_request;
100   enc_request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
101   enc_request.set_plaintext("Plain text");
102   enc_request.set_associated_data("ad");
103   StreamingAeadEncryptResponse enc_response;
104 
105   EXPECT_TRUE(streaming_aead.Encrypt(nullptr, &enc_request,
106                                      &enc_response).ok());
107   EXPECT_THAT(enc_response.err(), IsEmpty());
108 
109   StreamingAeadDecryptRequest dec_request;
110   dec_request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
111   dec_request.set_ciphertext(enc_response.ciphertext());
112   dec_request.set_associated_data("ad");
113   StreamingAeadDecryptResponse dec_response;
114 
115   EXPECT_TRUE(streaming_aead.Decrypt(nullptr, &dec_request,
116                                      &dec_response).ok());
117   EXPECT_THAT(dec_response.err(), IsEmpty());
118   EXPECT_THAT(dec_response.plaintext(), Eq("Plain text"));
119 }
120 
TEST_F(StreamingAeadImplTest,EncryptBadKeysetFail)121 TEST_F(StreamingAeadImplTest, EncryptBadKeysetFail) {
122   tink_testing_api::StreamingAeadImpl streaming_aead;
123   StreamingAeadEncryptRequest enc_request;
124   enc_request.mutable_annotated_keyset()->set_serialized_keyset("bad keyset");
125   enc_request.set_plaintext("Plain text");
126   enc_request.set_associated_data("ad");
127   StreamingAeadEncryptResponse enc_response;
128 
129   EXPECT_TRUE(streaming_aead.Encrypt(nullptr, &enc_request,
130                                      &enc_response).ok());
131   EXPECT_THAT(enc_response.err(), Not(IsEmpty()));
132 }
133 
TEST_F(StreamingAeadImplTest,DecryptBadCiphertextFail)134 TEST_F(StreamingAeadImplTest, DecryptBadCiphertextFail) {
135   tink_testing_api::StreamingAeadImpl streaming_aead;
136   std::string keyset = ValidKeyset();
137   StreamingAeadDecryptRequest dec_request;
138   dec_request.mutable_annotated_keyset()->set_serialized_keyset(keyset);
139   dec_request.set_ciphertext("bad ciphertext");
140   dec_request.set_associated_data("ad");
141   StreamingAeadDecryptResponse dec_response;
142 
143   EXPECT_TRUE(streaming_aead.Decrypt(nullptr, &dec_request,
144                                      &dec_response).ok());
145   EXPECT_THAT(dec_response.err(), Not(IsEmpty()));
146 }
147 
148 }  // namespace
149 }  // namespace tink
150 }  // namespace crypto
151