1 // Copyright 2021 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/experimental/pqcrypto/kem/util/test_util.h"
18
19 #include <vector>
20
21 #include "gtest/gtest.h"
22 #include "tink/aead/aes_gcm_key_manager.h"
23 #include "tink/hybrid_encrypt.h"
24 #include "tink/registry.h"
25 #include "tink/util/status.h"
26 #include "tink/util/statusor.h"
27 #include "tink/util/test_matchers.h"
28 #include "tink/util/test_util.h"
29 #include "proto/aes_eax.pb.h"
30 #include "proto/common.pb.h"
31 #include "proto/experimental/pqcrypto/cecpq2_aead_hkdf.pb.h"
32 #include "proto/tink.pb.h"
33
34 namespace crypto {
35 namespace tink {
36 namespace {
37
38 using google::crypto::tink::EcPointFormat;
39 using google::crypto::tink::EllipticCurveType;
40 using google::crypto::tink::HashType;
41
42 struct CommonHybridKeyParams {
43 EllipticCurveType ec_curve;
44 EcPointFormat ec_point_format;
45 HashType hash_type;
46 };
47
GetCommonHybridKeyParamsList()48 std::vector<CommonHybridKeyParams> GetCommonHybridKeyParamsList() {
49 std::vector<CommonHybridKeyParams> params_list;
50 for (auto ec_curve : {EllipticCurveType::CURVE25519}) {
51 for (auto ec_point_format : {EcPointFormat::COMPRESSED}) {
52 for (auto hash_type : {HashType::SHA256, HashType::SHA512}) {
53 CommonHybridKeyParams params;
54 params.ec_curve = ec_curve;
55 params.ec_point_format = ec_point_format;
56 params.hash_type = hash_type;
57 params_list.push_back(params);
58 }
59 }
60 }
61 return params_list;
62 }
63
TEST(TestUtilTest,GetCecpq2AeadHkdfTestKeyBasics)64 TEST(TestUtilTest, GetCecpq2AeadHkdfTestKeyBasics) {
65 auto curve_type = EllipticCurveType::CURVE25519;
66 auto ec_point_format = EcPointFormat::COMPRESSED;
67 auto hkdf_hash_type = HashType::SHA384;
68
69 auto cecpq2_key = test::GetCecpq2AeadHkdfTestKey(curve_type, ec_point_format,
70 hkdf_hash_type);
71
72 auto params = cecpq2_key.mutable_public_key()->mutable_params();
73
74 EXPECT_EQ(params->mutable_kem_params()->curve_type(), curve_type);
75 EXPECT_EQ(params->mutable_kem_params()->ec_point_format(), ec_point_format);
76 EXPECT_EQ(params->mutable_kem_params()->hkdf_hash_type(), hkdf_hash_type);
77 }
78
TEST(TestUtilTest,GetCecpq2AesGcmHkdfTestKeyBasics)79 TEST(TestUtilTest, GetCecpq2AesGcmHkdfTestKeyBasics) {
80 auto curve_type = EllipticCurveType::CURVE25519;
81 auto ec_point_format = EcPointFormat::COMPRESSED;
82 auto hkdf_hash_type = HashType::SHA384;
83 auto aes_gcm_key_size = 32;
84
85 auto cecpq2_key = test::GetCecpq2AesGcmHkdfTestKey(
86 curve_type, ec_point_format, hkdf_hash_type, aes_gcm_key_size);
87
88 auto params = cecpq2_key.mutable_public_key()->mutable_params();
89
90 EXPECT_EQ(params->mutable_kem_params()->curve_type(), curve_type);
91 EXPECT_EQ(params->mutable_kem_params()->ec_point_format(), ec_point_format);
92 EXPECT_EQ(params->mutable_kem_params()->hkdf_hash_type(), hkdf_hash_type);
93 }
94
TEST(TestUtilTest,GetCecpq2AesCtrHmacHkdfTestKeyBasics)95 TEST(TestUtilTest, GetCecpq2AesCtrHmacHkdfTestKeyBasics) {
96 auto curve_type = EllipticCurveType::CURVE25519;
97 auto ec_point_format = EcPointFormat::COMPRESSED;
98 auto hmac_hash_type = HashType::SHA384;
99
100 uint32_t aes_ctr_iv_size = 16;
101 // Generate and test many keys with various parameters
102 for (auto key_params : GetCommonHybridKeyParamsList()) {
103 for (uint32_t aes_ctr_key_size : {16, 32}) {
104 for (uint32_t hmac_tag_size : {16, 32}) {
105 for (uint32_t hmac_key_size : {16, 32}) {
106 auto cecpq2_key = test::GetCecpq2AesCtrHmacHkdfTestKey(
107 key_params.ec_curve, key_params.ec_point_format,
108 key_params.hash_type, aes_ctr_key_size, aes_ctr_iv_size,
109 hmac_hash_type, hmac_tag_size, hmac_key_size);
110 auto params = cecpq2_key.mutable_public_key()->mutable_params();
111 EXPECT_EQ(params->mutable_kem_params()->curve_type(), curve_type);
112 EXPECT_EQ(params->mutable_kem_params()->ec_point_format(),
113 ec_point_format);
114 }
115 }
116 }
117 }
118 }
119
TEST(TestUtilTest,GetCecpq2XChaCha20Poly1305HkdfTestKeyBasics)120 TEST(TestUtilTest, GetCecpq2XChaCha20Poly1305HkdfTestKeyBasics) {
121 auto curve_type = EllipticCurveType::CURVE25519;
122 auto ec_point_format = EcPointFormat::COMPRESSED;
123 auto hkdf_hash_type = HashType::SHA384;
124
125 auto cecpq2_key = test::GetCecpq2XChaCha20Poly1305HkdfTestKey(
126 curve_type, ec_point_format, hkdf_hash_type);
127
128 auto params = cecpq2_key.mutable_public_key()->mutable_params();
129
130 EXPECT_EQ(params->mutable_kem_params()->curve_type(), curve_type);
131 EXPECT_EQ(params->mutable_kem_params()->ec_point_format(), ec_point_format);
132 EXPECT_EQ(params->mutable_kem_params()->hkdf_hash_type(), hkdf_hash_type);
133 }
134
135 } // namespace
136 } // namespace tink
137 } // namespace crypto
138