Botan 3.11.0
Crypto and TLS for C&
kyber.h
Go to the documentation of this file.
1/*
2 * Crystals Kyber key encapsulation mechanism
3 * Based on the public domain reference implementation by the
4 * designers (https://github.com/pq-crystals/kyber)
5 *
6 * Further changes
7 * (C) 2021-2022 Jack Lloyd
8 * (C) 2021-2022 Manuel Glaser and Michael Boric, Rohde & Schwarz Cybersecurity
9 * (C) 2021-2022 René Meusel and Hannes Rantzsch, neXenio GmbH
10 *
11 * Botan is released under the Simplified BSD License (see license.txt)
12 */
13
14#ifndef BOTAN_KYBER_COMMON_H_
15#define BOTAN_KYBER_COMMON_H_
16
17#include <botan/pk_keys.h>
18#include <span>
19
20#if !defined(BOTAN_HAS_KYBER_90S) && !defined(BOTAN_HAS_KYBER) && !defined(BOTAN_HAS_ML_KEM)
21static_assert(
22 false,
23 "botan module 'kyber_common' is useful only when enabling at least one of those modules: 'kyber', 'kyber_90s', 'ml_kem'");
24#endif
25
26namespace Botan {
27
29 public:
30 enum Mode : uint8_t /* NOLINT(*-use-enum-class) */ {
31 // Kyber512 as proposed in round 3 of the NIST competition
32 Kyber512_R3 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 0,
33 // Kyber768 as proposed in round 3 of the NIST competition
34 Kyber768_R3 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 1,
35 // Kyber1024 as proposed in round 3 of the NIST competition
36 Kyber1024_R3 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 2,
37
38 Kyber512 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 0,
39 Kyber768 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 1,
40 Kyber1024 BOTAN_DEPRECATED("Kyber R3 is deprecated - use ML-KEM") = 2,
41
45
46 Kyber512_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 6,
47 Kyber768_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 7,
48 Kyber1024_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 8,
49 };
50
51 // NOLINTNEXTLINE(*-explicit-conversions)
52 KyberMode(Mode mode);
53
54 explicit KyberMode(const OID& oid);
55 explicit KyberMode(std::string_view str);
56
57 OID object_identifier() const;
58 std::string to_string() const;
59
60 Mode mode() const { return m_mode; }
61
62 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_90s() const;
63
64 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_modern() const;
65
66 bool is_ml_kem() const;
67
68 bool is_kyber_round3() const;
69
70 bool is_available() const;
71
72 bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
73
74 bool operator!=(const KyberMode& other) const { return !(*this == other); }
75
76 private:
77 Mode m_mode;
78};
79
80/// Byte encoding format of ML-KEM and ML-DSA the private key
81enum class MlPrivateKeyFormat : uint8_t {
82 /// Only supported for ML-KEM/ML-DSA keys:
83 /// - ML-KEM: 64-byte seed: d || z
84 /// - ML-DSA: 32-byte seed: xi (private_key_bits_with_format not yet
85 /// yet supported for ML-DSA)
87 /// The expanded format, i.e., the format specified in FIPS-203/204.
89};
90
91class Kyber_PublicKeyInternal;
92class Kyber_PrivateKeyInternal;
93
94class BOTAN_PUBLIC_API(3, 0) Kyber_PublicKey : public virtual Public_Key {
95 public:
96 Kyber_PublicKey(std::span<const uint8_t> pub_key, KyberMode mode);
97
98 Kyber_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
99
100 Kyber_PublicKey(const Kyber_PublicKey& other);
101 Kyber_PublicKey& operator=(const Kyber_PublicKey& other) = default;
104
105 ~Kyber_PublicKey() override = default;
106
107 std::string algo_name() const override;
108
110
111 OID object_identifier() const override;
112
113 size_t key_length() const override;
114
115 size_t estimated_strength() const override;
116
117 std::vector<uint8_t> raw_public_key_bits() const override;
118
119 std::vector<uint8_t> public_key_bits() const override;
120
121 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
122
123 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
124
125 bool supports_operation(PublicKeyOperation op) const override {
127 }
128
129 std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params,
130 std::string_view provider) const override;
131
132 KyberMode mode() const;
133
134 protected:
135 Kyber_PublicKey() = default;
136
137 static std::shared_ptr<Kyber_PublicKeyInternal> initialize_from_encoding(std::span<const uint8_t> pub_key,
138 KyberMode m);
139
140 protected:
143
144 std::shared_ptr<Kyber_PublicKeyInternal> m_public; // NOLINT(*non-private-member-variable*)
145};
146
149
150class BOTAN_PUBLIC_API(3, 0) Kyber_PrivateKey final : public virtual Kyber_PublicKey,
151 public virtual Private_Key {
152 public:
153 /**
154 * Create a new private key. The private key will be encoded as the 64 byte
155 * seed.
156 */
158
159 /**
160 * Import a private key using its key bytes. Supported are key bytes as
161 * 64-byte seeds (not supported for Kyber Round 3 instances),
162 * as well as the expanded encoding specified by FIPS 203. Note that the
163 * encoding used in this constructor is reflected by the calls for
164 * private_key_bits, private_key_info, etc.
165 */
166 Kyber_PrivateKey(std::span<const uint8_t> sk, KyberMode mode);
167
168 /**
169 * Import a private key using its key bytes. Supported are key bytes as
170 * 64-byte seeds (not supported for Kyber Round 3 instances),
171 * as well as the expanded encoding specified by FIPS 203. Note that the
172 * encoding used in this constructor is reflected by the calls for
173 * private_key_bits, private_key_info, etc.
174 */
175 Kyber_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
176
177 std::unique_ptr<Public_Key> public_key() const override;
178
180
182
183 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
184
185 std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
186 std::string_view params,
187 std::string_view provider) const override;
188
189 /**
190 * The private key format from which the key was loaded. It is the format
191 * used for the private_key_bits(), raw_private_key_bits() andFIPS
192 * private_key_info() methods.
193 *
194 * Note that keys in Seed format can be serialized to Expanded format
195 * using the method private_key_bits_with_format but NOT the other way
196 * around.
197 */
199
200 /**
201 * Encode the private key in the specified format. Note that the seed
202 * format is only available for new ML-KEM keys and those loaded from
203 * seeds.
204 * @throws Encoding_Error if the private key cannot be encoded in the
205 * requested format.
206 */
208
209 private:
211
212 std::shared_ptr<Kyber_PrivateKeyInternal> m_private;
213};
214
216
217} // namespace Botan
218
219#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:122
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:119
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:121
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
bool operator!=(const KyberMode &other) const
Definition kyber.h:74
KyberMode(Mode mode)
Definition kyber.cpp:76
Mode mode() const
Definition kyber.h:60
MlPrivateKeyFormat private_key_format() const
Definition kyber.cpp:318
secure_vector< uint8_t > private_key_bits_with_format(MlPrivateKeyFormat format) const
Definition kyber.cpp:325
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition kyber.cpp:296
std::unique_ptr< Public_Key > public_key() const override
Definition kyber.cpp:243
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition kyber.cpp:255
Kyber_PrivateKey(RandomNumberGenerator &rng, KyberMode mode)
Definition kyber.cpp:219
secure_vector< uint8_t > raw_private_key_bits() const override
Definition kyber.cpp:247
friend class Kyber_KEM_Decryptor
Definition kyber.h:210
secure_vector< uint8_t > private_key_bits() const override
Definition kyber.cpp:251
Kyber_PublicKey & operator=(const Kyber_PublicKey &other)=default
std::vector< uint8_t > public_key_bits() const override
Definition kyber.cpp:189
Kyber_PublicKey(Kyber_PublicKey &&other)=default
std::vector< uint8_t > raw_public_key_bits() const override
Definition kyber.cpp:185
bool check_key(RandomNumberGenerator &rng, bool strong) const override
Definition kyber.cpp:199
std::string algo_name() const override
Definition kyber.cpp:154
std::shared_ptr< Kyber_PublicKeyInternal > m_public
Definition kyber.h:144
friend class Kyber_KEM_Encryptor
Definition kyber.h:141
size_t key_length() const override
Definition kyber.cpp:195
AlgorithmIdentifier algorithm_identifier() const override
Definition kyber.cpp:158
~Kyber_PublicKey() override=default
static std::shared_ptr< Kyber_PublicKeyInternal > initialize_from_encoding(std::span< const uint8_t > pub_key, KyberMode m)
bool supports_operation(PublicKeyOperation op) const override
Definition kyber.h:125
KyberMode mode() const
Definition kyber.cpp:150
OID object_identifier() const override
Definition kyber.cpp:166
friend class Kyber_KEM_Decryptor
Definition kyber.h:142
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition kyber.cpp:212
Kyber_PublicKey(std::span< const uint8_t > pub_key, KyberMode mode)
Definition kyber.cpp:177
Kyber_PublicKey & operator=(Kyber_PublicKey &&other)=default
size_t estimated_strength() const override
Definition kyber.cpp:170
MlPrivateKeyFormat
Byte encoding format of ML-KEM and ML-DSA the private key.
Definition kyber.h:81
@ Expanded
The expanded format, i.e., the format specified in FIPS-203/204.
Definition kyber.h:88
PublicKeyOperation
Definition pk_keys.h:46
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
std::string to_string(ErrorType type)
Convert an ErrorType to string.
Definition exceptn.cpp:13