xref: /aosp_15_r20/external/tink/cc/daead/deterministic_aead_factory_test.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
1 // Copyright 2018 Google Inc.
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 "tink/daead/deterministic_aead_factory.h"
17 
18 #include <string>
19 #include <utility>
20 
21 #include "gtest/gtest.h"
22 #include "tink/core/key_manager_impl.h"
23 #include "tink/crypto_format.h"
24 #include "tink/daead/aes_siv_key_manager.h"
25 #include "tink/daead/deterministic_aead_config.h"
26 #include "tink/deterministic_aead.h"
27 #include "tink/internal/key_info.h"
28 #include "tink/keyset_handle.h"
29 #include "tink/util/status.h"
30 #include "tink/util/test_keyset_handle.h"
31 #include "tink/util/test_util.h"
32 #include "proto/aes_siv.pb.h"
33 
34 using crypto::tink::test::AddRawKey;
35 using crypto::tink::test::AddTinkKey;
36 using google::crypto::tink::AesSivKeyFormat;
37 using google::crypto::tink::KeyData;
38 using google::crypto::tink::Keyset;
39 using google::crypto::tink::KeyStatusType;
40 
41 
42 namespace crypto {
43 namespace tink {
44 namespace {
45 
46 class DeterministicAeadFactoryTest : public ::testing::Test {};
47 
TEST_F(DeterministicAeadFactoryTest,testBasic)48 TEST_F(DeterministicAeadFactoryTest, testBasic) {
49   Keyset keyset;
50   auto daead_result = DeterministicAeadFactory::GetPrimitive(
51       *TestKeysetHandle::GetKeysetHandle(keyset));
52   EXPECT_FALSE(daead_result.ok());
53   EXPECT_EQ(absl::StatusCode::kInvalidArgument, daead_result.status().code());
54   EXPECT_PRED_FORMAT2(testing::IsSubstring, "at least one key",
55                       std::string(daead_result.status().message()));
56 }
57 
TEST_F(DeterministicAeadFactoryTest,testPrimitive)58 TEST_F(DeterministicAeadFactoryTest, testPrimitive) {
59   // Prepare a template for generating keys for a Keyset.
60   AesSivKeyManager key_type_manager;
61   auto key_manager =
62       internal::MakeKeyManager<DeterministicAead>(&key_type_manager);
63   const KeyFactory& key_factory = key_manager->get_key_factory();
64   std::string key_type = key_manager->get_key_type();
65 
66   AesSivKeyFormat key_format;
67   key_format.set_key_size(64);
68 
69   // Prepare a Keyset.
70   Keyset keyset;
71   uint32_t key_id_1 = 1234543;
72   auto new_key = std::move(key_factory.NewKey(key_format).value());
73   AddTinkKey(key_type, key_id_1, *new_key, KeyStatusType::ENABLED,
74              KeyData::SYMMETRIC, &keyset);
75 
76   uint32_t key_id_2 = 726329;
77   new_key = std::move(key_factory.NewKey(key_format).value());
78   AddRawKey(key_type, key_id_2, *new_key, KeyStatusType::ENABLED,
79             KeyData::SYMMETRIC, &keyset);
80 
81   uint32_t key_id_3 = 7213743;
82   new_key = std::move(key_factory.NewKey(key_format).value());
83   AddTinkKey(key_type, key_id_3, *new_key, KeyStatusType::ENABLED,
84              KeyData::SYMMETRIC, &keyset);
85 
86   keyset.set_primary_key_id(key_id_3);
87 
88   // Initialize the registry.
89   ASSERT_TRUE(DeterministicAeadConfig::Register().ok());
90 
91   // Create a KeysetHandle and use it with the factory.
92   auto daead_result = DeterministicAeadFactory::GetPrimitive(
93       *TestKeysetHandle::GetKeysetHandle(keyset));
94   EXPECT_TRUE(daead_result.ok()) << daead_result.status();
95   auto daead = std::move(daead_result.value());
96 
97   // Test the resulting DeterministicAead-instance.
98   std::string plaintext = "some_plaintext";
99   std::string aad = "some_aad";
100 
101   auto encrypt_result = daead->EncryptDeterministically(plaintext, aad);
102   EXPECT_TRUE(encrypt_result.ok()) << encrypt_result.status();
103   std::string ciphertext = encrypt_result.value();
104   std::string prefix =
105       CryptoFormat::GetOutputPrefix(KeyInfoFromKey(keyset.key(2))).value();
106   EXPECT_PRED_FORMAT2(testing::IsSubstring, prefix, ciphertext);
107 
108   auto decrypt_result = daead->DecryptDeterministically(ciphertext, aad);
109   EXPECT_TRUE(decrypt_result.ok()) << decrypt_result.status();
110   EXPECT_EQ(plaintext, decrypt_result.value());
111 
112   decrypt_result = daead->DecryptDeterministically("some bad ciphertext", aad);
113   EXPECT_FALSE(decrypt_result.ok());
114   EXPECT_EQ(absl::StatusCode::kInvalidArgument,
115             decrypt_result.status().code());
116   EXPECT_PRED_FORMAT2(testing::IsSubstring, "decryption failed",
117                       std::string(decrypt_result.status().message()));
118 
119   // Create raw ciphertext with 2nd key, and decrypt
120   // with DeterministicAead-instance.
121   auto raw_daead =
122       std::move(key_manager->GetPrimitive(keyset.key(1).key_data()).value());
123   std::string raw_ciphertext =
124       raw_daead->EncryptDeterministically(plaintext, aad).value();
125   decrypt_result = daead->DecryptDeterministically(ciphertext, aad);
126   EXPECT_TRUE(decrypt_result.ok()) << decrypt_result.status();
127   EXPECT_EQ(plaintext, decrypt_result.value());
128 }
129 
130 }  // namespace
131 }  // namespace tink
132 }  // namespace crypto
133