xref: /aosp_15_r20/external/tink/cc/experimental/pqcrypto/kem/subtle/cecpq2_subtle_boringssl_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/subtle/cecpq2_subtle_boringssl_util.h"
18 
19 #include <utility>
20 
21 #include "openssl/curve25519.h"
22 #include "openssl/hrss.h"
23 #include "tink/subtle/common_enums.h"
24 #include "tink/subtle/random.h"
25 #include "tink/subtle/subtle_util.h"
26 #include "tink/util/secret_data.h"
27 
28 namespace crypto {
29 namespace tink {
30 namespace pqc {
31 
32 crypto::tink::util::StatusOr<crypto::tink::pqc::HrssKeyPair>
GenerateHrssKeyPair(util::SecretData hrss_key_entropy)33 GenerateHrssKeyPair(util::SecretData hrss_key_entropy) {
34   crypto::tink::pqc::HrssKeyPair hrss_key_pair;
35   hrss_key_pair.hrss_private_key_seed = std::move(hrss_key_entropy);
36 
37   struct HRSS_public_key hrss_public_key;
38   util::SecretUniquePtr<struct HRSS_private_key> hrss_private_key =
39       util::MakeSecretUniquePtr<struct HRSS_private_key>();
40 
41   // Generating a HRSS key pair
42   HRSS_generate_key(&hrss_public_key, hrss_private_key.get(),
43                     hrss_key_pair.hrss_private_key_seed.data());
44 
45   // Marshalling the HRSS public key
46   crypto::tink::subtle::ResizeStringUninitialized(
47       &(hrss_key_pair.hrss_public_key_marshaled), HRSS_PUBLIC_KEY_BYTES);
48   HRSS_marshal_public_key(
49       const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(
50           hrss_key_pair.hrss_public_key_marshaled.data())),
51       &hrss_public_key);
52 
53   return hrss_key_pair;
54 }
55 
56 crypto::tink::util::StatusOr<crypto::tink::pqc::Cecpq2KeyPair>
GenerateCecpq2Keypair(subtle::EllipticCurveType curve_type)57 GenerateCecpq2Keypair(subtle::EllipticCurveType curve_type) {
58   crypto::tink::pqc::Cecpq2KeyPair cecpq2_key_pair;
59 
60   // Generating a X25519 key pair
61   cecpq2_key_pair.x25519_key_pair.priv.resize(X25519_PRIVATE_KEY_LEN);
62   subtle::ResizeStringUninitialized(&(cecpq2_key_pair.x25519_key_pair.pub_x),
63                                     X25519_PUBLIC_VALUE_LEN);
64   X25519_keypair(const_cast<uint8_t *>(reinterpret_cast<const uint8_t *>(
65                      cecpq2_key_pair.x25519_key_pair.pub_x.data())),
66                  cecpq2_key_pair.x25519_key_pair.priv.data());
67 
68   // Generating a HRSS key pair
69   util::SecretData generate_hrss_key_entropy =
70       crypto::tink::subtle::Random::GetRandomKeyBytes(HRSS_GENERATE_KEY_BYTES);
71   auto hrss_key_pair_or_status = GenerateHrssKeyPair(generate_hrss_key_entropy);
72   cecpq2_key_pair.hrss_key_pair = std::move(hrss_key_pair_or_status.value());
73 
74   return cecpq2_key_pair;
75 }
76 
77 }  // namespace pqc
78 }  // namespace tink
79 }  // namespace crypto
80