Botan 3.10.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/exceptn.h>
18#include <botan/pk_keys.h>
19
20#include <span>
21
22#if !defined(BOTAN_HAS_KYBER_90S) && !defined(BOTAN_HAS_KYBER) && !defined(BOTAN_HAS_ML_KEM)
23static_assert(
24 false,
25 "botan module 'kyber_common' is useful only when enabling at least one of those modules: 'kyber', 'kyber_90s', 'ml_kem'");
26#endif
27
28namespace Botan {
29
31 public:
32 enum Mode : uint8_t /* NOLINT(*-use-enum-class) */ {
33 // Kyber512 as proposed in round 3 of the NIST competition
34 Kyber512_R3 = 0,
35 // Kyber768 as proposed in round 3 of the NIST competition
36 Kyber768_R3 = 1,
37 // Kyber1024 as proposed in round 3 of the NIST competition
38 Kyber1024_R3 = 2,
39
40 Kyber512 BOTAN_DEPRECATED("Use Kyber512_R3") = Kyber512_R3,
41 Kyber768 BOTAN_DEPRECATED("Use Kyber768_R3") = Kyber768_R3,
42 Kyber1024 BOTAN_DEPRECATED("Use Kyber1024_R3") = Kyber1024_R3,
43
44 ML_KEM_512 = 3,
45 ML_KEM_768 = 4,
46 ML_KEM_1024 = 5,
47
48 Kyber512_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 6,
49 Kyber768_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 7,
50 Kyber1024_90s BOTAN_DEPRECATED("Kyber 90s mode is deprecated") = 8,
51 };
52
53 // NOLINTNEXTLINE(*-explicit-conversions)
54 KyberMode(Mode mode);
55
56 explicit KyberMode(const OID& oid);
57 explicit KyberMode(std::string_view str);
58
59 OID object_identifier() const;
60 std::string to_string() const;
61
62 Mode mode() const { return m_mode; }
63
64 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_90s() const;
65
66 BOTAN_DEPRECATED("Kyber 90s mode is deprecated") bool is_modern() const;
67
68 bool is_ml_kem() const;
69
70 bool is_kyber_round3() const;
71
72 bool is_available() const;
73
74 bool operator==(const KyberMode& other) const { return m_mode == other.m_mode; }
75
76 bool operator!=(const KyberMode& other) const { return !(*this == other); }
77
78 private:
79 Mode m_mode;
80};
81
82/// Byte encoding format of ML-KEM and ML-DSA the private key
83enum class MlPrivateKeyFormat : uint8_t {
84 /// Only supported for ML-KEM/ML-DSA keys:
85 /// - ML-KEM: 64-byte seed: d || z
86 /// - ML-DSA: 32-byte seed: xi (private_key_bits_with_format not yet
87 /// yet supported for ML-DSA)
88 Seed,
89 /// The expanded format, i.e., the format specified in FIPS-203/204.
91};
92
93class Kyber_PublicKeyInternal;
94class Kyber_PrivateKeyInternal;
95
96class BOTAN_PUBLIC_API(3, 0) Kyber_PublicKey : public virtual Public_Key {
97 public:
98 Kyber_PublicKey(std::span<const uint8_t> pub_key, KyberMode mode);
99
100 Kyber_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
101
102 Kyber_PublicKey(const Kyber_PublicKey& other);
103 Kyber_PublicKey& operator=(const Kyber_PublicKey& other) = default;
106
107 ~Kyber_PublicKey() override = default;
108
109 std::string algo_name() const override;
110
111 AlgorithmIdentifier algorithm_identifier() const override;
112
113 OID object_identifier() const override;
114
115 size_t key_length() const override;
116
117 size_t estimated_strength() const override;
118
119 std::vector<uint8_t> raw_public_key_bits() const override;
120
121 std::vector<uint8_t> public_key_bits() const override;
122
123 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
124
125 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
126
127 bool supports_operation(PublicKeyOperation op) const override {
128 return (op == PublicKeyOperation::KeyEncapsulation);
129 }
130
131 std::unique_ptr<PK_Ops::KEM_Encryption> create_kem_encryption_op(std::string_view params,
132 std::string_view provider) const override;
133
134 KyberMode mode() const;
135
136 protected:
137 Kyber_PublicKey() = default;
138
139 static std::shared_ptr<Kyber_PublicKeyInternal> initialize_from_encoding(std::span<const uint8_t> pub_key,
140 KyberMode m);
141
142 protected:
145
146 std::shared_ptr<Kyber_PublicKeyInternal> m_public; // NOLINT(*non-private-member-variable*)
147};
148
151
153 public virtual Private_Key {
154 public:
155 /**
156 * Create a new private key. The private key will be encoded as the 64 byte
157 * seed.
158 */
160
161 /**
162 * Import a private key using its key bytes. Supported are key bytes as
163 * 64-byte seeds (not supported for Kyber Round 3 instances),
164 * as well as the expanded encoding specified by FIPS 203. Note that the
165 * encoding used in this constructor is reflected by the calls for
166 * private_key_bits, private_key_info, etc.
167 */
168 Kyber_PrivateKey(std::span<const uint8_t> sk, KyberMode mode);
169
170 /**
171 * Import a private key using its key bytes. Supported are key bytes as
172 * 64-byte seeds (not supported for Kyber Round 3 instances),
173 * as well as the expanded encoding specified by FIPS 203. Note that the
174 * encoding used in this constructor is reflected by the calls for
175 * private_key_bits, private_key_info, etc.
176 */
177 Kyber_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits);
178
179 std::unique_ptr<Public_Key> public_key() const override;
180
181 secure_vector<uint8_t> private_key_bits() const override;
182
183 secure_vector<uint8_t> raw_private_key_bits() const override;
184
185 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
186
187 std::unique_ptr<PK_Ops::KEM_Decryption> create_kem_decryption_op(RandomNumberGenerator& rng,
188 std::string_view params,
189 std::string_view provider) const override;
190
191 /**
192 * The private key format from which the key was loaded. It is the format
193 * used for the private_key_bits(), raw_private_key_bits() andFIPS
194 * private_key_info() methods.
195 *
196 * Note that keys in Seed format can be serialized to Expanded format
197 * using the method private_key_bits_with_format but NOT the other way
198 * around.
199 */
200 MlPrivateKeyFormat private_key_format() const;
201
202 /**
203 * Encode the private key in the specified format. Note that the seed
204 * format is only available for new ML-KEM keys and those loaded from
205 * seeds.
206 * @throws Encoding_Error if the private key cannot be encoded in the
207 * requested format.
208 */
209 secure_vector<uint8_t> private_key_bits_with_format(MlPrivateKeyFormat format) const;
210
211 private:
213
214 std::shared_ptr<Kyber_PrivateKeyInternal> m_private;
215};
216
218
219} // namespace Botan
220
221#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:76
Mode mode() const
Definition kyber.h:62
Kyber_PublicKey & operator=(const Kyber_PublicKey &other)=default
Kyber_PublicKey(Kyber_PublicKey &&other)=default
std::shared_ptr< Kyber_PublicKeyInternal > m_public
Definition kyber.h:146
~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:127
Kyber_PublicKey & operator=(Kyber_PublicKey &&other)=default
int(* final)(unsigned char *, CTX *)
MlPrivateKeyFormat
Byte encoding format of ML-KEM and ML-DSA the private key.
Definition kyber.h:83
@ Expanded
The expanded format, i.e., the format specified in FIPS-203/204.
PublicKeyOperation
Definition pk_keys.h:46
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69