1 // Copyright 2023 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 "tink/config/v0.h"
18
19 #include <memory>
20 #include <string>
21
22 #include "gmock/gmock.h"
23 #include "gtest/gtest.h"
24 #include "tink/aead.h"
25 #include "tink/aead/aead_key_templates.h"
26 #include "tink/aead/aes_ctr_hmac_aead_key_manager.h"
27 #include "tink/aead/aes_eax_key_manager.h"
28 #include "tink/aead/aes_gcm_key_manager.h"
29 #include "tink/aead/aes_gcm_siv_key_manager.h"
30 #include "tink/aead/xchacha20_poly1305_key_manager.h"
31 #include "tink/chunked_mac.h"
32 #include "tink/config/key_gen_v0.h"
33 #include "tink/configuration.h"
34 #include "tink/daead/aes_siv_key_manager.h"
35 #include "tink/deterministic_aead.h"
36 #include "tink/hybrid/ecies_aead_hkdf_public_key_manager.h"
37 #include "tink/hybrid/internal/hpke_public_key_manager.h"
38 #include "tink/hybrid_decrypt.h"
39 #include "tink/hybrid_encrypt.h"
40 #include "tink/internal/configuration_impl.h"
41 #include "tink/internal/keyset_wrapper_store.h"
42 #include "tink/keyset_handle.h"
43 #include "tink/mac.h"
44 #include "tink/mac/aes_cmac_key_manager.h"
45 #include "tink/mac/hmac_key_manager.h"
46 #include "tink/prf/aes_cmac_prf_key_manager.h"
47 #include "tink/prf/hkdf_prf_key_manager.h"
48 #include "tink/prf/hmac_prf_key_manager.h"
49 #include "tink/prf/prf_set.h"
50 #include "tink/public_key_sign.h"
51 #include "tink/public_key_verify.h"
52 #include "tink/signature/ecdsa_verify_key_manager.h"
53 #include "tink/signature/ed25519_verify_key_manager.h"
54 #include "tink/signature/rsa_ssa_pkcs1_verify_key_manager.h"
55 #include "tink/signature/rsa_ssa_pss_verify_key_manager.h"
56 #include "tink/streaming_aead.h"
57 #include "tink/streamingaead/aes_ctr_hmac_streaming_key_manager.h"
58 #include "tink/streamingaead/aes_gcm_hkdf_streaming_key_manager.h"
59 #include "tink/util/test_matchers.h"
60
61 namespace crypto {
62 namespace tink {
63 namespace {
64
65 using ::crypto::tink::test::IsOk;
66 using ::crypto::tink::test::IsOkAndHolds;
67
TEST(V0Test,PrimitiveWrappers)68 TEST(V0Test, PrimitiveWrappers) {
69 util::StatusOr<const internal::KeysetWrapperStore*> store =
70 internal::ConfigurationImpl::GetKeysetWrapperStore(ConfigV0());
71 ASSERT_THAT(store, IsOk());
72
73 EXPECT_THAT((*store)->Get<Mac>(), IsOk());
74 EXPECT_THAT((*store)->Get<ChunkedMac>(), IsOk());
75 EXPECT_THAT((*store)->Get<Aead>(), IsOk());
76 EXPECT_THAT((*store)->Get<DeterministicAead>(), IsOk());
77 EXPECT_THAT((*store)->Get<StreamingAead>(), IsOk());
78 EXPECT_THAT((*store)->Get<HybridEncrypt>(), IsOk());
79 EXPECT_THAT((*store)->Get<HybridDecrypt>(), IsOk());
80 EXPECT_THAT((*store)->Get<PrfSet>(), IsOk());
81 EXPECT_THAT((*store)->Get<PublicKeySign>(), IsOk());
82 EXPECT_THAT((*store)->Get<PublicKeyVerify>(), IsOk());
83 }
84
TEST(V0Test,KeyManagers)85 TEST(V0Test, KeyManagers) {
86 util::StatusOr<const internal::KeyTypeInfoStore*> store =
87 internal::ConfigurationImpl::GetKeyTypeInfoStore(ConfigV0());
88 ASSERT_THAT(store, IsOk());
89
90 EXPECT_THAT((*store)->Get(HmacKeyManager().get_key_type()), IsOk());
91 EXPECT_THAT((*store)->Get(AesCmacKeyManager().get_key_type()), IsOk());
92 EXPECT_THAT((*store)->Get(AesCtrHmacAeadKeyManager().get_key_type()), IsOk());
93 EXPECT_THAT((*store)->Get(AesGcmKeyManager().get_key_type()), IsOk());
94 EXPECT_THAT((*store)->Get(AesGcmSivKeyManager().get_key_type()), IsOk());
95 EXPECT_THAT((*store)->Get(AesEaxKeyManager().get_key_type()), IsOk());
96 EXPECT_THAT((*store)->Get(XChaCha20Poly1305KeyManager().get_key_type()),
97 IsOk());
98 EXPECT_THAT((*store)->Get(AesSivKeyManager().get_key_type()), IsOk());
99 EXPECT_THAT((*store)->Get(AesGcmHkdfStreamingKeyManager().get_key_type()),
100 IsOk());
101 EXPECT_THAT((*store)->Get(AesCtrHmacStreamingKeyManager().get_key_type()),
102 IsOk());
103 EXPECT_THAT((*store)->Get(EciesAeadHkdfPublicKeyManager().get_key_type()),
104 IsOk());
105 EXPECT_THAT((*store)->Get(internal::HpkePublicKeyManager().get_key_type()),
106 IsOk());
107 EXPECT_THAT((*store)->Get(HmacPrfKeyManager().get_key_type()), IsOk());
108 EXPECT_THAT((*store)->Get(HkdfPrfKeyManager().get_key_type()), IsOk());
109 EXPECT_THAT((*store)->Get(AesCmacPrfKeyManager().get_key_type()), IsOk());
110 EXPECT_THAT((*store)->Get(EcdsaVerifyKeyManager().get_key_type()), IsOk());
111 EXPECT_THAT((*store)->Get(RsaSsaPssVerifyKeyManager().get_key_type()),
112 IsOk());
113 EXPECT_THAT((*store)->Get(RsaSsaPkcs1VerifyKeyManager().get_key_type()),
114 IsOk());
115 EXPECT_THAT((*store)->Get(Ed25519VerifyKeyManager().get_key_type()), IsOk());
116 }
117
TEST(V0Test,GetPrimitive)118 TEST(V0Test, GetPrimitive) {
119 util::StatusOr<std::unique_ptr<KeysetHandle>> handle =
120 KeysetHandle::GenerateNew(AeadKeyTemplates::Aes128Gcm(),
121 KeyGenConfigV0());
122 ASSERT_THAT(handle, IsOk());
123
124 util::StatusOr<std::unique_ptr<Aead>> aead =
125 (*handle)->GetPrimitive<Aead>(ConfigV0());
126 ASSERT_THAT(aead, IsOk());
127
128 std::string plaintext = "plaintext";
129 std::string ad = "ad";
130 util::StatusOr<std::string> ciphertext = (*aead)->Encrypt(plaintext, ad);
131 ASSERT_THAT(ciphertext, IsOk());
132 EXPECT_THAT((*aead)->Decrypt(*ciphertext, ad), IsOkAndHolds(plaintext));
133 }
134
135 } // namespace
136 } // namespace tink
137 } // namespace crypto
138