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

randombit / botan / 12370093224

17 Dec 2024 09:27AM UTC coverage: 91.262% (+0.003%) from 91.259%
12370093224

Pull #4478

github

web-flow
Merge b307ce8bd into 694344901
Pull Request #4478: Fix msvc warnings

93398 of 102340 relevant lines covered (91.26%)

11403200.06 hits per line

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

83.57
/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
McEliece_PrivateKey::McEliece_PrivateKey(RandomNumberGenerator& rng, size_t code_length, size_t t) {
93✔
48
   uint32_t ext_deg = ceil_log2(code_length);
93✔
49
   *this = generate_mceliece_key(rng, ext_deg, code_length, t);
93✔
50
}
93✔
51

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

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

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

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

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

73
   return plaintext;
2,641✔
74
}
×
75

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

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

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

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

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

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

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

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

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

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

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

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

168
   return true;
169
}
425✔
170

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

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

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

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

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

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

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

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

261
   return true;
262
}
263

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

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

281
namespace {
282

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

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

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

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

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

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

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

315
      const McEliece_PublicKey& m_key;
316
};
317

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

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

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

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

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

343
      const McEliece_PrivateKey& m_key;
344
};
345

346
}  // namespace
347

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

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

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

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