Botan 3.14.1
Crypto and TLS for C&
kyber_keys.h
Go to the documentation of this file.
1/*
2 * Crystals Kyber Internal Key Types
3 *
4 * (C) 2021-2024 Jack Lloyd
5 * (C) 2021-2022 Manuel Glaser and Michael Boric, Rohde & Schwarz Cybersecurity
6 * (C) 2021-2022 René Meusel and Hannes Rantzsch, neXenio GmbH
7 * (C) 2024 René Meusel, Rohde & Schwarz Cybersecurity
8 *
9 * Botan is released under the Simplified BSD License (see license.txt)
10 */
11
12#ifndef BOTAN_KYBER_INTERNAL_KEYS_H_
13#define BOTAN_KYBER_INTERNAL_KEYS_H_
14
15#include <botan/module_lattice_keys.h>
16#include <botan/internal/ct_utils.h>
17#include <botan/internal/kyber_algos.h>
18#include <botan/internal/kyber_constants.h>
19#include <botan/internal/kyber_types.h>
20
21namespace Botan {
22
23class Kyber_Keypair_Codec /* NOLINT(*-special-member-functions) */ {
24 public:
25 virtual ~Kyber_Keypair_Codec() = default;
27 virtual KyberInternalKeypair decode_keypair(std::span<const uint8_t> private_key, KyberConstants mode) const = 0;
28};
29
30/// Codec for expanded private keys (as specified in FIPS 203)
32 public:
33 KyberInternalKeypair decode_keypair(std::span<const uint8_t> buffer, KyberConstants mode) const override;
35};
36
37/// Codec for private keys as 64-byte seeds: d || z
39 public:
40 KyberInternalKeypair decode_keypair(std::span<const uint8_t> buffer, KyberConstants mode) const override;
42};
43
44/// A decoded private key together with the encoding format it was found in
49
50/**
51 * Decodes an ML-KEM private key from any of the three CHOICE alternatives of
52 * the ML-KEM-PrivateKey structure specified in RFC 9935, Section 6. For
53 * backwards compatibility, also the raw 64-byte seed and the raw expanded key
54 * of FIPS 203 (without any ASN.1 wrapping) are accepted.
55 */
56KyberDecodedKeypair decode_ml_kem_private_key(std::span<const uint8_t> private_key, KyberConstants mode);
57
58/**
59 * Encodes an ML-KEM private key as the CHOICE alternative of the
60 * ML-KEM-PrivateKey structure of RFC 9935 selected by @p format.
61 * @throws Encoding_Error if @p format requires the seed but the key pair does not contain it
62 */
64
66 public:
69
73 const KyberPolyMat& At,
74 const KyberConstants& mode) const;
75
78 const KyberPolyMat& At,
79 const KyberConstants& mode) const {
80 KyberCompressedCiphertext ct(m_mode.ciphertext_bytes());
81 indcpa_encrypt(ct, m, r, At, mode);
82 return ct;
83 }
84
85 const KyberPolyVecNTT& t() const { return m_t; }
86
87 const KyberSeedRho& rho() const { return m_rho; }
88
89 const KyberConstants& mode() const { return m_mode; }
90
91 const KyberSerializedPublicKey& public_key_bits_raw() const { return m_public_key_bits_raw; }
92
93 const KyberHashedPublicKey& H_public_key_bits_raw() const { return m_H_public_key_bits_raw; }
94
96
97 private:
98 const KyberConstants m_mode;
99 const KyberSerializedPublicKey m_public_key_bits_raw;
100 const KyberHashedPublicKey m_H_public_key_bits_raw;
101 KyberPolyVecNTT m_t;
102 const KyberSeedRho m_rho;
103};
104
106 public:
108 m_mode(std::move(mode)), m_s(std::move(s)), m_seed(std::move(seed)) {}
109
111
112 const KyberPolyVecNTT& s() const { return m_s; }
113
114 const KyberPrivateKeySeed& seed() const { return m_seed; }
115
116 const KyberImplicitRejectionValue& z() const { return m_seed.z; }
117
118 const KyberConstants& mode() const { return m_mode; }
119
121
122 void _const_time_poison() const { CT::poison_all(m_s, m_seed.d, m_seed.z); }
123
124 void _const_time_unpoison() const { CT::unpoison_all(m_s, m_seed.d, m_seed.z); }
125
126 private:
127 KyberConstants m_mode;
128 KyberPolyVecNTT m_s;
129 KyberPrivateKeySeed m_seed;
130};
131
132} // namespace Botan
133
134#endif
Codec for expanded private keys (as specified in FIPS 203).
Definition kyber_keys.h:31
secure_vector< uint8_t > encode_keypair(KyberInternalKeypair private_key) const override
KyberInternalKeypair decode_keypair(std::span< const uint8_t > buffer, KyberConstants mode) const override
virtual secure_vector< uint8_t > encode_keypair(KyberInternalKeypair keypair) const =0
virtual KyberInternalKeypair decode_keypair(std::span< const uint8_t > private_key, KyberConstants mode) const =0
virtual ~Kyber_Keypair_Codec()=default
KyberMessage indcpa_decrypt(StrongSpan< const KyberCompressedCiphertext > ct) const
const KyberConstants & mode() const
Definition kyber_keys.h:118
const KyberPrivateKeySeed & seed() const
Definition kyber_keys.h:114
Kyber_PrivateKeyInternal(KyberConstants mode, KyberPolyVecNTT s, KyberPrivateKeySeed seed)
Definition kyber_keys.h:107
const KyberPolyVecNTT & s() const
Definition kyber_keys.h:112
const KyberImplicitRejectionValue & z() const
Definition kyber_keys.h:116
Kyber_PublicKeyInternal(KyberConstants mode, KyberSerializedPublicKey public_key)
const KyberHashedPublicKey & H_public_key_bits_raw() const
Definition kyber_keys.h:93
const KyberPolyVecNTT & t() const
Definition kyber_keys.h:85
const KyberSeedRho & rho() const
Definition kyber_keys.h:87
KyberCompressedCiphertext indcpa_encrypt(const KyberMessage &m, const KyberEncryptionRandomness &r, const KyberPolyMat &At, const KyberConstants &mode) const
Definition kyber_keys.h:76
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
Definition kyber_keys.h:89
const KyberSerializedPublicKey & public_key_bits_raw() const
Definition kyber_keys.h:91
Codec for private keys as 64-byte seeds: d || z.
Definition kyber_keys.h:38
KyberInternalKeypair decode_keypair(std::span< const uint8_t > buffer, KyberConstants mode) const override
secure_vector< uint8_t > encode_keypair(KyberInternalKeypair keypair) const override
constexpr void poison_all(const Ts &... ts)
Definition ct_utils.h:201
constexpr void unpoison_all(const Ts &... ts)
Definition ct_utils.h:207
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 KyberEncryptionRandomness_ > KyberEncryptionRandomness
Random value used to generate the Kyber ciphertext.
Definition kyber_types.h:48
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.
Definition kyber_types.h:45
secure_vector< uint8_t > encode_ml_kem_private_key(const KyberInternalKeypair &keypair, MlPrivateKeyFormat format)
Botan::CRYSTALS::PolynomialVector< KyberPolyTraits, Botan::CRYSTALS::Domain::NTT > KyberPolyVecNTT
Definition kyber_types.h:26
Strong< std::vector< uint8_t >, struct KyberSeedRho_ > KyberSeedRho
Public seed value to generate the Kyber matrix A.
Definition kyber_types.h:36
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:128
Strong< std::vector< uint8_t >, struct KyberCompressedCiphertext_ > KyberCompressedCiphertext
Compressed and serialized ciphertext value.
Definition kyber_types.h:63
std::pair< std::shared_ptr< const Kyber_PublicKeyInternal >, std::shared_ptr< const Kyber_PrivateKeyInternal > > KyberInternalKeypair
Definition kyber_types.h:73
Botan::CRYSTALS::PolynomialMatrix< KyberPolyTraits > KyberPolyMat
Definition kyber_types.h:27
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
A decoded private key together with the encoding format it was found in.
Definition kyber_keys.h:45
KyberInternalKeypair keypair
Definition kyber_keys.h:46
MlPrivateKeyFormat format
Definition kyber_keys.h:47