xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/kem/util/test_util.cc (revision e7b1675dde1b92d52ec075b0a92829627f2c52a5)
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 <memory>
20 #include <string>
21 #include <utility>
22 
23 #include "openssl/curve25519.h"
24 #include "openssl/hrss.h"
25 #include "tink/aead/aes_ctr_hmac_aead_key_manager.h"
26 #include "tink/aead/aes_gcm_key_manager.h"
27 #include "tink/aead/xchacha20_poly1305_key_manager.h"
28 #include "tink/experimental/pqcrypto/kem/subtle/cecpq2_subtle_boringssl_util.h"
29 #include "tink/subtle/subtle_util.h"
30 #include "tink/util/enums.h"
31 #include "proto/aes_ctr.pb.h"
32 #include "proto/aes_ctr_hmac_aead.pb.h"
33 #include "proto/hmac.pb.h"
34 #include "proto/xchacha20_poly1305.pb.h"
35 
36 namespace crypto {
37 namespace tink {
38 namespace test {
39 
GetCecpq2AeadHkdfTestKey(google::crypto::tink::EllipticCurveType curve_type,google::crypto::tink::EcPointFormat ec_point_format,google::crypto::tink::HashType hash_type)40 google::crypto::tink::Cecpq2AeadHkdfPrivateKey GetCecpq2AeadHkdfTestKey(
41     google::crypto::tink::EllipticCurveType curve_type,
42     google::crypto::tink::EcPointFormat ec_point_format,
43     google::crypto::tink::HashType hash_type) {
44   google::crypto::tink::Cecpq2AeadHkdfPrivateKey cecpq2_key_pair_proto;
45   cecpq2_key_pair_proto.set_version(0);
46 
47   auto cecpq2_key_pair_or_status =
48       pqc::GenerateCecpq2Keypair(util::Enums::ProtoToSubtle(curve_type));
49   auto cecpq2_key_pair = std::move(cecpq2_key_pair_or_status.value());
50 
51   std::string hrss_priv_key_seed_str(
52       reinterpret_cast<const char *>(
53           cecpq2_key_pair.hrss_key_pair.hrss_private_key_seed.data()),
54       HRSS_GENERATE_KEY_BYTES);
55   cecpq2_key_pair_proto.set_hrss_private_key_seed(hrss_priv_key_seed_str);
56 
57   cecpq2_key_pair_proto.set_x25519_private_key(
58       std::string(reinterpret_cast<const char *>(
59                       cecpq2_key_pair.x25519_key_pair.priv.data()),
60                   X25519_PRIVATE_KEY_LEN));
61 
62   auto public_key = cecpq2_key_pair_proto.mutable_public_key();
63   public_key->set_version(0);
64   public_key->set_x25519_public_key_x(cecpq2_key_pair.x25519_key_pair.pub_x);
65   public_key->set_x25519_public_key_y(cecpq2_key_pair.x25519_key_pair.pub_y);
66   public_key->set_hrss_public_key_marshalled(
67       cecpq2_key_pair.hrss_key_pair.hrss_public_key_marshaled);
68 
69   auto params = public_key->mutable_params();
70   params->mutable_kem_params()->set_ec_point_format(ec_point_format);
71   params->mutable_kem_params()->set_curve_type(curve_type);
72   params->mutable_kem_params()->set_hkdf_hash_type(hash_type);
73 
74   return cecpq2_key_pair_proto;
75 }
76 
GetCecpq2AesGcmHkdfTestKey(google::crypto::tink::EllipticCurveType curve_type,google::crypto::tink::EcPointFormat ec_point_format,google::crypto::tink::HashType hash_type,uint32_t aes_gcm_key_size)77 google::crypto::tink::Cecpq2AeadHkdfPrivateKey GetCecpq2AesGcmHkdfTestKey(
78     google::crypto::tink::EllipticCurveType curve_type,
79     google::crypto::tink::EcPointFormat ec_point_format,
80     google::crypto::tink::HashType hash_type, uint32_t aes_gcm_key_size) {
81   auto cecpq2_key =
82       GetCecpq2AeadHkdfTestKey(curve_type, ec_point_format, hash_type);
83   auto params = cecpq2_key.mutable_public_key()->mutable_params();
84 
85   google::crypto::tink::AesGcmKeyFormat key_format;
86   key_format.set_key_size(aes_gcm_key_size);
87   auto aead_dem = params->mutable_dem_params()->mutable_aead_dem();
88   std::unique_ptr<AesGcmKeyManager> key_manager(new AesGcmKeyManager());
89   std::string dem_key_type = key_manager->get_key_type();
90   aead_dem->set_type_url(dem_key_type);
91   aead_dem->set_value(key_format.SerializeAsString());
92   return cecpq2_key;
93 }
94 
GetCecpq2AesCtrHmacHkdfTestKey(google::crypto::tink::EllipticCurveType curve_type,google::crypto::tink::EcPointFormat ec_point_format,google::crypto::tink::HashType hash_type,uint32_t aes_ctr_key_size,uint32_t aes_ctr_iv_size,google::crypto::tink::HashType hmac_hash_type,uint32_t hmac_tag_size,uint32_t hmac_key_size)95 google::crypto::tink::Cecpq2AeadHkdfPrivateKey GetCecpq2AesCtrHmacHkdfTestKey(
96     google::crypto::tink::EllipticCurveType curve_type,
97     google::crypto::tink::EcPointFormat ec_point_format,
98     google::crypto::tink::HashType hash_type, uint32_t aes_ctr_key_size,
99     uint32_t aes_ctr_iv_size, google::crypto::tink::HashType hmac_hash_type,
100     uint32_t hmac_tag_size, uint32_t hmac_key_size) {
101   auto ecies_key =
102       GetCecpq2AeadHkdfTestKey(curve_type, ec_point_format, hash_type);
103 
104   google::crypto::tink::AesCtrHmacAeadKeyFormat key_format;
105   auto aes_ctr_key_format = key_format.mutable_aes_ctr_key_format();
106   auto aes_ctr_params = aes_ctr_key_format->mutable_params();
107   aes_ctr_params->set_iv_size(aes_ctr_iv_size);
108   aes_ctr_key_format->set_key_size(aes_ctr_key_size);
109 
110   auto hmac_key_format = key_format.mutable_hmac_key_format();
111   auto hmac_params = hmac_key_format->mutable_params();
112   hmac_params->set_hash(hmac_hash_type);
113   hmac_params->set_tag_size(hmac_tag_size);
114   hmac_key_format->set_key_size(hmac_key_size);
115 
116   auto params = ecies_key.mutable_public_key()->mutable_params();
117   auto aead_dem = params->mutable_dem_params()->mutable_aead_dem();
118 
119   std::unique_ptr<AesCtrHmacAeadKeyManager> key_manager(
120       new AesCtrHmacAeadKeyManager());
121   std::string dem_key_type = key_manager->get_key_type();
122   aead_dem->set_type_url(dem_key_type);
123   aead_dem->set_value(key_format.SerializeAsString());
124   return ecies_key;
125 }
126 
127 google::crypto::tink::Cecpq2AeadHkdfPrivateKey
GetCecpq2XChaCha20Poly1305HkdfTestKey(google::crypto::tink::EllipticCurveType curve_type,google::crypto::tink::EcPointFormat ec_point_format,google::crypto::tink::HashType hash_type)128 GetCecpq2XChaCha20Poly1305HkdfTestKey(
129     google::crypto::tink::EllipticCurveType curve_type,
130     google::crypto::tink::EcPointFormat ec_point_format,
131     google::crypto::tink::HashType hash_type) {
132   auto ecies_key =
133       GetCecpq2AeadHkdfTestKey(curve_type, ec_point_format, hash_type);
134   auto params = ecies_key.mutable_public_key()->mutable_params();
135 
136   google::crypto::tink::XChaCha20Poly1305KeyFormat key_format;
137   auto aead_dem = params->mutable_dem_params()->mutable_aead_dem();
138   std::unique_ptr<XChaCha20Poly1305KeyManager> key_manager(
139       new XChaCha20Poly1305KeyManager());
140   std::string dem_key_type = key_manager->get_key_type();
141   aead_dem->set_type_url(dem_key_type);
142   aead_dem->set_value(key_format.SerializeAsString());
143 
144   return ecies_key;
145 }
146 
147 }  // namespace test
148 }  // namespace tink
149 }  // namespace crypto
150