1 // Copyright 2022 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 #include "walkthrough/write_cleartext_keyset.h"
17
18 #include <memory>
19 #include <ostream>
20 #include <sstream>
21 #include <string>
22
23 #include "gmock/gmock.h"
24 #include "gtest/gtest.h"
25 #include "absl/strings/string_view.h"
26 #include "tink/aead.h"
27 #include "tink/aead/aead_config.h"
28 #include "walkthrough/load_cleartext_keyset.h"
29 #include "tink/util/test_matchers.h"
30
31 namespace tink_walkthrough {
32 namespace {
33
34 using ::crypto::tink::Aead;
35 using ::crypto::tink::KeysetHandle;
36 using ::crypto::tink::test::IsOk;
37 using ::crypto::tink::test::IsOkAndHolds;
38 using ::crypto::tink::util::StatusOr;
39
40 constexpr absl::string_view kSerializedKeyset = R"string({
41 "key": [
42 {
43 "keyData": {
44 "keyMaterialType": "SYMMETRIC",
45 "typeUrl": "type.googleapis.com/google.crypto.tink.AesGcmKey",
46 "value": "GiBWyUfGgYk3RTRhj/LIUzSudIWlyjCftCOypTr0jCNSLg=="
47 },
48 "keyId": 294406504,
49 "outputPrefixType": "TINK",
50 "status": "ENABLED"
51 }
52 ],
53 "primaryKeyId": 294406504
54 })string";
55
TEST(WriteCleartextKeysetTest,WriteKeysetSerializesCorrectly)56 TEST(WriteCleartextKeysetTest, WriteKeysetSerializesCorrectly) {
57 ASSERT_THAT(crypto::tink::AeadConfig::Register(), IsOk());
58 StatusOr<std::unique_ptr<KeysetHandle>> keyset =
59 LoadKeyset(kSerializedKeyset);
60
61 std::stringbuf buffer;
62 auto output_stream = absl::make_unique<std::ostream>(&buffer);
63 ASSERT_THAT(WriteKeyset(**keyset, std::move(output_stream)), IsOk());
64
65 StatusOr<std::unique_ptr<Aead>> aead = (*keyset)->GetPrimitive<Aead>();
66
67 // Make sure the encrypted keyset was written correctly by loading it and
68 // trying to decrypt ciphertext.
69 StatusOr<std::unique_ptr<KeysetHandle>> loaded_keyset =
70 LoadKeyset(buffer.str());
71 ASSERT_THAT(loaded_keyset, IsOk());
72 StatusOr<std::unique_ptr<Aead>> loaded_keyset_aead =
73 (*loaded_keyset)->GetPrimitive<Aead>();
74 ASSERT_THAT(loaded_keyset_aead, IsOk());
75
76 constexpr absl::string_view kPlaintext = "Some plaintext";
77 constexpr absl::string_view kAssociatedData = "Some associated data";
78
79 StatusOr<std::string> ciphertext =
80 (*aead)->Encrypt(kPlaintext, kAssociatedData);
81 EXPECT_THAT((*loaded_keyset_aead)->Decrypt(*ciphertext, kAssociatedData),
82 IsOkAndHolds(kPlaintext));
83 ciphertext = (*loaded_keyset_aead)->Encrypt(kPlaintext, kAssociatedData);
84 EXPECT_THAT((*aead)->Decrypt(*ciphertext, kAssociatedData),
85 IsOkAndHolds(kPlaintext));
86 }
87
88 } // namespace
89 } // namespace tink_walkthrough
90