12#include <botan/internal/kyber_keys.h>
14#include <botan/asn1_obj.h>
15#include <botan/ber_dec.h>
16#include <botan/der_enc.h>
17#include <botan/exceptn.h>
18#include <botan/mem_ops.h>
19#include <botan/internal/buffer_slicer.h>
20#include <botan/internal/concat_util.h>
21#include <botan/internal/kyber_symmetric_primitives.h>
28 if(public_key.size() != expected_length) {
29 throw Decoding_Error(
"Public key does not have the correct byte count");
59 std::make_shared<Kyber_PublicKeyInternal>(mode, std::move(pub_key)),
60 std::make_shared<Kyber_PrivateKeyInternal>(
69 BOTAN_ASSERT(keypair.first && keypair.second,
"reading private key encoding");
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");
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();
92 const auto [K, r] = sym.G(m, pk_hash);
95 const auto ct = pk.indcpa_encrypt(m, r, At, pk.mode());
96 const auto m_prime = priv.indcpa_decrypt(ct);
98 throw Decoding_Error(
"ML-KEM expanded private key is inconsistent with its public key");
108 const auto& mode = keypair.first->mode();
111 keypair.first->public_key_bits_raw(),
112 keypair.first->H_public_key_bits_raw(),
113 keypair.second->z());
131 const auto& seed = keypair.second->seed();
132 BOTAN_ARG_CHECK(seed.d.has_value(),
"Cannot encode keypair without the full private seed");
140 throw Decoding_Error(
"invalid length of ML-KEM private key seed");
142 return Seed_Expanding_Keypair_Codec().decode_keypair(seed, std::move(mode));
146 if(expanded.size() != mode.expanded_private_key_bytes()) {
147 throw Decoding_Error(
"invalid length of ML-KEM expanded private key");
160 .keypair = decode_ml_kem_seed(private_key, std::move(mode)),
169 .keypair = decode_ml_kem_expanded(private_key, std::move(mode)),
177 const auto obj = [&]() {
179 return dec.peek_next_object();
191 .keypair = decode_ml_kem_seed(seed, std::move(mode)),
202 .keypair = decode_ml_kem_expanded(expanded, std::move(mode)),
218 throw Decoding_Error(
"invalid length of ML-KEM expanded private key");
222 auto keypair = decode_ml_kem_seed(seed, std::move(mode));
224 if(expanded_from_seed.size() != expanded.size() || !
constant_time_compare(expanded_from_seed, expanded)) {
225 throw Decoding_Error(
"seed and expanded key in ML-KEM private key do not match");
229 .keypair = std::move(keypair),
240 "RFC 9935 private key encodings are only defined for ML-KEM");
242 throw Encoding_Error(
"ML-KEM private key does not contain the seed, cannot encode it in the requested format");
272 m_mode(std::move(
mode)),
273 m_public_key_bits_raw(validate_public_key_length(std::move(public_key), m_mode.public_key_bytes())),
274 m_H_public_key_bits_raw(m_mode.symmetric_primitives().H(m_public_key_bits_raw)),
276 std::span{m_public_key_bits_raw}.first(m_mode.polynomial_vector_bytes()), m_mode)),
280 m_mode(std::move(
mode)),
281 m_public_key_bits_raw(
concat(
Kyber_Algos::encode_polynomial_vector<std::vector<uint8_t>>(
t, m_mode),
rho)),
282 m_H_public_key_bits_raw(m_mode.symmetric_primitives().H(m_public_key_bits_raw)),
284 m_rho(std::move(
rho)) {}
306 auto u = inverse_ntt(At * y);
311 auto v = inverse_ntt(m_t * y);
327 v -= inverse_ntt(m_s * ntt(std::move(u)));
#define BOTAN_ASSERT_NOMSG(expr)
#define BOTAN_ASSERT_NONNULL(ptr)
#define BOTAN_ARG_CHECK(expr, msg)
#define BOTAN_ASSERT(expr, assertion_made)
auto copy(const size_t count)
std::span< const uint8_t > take(const size_t count)
secure_vector< uint8_t > get_contents()
DER_Encoder & start_sequence()
DER_Encoder & encode(bool b)
Codec for expanded private keys (as specified in FIPS 203).
secure_vector< uint8_t > encode_keypair(KyberInternalKeypair private_key) const override
KyberInternalKeypair decode_keypair(std::span< const uint8_t > buffer, KyberConstants mode) const override
static constexpr size_t SEED_BYTES
size_t public_key_bytes() const
byte length of an encoded public key
size_t seed_private_key_bytes() const
byte length of an private key encoded as the seed: d || z
size_t polynomial_vector_bytes() const
byte length of an encoded polynomial vector
size_t expanded_private_key_bytes() const
byte length of a private key with expanded encoding as defined
static constexpr size_t PUBLIC_KEY_HASH_BYTES
KyberPolyVec sample_polynomial_vector_cbd_eta1()
std::pair< KyberPolyVec, KyberPoly > sample_polynomial_vector_and_poly_cbd_eta2()
Samples a polynomial vector and a single polynomial in one PRF batch.
KyberMessage indcpa_decrypt(StrongSpan< const KyberCompressedCiphertext > ct) const
const KyberPolyVecNTT & t() const
const KyberSeedRho & rho() const
void indcpa_encrypt(StrongSpan< KyberCompressedCiphertext > out_ct, StrongSpan< const KyberMessage > m, StrongSpan< const KyberEncryptionRandomness > r, const KyberPolyMat &At, const KyberConstants &mode) const
const KyberConstants & mode() const
Kyber_PublicKeyInternal()=delete
Codec for private keys as 64-byte seeds: d || z.
KyberInternalKeypair decode_keypair(std::span< const uint8_t > buffer, KyberConstants mode) const override
secure_vector< uint8_t > encode_keypair(KyberInternalKeypair keypair) const override
size_type size() const noexcept(noexcept(this->get().size()))
constexpr auto scoped_poison(const Ts &... xs)
constexpr void unpoison_all(const Ts &... ts)
constexpr void unpoison(const T *p, size_t n)
void encode_polynomial_vector(std::span< uint8_t > out, const KyberPolyVecNTT &vec)
KyberMessage polynomial_to_message(const KyberPoly &p)
KyberPolyMat sample_matrix(StrongSpan< const KyberSeedRho > seed, bool transposed, const KyberConstants &mode)
void compress_ciphertext(StrongSpan< KyberCompressedCiphertext > out, const KyberPolyVec &u, const KyberPoly &v, const KyberConstants &m_mode)
KyberInternalKeypair expand_keypair(KyberPrivateKeySeed seed, KyberConstants mode)
KyberPoly polynomial_from_message(StrongSpan< const KyberMessage > msg)
std::pair< KyberPolyVec, KyberPoly > decompress_ciphertext(StrongSpan< const KyberCompressedCiphertext > ct, 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.
KyberDecodedKeypair decode_ml_kem_private_key(std::span< const uint8_t > private_key, KyberConstants mode)
Strong< secure_vector< uint8_t >, struct KyberMessage_ > KyberMessage
Random message value to be encrypted by the CPA-secure Kyber encryption scheme.
secure_vector< uint8_t > encode_ml_kem_private_key(const KyberInternalKeypair &keypair, MlPrivateKeyFormat format)
Botan::CRYSTALS::PolynomialVector< KyberPolyTraits, Botan::CRYSTALS::Domain::NTT > KyberPolyVecNTT
Strong< secure_vector< uint8_t >, struct KyberSeedRandomness_ > KyberSeedRandomness
Principal seed used to generate Kyber key pairs.
Strong< std::vector< uint8_t >, struct KyberSeedRho_ > KyberSeedRho
Public seed value to generate the Kyber matrix A.
constexpr auto concat(Rs &&... ranges)
std::vector< T, secure_allocator< T > > secure_vector
std::pair< std::shared_ptr< const Kyber_PublicKeyInternal >, std::shared_ptr< const Kyber_PrivateKeyInternal > > KyberInternalKeypair
bool constant_time_compare(std::span< const uint8_t > x, std::span< const uint8_t > y)
Botan::CRYSTALS::PolynomialMatrix< KyberPolyTraits > KyberPolyMat
Strong< std::vector< uint8_t >, struct KyberHashedPublicKey_ > KyberHashedPublicKey
Hash value of the serialized public key.
Strong< std::vector< uint8_t >, struct KyberSerializedPublicKey_ > KyberSerializedPublicKey
Public key in serialized form (t || rho).
A decoded private key together with the encoding format it was found in.