• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

randombit / botan / 24922258954

24 Apr 2026 12:23PM UTC coverage: 89.387% (-0.01%) from 89.401%
24922258954

push

github

web-flow
Merge pull request #5546 from randombit/jack/crldp-fixes

Fix some bugs relating to CRL distribution point handling

106817 of 119500 relevant lines covered (89.39%)

11464956.38 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

81.74
/src/lib/pubkey/mce/mceliece_key.cpp
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

26
namespace Botan {
27

28
McEliece_PrivateKey::McEliece_PrivateKey(const McEliece_PrivateKey&) = default;
×
29
McEliece_PrivateKey::McEliece_PrivateKey(McEliece_PrivateKey&&) noexcept = default;
×
30
McEliece_PrivateKey& McEliece_PrivateKey::operator=(const McEliece_PrivateKey&) = default;
×
31
McEliece_PrivateKey& McEliece_PrivateKey::operator=(McEliece_PrivateKey&&) noexcept = default;
94✔
32
McEliece_PrivateKey::~McEliece_PrivateKey() = default;
650✔
33

34
McEliece_PrivateKey::McEliece_PrivateKey(const polyn_gf2m& goppa_polyn,
94✔
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) :
94✔
39
      McEliece_PublicKey(public_matrix, goppa_polyn.get_degree(), inverse_support.size()),
94✔
40
      m_g{goppa_polyn},
188✔
41
      m_sqrtmod(square_root_matrix),
94✔
42
      m_Linv(inverse_support),
94✔
43
      m_coeffs(parity_check_matrix_coeffs),
94✔
44
      m_codimension(static_cast<size_t>(ceil_log2(inverse_support.size())) * goppa_polyn.get_degree()),
188✔
45
      m_dimension(inverse_support.size() - m_codimension) {}
188✔
46

47
// NOLINTNEXTLINE(*-member-init)
48
McEliece_PrivateKey::McEliece_PrivateKey(RandomNumberGenerator& rng, size_t code_length, size_t t) {
94✔
49
   const uint32_t ext_deg = ceil_log2(code_length);
94✔
50
   *this = generate_mceliece_key(rng, ext_deg, code_length, t);
94✔
51
}
94✔
52

53
const polyn_gf2m& McEliece_PrivateKey::get_goppa_polyn() const {
10,644✔
54
   return m_g[0];
10,644✔
55
}
56

57
size_t McEliece_PublicKey::get_message_word_bit_length() const {
10,518✔
58
   const size_t codimension = ceil_log2(m_code_length) * m_t;
10,518✔
59
   return m_code_length - codimension;
10,518✔
60
}
61

62
secure_vector<uint8_t> McEliece_PublicKey::random_plaintext_element(RandomNumberGenerator& rng) const {
2,652✔
63
   const size_t bits = get_message_word_bit_length();
2,652✔
64

65
   secure_vector<uint8_t> plaintext((bits + 7) / 8);
2,652✔
66
   rng.randomize(plaintext.data(), plaintext.size());
2,652✔
67

68
   // unset unused bits in the last plaintext byte
69
   if(const uint32_t used = bits % 8) {
2,652✔
70
      const uint8_t mask = (1 << used) - 1;
1,939✔
71
      plaintext[plaintext.size() - 1] &= mask;
1,939✔
72
   }
73

74
   return plaintext;
2,652✔
75
}
×
76

77
AlgorithmIdentifier McEliece_PublicKey::algorithm_identifier() const {
13✔
78
   return AlgorithmIdentifier(object_identifier(), AlgorithmIdentifier::USE_EMPTY_PARAM);
13✔
79
}
80

81
std::vector<uint8_t> McEliece_PublicKey::raw_public_key_bits() const {
×
82
   return m_public_matrix;
×
83
}
84

85
std::vector<uint8_t> McEliece_PublicKey::public_key_bits() const {
267✔
86
   std::vector<uint8_t> output;
267✔
87
   DER_Encoder(output)
534✔
88
      .start_sequence()
267✔
89
      .start_sequence()
267✔
90
      .encode(get_code_length())
267✔
91
      .encode(get_t())
267✔
92
      .end_cons()
267✔
93
      .encode(m_public_matrix, ASN1_Type::OctetString)
267✔
94
      .end_cons();
267✔
95
   return output;
267✔
96
}
×
97

98
size_t McEliece_PublicKey::key_length() const {
×
99
   return m_code_length;
×
100
}
101

102
size_t McEliece_PublicKey::estimated_strength() const {
2✔
103
   return mceliece_work_factor(m_code_length, m_t);
2✔
104
}
105

106
McEliece_PublicKey::McEliece_PublicKey(std::span<const uint8_t> key_bits) {
86✔
107
   BER_Decoder dec(key_bits, BER_Decoder::Limits::DER());
86✔
108
   size_t n = 0;
86✔
109
   size_t t = 0;
86✔
110
   dec.start_sequence()
172✔
111
      .start_sequence()
172✔
112
      .decode(n)
86✔
113
      .decode(t)
86✔
114
      .end_cons()
86✔
115
      .decode(m_public_matrix, ASN1_Type::OctetString)
86✔
116
      .end_cons();
86✔
117

118
   if(n == 0 || t == 0) {
86✔
119
      throw Decoding_Error("Invalid McEliece parameters");
×
120
   }
121

122
   // GF(2^m) field requires extension degree in [2, 16]
123
   const size_t ext_deg = ceil_log2(n);
86✔
124
   if(ext_deg < 2 || ext_deg > 16) {
86✔
125
      throw Decoding_Error("McEliece code length out of supported range");
×
126
   }
127

128
   // Since ext_deg >= 2, t >= n already implies ext_deg * t > n
129
   if(t >= n) {
86✔
130
      throw Decoding_Error("McEliece parameters are inconsistent");
×
131
   }
132

133
   const size_t codimension = ext_deg * t;
86✔
134

135
   // codimension must be strictly less than n, otherwise the code has no message bits
136
   if(codimension >= n) {
86✔
137
      throw Decoding_Error("McEliece parameters are inconsistent");
×
138
   }
139

140
   const size_t dimension = n - codimension;
86✔
141

142
   // public matrix is a dimension x codimension binary matrix stored as uint32_t rows
143
   const size_t expected_pubmat_size = dimension * bit_size_to_32bit_size(codimension) * sizeof(uint32_t);
86✔
144
   if(m_public_matrix.size() != expected_pubmat_size) {
86✔
145
      throw Decoding_Error("McEliece public matrix size does not match parameters");
×
146
   }
147

148
   m_t = t;
86✔
149
   m_code_length = n;
86✔
150
}
86✔
151

152
secure_vector<uint8_t> McEliece_PrivateKey::private_key_bits() const {
270✔
153
   DER_Encoder enc;
270✔
154
   enc.start_sequence()
270✔
155
      .start_sequence()
270✔
156
      .encode(get_code_length())
270✔
157
      .encode(get_t())
270✔
158
      .end_cons()
270✔
159
      .encode(m_public_matrix, ASN1_Type::OctetString)
270✔
160
      .encode(m_g[0].encode(), ASN1_Type::OctetString);  // g as octet string
540✔
161
   enc.start_sequence();
270✔
162
   for(const auto& x : m_sqrtmod) {
4,544✔
163
      enc.encode(x.encode(), ASN1_Type::OctetString);
12,822✔
164
   }
165
   enc.end_cons();
270✔
166
   secure_vector<uint8_t> enc_support;
270✔
167

168
   for(const uint16_t Linv : m_Linv) {
387,758✔
169
      enc_support.push_back(get_byte<0>(Linv));
387,488✔
170
      enc_support.push_back(get_byte<1>(Linv));
387,488✔
171
   }
172
   enc.encode(enc_support, ASN1_Type::OctetString);
270✔
173
   secure_vector<uint8_t> enc_H;
270✔
174
   for(const uint32_t coef : m_coeffs) {
8,999,790✔
175
      enc_H.push_back(get_byte<0>(coef));
8,999,520✔
176
      enc_H.push_back(get_byte<1>(coef));
8,999,520✔
177
      enc_H.push_back(get_byte<2>(coef));
8,999,520✔
178
      enc_H.push_back(get_byte<3>(coef));
8,999,520✔
179
   }
180
   enc.encode(enc_H, ASN1_Type::OctetString);
270✔
181
   enc.end_cons();
270✔
182
   return enc.get_contents();
540✔
183
}
540✔
184

185
bool McEliece_PrivateKey::check_key(RandomNumberGenerator& rng, bool /*unused*/) const {
85✔
186
   const secure_vector<uint8_t> plaintext = this->random_plaintext_element(rng);
85✔
187

188
   secure_vector<uint8_t> ciphertext;
85✔
189
   secure_vector<uint8_t> errors;
85✔
190
   mceliece_encrypt(ciphertext, errors, plaintext, *this, rng);
85✔
191

192
   secure_vector<uint8_t> plaintext_out;
85✔
193
   secure_vector<uint8_t> errors_out;
85✔
194
   mceliece_decrypt(plaintext_out, errors_out, ciphertext, *this);
85✔
195

196
   if(errors != errors_out || plaintext != plaintext_out) {
85✔
197
      return false;
198
   }
199

200
   return true;
201
}
425✔
202

203
McEliece_PrivateKey::McEliece_PrivateKey(std::span<const uint8_t> key_bits) {
89✔
204
   size_t n = 0;
89✔
205
   size_t t = 0;
89✔
206
   secure_vector<uint8_t> enc_g;
89✔
207
   BER_Decoder dec_base(key_bits, BER_Decoder::Limits::DER());
89✔
208
   BER_Decoder dec = dec_base.start_sequence()
178✔
209
                        .start_sequence()
178✔
210
                        .decode(n)
89✔
211
                        .decode(t)
89✔
212
                        .end_cons()
89✔
213
                        .decode(m_public_matrix, ASN1_Type::OctetString)
89✔
214
                        .decode(enc_g, ASN1_Type::OctetString);
89✔
215

216
   if(t == 0 || n == 0) {
89✔
217
      throw Decoding_Error("invalid McEliece parameters");
×
218
   }
219

220
   const uint32_t ext_deg = ceil_log2(n);
89✔
221

222
   if(ext_deg < 2 || ext_deg > 16) {
89✔
223
      throw Decoding_Error("McEliece code length out of supported range");
×
224
   }
225

226
   // Since ext_deg >= 2, t >= n already implies ext_deg * t > n
227
   if(t >= n) {
89✔
228
      throw Decoding_Error("McEliece parameters are inconsistent");
×
229
   }
230

231
   const size_t codimension = ext_deg * t;
89✔
232

233
   if(codimension >= n) {
89✔
234
      throw Decoding_Error("McEliece parameters are inconsistent");
×
235
   }
236

237
   const size_t dimension = n - codimension;
89✔
238
   const size_t expected_pubmat_size = dimension * bit_size_to_32bit_size(codimension) * sizeof(uint32_t);
89✔
239
   if(m_public_matrix.size() != expected_pubmat_size) {
89✔
240
      throw Decoding_Error("McEliece public matrix size does not match parameters");
×
241
   }
242

243
   m_code_length = n;
89✔
244
   m_t = t;
89✔
245
   m_codimension = codimension;
89✔
246
   m_dimension = dimension;
89✔
247

248
   auto sp_field = std::make_shared<GF2m_Field>(ext_deg);
89✔
249
   m_g = {polyn_gf2m(enc_g, sp_field)};
267✔
250
   if(m_g[0].get_degree() != static_cast<int>(t)) {
89✔
251
      throw Decoding_Error("degree of decoded Goppa polynomial is incorrect");
×
252
   }
253
   BER_Decoder dec2 = dec.start_sequence();
89✔
254
   for(uint32_t i = 0; i < t / 2; i++) {
1,471✔
255
      secure_vector<uint8_t> sqrt_enc;
1,382✔
256
      dec2.decode(sqrt_enc, ASN1_Type::OctetString);
1,382✔
257
      while(sqrt_enc.size() < (t * 2)) {
1,382✔
258
         // ensure that the length is always t
259
         sqrt_enc.push_back(0);
×
260
         sqrt_enc.push_back(0);
×
261
      }
262
      if(sqrt_enc.size() != t * 2) {
1,382✔
263
         throw Decoding_Error("length of square root polynomial entry is too large");
×
264
      }
265
      m_sqrtmod.push_back(polyn_gf2m(sqrt_enc, sp_field));
2,764✔
266
   }
1,382✔
267
   secure_vector<uint8_t> enc_support;
89✔
268
   BER_Decoder dec3 = dec2.end_cons().decode(enc_support, ASN1_Type::OctetString);
89✔
269
   if(enc_support.size() % 2 != 0) {
89✔
270
      throw Decoding_Error("encoded support has odd length");
×
271
   }
272
   if(enc_support.size() / 2 != n) {
89✔
273
      throw Decoding_Error("encoded support has length different from code length");
×
274
   }
275
   for(uint32_t i = 0; i < n * 2; i += 2) {
124,057✔
276
      const gf2m el = (enc_support[i] << 8) | enc_support[i + 1];
123,968✔
277
      m_Linv.push_back(el);
123,968✔
278
   }
279
   secure_vector<uint8_t> enc_H;
89✔
280
   dec3.decode(enc_H, ASN1_Type::OctetString).end_cons();
89✔
281
   if(enc_H.size() % 4 != 0) {
89✔
282
      throw Decoding_Error("encoded parity check matrix has length which is not a multiple of four");
×
283
   }
284
   if(enc_H.size() / 4 != bit_size_to_32bit_size(m_codimension) * m_code_length) {
89✔
285
      throw Decoding_Error("encoded parity check matrix has wrong length");
×
286
   }
287

288
   for(uint32_t i = 0; i < enc_H.size(); i += 4) {
2,801,081✔
289
      const uint32_t coeff = (enc_H[i] << 24) | (enc_H[i + 1] << 16) | (enc_H[i + 2] << 8) | enc_H[i + 3];
2,800,992✔
290
      m_coeffs.push_back(coeff);
2,800,992✔
291
   }
292
}
445✔
293

294
bool McEliece_PrivateKey::operator==(const McEliece_PrivateKey& other) const {
×
295
   if(*static_cast<const McEliece_PublicKey*>(this) != *static_cast<const McEliece_PublicKey*>(&other)) {
×
296
      return false;
297
   }
298
   if(m_g != other.m_g) {
×
299
      return false;
300
   }
301

302
   if(m_sqrtmod != other.m_sqrtmod) {
×
303
      return false;
304
   }
305
   if(m_Linv != other.m_Linv) {
×
306
      return false;
307
   }
308
   if(m_coeffs != other.m_coeffs) {
×
309
      return false;
310
   }
311

312
   if(m_codimension != other.m_codimension || m_dimension != other.m_dimension) {
×
313
      return false;
314
   }
315

316
   return true;
317
}
318

319
std::unique_ptr<Public_Key> McEliece_PrivateKey::public_key() const {
3✔
320
   return std::make_unique<McEliece_PublicKey>(get_public_matrix(), get_t(), get_code_length());
3✔
321
}
322

323
bool McEliece_PublicKey::operator==(const McEliece_PublicKey& other) const {
×
324
   if(m_public_matrix != other.m_public_matrix) {
×
325
      return false;
326
   }
327
   if(m_t != other.m_t) {
×
328
      return false;
329
   }
330
   if(m_code_length != other.m_code_length) {
×
331
      return false;
×
332
   }
333
   return true;
334
}
335

336
namespace {
337

338
class MCE_KEM_Encryptor final : public PK_Ops::KEM_Encryption_with_KDF {
339
   public:
340
      MCE_KEM_Encryptor(const McEliece_PublicKey& key, std::string_view kdf) :
102✔
341
            KEM_Encryption_with_KDF(kdf), m_key(key) {}
102✔
342

343
   private:
344
      size_t raw_kem_shared_key_length() const override {
2,589✔
345
         const size_t err_sz = (m_key.get_code_length() + 7) / 8;
2,589✔
346
         const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
2,589✔
347
         return ptext_sz + err_sz;
2,589✔
348
      }
349

350
      size_t encapsulated_key_length() const override { return (m_key.get_code_length() + 7) / 8; }
7,701✔
351

352
      void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
2,567✔
353
                           std::span<uint8_t> raw_shared_key,
354
                           RandomNumberGenerator& rng) override {
355
         secure_vector<uint8_t> plaintext = m_key.random_plaintext_element(rng);
2,567✔
356

357
         secure_vector<uint8_t> ciphertext;
2,567✔
358
         secure_vector<uint8_t> error_mask;
2,567✔
359
         mceliece_encrypt(ciphertext, error_mask, plaintext, m_key, rng);
2,567✔
360

361
         // TODO: Perhaps avoid the copies below
362
         BOTAN_ASSERT_NOMSG(out_encapsulated_key.size() == ciphertext.size());
2,567✔
363
         std::copy(ciphertext.begin(), ciphertext.end(), out_encapsulated_key.begin());
2,567✔
364

365
         BOTAN_ASSERT_NOMSG(raw_shared_key.size() == plaintext.size() + error_mask.size());
2,567✔
366
         BufferStuffer bs(raw_shared_key);
2,567✔
367
         bs.append(plaintext);
2,567✔
368
         bs.append(error_mask);
2,567✔
369
      }
7,701✔
370

371
      const McEliece_PublicKey& m_key;
372
};
373

374
class MCE_KEM_Decryptor final : public PK_Ops::KEM_Decryption_with_KDF {
375
   public:
376
      MCE_KEM_Decryptor(const McEliece_PrivateKey& key, std::string_view kdf) :
102✔
377
            KEM_Decryption_with_KDF(kdf), m_key(key) {}
102✔
378

379
   private:
380
      size_t raw_kem_shared_key_length() const override {
2,616✔
381
         const size_t err_sz = (m_key.get_code_length() + 7) / 8;
2,616✔
382
         const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
2,616✔
383
         return ptext_sz + err_sz;
2,616✔
384
      }
385

386
      size_t encapsulated_key_length() const override { return (m_key.get_code_length() + 7) / 8; }
×
387

388
      void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) override {
2,576✔
389
         secure_vector<uint8_t> plaintext;
2,576✔
390
         secure_vector<uint8_t> error_mask;
2,576✔
391
         mceliece_decrypt(plaintext, error_mask, encapsulated_key.data(), encapsulated_key.size(), m_key);
2,576✔
392

393
         // TODO: perhaps avoid the copies below
394
         BOTAN_ASSERT_NOMSG(out_shared_key.size() == plaintext.size() + error_mask.size());
2,576✔
395
         BufferStuffer bs(out_shared_key);
2,576✔
396
         bs.append(plaintext);
2,576✔
397
         bs.append(error_mask);
2,576✔
398
      }
5,152✔
399

400
      const McEliece_PrivateKey& m_key;
401
};
402

403
}  // namespace
404

405
std::unique_ptr<Private_Key> McEliece_PublicKey::generate_another(RandomNumberGenerator& rng) const {
×
406
   return std::make_unique<McEliece_PrivateKey>(rng, get_code_length(), get_t());
×
407
}
408

409
std::unique_ptr<PK_Ops::KEM_Encryption> McEliece_PublicKey::create_kem_encryption_op(std::string_view params,
102✔
410
                                                                                     std::string_view provider) const {
411
   if(provider == "base" || provider.empty()) {
102✔
412
      return std::make_unique<MCE_KEM_Encryptor>(*this, params);
102✔
413
   }
414
   throw Provider_Not_Found(algo_name(), provider);
×
415
}
416

417
std::unique_ptr<PK_Ops::KEM_Decryption> McEliece_PrivateKey::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
102✔
418
                                                                                      std::string_view params,
419
                                                                                      std::string_view provider) const {
420
   if(provider == "base" || provider.empty()) {
102✔
421
      return std::make_unique<MCE_KEM_Decryptor>(*this, params);
102✔
422
   }
423
   throw Provider_Not_Found(algo_name(), provider);
×
424
}
425

426
}  // namespace Botan
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc