Botan 3.12.0
Crypto and TLS for C&
mceliece_key.cpp
Go to the documentation of this file.
1/*
2 * (C) Copyright Projet SECRET, INRIA, Rocquencourt
3 * (C) Bhaskar Biswas and Nicolas Sendrier
4 *
5 * (C) 2014 cryptosource GmbH
6 * (C) 2014 Falko Strenzke fstrenzke@cryptosource.de
7 * (C) 2015 Jack Lloyd
8 *
9 * Botan is released under the Simplified BSD License (see license.txt)
10 *
11 */
12
13#include <botan/mceliece.h>
14
15#include <botan/ber_dec.h>
16#include <botan/der_enc.h>
17#include <botan/rng.h>
18#include <botan/internal/bit_ops.h>
19#include <botan/internal/buffer_stuffer.h>
20#include <botan/internal/code_based_util.h>
21#include <botan/internal/loadstor.h>
22#include <botan/internal/mce_internal.h>
23#include <botan/internal/pk_ops_impl.h>
24#include <botan/internal/polyn_gf2m.h>
25
26namespace Botan {
27
31McEliece_PrivateKey& McEliece_PrivateKey::operator=(McEliece_PrivateKey&&) noexcept = default;
33
35 const std::vector<uint32_t>& parity_check_matrix_coeffs,
36 const std::vector<polyn_gf2m>& square_root_matrix,
37 const std::vector<gf2m>& inverse_support,
38 const std::vector<uint8_t>& public_matrix) :
39 McEliece_PublicKey(public_matrix, goppa_polyn.get_degree(), inverse_support.size()),
40 m_g{goppa_polyn},
41 m_sqrtmod(square_root_matrix),
42 m_Linv(inverse_support),
43 m_coeffs(parity_check_matrix_coeffs),
44 m_codimension(static_cast<size_t>(ceil_log2(inverse_support.size())) * goppa_polyn.get_degree()),
45 m_dimension(inverse_support.size() - m_codimension) {}
46
47// NOLINTNEXTLINE(*-member-init)
49 const uint32_t ext_deg = ceil_log2(code_length);
50 *this = generate_mceliece_key(rng, ext_deg, code_length, t);
51}
52
54 return m_g[0];
55}
56
58 const size_t codimension = ceil_log2(m_code_length) * m_t;
59 return m_code_length - codimension;
60}
61
63 const size_t bits = get_message_word_bit_length();
64
65 secure_vector<uint8_t> plaintext((bits + 7) / 8);
66 rng.randomize(plaintext.data(), plaintext.size());
67
68 // unset unused bits in the last plaintext byte
69 if(const uint32_t used = bits % 8) {
70 const uint8_t mask = (1 << used) - 1;
71 plaintext[plaintext.size() - 1] &= mask;
72 }
73
74 return plaintext;
75}
76
80
81std::vector<uint8_t> McEliece_PublicKey::raw_public_key_bits() const {
82 return m_public_matrix;
83}
84
85std::vector<uint8_t> McEliece_PublicKey::public_key_bits() const {
86 std::vector<uint8_t> output;
87 DER_Encoder(output)
91 .encode(get_t())
92 .end_cons()
94 .end_cons();
95 return output;
96}
97
99 return m_code_length;
100}
101
105
106McEliece_PublicKey::McEliece_PublicKey(std::span<const uint8_t> key_bits) {
107 BER_Decoder dec(key_bits, BER_Decoder::Limits::DER());
108 size_t n = 0;
109 size_t t = 0;
110 dec.start_sequence()
112 .decode(n)
113 .decode(t)
114 .end_cons()
116 .end_cons()
117 .verify_end();
118
119 if(n == 0 || t == 0) {
120 throw Decoding_Error("Invalid McEliece parameters");
121 }
122
123 // GF(2^m) field requires extension degree in [2, 16]
124 const size_t ext_deg = ceil_log2(n);
125 if(ext_deg < 2 || ext_deg > 16) {
126 throw Decoding_Error("McEliece code length out of supported range");
127 }
128
129 // Since ext_deg >= 2, t >= n already implies ext_deg * t > n
130 if(t >= n) {
131 throw Decoding_Error("McEliece parameters are inconsistent");
132 }
133
134 const size_t codimension = ext_deg * t;
135
136 // codimension must be strictly less than n, otherwise the code has no message bits
137 if(codimension >= n) {
138 throw Decoding_Error("McEliece parameters are inconsistent");
139 }
140
141 const size_t dimension = n - codimension;
142
143 // public matrix is a dimension x codimension binary matrix stored as uint32_t rows
144 const size_t expected_pubmat_size = dimension * bit_size_to_32bit_size(codimension) * sizeof(uint32_t);
145 if(m_public_matrix.size() != expected_pubmat_size) {
146 throw Decoding_Error("McEliece public matrix size does not match parameters");
147 }
148
149 m_t = t;
150 m_code_length = n;
151}
152
154 DER_Encoder enc;
155 enc.start_sequence()
158 .encode(get_t())
159 .end_cons()
161 .encode(m_g[0].encode(), ASN1_Type::OctetString); // g as octet string
162 enc.start_sequence();
163 for(const auto& x : m_sqrtmod) {
164 enc.encode(x.encode(), ASN1_Type::OctetString);
165 }
166 enc.end_cons();
167 secure_vector<uint8_t> enc_support;
168
169 for(const uint16_t Linv : m_Linv) {
170 enc_support.push_back(get_byte<0>(Linv));
171 enc_support.push_back(get_byte<1>(Linv));
172 }
173 enc.encode(enc_support, ASN1_Type::OctetString);
175 for(const uint32_t coef : m_coeffs) {
176 enc_H.push_back(get_byte<0>(coef));
177 enc_H.push_back(get_byte<1>(coef));
178 enc_H.push_back(get_byte<2>(coef));
179 enc_H.push_back(get_byte<3>(coef));
180 }
181 enc.encode(enc_H, ASN1_Type::OctetString);
182 enc.end_cons();
183 return enc.get_contents();
184}
185
186bool McEliece_PrivateKey::check_key(RandomNumberGenerator& rng, bool /*unused*/) const {
187 const secure_vector<uint8_t> plaintext = this->random_plaintext_element(rng);
188
189 secure_vector<uint8_t> ciphertext;
191 mceliece_encrypt(ciphertext, errors, plaintext, *this, rng);
192
193 secure_vector<uint8_t> plaintext_out;
194 secure_vector<uint8_t> errors_out;
195 mceliece_decrypt(plaintext_out, errors_out, ciphertext, *this);
196
197 if(errors != errors_out || plaintext != plaintext_out) {
198 return false;
199 }
200
201 return true;
202}
203
204McEliece_PrivateKey::McEliece_PrivateKey(std::span<const uint8_t> key_bits) {
205 size_t n = 0;
206 size_t t = 0;
208 BER_Decoder dec_base(key_bits, BER_Decoder::Limits::DER());
209 BER_Decoder dec = dec_base.start_sequence();
210 dec.start_sequence().decode(n).decode(t).end_cons();
212
213 if(t == 0 || n == 0) {
214 throw Decoding_Error("invalid McEliece parameters");
215 }
216
217 const uint32_t ext_deg = ceil_log2(n);
218
219 if(ext_deg < 2 || ext_deg > 16) {
220 throw Decoding_Error("McEliece code length out of supported range");
221 }
222
223 // Since ext_deg >= 2, t >= n already implies ext_deg * t > n
224 if(t >= n) {
225 throw Decoding_Error("McEliece parameters are inconsistent");
226 }
227
228 const size_t codimension = ext_deg * t;
229
230 if(codimension >= n) {
231 throw Decoding_Error("McEliece parameters are inconsistent");
232 }
233
234 const size_t dimension = n - codimension;
235 const size_t expected_pubmat_size = dimension * bit_size_to_32bit_size(codimension) * sizeof(uint32_t);
236 if(m_public_matrix.size() != expected_pubmat_size) {
237 throw Decoding_Error("McEliece public matrix size does not match parameters");
238 }
239
240 m_code_length = n;
241 m_t = t;
242 m_codimension = codimension;
243 m_dimension = dimension;
244
245 auto sp_field = std::make_shared<GF2m_Field>(ext_deg);
246 m_g = {polyn_gf2m(enc_g, sp_field)};
247 if(m_g[0].get_degree() != static_cast<int>(t)) {
248 throw Decoding_Error("degree of decoded Goppa polynomial is incorrect");
249 }
250 BER_Decoder dec2 = dec.start_sequence();
251 for(uint32_t i = 0; i < t / 2; i++) {
252 secure_vector<uint8_t> sqrt_enc;
253 dec2.decode(sqrt_enc, ASN1_Type::OctetString);
254 while(sqrt_enc.size() < (t * 2)) {
255 // ensure that the length is always t
256 sqrt_enc.push_back(0);
257 sqrt_enc.push_back(0);
258 }
259 if(sqrt_enc.size() != t * 2) {
260 throw Decoding_Error("length of square root polynomial entry is too large");
261 }
262 m_sqrtmod.push_back(polyn_gf2m(sqrt_enc, sp_field));
263 }
264 secure_vector<uint8_t> enc_support;
265 dec2.end_cons();
266 dec.decode(enc_support, ASN1_Type::OctetString);
267 if(enc_support.size() % 2 != 0) {
268 throw Decoding_Error("encoded support has odd length");
269 }
270 if(enc_support.size() / 2 != n) {
271 throw Decoding_Error("encoded support has length different from code length");
272 }
273 for(uint32_t i = 0; i < n * 2; i += 2) {
274 const gf2m el = (enc_support[i] << 8) | enc_support[i + 1];
275 m_Linv.push_back(el);
276 }
279 if(enc_H.size() % 4 != 0) {
280 throw Decoding_Error("encoded parity check matrix has length which is not a multiple of four");
281 }
282 if(enc_H.size() / 4 != bit_size_to_32bit_size(m_codimension) * m_code_length) {
283 throw Decoding_Error("encoded parity check matrix has wrong length");
284 }
285
286 for(uint32_t i = 0; i < enc_H.size(); i += 4) {
287 const uint32_t coeff = (enc_H[i] << 24) | (enc_H[i + 1] << 16) | (enc_H[i + 2] << 8) | enc_H[i + 3];
288 m_coeffs.push_back(coeff);
289 }
290}
291
293 if(*static_cast<const McEliece_PublicKey*>(this) != *static_cast<const McEliece_PublicKey*>(&other)) {
294 return false;
295 }
296 if(m_g != other.m_g) {
297 return false;
298 }
299
300 if(m_sqrtmod != other.m_sqrtmod) {
301 return false;
302 }
303 if(m_Linv != other.m_Linv) {
304 return false;
305 }
306 if(m_coeffs != other.m_coeffs) {
307 return false;
308 }
309
310 if(m_codimension != other.m_codimension || m_dimension != other.m_dimension) {
311 return false;
312 }
313
314 return true;
315}
316
317std::unique_ptr<Public_Key> McEliece_PrivateKey::public_key() const {
318 return std::make_unique<McEliece_PublicKey>(get_public_matrix(), get_t(), get_code_length());
319}
320
322 if(m_public_matrix != other.m_public_matrix) {
323 return false;
324 }
325 if(m_t != other.m_t) {
326 return false;
327 }
328 if(m_code_length != other.m_code_length) {
329 return false;
330 }
331 return true;
332}
333
334namespace {
335
336class MCE_KEM_Encryptor final : public PK_Ops::KEM_Encryption_with_KDF {
337 public:
338 MCE_KEM_Encryptor(const McEliece_PublicKey& key, std::string_view kdf) :
339 KEM_Encryption_with_KDF(kdf), m_key(key) {}
340
341 private:
342 size_t raw_kem_shared_key_length() const override {
343 const size_t err_sz = (m_key.get_code_length() + 7) / 8;
344 const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
345 return ptext_sz + err_sz;
346 }
347
348 size_t encapsulated_key_length() const override { return (m_key.get_code_length() + 7) / 8; }
349
350 void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
351 std::span<uint8_t> raw_shared_key,
352 RandomNumberGenerator& rng) override {
353 secure_vector<uint8_t> plaintext = m_key.random_plaintext_element(rng);
354
355 secure_vector<uint8_t> ciphertext;
356 secure_vector<uint8_t> error_mask;
357 mceliece_encrypt(ciphertext, error_mask, plaintext, m_key, rng);
358
359 // TODO: Perhaps avoid the copies below
360 BOTAN_ASSERT_NOMSG(out_encapsulated_key.size() == ciphertext.size());
361 std::copy(ciphertext.begin(), ciphertext.end(), out_encapsulated_key.begin());
362
363 BOTAN_ASSERT_NOMSG(raw_shared_key.size() == plaintext.size() + error_mask.size());
364 BufferStuffer bs(raw_shared_key);
365 bs.append(plaintext);
366 bs.append(error_mask);
367 }
368
369 const McEliece_PublicKey& m_key;
370};
371
372class MCE_KEM_Decryptor final : public PK_Ops::KEM_Decryption_with_KDF {
373 public:
374 MCE_KEM_Decryptor(const McEliece_PrivateKey& key, std::string_view kdf) :
375 KEM_Decryption_with_KDF(kdf), m_key(key) {}
376
377 private:
378 size_t raw_kem_shared_key_length() const override {
379 const size_t err_sz = (m_key.get_code_length() + 7) / 8;
380 const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
381 return ptext_sz + err_sz;
382 }
383
384 size_t encapsulated_key_length() const override { return (m_key.get_code_length() + 7) / 8; }
385
386 void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) override {
387 secure_vector<uint8_t> plaintext;
388 secure_vector<uint8_t> error_mask;
389 mceliece_decrypt(plaintext, error_mask, encapsulated_key.data(), encapsulated_key.size(), m_key);
390
391 // TODO: perhaps avoid the copies below
392 BOTAN_ASSERT_NOMSG(out_shared_key.size() == plaintext.size() + error_mask.size());
393 BufferStuffer bs(out_shared_key);
394 bs.append(plaintext);
395 bs.append(error_mask);
396 }
397
398 const McEliece_PrivateKey& m_key;
399};
400
401} // namespace
402
403std::unique_ptr<Private_Key> McEliece_PublicKey::generate_another(RandomNumberGenerator& rng) const {
404 return std::make_unique<McEliece_PrivateKey>(rng, get_code_length(), get_t());
405}
406
407std::unique_ptr<PK_Ops::KEM_Encryption> McEliece_PublicKey::create_kem_encryption_op(std::string_view params,
408 std::string_view provider) const {
409 if(provider == "base" || provider.empty()) {
410 return std::make_unique<MCE_KEM_Encryptor>(*this, params);
411 }
412 throw Provider_Not_Found(algo_name(), provider);
413}
414
415std::unique_ptr<PK_Ops::KEM_Decryption> McEliece_PrivateKey::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
416 std::string_view params,
417 std::string_view provider) const {
418 if(provider == "base" || provider.empty()) {
419 return std::make_unique<MCE_KEM_Decryptor>(*this, params);
420 }
421 throw Provider_Not_Found(algo_name(), provider);
422}
423
424} // namespace Botan
#define BOTAN_ASSERT_NOMSG(expr)
Definition assert.h:75
virtual OID object_identifier() const
Definition pk_keys.cpp:22
static Limits DER()
Definition ber_dec.h:35
void push_back(const BER_Object &obj)
Definition ber_dec.cpp:500
BER_Decoder & decode(bool &out)
Definition ber_dec.h:220
BER_Decoder & verify_end()
Definition ber_dec.cpp:381
BER_Decoder & end_cons()
Definition ber_dec.cpp:524
BER_Decoder start_sequence()
Definition ber_dec.h:160
secure_vector< uint8_t > get_contents()
Definition der_enc.cpp:134
DER_Encoder & start_sequence()
Definition der_enc.h:67
DER_Encoder & end_cons()
Definition der_enc.cpp:173
DER_Encoder & encode(bool b)
Definition der_enc.cpp:245
secure_vector< uint8_t > private_key_bits() const override
McEliece_PrivateKey(RandomNumberGenerator &rng, size_t code_length, size_t t)
std::unique_ptr< Public_Key > public_key() const override
std::unique_ptr< PK_Ops::KEM_Decryption > create_kem_decryption_op(RandomNumberGenerator &rng, std::string_view params, std::string_view provider) const override
const polyn_gf2m & get_goppa_polyn() const
bool operator==(const McEliece_PrivateKey &other) const
bool check_key(RandomNumberGenerator &rng, bool strong) const override
secure_vector< uint8_t > random_plaintext_element(RandomNumberGenerator &rng) const
size_t get_message_word_bit_length() const
std::vector< uint8_t > raw_public_key_bits() const override
size_t get_t() const
Definition mceliece.h:50
std::unique_ptr< PK_Ops::KEM_Encryption > create_kem_encryption_op(std::string_view params, std::string_view provider) const override
McEliece_PublicKey(std::span< const uint8_t > key_bits)
std::string algo_name() const override
Definition mceliece.h:38
std::vector< uint8_t > public_key_bits() const override
std::unique_ptr< Private_Key > generate_another(RandomNumberGenerator &rng) const final
std::vector< uint8_t > m_public_matrix
Definition mceliece.h:74
const std::vector< uint8_t > & get_public_matrix() const
Definition mceliece.h:56
size_t estimated_strength() const override
size_t get_code_length() const
Definition mceliece.h:52
bool operator==(const McEliece_PublicKey &other) const
AlgorithmIdentifier algorithm_identifier() const override
size_t key_length() const override
void randomize(std::span< uint8_t > output)
Definition rng.h:75
constexpr uint8_t get_byte(T input)
Definition loadstor.h:79
void mceliece_decrypt(secure_vector< uint8_t > &plaintext_out, secure_vector< uint8_t > &error_mask_out, const secure_vector< uint8_t > &ciphertext, const McEliece_PrivateKey &key)
void mceliece_encrypt(secure_vector< uint8_t > &ciphertext_out, secure_vector< uint8_t > &error_mask_out, const secure_vector< uint8_t > &plaintext, const McEliece_PublicKey &key, RandomNumberGenerator &rng)
Definition mceliece.cpp:109
constexpr uint8_t ceil_log2(T x)
Definition bit_ops.h:140
McEliece_PrivateKey generate_mceliece_key(RandomNumberGenerator &rng, size_t ext_deg, size_t code_length, size_t t)
size_t mceliece_work_factor(size_t n, size_t t)
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
size_t bit_size_to_32bit_size(size_t bit_size)
uint16_t gf2m