Botan 3.14.1
Crypto and TLS for C&
Botan::Expanded_Keypair_Codec Class Referencefinal

Codec for expanded private keys (as specified in FIPS 203). More...

#include <kyber_keys.h>

Inheritance diagram for Botan::Expanded_Keypair_Codec:
Botan::Kyber_Keypair_Codec

Public Member Functions

KyberInternalKeypair decode_keypair (std::span< const uint8_t > buffer, KyberConstants mode) const override
secure_vector< uint8_t > encode_keypair (KyberInternalKeypair private_key) const override

Detailed Description

Codec for expanded private keys (as specified in FIPS 203).

Definition at line 31 of file kyber_keys.h.

Member Function Documentation

◆ decode_keypair()

KyberInternalKeypair Botan::Expanded_Keypair_Codec::decode_keypair ( std::span< const uint8_t > sk,
KyberConstants mode ) const
overridevirtual

Key decoding as specified in Crystals Kyber (Version 3.01), Algorithms 4 (CPAPKE.KeyGen()), and 7 (CCAKEM.KeyGen())

Public Key: pk := (encode(t) || rho) Secret Key: sk' := encode(s)

Expanded Secret Key: sk := (sk' || pk || H(pk) || z)

Implements Botan::Kyber_Keypair_Codec.

Definition at line 45 of file kyber_keys.cpp.

45 {
46 const auto scope = CT::scoped_poison(sk);
47 BufferSlicer s(sk);
48
49 auto skpv = Kyber_Algos::decode_polynomial_vector(s.take(mode.polynomial_vector_bytes()), mode);
50 auto pub_key = s.copy<KyberSerializedPublicKey>(mode.public_key_bytes());
53
54 BOTAN_ASSERT_NOMSG(s.empty());
55
56 CT::unpoison_all(pub_key, puk_key_hash, skpv, z);
57
59 std::make_shared<Kyber_PublicKeyInternal>(mode, std::move(pub_key)),
60 std::make_shared<Kyber_PrivateKeyInternal>(
61 std::move(mode),
62 std::move(skpv),
63 KyberPrivateKeySeed{std::nullopt, // Reading from an expanded and encoded
64 // private key cannot reconstruct the
65 // original seed from key generation.
66 std::move(z)}),
67 };
68
69 BOTAN_ASSERT(keypair.first && keypair.second, "reading private key encoding");
70
71 const auto& computed_hash = keypair.first->H_public_key_bits_raw();
72 if(computed_hash.size() != puk_key_hash.size() ||
73 !std::equal(computed_hash.begin(), computed_hash.end(), puk_key_hash.begin())) {
74 throw Decoding_Error("Public key hash does not match the hash stored in the private key");
75 }
76
77 {
78 // Pairwise consistency check: the hash check above does not detect a
79 // secret vector s that does not belong to the public key (see RFC 9935,
80 // Appendix C.4, example 2). A deterministic K-PKE encryption of an
81 // arbitrary message with the public key must decrypt correctly with s.
82 //
83 // TODO(Botan4): reuse the ML_KEM_Encryptor/Decryptor helpers for this
84 // roundtrip check. The encryptor might need an additional
85 // interface to accept a fixed randomness value.
86 // See: https://github.com/randombit/botan/pull/5934#discussion_r4454849589
87 const auto& pk = *keypair.first;
88 const auto& priv = *keypair.second;
89 const auto& sym = pk.mode().symmetric_primitives();
90 const auto& pk_hash = pk.H_public_key_bits_raw();
91 const KyberMessage m(pk_hash.begin(), pk_hash.end());
92 const auto [K, r] = sym.G(m, pk_hash);
93 BOTAN_UNUSED(K);
94 const auto At = Kyber_Algos::sample_matrix(pk.rho(), true /* transposed */, pk.mode());
95 const auto ct = pk.indcpa_encrypt(m, r, At, pk.mode());
96 const auto m_prime = priv.indcpa_decrypt(ct);
97 if(m_prime.size() != m.size() || !constant_time_compare(m_prime, m)) {
98 throw Decoding_Error("ML-KEM expanded private key is inconsistent with its public key");
99 }
100 }
101
102 return keypair;
103}
#define BOTAN_UNUSED
Definition assert.h:151
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:82
#define BOTAN_ASSERT(expr, assertion_made)
Definition assert.h:69
static constexpr size_t SEED_BYTES
static constexpr size_t PUBLIC_KEY_HASH_BYTES
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:222
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:207
KyberPolyMat sample_matrix(StrongSpan< const KyberSeedRho > seed, bool transposed, const KyberConstants &mode)
KyberPolyVecNTT decode_polynomial_vector(std::span< const uint8_t > a, const KyberConstants &mode)
Strong< secure_vector< uint8_t >, struct KyberImplicitRejectionValue_ > KyberImplicitRejectionValue
Secret random value (called Z in the spec), used for implicit rejection in the decapsulation.
Definition kyber_types.h:42
Strong< secure_vector< uint8_t >, struct KyberMessage_ > KyberMessage
Random message value to be encrypted by the CPA-secure Kyber encryption scheme.
Definition kyber_types.h:45
std::pair< std::shared_ptr< const Kyber_PublicKeyInternal >, std::shared_ptr< const Kyber_PrivateKeyInternal > > KyberInternalKeypair
Definition kyber_types.h:73
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Definition mem_ops.cpp:17
Strong< std::vector< uint8_t >, struct KyberHashedPublicKey_ > KyberHashedPublicKey
Hash value of the serialized public key.
Definition kyber_types.h:60
Strong< std::vector< uint8_t >, struct KyberSerializedPublicKey_ > KyberSerializedPublicKey
Public key in serialized form (t || rho).
Definition kyber_types.h:57

References BOTAN_ASSERT, BOTAN_ASSERT_NOMSG, BOTAN_UNUSED, Botan::constant_time_compare(), Botan::BufferSlicer::copy(), Botan::Kyber_Algos::decode_polynomial_vector(), Botan::BufferSlicer::empty(), Botan::KyberConstants::polynomial_vector_bytes(), Botan::KyberConstants::public_key_bytes(), Botan::KyberConstants::PUBLIC_KEY_HASH_BYTES, Botan::Kyber_Algos::sample_matrix(), Botan::CT::scoped_poison(), Botan::KyberConstants::SEED_BYTES, Botan::detail::Container_Strong_Adapter_Base< T >::size(), Botan::BufferSlicer::take(), and Botan::CT::unpoison_all().

Referenced by Botan::Kyber_PrivateKey::Kyber_PrivateKey().

◆ encode_keypair()

secure_vector< uint8_t > Botan::Expanded_Keypair_Codec::encode_keypair ( KyberInternalKeypair private_key) const
overridevirtual

Implements Botan::Kyber_Keypair_Codec.

Definition at line 105 of file kyber_keys.cpp.

105 {
106 BOTAN_ASSERT_NONNULL(keypair.first);
107 BOTAN_ASSERT_NONNULL(keypair.second);
108 const auto& mode = keypair.first->mode();
109 const auto scope = CT::scoped_poison(*keypair.second);
110 auto result = concat(Kyber_Algos::encode_polynomial_vector(keypair.second->s(), mode),
111 keypair.first->public_key_bits_raw(),
112 keypair.first->H_public_key_bits_raw(),
113 keypair.second->z());
114 CT::unpoison(result);
115 return result;
116}
#define BOTAN_ASSERT_NONNULL(ptr)
Definition assert.h:121
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:67
void encode_polynomial_vector(std::span< uint8_t > out, const KyberPolyVecNTT &vec)
constexpr auto concat(Rs &&... ranges)
Definition concat_util.h:95

References BOTAN_ASSERT_NONNULL, Botan::concat(), Botan::Kyber_Algos::encode_polynomial_vector(), Botan::CT::scoped_poison(), and Botan::CT::unpoison().

Referenced by Botan::decode_ml_kem_private_key(), and Botan::Kyber_PrivateKey::formatted_raw_private_key_bits().


The documentation for this class was generated from the following files: