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

randombit / botan / 16245054916

13 Jul 2025 03:51AM UTC coverage: 90.581% (+0.009%) from 90.572%
16245054916

Pull #4982

github

web-flow
Merge 11956826b into e87c8e33a
Pull Request #4982: Enable and fix clang-tidy warning cert-err58-cpp

99186 of 109500 relevant lines covered (90.58%)

12549723.2 hits per line

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

83.64
/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/code_based_util.h>
20
#include <botan/internal/loadstor.h>
21
#include <botan/internal/mce_internal.h>
22
#include <botan/internal/pk_ops_impl.h>
23
#include <botan/internal/polyn_gf2m.h>
24
#include <botan/internal/stl_util.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;
93✔
32
McEliece_PrivateKey::~McEliece_PrivateKey() = default;
645✔
33

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

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

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

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

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

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

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

74
   return plaintext;
2,641✔
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);
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
   m_t = t;
86✔
118
   m_code_length = n;
86✔
119
}
86✔
120

121
secure_vector<uint8_t> McEliece_PrivateKey::private_key_bits() const {
270✔
122
   DER_Encoder enc;
270✔
123
   enc.start_sequence()
270✔
124
      .start_sequence()
270✔
125
      .encode(get_code_length())
270✔
126
      .encode(get_t())
270✔
127
      .end_cons()
270✔
128
      .encode(m_public_matrix, ASN1_Type::OctetString)
270✔
129
      .encode(m_g[0].encode(), ASN1_Type::OctetString);  // g as octet string
540✔
130
   enc.start_sequence();
270✔
131
   for(size_t i = 0; i < m_sqrtmod.size(); i++) {
4,544✔
132
      enc.encode(m_sqrtmod[i].encode(), ASN1_Type::OctetString);
12,822✔
133
   }
134
   enc.end_cons();
270✔
135
   secure_vector<uint8_t> enc_support;
270✔
136

137
   for(uint16_t Linv : m_Linv) {
387,758✔
138
      enc_support.push_back(get_byte<0>(Linv));
387,488✔
139
      enc_support.push_back(get_byte<1>(Linv));
387,488✔
140
   }
141
   enc.encode(enc_support, ASN1_Type::OctetString);
270✔
142
   secure_vector<uint8_t> enc_H;
270✔
143
   for(uint32_t coef : m_coeffs) {
8,999,790✔
144
      enc_H.push_back(get_byte<0>(coef));
8,999,520✔
145
      enc_H.push_back(get_byte<1>(coef));
8,999,520✔
146
      enc_H.push_back(get_byte<2>(coef));
8,999,520✔
147
      enc_H.push_back(get_byte<3>(coef));
8,999,520✔
148
   }
149
   enc.encode(enc_H, ASN1_Type::OctetString);
270✔
150
   enc.end_cons();
270✔
151
   return enc.get_contents();
540✔
152
}
540✔
153

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

157
   secure_vector<uint8_t> ciphertext;
85✔
158
   secure_vector<uint8_t> errors;
85✔
159
   mceliece_encrypt(ciphertext, errors, plaintext, *this, rng);
85✔
160

161
   secure_vector<uint8_t> plaintext_out;
85✔
162
   secure_vector<uint8_t> errors_out;
85✔
163
   mceliece_decrypt(plaintext_out, errors_out, ciphertext, *this);
85✔
164

165
   if(errors != errors_out || plaintext != plaintext_out) {
85✔
166
      return false;
167
   }
168

169
   return true;
170
}
425✔
171

172
McEliece_PrivateKey::McEliece_PrivateKey(std::span<const uint8_t> key_bits) {
89✔
173
   size_t n = 0;
89✔
174
   size_t t = 0;
89✔
175
   secure_vector<uint8_t> enc_g;
89✔
176
   BER_Decoder dec_base(key_bits);
89✔
177
   BER_Decoder dec = dec_base.start_sequence()
178✔
178
                        .start_sequence()
178✔
179
                        .decode(n)
89✔
180
                        .decode(t)
89✔
181
                        .end_cons()
89✔
182
                        .decode(m_public_matrix, ASN1_Type::OctetString)
89✔
183
                        .decode(enc_g, ASN1_Type::OctetString);
89✔
184

185
   if(t == 0 || n == 0) {
89✔
186
      throw Decoding_Error("invalid McEliece parameters");
×
187
   }
188

189
   uint32_t ext_deg = ceil_log2(n);
89✔
190
   m_code_length = n;
89✔
191
   m_t = t;
89✔
192
   m_codimension = (ext_deg * t);
89✔
193
   m_dimension = (n - m_codimension);
89✔
194

195
   auto sp_field = std::make_shared<GF2m_Field>(ext_deg);
89✔
196
   m_g = {polyn_gf2m(enc_g, sp_field)};
267✔
197
   if(m_g[0].get_degree() != static_cast<int>(t)) {
89✔
198
      throw Decoding_Error("degree of decoded Goppa polynomial is incorrect");
×
199
   }
200
   BER_Decoder dec2 = dec.start_sequence();
89✔
201
   for(uint32_t i = 0; i < t / 2; i++) {
1,471✔
202
      secure_vector<uint8_t> sqrt_enc;
1,382✔
203
      dec2.decode(sqrt_enc, ASN1_Type::OctetString);
1,382✔
204
      while(sqrt_enc.size() < (t * 2)) {
1,382✔
205
         // ensure that the length is always t
206
         sqrt_enc.push_back(0);
×
207
         sqrt_enc.push_back(0);
×
208
      }
209
      if(sqrt_enc.size() != t * 2) {
1,382✔
210
         throw Decoding_Error("length of square root polynomial entry is too large");
×
211
      }
212
      m_sqrtmod.push_back(polyn_gf2m(sqrt_enc, sp_field));
2,764✔
213
   }
1,382✔
214
   secure_vector<uint8_t> enc_support;
89✔
215
   BER_Decoder dec3 = dec2.end_cons().decode(enc_support, ASN1_Type::OctetString);
89✔
216
   if(enc_support.size() % 2) {
89✔
217
      throw Decoding_Error("encoded support has odd length");
×
218
   }
219
   if(enc_support.size() / 2 != n) {
89✔
220
      throw Decoding_Error("encoded support has length different from code length");
×
221
   }
222
   for(uint32_t i = 0; i < n * 2; i += 2) {
124,057✔
223
      gf2m el = (enc_support[i] << 8) | enc_support[i + 1];
123,968✔
224
      m_Linv.push_back(el);
123,968✔
225
   }
226
   secure_vector<uint8_t> enc_H;
89✔
227
   dec3.decode(enc_H, ASN1_Type::OctetString).end_cons();
89✔
228
   if(enc_H.size() % 4) {
89✔
229
      throw Decoding_Error("encoded parity check matrix has length which is not a multiple of four");
×
230
   }
231
   if(enc_H.size() / 4 != bit_size_to_32bit_size(m_codimension) * m_code_length) {
89✔
232
      throw Decoding_Error("encoded parity check matrix has wrong length");
×
233
   }
234

235
   for(uint32_t i = 0; i < enc_H.size(); i += 4) {
2,801,081✔
236
      uint32_t coeff = (enc_H[i] << 24) | (enc_H[i + 1] << 16) | (enc_H[i + 2] << 8) | enc_H[i + 3];
2,800,992✔
237
      m_coeffs.push_back(coeff);
2,800,992✔
238
   }
239
}
445✔
240

241
bool McEliece_PrivateKey::operator==(const McEliece_PrivateKey& other) const {
×
242
   if(*static_cast<const McEliece_PublicKey*>(this) != *static_cast<const McEliece_PublicKey*>(&other)) {
×
243
      return false;
244
   }
245
   if(m_g != other.m_g) {
×
246
      return false;
247
   }
248

249
   if(m_sqrtmod != other.m_sqrtmod) {
×
250
      return false;
251
   }
252
   if(m_Linv != other.m_Linv) {
×
253
      return false;
254
   }
255
   if(m_coeffs != other.m_coeffs) {
×
256
      return false;
257
   }
258

259
   if(m_codimension != other.m_codimension || m_dimension != other.m_dimension) {
×
260
      return false;
261
   }
262

263
   return true;
264
}
265

266
std::unique_ptr<Public_Key> McEliece_PrivateKey::public_key() const {
2✔
267
   return std::make_unique<McEliece_PublicKey>(get_public_matrix(), get_t(), get_code_length());
2✔
268
}
269

270
bool McEliece_PublicKey::operator==(const McEliece_PublicKey& other) const {
×
271
   if(m_public_matrix != other.m_public_matrix) {
×
272
      return false;
273
   }
274
   if(m_t != other.m_t) {
×
275
      return false;
276
   }
277
   if(m_code_length != other.m_code_length) {
×
278
      return false;
×
279
   }
280
   return true;
281
}
282

283
namespace {
284

285
class MCE_KEM_Encryptor final : public PK_Ops::KEM_Encryption_with_KDF {
286
   public:
287
      MCE_KEM_Encryptor(const McEliece_PublicKey& key, std::string_view kdf) :
91✔
288
            KEM_Encryption_with_KDF(kdf), m_key(key) {}
91✔
289

290
   private:
291
      size_t raw_kem_shared_key_length() const override {
2,556✔
292
         const size_t err_sz = (m_key.get_code_length() + 7) / 8;
2,556✔
293
         const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
2,556✔
294
         return ptext_sz + err_sz;
2,556✔
295
      }
296

297
      size_t encapsulated_key_length() const override { return (m_key.get_code_length() + 7) / 8; }
7,668✔
298

299
      void raw_kem_encrypt(std::span<uint8_t> out_encapsulated_key,
2,556✔
300
                           std::span<uint8_t> raw_shared_key,
301
                           RandomNumberGenerator& rng) override {
302
         secure_vector<uint8_t> plaintext = m_key.random_plaintext_element(rng);
2,556✔
303

304
         secure_vector<uint8_t> ciphertext, error_mask;
2,556✔
305
         mceliece_encrypt(ciphertext, error_mask, plaintext, m_key, rng);
2,556✔
306

307
         // TODO: Perhaps avoid the copies below
308
         BOTAN_ASSERT_NOMSG(out_encapsulated_key.size() == ciphertext.size());
2,556✔
309
         std::copy(ciphertext.begin(), ciphertext.end(), out_encapsulated_key.begin());
2,556✔
310

311
         BOTAN_ASSERT_NOMSG(raw_shared_key.size() == plaintext.size() + error_mask.size());
2,556✔
312
         BufferStuffer bs(raw_shared_key);
2,556✔
313
         bs.append(plaintext);
2,556✔
314
         bs.append(error_mask);
2,556✔
315
      }
7,668✔
316

317
      const McEliece_PublicKey& m_key;
318
};
319

320
class MCE_KEM_Decryptor final : public PK_Ops::KEM_Decryption_with_KDF {
321
   public:
322
      MCE_KEM_Decryptor(const McEliece_PrivateKey& key, std::string_view kdf) :
91✔
323
            KEM_Decryption_with_KDF(kdf), m_key(key) {}
91✔
324

325
   private:
326
      size_t raw_kem_shared_key_length() const override {
2,556✔
327
         const size_t err_sz = (m_key.get_code_length() + 7) / 8;
2,556✔
328
         const size_t ptext_sz = (m_key.get_message_word_bit_length() + 7) / 8;
2,556✔
329
         return ptext_sz + err_sz;
2,556✔
330
      }
331

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

334
      void raw_kem_decrypt(std::span<uint8_t> out_shared_key, std::span<const uint8_t> encapsulated_key) override {
2,556✔
335
         secure_vector<uint8_t> plaintext, error_mask;
2,556✔
336
         mceliece_decrypt(plaintext, error_mask, encapsulated_key.data(), encapsulated_key.size(), m_key);
2,556✔
337

338
         // TODO: perhaps avoid the copies below
339
         BOTAN_ASSERT_NOMSG(out_shared_key.size() == plaintext.size() + error_mask.size());
2,556✔
340
         BufferStuffer bs(out_shared_key);
2,556✔
341
         bs.append(plaintext);
2,556✔
342
         bs.append(error_mask);
2,556✔
343
      }
5,112✔
344

345
      const McEliece_PrivateKey& m_key;
346
};
347

348
}  // namespace
349

350
std::unique_ptr<Private_Key> McEliece_PublicKey::generate_another(RandomNumberGenerator& rng) const {
×
351
   return std::make_unique<McEliece_PrivateKey>(rng, get_code_length(), get_t());
×
352
}
353

354
std::unique_ptr<PK_Ops::KEM_Encryption> McEliece_PublicKey::create_kem_encryption_op(std::string_view params,
91✔
355
                                                                                     std::string_view provider) const {
356
   if(provider == "base" || provider.empty()) {
91✔
357
      return std::make_unique<MCE_KEM_Encryptor>(*this, params);
91✔
358
   }
359
   throw Provider_Not_Found(algo_name(), provider);
×
360
}
361

362
std::unique_ptr<PK_Ops::KEM_Decryption> McEliece_PrivateKey::create_kem_decryption_op(RandomNumberGenerator& /*rng*/,
91✔
363
                                                                                      std::string_view params,
364
                                                                                      std::string_view provider) const {
365
   if(provider == "base" || provider.empty()) {
91✔
366
      return std::make_unique<MCE_KEM_Decryptor>(*this, params);
91✔
367
   }
368
   throw Provider_Not_Found(algo_name(), provider);
×
369
}
370

371
}  // 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

© 2025 Coveralls, Inc