Botan 3.6.1
Crypto and TLS for C&
frodokem.cpp
Go to the documentation of this file.
1/*
2 * FrodoKEM implemenation
3 * Based on the MIT licensed reference implementation by the designers
4 * (https://github.com/microsoft/PQCrypto-LWEKE/tree/master/src)
5 *
6 * The Fellowship of the FrodoKEM:
7 * (C) 2023 Jack Lloyd
8 * 2023 René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
9 *
10 * Botan is released under the Simplified BSD License (see license.txt)
11 */
12
13#include <botan/frodokem.h>
14
15#include <botan/assert.h>
16#include <botan/hex.h>
17#include <botan/rng.h>
18#include <botan/xof.h>
19#include <botan/internal/ct_utils.h>
20#include <botan/internal/frodo_constants.h>
21#include <botan/internal/frodo_matrix.h>
22#include <botan/internal/frodo_types.h>
23#include <botan/internal/loadstor.h>
24#include <botan/internal/pk_ops_impl.h>
25#include <botan/internal/stl_util.h>
26
27#include <algorithm>
28#include <memory>
29#include <tuple>
30#include <vector>
31
32namespace Botan {
33
34class FrodoKEM_PublicKeyInternal {
35 public:
36 FrodoKEM_PublicKeyInternal(FrodoKEMConstants constants, FrodoSeedA seed_a, FrodoMatrix b) :
37 m_constants(std::move(constants)), m_seed_a(std::move(seed_a)), m_b(std::move(b)) {
38 auto& shake = m_constants.SHAKE_XOF();
39 shake.update(serialize());
40 m_hash = shake.output<FrodoPublicKeyHash>(m_constants.len_sec_bytes());
41 }
42
43 const FrodoKEMConstants& constants() const { return m_constants; }
44
45 const FrodoSeedA& seed_a() const { return m_seed_a; }
46
47 const FrodoMatrix& b() const { return m_b; }
48
49 const FrodoPublicKeyHash& hash() const { return m_hash; }
50
51 std::vector<uint8_t> serialize() const { return concat<std::vector<uint8_t>>(seed_a(), b().pack(m_constants)); }
52
53 private:
54 FrodoKEMConstants m_constants;
55 FrodoSeedA m_seed_a;
56 FrodoMatrix m_b;
57 FrodoPublicKeyHash m_hash;
58};
59
60class FrodoKEM_PrivateKeyInternal {
61 public:
62 FrodoKEM_PrivateKeyInternal(FrodoSeedS s, FrodoMatrix s_trans) :
63 m_s(std::move(s)), m_s_trans(std::move(s_trans)) {}
64
65 const FrodoSeedS& s() const { return m_s; }
66
67 const FrodoMatrix& s_trans() const { return m_s_trans; }
68
69 constexpr void _const_time_poison() const { CT::poison_all(m_s, m_s_trans); }
70
71 constexpr void _const_time_unpoison() const { CT::unpoison_all(m_s, m_s_trans); }
72
73 private:
74 FrodoSeedS m_s;
75 FrodoMatrix m_s_trans;
76};
77
78//
79// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
80//
81
82class Frodo_KEM_Encryptor final : public PK_Ops::KEM_Encryption_with_KDF {
83 public:
84 Frodo_KEM_Encryptor(std::shared_ptr<FrodoKEM_PublicKeyInternal> key, std::string_view kdf) :
85 KEM_Encryption_with_KDF(kdf), m_public_key(std::move(key)) {}
86
87 size_t raw_kem_shared_key_length() const override { return m_public_key->constants().len_sec_bytes(); }
88
89 size_t encapsulated_key_length() const override { return m_public_key->constants().len_ct_bytes(); }
90
91 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
92 std::span<uint8_t> out_shared_key,
93 RandomNumberGenerator& rng) override {
94 const auto& consts = m_public_key->constants();
95 auto& shake = consts.SHAKE_XOF();
96 auto sample_generator = FrodoMatrix::make_sample_generator(consts, shake);
97
98 BufferStuffer out_ct_bs(out_encapsulated_key);
99
100 auto c_1 = out_ct_bs.next<FrodoPackedMatrix>(consts.len_packed_b_bytes());
101 auto c_2 = out_ct_bs.next<FrodoPackedMatrix>(consts.len_packed_c_bytes());
102 auto salt = out_ct_bs.next<FrodoSalt>(consts.len_salt_bytes());
103
104 BOTAN_ASSERT_NOMSG(out_ct_bs.full());
105
106 const auto u = rng.random_vec<FrodoPlaintext>(consts.len_sec_bytes());
107 rng.randomize(salt);
108
109 CT::poison(u);
110
111 shake.update(m_public_key->hash());
112 shake.update(u);
113 shake.update(salt);
114 const auto seed_se = shake.output<FrodoSeedSE>(consts.len_se_bytes());
115 const auto k = shake.output<FrodoIntermediateSharedSecret>(consts.len_sec_bytes());
116 shake.clear();
117
118 shake.update(consts.encapsulation_domain_separator());
119 shake.update(seed_se);
120
121 const auto s_p = sample_generator(std::tuple(consts.n_bar(), consts.n()));
122
123 const auto e_p = sample_generator(std::tuple(consts.n_bar(), consts.n()));
124
125 const auto b_p = FrodoMatrix::mul_add_sa_plus_e(consts, s_p, e_p, m_public_key->seed_a());
126
127 b_p.pack(consts, c_1);
128
129 const auto e_pp = sample_generator(std::tuple(consts.n_bar(), consts.n_bar()));
130 shake.clear();
131
132 const auto v = FrodoMatrix::mul_add_sb_plus_e(consts, m_public_key->b(), s_p, e_pp);
133
134 const auto encoded = FrodoMatrix::encode(consts, u);
135
136 const auto c = FrodoMatrix::add(consts, v, encoded);
137
138 c.pack(consts, c_2);
139
140 shake.update(out_encapsulated_key);
141 shake.update(k);
142 shake.output(out_shared_key);
143
144 CT::unpoison_all(out_shared_key, out_encapsulated_key);
145 }
146
147 private:
148 std::shared_ptr<FrodoKEM_PublicKeyInternal> m_public_key;
149};
150
151class Frodo_KEM_Decryptor final : public PK_Ops::KEM_Decryption_with_KDF {
152 public:
153 Frodo_KEM_Decryptor(std::shared_ptr<FrodoKEM_PublicKeyInternal> public_key,
154 std::shared_ptr<FrodoKEM_PrivateKeyInternal> private_key,
155 std::string_view kdf) :
156 KEM_Decryption_with_KDF(kdf), m_public_key(std::move(public_key)), m_private_key(std::move(private_key)) {}
157
158 size_t raw_kem_shared_key_length() const override { return m_public_key->constants().len_sec_bytes(); }
159
160 size_t encapsulated_key_length() const override { return m_public_key->constants().len_ct_bytes(); }
161
162 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) override {
163 auto scope = CT::scoped_poison(*m_private_key);
164
165 const auto& consts = m_public_key->constants();
166 auto& shake = consts.SHAKE_XOF();
167 auto sample_generator = FrodoMatrix::make_sample_generator(consts, shake);
168
169 if(encapsulated_key.size() != consts.len_ct_bytes()) {
170 throw Invalid_Argument("FrodoKEM ciphertext does not have the correct byte count");
171 }
172
173 BufferSlicer ct_bs(encapsulated_key);
174 auto c_1 = ct_bs.take<FrodoPackedMatrix>(consts.len_packed_b_bytes());
175 auto c_2 = ct_bs.take<FrodoPackedMatrix>(consts.len_packed_c_bytes());
176 auto salt = ct_bs.take<FrodoSalt>(consts.len_salt_bytes());
177 BOTAN_ASSERT_NOMSG(ct_bs.empty());
178
179 const auto b_p = FrodoMatrix::unpack(consts, {consts.n_bar(), consts.n()}, c_1);
180 const auto c = FrodoMatrix::unpack(consts, {consts.n_bar(), consts.n_bar()}, c_2);
181
182 const auto w = FrodoMatrix::mul_bs(consts, b_p, m_private_key->s_trans());
183 const auto m = FrodoMatrix::sub(consts, c, w);
184
185 const auto seed_u_p = m.decode(consts);
186
187 shake.update(m_public_key->hash());
188 shake.update(seed_u_p);
189 shake.update(salt);
190
191 const auto seed_se_p = shake.output<FrodoSeedSE>(consts.len_se_bytes());
192 const auto k_p = shake.output<FrodoIntermediateSharedSecret>(consts.len_sec_bytes());
193 shake.clear();
194
195 shake.update(consts.encapsulation_domain_separator());
196 shake.update(seed_se_p);
197 const auto s_p = sample_generator(std::tuple(consts.n_bar(), consts.n()));
198
199 const auto e_p = sample_generator(std::tuple(consts.n_bar(), consts.n()));
200
201 auto b_pp = FrodoMatrix::mul_add_sa_plus_e(consts, s_p, e_p, m_public_key->seed_a());
202
203 const auto e_pp = sample_generator(std::tuple(consts.n_bar(), consts.n_bar()));
204 shake.clear();
205
206 const auto v = FrodoMatrix::mul_add_sb_plus_e(consts, m_public_key->b(), s_p, e_pp);
207
208 const auto encoded = FrodoMatrix::encode(consts, seed_u_p);
209 auto c_p = FrodoMatrix::add(consts, v, encoded);
210
211 // b_p and c are unpacked values that are reduced by definition.
212 // b_pp and c_p are calculated values that need the reduction for
213 // an unambiguous comparison that is required next.
214 b_pp.reduce(consts);
215 c_p.reduce(consts);
216
217 // The spec concats the matrices b_p and c (b_pp and c_p respectively)
218 // and performs a single CT comparison. For convenience we compare the
219 // matrices individually in CT and CT-&& the resulting masks.
220 const auto cmp = b_p.constant_time_compare(b_pp) & c.constant_time_compare(c_p);
221
222 std::vector<uint8_t> k_bar(consts.len_sec_bytes(), 0);
223 CT::conditional_copy_mem(cmp, k_bar.data(), k_p.data(), m_private_key->s().data(), consts.len_sec_bytes());
224
225 shake.update(encapsulated_key);
226 shake.update(k_bar);
227 shake.output(out_shared_key);
228
229 CT::unpoison(out_shared_key);
230 }
231
232 private:
233 std::shared_ptr<FrodoKEM_PublicKeyInternal> m_public_key;
234 std::shared_ptr<FrodoKEM_PrivateKeyInternal> m_private_key;
235};
236
237//
238// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
239//
240
241FrodoKEM_PublicKey::FrodoKEM_PublicKey(std::span<const uint8_t> pub_key, FrodoKEMMode mode) {
242 FrodoKEMConstants consts(mode);
243 if(pub_key.size() != consts.len_public_key_bytes()) {
244 throw Invalid_Argument("FrodoKEM public key does not have the correct byte count");
245 }
246
247 BufferSlicer pk_bs(pub_key);
248 auto seed_a = pk_bs.copy<FrodoSeedA>(consts.len_a_bytes());
249 const auto packed_b = pk_bs.take<FrodoPackedMatrix>(consts.d() * consts.n() * consts.n_bar() / 8);
250 BOTAN_ASSERT_NOMSG(pk_bs.empty());
251
252 auto b = FrodoMatrix::unpack(consts, std::tuple(consts.n(), consts.n_bar()), packed_b);
253
254 m_public = std::make_shared<FrodoKEM_PublicKeyInternal>(std::move(consts), std::move(seed_a), std::move(b));
255}
256
257FrodoKEM_PublicKey::FrodoKEM_PublicKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
258 FrodoKEM_PublicKey(key_bits, FrodoKEMMode(alg_id.oid())) {}
259
261 m_public = std::make_shared<FrodoKEM_PublicKeyInternal>(
262 other.m_public->constants(), other.m_public->seed_a(), other.m_public->b());
263}
264
266 if(this != &other) {
267 m_public = std::make_shared<FrodoKEM_PublicKeyInternal>(
268 other.m_public->constants(), other.m_public->seed_a(), other.m_public->b());
269 }
270 return *this;
271}
272
276
278 return m_public->constants().mode().object_identifier();
279}
280
282 return m_public->constants().n();
283}
284
286 return m_public->constants().estimated_strength();
287}
288
289std::vector<uint8_t> FrodoKEM_PublicKey::raw_public_key_bits() const {
290 return concat<std::vector<uint8_t>>(m_public->seed_a(), m_public->b().pack(m_public->constants()));
291}
292
293std::vector<uint8_t> FrodoKEM_PublicKey::public_key_bits() const {
294 // Currently, there isn't a finalized definition of an ASN.1 structure for
295 // FrodoKEM public keys. Therefore, we return the raw public key bits.
296 return raw_public_key_bits();
297}
298
300 return true;
301}
302
303std::unique_ptr<Private_Key> FrodoKEM_PublicKey::generate_another(RandomNumberGenerator& rng) const {
304 return std::make_unique<FrodoKEM_PrivateKey>(rng, m_public->constants().mode());
305}
306
307std::unique_ptr<PK_Ops::KEM_Encryption> FrodoKEM_PublicKey::create_kem_encryption_op(std::string_view params,
308 std::string_view provider) const {
309 if(provider.empty() || provider == "base") {
310 return std::make_unique<Frodo_KEM_Encryptor>(m_public, params);
311 }
312 throw Provider_Not_Found(algo_name(), provider);
313}
314
315//
316// - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
317//
318
320 FrodoKEMConstants consts(mode);
321 auto& shake = consts.SHAKE_XOF();
322
323 auto s = rng.random_vec<FrodoSeedS>(consts.len_sec_bytes());
324 const auto seed_se = rng.random_vec<FrodoSeedSE>(consts.len_se_bytes());
325 const auto z = rng.random_vec<FrodoSeedZ>(consts.len_a_bytes());
326
327 CT::poison_all(s, seed_se);
328
329 shake.update(z);
330 auto seed_a = shake.output<FrodoSeedA>(consts.len_a_bytes());
331 shake.clear();
332
333 shake.update(consts.keygen_domain_separator());
334 shake.update(seed_se);
335
336 auto sample_generator = FrodoMatrix::make_sample_generator(consts, shake);
337 auto s_trans = sample_generator(std::tuple(consts.n_bar(), consts.n()));
338 auto e = sample_generator(std::tuple(consts.n(), consts.n_bar()));
339 shake.clear();
340
341 auto b = FrodoMatrix::mul_add_as_plus_e(consts, s_trans, e, seed_a);
342
343 CT::unpoison_all(s, s_trans, b);
344
345 m_public = std::make_shared<FrodoKEM_PublicKeyInternal>(std::move(consts), std::move(seed_a), std::move(b));
346 m_private = std::make_shared<FrodoKEM_PrivateKeyInternal>(std::move(s), std::move(s_trans));
347}
348
349FrodoKEM_PrivateKey::FrodoKEM_PrivateKey(std::span<const uint8_t> sk, FrodoKEMMode mode) {
350 FrodoKEMConstants consts(mode);
351
352 if(sk.size() != consts.len_private_key_bytes()) {
353 throw Invalid_Argument("FrodoKEM private key does not have the correct byte count");
354 }
355
356 BufferSlicer sk_bs(sk);
357 auto s = sk_bs.copy<FrodoSeedS>(consts.len_sec_bytes());
358 auto seed_a = sk_bs.copy<FrodoSeedA>(consts.len_a_bytes());
359 const auto packed_b = sk_bs.take<FrodoPackedMatrix>(consts.d() * consts.n() * consts.n_bar() / 8);
360 const auto s_trans_bytes = sk_bs.take<FrodoSerializedMatrix>(consts.n_bar() * consts.n() * 2);
361 const auto pkh = sk_bs.copy<FrodoPublicKeyHash>(consts.len_sec_bytes());
362 BOTAN_ASSERT_NOMSG(sk_bs.empty());
363
364 auto b = FrodoMatrix::unpack(consts, std::tuple(consts.n(), consts.n_bar()), packed_b);
365 auto s_trans = FrodoMatrix::deserialize({consts.n_bar(), consts.n()}, s_trans_bytes);
366
367 m_public = std::make_shared<FrodoKEM_PublicKeyInternal>(std::move(consts), std::move(seed_a), std::move(b));
368 m_private = std::make_shared<FrodoKEM_PrivateKeyInternal>(std::move(s), std::move(s_trans));
369
370 BOTAN_STATE_CHECK(pkh == m_public->hash());
371}
372
373FrodoKEM_PrivateKey::FrodoKEM_PrivateKey(const AlgorithmIdentifier& alg_id, std::span<const uint8_t> key_bits) :
374 FrodoKEM_PrivateKey(key_bits, FrodoKEMMode(alg_id.oid())) {}
375
376std::unique_ptr<Public_Key> FrodoKEM_PrivateKey::public_key() const {
377 return std::make_unique<FrodoKEM_PublicKey>(*this);
378}
379
381 return raw_private_key_bits(); // TODO: check if we need to do something else here
382}
383
385 return concat<secure_vector<uint8_t>>(m_private->s(),
386 m_public->seed_a(),
387 m_public->b().pack(m_public->constants()),
388 m_private->s_trans().serialize(),
389 m_public->hash());
390}
391
392std::unique_ptr<PK_Ops::KEM_Decryption> FrodoKEM_PrivateKey::create_kem_decryption_op(RandomNumberGenerator& rng,
393 std::string_view params,
394 std::string_view provider) const {
395 BOTAN_UNUSED(rng);
396 if(provider.empty() || provider == "base") {
397 return std::make_unique<Frodo_KEM_Decryptor>(m_public, m_private, params);
398 }
399 throw Provider_Not_Found(algo_name(), provider);
400}
401
402} // namespace Botan
#define BOTAN_UNUSED
Definition assert.h:118
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:59
#define BOTAN_STATE_CHECK(expr)
Definition assert.h:41
auto copy(const size_t count)
Definition stl_util.h:89
bool empty() const
Definition stl_util.h:129
std::span< const uint8_t > take(const size_t count)
Definition stl_util.h:98
FrodoDomainSeparator keygen_domain_separator() const
size_t len_private_key_bytes() const
size_t len_public_key_bytes() const
FrodoKEM_PrivateKey(RandomNumberGenerator &rng, FrodoKEMMode mode)
Definition frodokem.cpp:319
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
Definition frodokem.cpp:392
secure_vector< uint8_t > raw_private_key_bits() const override
Definition frodokem.cpp:384
secure_vector< uint8_t > private_key_bits() const override
Definition frodokem.cpp:380
std::unique_ptr< Public_Key > public_key() const override
Definition frodokem.cpp:376
size_t estimated_strength() const override
Definition frodokem.cpp:285
AlgorithmIdentifier algorithm_identifier() const override
Definition frodokem.cpp:273
std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const override
Definition frodokem.cpp:307
size_t key_length() const override
Definition frodokem.cpp:281
std::shared_ptr< FrodoKEM_PublicKeyInternal > m_public
Definition frodokem.h:76
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
Definition frodokem.cpp:303
std::vector< uint8_t > public_key_bits() const override
Definition frodokem.cpp:293
FrodoKEM_PublicKey & operator=(const FrodoKEM_PublicKey &other)
Definition frodokem.cpp:265
std::string algo_name() const override
Definition frodokem.h:47
std::vector< uint8_t > raw_public_key_bits() const override
Definition frodokem.cpp:289
bool check_key(RandomNumberGenerator &, bool) const override
Definition frodokem.cpp:299
OID object_identifier() const override
Definition frodokem.cpp:277
static FrodoMatrix mul_add_sa_plus_e(const FrodoKEMConstants &constants, const FrodoMatrix &s, const FrodoMatrix &e, StrongSpan< const FrodoSeedA > seed_a)
static std::function< FrodoMatrix(const Dimensions &dimensions)> make_sample_generator(const FrodoKEMConstants &constants, Botan::XOF &shake)
static FrodoMatrix mul_add_sb_plus_e(const FrodoKEMConstants &constants, const FrodoMatrix &b, const FrodoMatrix &s, const FrodoMatrix &e)
static FrodoMatrix mul_add_as_plus_e(const FrodoKEMConstants &constants, const FrodoMatrix &s, const FrodoMatrix &e, StrongSpan< const FrodoSeedA > seed_a)
static FrodoMatrix sub(const FrodoKEMConstants &constants, const FrodoMatrix &a, const FrodoMatrix &b)
static FrodoMatrix add(const FrodoKEMConstants &constants, const FrodoMatrix &a, const FrodoMatrix &b)
static FrodoMatrix encode(const FrodoKEMConstants &constants, StrongSpan< const FrodoPlaintext > in)
static FrodoMatrix unpack(const FrodoKEMConstants &constants, const Dimensions &dimensions, StrongSpan< const FrodoPackedMatrix > packed_bytes)
static FrodoMatrix mul_bs(const FrodoKEMConstants &constants, const FrodoMatrix &b_p, const FrodoMatrix &s)
static FrodoMatrix deserialize(const Dimensions &dimensions, StrongSpan< const FrodoSerializedMatrix > bytes)
KEM_Decryption_with_KDF(std::string_view kdf)
Definition pk_ops.cpp:238
KEM_Encryption_with_KDF(std::string_view kdf)
Definition pk_ops.cpp:205
void random_vec(std::span< uint8_t > v)
Definition rng.h:180
void update(std::span< const uint8_t > input)
Definition xof.h:142
int(* final)(unsigned char *, CTX *)
constexpr void unpoison_all(Ts &&... ts)
Definition ct_utils.h:201
constexpr auto scoped_poison(const Ts &... xs)
Definition ct_utils.h:216
constexpr Mask< T > conditional_copy_mem(Mask< T > mask, T *to, const T *from0, const T *from1, size_t elems)
Definition ct_utils.h:699
constexpr void poison_all(Ts &&... ts)
Definition ct_utils.h:195
constexpr void unpoison(const T *p, size_t n)
Definition ct_utils.h:64
constexpr void poison(const T *p, size_t n)
Definition ct_utils.h:53
Strong< secure_vector< uint8_t >, struct FrodoSeedSE_ > FrodoSeedSE
Definition frodo_types.h:29
Strong< secure_vector< uint8_t >, struct FrodoIntermediateSharedSecret_ > FrodoIntermediateSharedSecret
Definition frodo_types.h:56
Strong< std::vector< uint8_t >, struct FrodoPublicKeyHash_ > FrodoPublicKeyHash
Definition frodo_types.h:38
constexpr auto concat(Rs &&... ranges)
Definition stl_util.h:263
const SIMD_8x32 & b
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:61
Strong< std::vector< uint8_t >, struct FrodoSeedA_ > FrodoSeedA
Definition frodo_types.h:23
Strong< secure_vector< uint8_t >, struct FrodoPlaintext_ > FrodoPlaintext
Definition frodo_types.h:50
Strong< std::vector< uint8_t >, struct FrodoSalt_ > FrodoSalt
Definition frodo_types.h:53
Strong< std::vector< uint8_t >, struct FrodoPackedMatrix_ > FrodoPackedMatrix
Definition frodo_types.h:41
Strong< secure_vector< uint8_t >, struct FrodoSeedS_ > FrodoSeedS
Definition frodo_types.h:26