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

randombit / botan / 5134090420

31 May 2023 03:12PM UTC coverage: 91.721% (-0.3%) from 91.995%
5134090420

push

github

randombit
Merge GH #3565 Disable noisy/pointless pylint warnings

76048 of 82912 relevant lines covered (91.72%)

11755290.1 hits per line

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

86.54
/src/tests/test_pubkey.cpp
1
/*
2
* (C) 2009,2015 Jack Lloyd
3
* (C) 2017 Ribose Inc
4
*
5
* Botan is released under the Simplified BSD License (see license.txt)
6
*/
7

8
#include "test_pubkey.h"
9

10
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
11

12
   #include "test_rng.h"
13

14
   #include <botan/data_src.h>
15
   #include <botan/hex.h>
16
   #include <botan/pk_algs.h>
17
   #include <botan/pkcs8.h>
18
   #include <botan/pubkey.h>
19
   #include <botan/x509_key.h>
20

21
   #if defined(BOTAN_HAS_HMAC_DRBG)
22
      #include <botan/hmac_drbg.h>
23
   #endif
24

25
namespace Botan_Tests {
26

27
void check_invalid_signatures(Test::Result& result,
1,867✔
28
                              Botan::PK_Verifier& verifier,
29
                              const std::vector<uint8_t>& message,
30
                              const std::vector<uint8_t>& signature) {
31
   const size_t tests_to_run = (Test::run_long_tests() ? 20 : 5);
1,867✔
32

33
   const std::vector<uint8_t> zero_sig(signature.size());
1,867✔
34
   result.test_eq("all zero signature invalid", verifier.verify_message(message, zero_sig), false);
1,867✔
35

36
   for(size_t i = 0; i < tests_to_run; ++i) {
39,207✔
37
      const std::vector<uint8_t> bad_sig = Test::mutate_vec(signature);
37,340✔
38

39
      try {
37,340✔
40
         if(!result.test_eq("incorrect signature invalid", verifier.verify_message(message, bad_sig), false)) {
74,680✔
41
            result.test_note("Accepted invalid signature " + Botan::hex_encode(bad_sig));
×
42
         }
43
      } catch(std::exception& e) {
×
44
         result.test_note("Accepted invalid signature " + Botan::hex_encode(bad_sig));
×
45
         result.test_failure("Modified signature rejected with exception", e.what());
×
46
      }
×
47
   }
37,340✔
48
}
1,867✔
49

50
void check_invalid_ciphertexts(Test::Result& result,
252✔
51
                               Botan::PK_Decryptor& decryptor,
52
                               const std::vector<uint8_t>& plaintext,
53
                               const std::vector<uint8_t>& ciphertext) {
54
   const size_t tests_to_run = (Test::run_long_tests() ? 20 : 5);
252✔
55

56
   size_t ciphertext_accepted = 0, ciphertext_rejected = 0;
252✔
57

58
   for(size_t i = 0; i < tests_to_run; ++i) {
5,292✔
59
      const std::vector<uint8_t> bad_ctext = Test::mutate_vec(ciphertext);
5,040✔
60

61
      try {
5,040✔
62
         const Botan::secure_vector<uint8_t> decrypted = decryptor.decrypt(bad_ctext);
5,040✔
63
         ++ciphertext_accepted;
1,940✔
64

65
         if(!result.test_ne("incorrect ciphertext different", decrypted, plaintext)) {
5,820✔
66
            result.test_eq("used corrupted ciphertext", bad_ctext, ciphertext);
×
67
         }
68
      } catch(std::exception&) {
5,040✔
69
         ++ciphertext_rejected;
3,100✔
70
      }
3,100✔
71
   }
5,040✔
72

73
   result.test_note("Accepted " + std::to_string(ciphertext_accepted) + " invalid ciphertexts, rejected " +
504✔
74
                    std::to_string(ciphertext_rejected));
252✔
75
}
252✔
76

77
std::string PK_Test::choose_padding(const VarMap& vars, const std::string& pad_hdr) {
14,671✔
78
   if(!pad_hdr.empty()) {
14,671✔
79
      return pad_hdr;
16,201✔
80
   }
81
   return vars.get_opt_str("Padding", this->default_padding(vars));
26,449✔
82
}
83

84
std::vector<std::string> PK_Test::possible_providers(const std::string& /*params*/) {
16,234✔
85
   return Test::provider_filter({"base", "commoncrypto", "openssl", "tpm"});
81,170✔
86
}
87

88
Test::Result PK_Signature_Generation_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
1,161✔
89
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
1,161✔
90
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
1,161✔
91
   const std::string padding = choose_padding(vars, pad_hdr);
1,161✔
92

93
   std::ostringstream test_name;
1,161✔
94
   test_name << algo_name();
2,322✔
95
   if(vars.has_key("Group")) {
2,322✔
96
      test_name << "-" << vars.get_req_str("Group");
254✔
97
   }
98
   test_name << "/" << padding << " signature generation";
1,161✔
99

100
   Test::Result result(test_name.str());
1,161✔
101

102
   std::unique_ptr<Botan::Private_Key> privkey;
1,161✔
103
   try {
1,161✔
104
      privkey = load_private_key(vars);
1,161✔
105
   } catch(Botan::Lookup_Error& e) {
×
106
      result.note_missing(e.what());
×
107
      return result;
×
108
   }
×
109

110
   result.confirm("private key claims to support signatures",
2,322✔
111
                  privkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,161✔
112

113
   auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
1,161✔
114

115
   result.confirm("public key claims to support signatures",
2,322✔
116
                  privkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,161✔
117

118
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,161✔
119

120
   for(const auto& verify_provider : possible_providers(algo_name())) {
6,966✔
121
      std::unique_ptr<Botan::PK_Verifier> verifier;
4,644✔
122

123
      try {
4,644✔
124
         verifier =
4,644✔
125
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::Standard, verify_provider);
5,805✔
126
      } catch(Botan::Lookup_Error&) {
3,483✔
127
         //result.test_note("Skipping verifying with " + verify_provider);
128
         continue;
3,483✔
129
      }
3,483✔
130

131
      result.test_eq("KAT signature valid", verifier->verify_message(message, signature), true);
1,161✔
132

133
      check_invalid_signatures(result, *verifier, message, signature);
1,161✔
134

135
      result.test_eq("KAT signature valid (try 2)", verifier->verify_message(message, signature), true);
1,161✔
136

137
      verifiers.push_back(std::move(verifier));
1,161✔
138
   }
5,805✔
139

140
   for(const auto& sign_provider : possible_providers(algo_name())) {
6,966✔
141
      std::unique_ptr<Botan::PK_Signer> signer;
4,644✔
142

143
      std::vector<uint8_t> generated_signature;
4,644✔
144

145
      try {
4,644✔
146
         signer = std::make_unique<Botan::PK_Signer>(
4,644✔
147
            *privkey, Test::rng(), padding, Botan::Signature_Format::Standard, sign_provider);
5,805✔
148

149
         if(vars.has_key("Nonce")) {
2,322✔
150
            auto rng = test_rng(vars.get_req_bin("Nonce"));
446✔
151
            generated_signature = signer->sign_message(message, *rng);
446✔
152
         } else {
223✔
153
            generated_signature = signer->sign_message(message, Test::rng());
1,876✔
154
         }
155

156
         result.test_lte(
1,161✔
157
            "Generated signature within announced bound", generated_signature.size(), signer->signature_length());
158
      } catch(Botan::Lookup_Error&) {
3,483✔
159
         //result.test_note("Skipping signing with " + sign_provider);
160
         continue;
3,483✔
161
      }
3,483✔
162

163
      if(sign_provider == "base") {
1,161✔
164
         result.test_eq("generated signature matches KAT", generated_signature, signature);
2,322✔
165
      } else if(generated_signature != signature) {
×
166
         for(std::unique_ptr<Botan::PK_Verifier>& verifier : verifiers) {
×
167
            if(!result.test_eq(
×
168
                  "generated signature valid", verifier->verify_message(message, generated_signature), true)) {
×
169
               result.test_failure("generated signature", generated_signature);
×
170
            }
171
         }
172
      }
173
   }
5,805✔
174

175
   return result;
1,161✔
176
}
5,988✔
177

178
Botan::Signature_Format PK_Signature_Verification_Test::sig_format() const { return Botan::Signature_Format::Standard; }
3,072✔
179

180
Test::Result PK_Signature_Verification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
12,632✔
181
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
12,632✔
182
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
12,632✔
183
   const std::string padding = choose_padding(vars, pad_hdr);
12,632✔
184

185
   const bool expected_valid = (vars.get_opt_sz("Valid", 1) == 1);
12,632✔
186

187
   auto pubkey = load_public_key(vars);
12,632✔
188

189
   std::ostringstream result_name;
12,632✔
190
   result_name << algo_name();
25,264✔
191
   if(vars.has_key("Group")) {
25,264✔
192
      result_name << "-" << vars.get_req_str("Group");
24,134✔
193
   }
194
   if(!padding.empty()) {
12,632✔
195
      result_name << "/" << padding;
12,632✔
196
   }
197
   result_name << " signature verification";
12,632✔
198
   Test::Result result(result_name.str());
12,632✔
199

200
   result.confirm("public key claims to support signatures",
25,264✔
201
                  pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
12,632✔
202

203
   for(const auto& verify_provider : possible_providers(algo_name())) {
75,792✔
204
      std::unique_ptr<Botan::PK_Verifier> verifier;
50,528✔
205

206
      try {
50,528✔
207
         verifier = std::make_unique<Botan::PK_Verifier>(*pubkey, padding, sig_format(), verify_provider);
58,910✔
208
      } catch(Botan::Lookup_Error&) {
42,146✔
209
         //result.test_note("Skipping verifying with " + verify_provider);
210
      }
42,146✔
211

212
      if(verifier) {
50,528✔
213
         try {
8,382✔
214
            const bool verified = verifier->verify_message(message, signature);
8,382✔
215

216
            if(expected_valid) {
8,382✔
217
               result.test_eq("correct signature valid with " + verify_provider, verified, true);
4,180✔
218

219
               if(test_random_invalid_sigs()) {
4,180✔
220
                  check_invalid_signatures(result, *verifier, message, signature);
705✔
221
               }
222
            } else {
223
               result.confirm("incorrect signature is rejected", verified == false);
12,606✔
224
            }
225
         } catch(std::exception& e) {
×
226
            result.test_failure("verification threw exception", e.what());
×
227
         }
×
228
      }
229
   }
63,160✔
230

231
   return result;
25,264✔
232
}
50,537✔
233

234
Test::Result PK_Signature_NonVerification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
702✔
235
   const std::string padding = choose_padding(vars, pad_hdr);
702✔
236
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
702✔
237
   auto pubkey = load_public_key(vars);
702✔
238

239
   const std::vector<uint8_t> invalid_signature = vars.get_req_bin("InvalidSignature");
702✔
240

241
   Test::Result result(algo_name() + "/" + padding + " verify invalid signature");
1,404✔
242

243
   for(const auto& verify_provider : possible_providers(algo_name())) {
4,212✔
244
      std::unique_ptr<Botan::PK_Verifier> verifier;
2,808✔
245

246
      try {
2,808✔
247
         verifier =
2,808✔
248
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::Standard, verify_provider);
3,510✔
249
         result.test_eq("incorrect signature rejected", verifier->verify_message(message, invalid_signature), false);
1,404✔
250
      } catch(Botan::Lookup_Error&) {
2,106✔
251
         result.test_note("Skipping verifying with " + verify_provider);
2,106✔
252
      }
2,106✔
253
   }
3,510✔
254

255
   return result;
702✔
256
}
2,247✔
257

258
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
259
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
260
   const std::string padding = m_padding;
1✔
261

262
   auto privkey = key();
1✔
263

264
   Test::Result result(algo_name() + "/" + padding + " signature sign/verify using DER format");
2✔
265

266
   for(const auto& provider : possible_providers(algo_name())) {
3✔
267
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
268
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
269

270
      try {
1✔
271
         signer = std::make_unique<Botan::PK_Signer>(
1✔
272
            *privkey, Test::rng(), padding, Botan::Signature_Format::DerSequence, provider);
2✔
273
         verifier =
1✔
274
            std::make_unique<Botan::PK_Verifier>(*privkey, padding, Botan::Signature_Format::DerSequence, provider);
2✔
275
      } catch(Botan::Lookup_Error& e) {
×
276
         result.test_note("Skipping sign/verify with " + provider, e.what());
×
277
      }
×
278

279
      if(signer && verifier) {
1✔
280
         try {
1✔
281
            std::vector<uint8_t> generated_signature = signer->sign_message(message, Test::rng());
1✔
282
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
283

284
            result.test_eq("correct signature valid with " + provider, verified, true);
1✔
285

286
            if(test_random_invalid_sigs()) {
1✔
287
               check_invalid_signatures(result, *verifier, message, generated_signature);
1✔
288
            }
289
         } catch(std::exception& e) {
1✔
290
            result.test_failure("verification threw exception", e.what());
×
291
         }
×
292
      }
293
   }
2✔
294

295
   return {result};
2✔
296
}
3✔
297

298
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo) {
1✔
299
   std::vector<std::string> pk_provider =
1✔
300
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
6✔
301
   return Test::provider_filter(pk_provider);
2✔
302
}
1✔
303

304
Test::Result PK_Encryption_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
134✔
305
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
134✔
306
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
134✔
307
   const std::string padding = choose_padding(vars, pad_hdr);
134✔
308

309
   Test::Result result(algo_name() + (padding.empty() ? padding : "/" + padding) + " encryption");
268✔
310

311
   auto privkey = load_private_key(vars);
134✔
312

313
   result.confirm("private key claims to support encryption",
268✔
314
                  privkey->supports_operation(Botan::PublicKeyOperation::Encryption));
134✔
315

316
   // instead slice the private key to work around elgamal test inputs
317
   //auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
318
   Botan::Public_Key* pubkey = privkey.get();
134✔
319

320
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
134✔
321

322
   for(const auto& dec_provider : possible_providers(algo_name())) {
804✔
323
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
536✔
324

325
      try {
536✔
326
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
536✔
327
      } catch(Botan::Lookup_Error&) {
402✔
328
         continue;
402✔
329
      }
402✔
330

331
      Botan::secure_vector<uint8_t> decrypted;
134✔
332
      try {
134✔
333
         decrypted = decryptor->decrypt(ciphertext);
134✔
334

335
         result.test_lte("Plaintext within length", decrypted.size(), decryptor->plaintext_length(ciphertext.size()));
268✔
336
      } catch(Botan::Exception& e) {
×
337
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
338
      }
×
339

340
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
134✔
341
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
134✔
342
   }
804✔
343

344
   for(const auto& enc_provider : possible_providers(algo_name())) {
804✔
345
      std::unique_ptr<Botan::PK_Encryptor> encryptor;
536✔
346

347
      try {
536✔
348
         encryptor = std::make_unique<Botan::PK_Encryptor_EME>(*pubkey, Test::rng(), padding, enc_provider);
536✔
349
      } catch(Botan::Lookup_Error&) {
402✔
350
         continue;
402✔
351
      }
402✔
352

353
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
134✔
354
      if(vars.has_key("Nonce")) {
268✔
355
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
118✔
356
      }
357

358
      if(padding == "Raw") {
134✔
359
         /*
360
         Hack for RSA with no padding since sometimes one more bit will fit in but maximum_input_size
361
         rounds down to nearest byte
362
         */
363
         result.test_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size() + 1);
162✔
364
      } else {
365
         result.test_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size());
106✔
366
      }
367

368
      const std::vector<uint8_t> generated_ciphertext = encryptor->encrypt(plaintext, kat_rng ? *kat_rng : Test::rng());
134✔
369

370
      result.test_lte(
134✔
371
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
134✔
372

373
      if(enc_provider == "base") {
134✔
374
         result.test_eq(enc_provider, "generated ciphertext matches KAT", generated_ciphertext, ciphertext);
268✔
375
      } else if(generated_ciphertext != ciphertext) {
×
376
         for(std::unique_ptr<Botan::PK_Decryptor>& dec : decryptors) {
×
377
            result.test_eq("decryption of generated ciphertext", dec->decrypt(generated_ciphertext), plaintext);
×
378
         }
379
      }
380
   }
729✔
381

382
   return result;
268✔
383
}
536✔
384

385
Test::Result PK_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
42✔
386
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
42✔
387
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
42✔
388
   const std::string padding = choose_padding(vars, pad_hdr);
42✔
389

390
   Test::Result result(algo_name() + (padding.empty() ? padding : "/" + padding) + " decryption");
84✔
391

392
   auto privkey = load_private_key(vars);
42✔
393

394
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
395
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
396

397
      try {
168✔
398
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
168✔
399
      } catch(Botan::Lookup_Error&) {
126✔
400
         continue;
126✔
401
      }
126✔
402

403
      Botan::secure_vector<uint8_t> decrypted;
42✔
404
      try {
42✔
405
         decrypted = decryptor->decrypt(ciphertext);
42✔
406
      } catch(Botan::Exception& e) {
×
407
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
408
      }
×
409

410
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
42✔
411
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
42✔
412
   }
252✔
413

414
   return result;
42✔
415
}
151✔
416

417
Test::Result PK_KEM_Test::run_one_test(const std::string& /*header*/, const VarMap& vars) {
10✔
418
   const std::vector<uint8_t> K = vars.get_req_bin("K");
10✔
419
   const std::vector<uint8_t> C0 = vars.get_req_bin("C0");
10✔
420
   const std::vector<uint8_t> salt = vars.get_opt_bin("Salt");
10✔
421
   const std::string kdf = vars.get_req_str("KDF");
10✔
422

423
   Test::Result result(algo_name() + "/" + kdf + " KEM");
20✔
424

425
   auto privkey = load_private_key(vars);
10✔
426

427
   result.confirm("private key claims to support KEM",
20✔
428
                  privkey->supports_operation(Botan::PublicKeyOperation::KeyEncapsulation));
10✔
429

430
   const Botan::Public_Key& pubkey = *privkey;
10✔
431

432
   const size_t desired_key_len = K.size();
10✔
433

434
   std::unique_ptr<Botan::PK_KEM_Encryptor> enc;
10✔
435
   try {
10✔
436
      enc = std::make_unique<Botan::PK_KEM_Encryptor>(pubkey, kdf);
20✔
437
   } catch(Botan::Lookup_Error&) {
×
438
      result.test_note("Skipping due to missing KDF: " + kdf);
×
439
      return result;
×
440
   }
×
441

442
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
20✔
443

444
   Botan::secure_vector<uint8_t> produced_encap_key, shared_key;
10✔
445
   enc->encrypt(produced_encap_key, shared_key, desired_key_len, fixed_output_rng, salt);
10✔
446

447
   result.test_eq(
10✔
448
      "encapsulated key length matches expected", produced_encap_key.size(), enc->encapsulated_key_length());
449

450
   result.test_eq("shared key length matches expected", shared_key.size(), enc->shared_key_length(desired_key_len));
10✔
451

452
   result.test_eq("C0 matches", produced_encap_key, C0);
10✔
453
   result.test_eq("K matches", shared_key, K);
10✔
454

455
   std::unique_ptr<Botan::PK_KEM_Decryptor> dec;
10✔
456
   try {
10✔
457
      dec = std::make_unique<Botan::PK_KEM_Decryptor>(*privkey, Test::rng(), kdf);
20✔
458
   } catch(Botan::Lookup_Error& e) {
×
459
      result.test_note("Skipping test", e.what());
×
460
      return result;
×
461
   }
×
462

463
   const Botan::secure_vector<uint8_t> decr_shared_key =
10✔
464
      dec->decrypt(C0.data(), C0.size(), desired_key_len, salt.data(), salt.size());
10✔
465

466
   result.test_eq(
10✔
467
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
468

469
   result.test_eq("decrypted K matches", decr_shared_key, K);
10✔
470

471
   return result;
10✔
472
}
75✔
473

474
Test::Result PK_Key_Agreement_Test::run_one_test(const std::string& header, const VarMap& vars) {
268✔
475
   const std::vector<uint8_t> shared = vars.get_req_bin("K");
268✔
476
   const std::string kdf = vars.get_opt_str("KDF", default_kdf(vars));
536✔
477

478
   Test::Result result(algo_name() + "/" + kdf + (header.empty() ? header : " " + header) + " key agreement");
608✔
479

480
   auto privkey = load_our_key(header, vars);
268✔
481

482
   result.confirm("private key claims to support key agreement",
536✔
483
                  privkey->supports_operation(Botan::PublicKeyOperation::KeyAgreement));
268✔
484

485
   const std::vector<uint8_t> pubkey = load_their_key(header, vars);
268✔
486

487
   const size_t key_len = vars.get_opt_sz("OutLen", 0);
268✔
488

489
   for(const auto& provider : possible_providers(algo_name())) {
1,608✔
490
      std::unique_ptr<Botan::PK_Key_Agreement> kas;
1,072✔
491

492
      try {
1,072✔
493
         kas = std::make_unique<Botan::PK_Key_Agreement>(*privkey, Test::rng(), kdf, provider);
1,340✔
494

495
         auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
268✔
496
         result.test_eq(provider, "agreement", derived_key, shared);
268✔
497

498
         if(key_len == 0 && kdf == "Raw") {
268✔
499
            result.test_eq("Expected size", derived_key.size(), kas->agreed_value_size());
528✔
500
         }
501
      } catch(Botan::Lookup_Error&) {
1,072✔
502
         //result.test_note("Skipping key agreement with with " + provider);
503
      }
804✔
504
   }
1,340✔
505

506
   return result;
268✔
507
}
804✔
508

509
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo) {
42✔
510
   std::vector<std::string> pk_provider =
42✔
511
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
252✔
512
   return Test::provider_filter(pk_provider);
84✔
513
}
42✔
514

515
namespace {
516

517
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && \
518
      (defined(BOTAN_HAS_SHA2_32) || defined(BOTAN_HAS_SCRYPT))
519
void test_pbe_roundtrip(Test::Result& result,
84✔
520
                        const Botan::Private_Key& key,
521
                        const std::string& pbe_algo,
522
                        const std::string& passphrase) {
523
   const auto pkcs8 = key.private_key_info();
84✔
524

525
   try {
84✔
526
      Botan::DataSource_Memory data_src(
84✔
527
         Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
84✔
528

529
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
84✔
530

531
      result.confirm("recovered private key from encrypted blob", loaded != nullptr);
168✔
532
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
190✔
533
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
252✔
534
   } catch(std::exception& e) {
168✔
535
      result.test_failure("roundtrip encrypted PEM private key", e.what());
×
536
   }
×
537

538
   try {
84✔
539
      Botan::DataSource_Memory data_src(
84✔
540
         Botan::PKCS8::BER_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
168✔
541

542
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
84✔
543

544
      result.confirm("recovered private key from BER blob", loaded != nullptr);
168✔
545
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
190✔
546
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
252✔
547
   } catch(std::exception& e) {
168✔
548
      result.test_failure("roundtrip encrypted BER private key", e.what());
×
549
   }
×
550
}
84✔
551
   #endif
552

553
}  // namespace
554

555
std::vector<Test::Result> PK_Key_Generation_Test::run() {
15✔
556
   std::vector<Test::Result> results;
15✔
557

558
   for(const auto& param : keygen_params()) {
57✔
559
      const std::string report_name = algo_name() + (param.empty() ? param : " " + param);
86✔
560

561
      Test::Result result(report_name + " keygen");
42✔
562

563
      const std::vector<std::string> providers = possible_providers(algo_name());
42✔
564

565
      if(providers.empty()) {
42✔
566
         result.note_missing("provider key generation " + algo_name());
×
567
      }
568

569
      result.start_timer();
42✔
570
      for(auto&& prov : providers) {
84✔
571
         auto key_p = Botan::create_private_key(algo_name(), Test::rng(), param, prov);
42✔
572

573
         if(key_p == nullptr) {
42✔
574
            result.test_failure("create_private_key returned null, should throw instead");
×
575
            continue;
×
576
         }
577

578
         const Botan::Private_Key& key = *key_p;
42✔
579

580
         try {
42✔
581
            result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
126✔
582
         } catch(Botan::Lookup_Error&) {}
×
583

584
         result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
42✔
585
         result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
42✔
586

587
         auto public_key = key.public_key();
42✔
588

589
         result.test_eq("public_key has same name", public_key->algo_name(), key.algo_name());
95✔
590

591
         result.test_eq(
126✔
592
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
84✔
593

594
         // Test PEM public key round trips OK
595
         try {
42✔
596
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
42✔
597
            auto loaded = Botan::X509::load_key(data_src);
42✔
598

599
            result.confirm("recovered public key from private", loaded != nullptr);
84✔
600
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
95✔
601

602
            try {
42✔
603
               result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
84✔
604
            } catch(Botan::Lookup_Error&) {}
×
605
         } catch(std::exception& e) {
84✔
606
            result.test_failure("roundtrip PEM public key", e.what());
×
607
         }
×
608

609
         // Test DER public key round trips OK
610
         try {
42✔
611
            const auto ber = key.subject_public_key();
42✔
612
            Botan::DataSource_Memory data_src(ber);
42✔
613
            auto loaded = Botan::X509::load_key(data_src);
42✔
614

615
            result.confirm("recovered public key from private", loaded != nullptr);
84✔
616
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
95✔
617
            result.test_eq("public key has same encoding", loaded->subject_public_key(), ber);
126✔
618
         } catch(std::exception& e) {
126✔
619
            result.test_failure("roundtrip BER public key", e.what());
×
620
         }
×
621

622
         // Test PEM private key round trips OK
623
         try {
42✔
624
            const auto ber = key.private_key_info();
42✔
625
            Botan::DataSource_Memory data_src(ber);
42✔
626
            auto loaded = Botan::PKCS8::load_key(data_src);
42✔
627

628
            result.confirm("recovered private key from PEM blob", loaded != nullptr);
84✔
629
            result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
95✔
630
            result.test_eq("reloaded key has same encoding", loaded->private_key_info(), ber);
126✔
631
         } catch(std::exception& e) {
126✔
632
            result.test_failure("roundtrip PEM private key", e.what());
×
633
         }
×
634

635
         try {
42✔
636
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
42✔
637
            auto loaded = Botan::PKCS8::load_key(data_src);
42✔
638

639
            result.confirm("recovered public key from private", loaded != nullptr);
84✔
640
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
106✔
641
         } catch(std::exception& e) {
84✔
642
            result.test_failure("roundtrip BER private key", e.what());
×
643
         }
×
644

645
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
646

647
         test_pbe_roundtrip(result, key, "PBE-PKCS5v20(AES-128/CBC,SHA-256)", Test::random_password());
84✔
648
   #endif
649

650
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
651

652
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", Test::random_password());
115✔
653
   #endif
654
      }
84✔
655

656
      result.end_timer();
42✔
657

658
      results.push_back(result);
42✔
659
   }
85✔
660

661
   return results;
15✔
662
}
×
663

664
Test::Result PK_Key_Validity_Test::run_one_test(const std::string& header, const VarMap& vars) {
9✔
665
   Test::Result result(algo_name() + " key validity");
9✔
666

667
   if(header != "Valid" && header != "Invalid") {
9✔
668
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
669
   }
670

671
   const bool expected_valid = (header == "Valid");
9✔
672
   auto pubkey = load_public_key(vars);
9✔
673

674
   const bool tested_valid = pubkey->check_key(rng(), true);
9✔
675

676
   result.test_eq("Expected validation result", expected_valid, tested_valid);
9✔
677

678
   return result;
9✔
679
}
9✔
680

681
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
682
                                                                   const std::string& test_src) :
2✔
683
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
6✔
684

685
Test::Result PK_Key_Generation_Stability_Test::run_one_test(const std::string&, const VarMap& vars) {
3✔
686
   const std::string key_param = vars.get_opt_str("KeyParams", "");
6✔
687
   const std::string rng_algo = vars.get_req_str("Rng");
3✔
688
   const std::string rng_params = vars.get_opt_str("RngParams", "");
6✔
689
   const std::vector<uint8_t> rng_seed = vars.get_req_bin("RngSeed");
3✔
690
   const std::vector<uint8_t> expected_key = vars.get_req_bin("Key");
3✔
691

692
   std::ostringstream report_name;
3✔
693

694
   report_name << algo_name();
6✔
695
   if(!key_param.empty()) {
3✔
696
      report_name << " " << key_param;
3✔
697
   }
698
   report_name << " keygen stability";
3✔
699

700
   Test::Result result(report_name.str());
3✔
701

702
   result.start_timer();
3✔
703

704
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
3✔
705

706
   #if defined(BOTAN_HAS_HMAC_DRBG)
707
   if(rng_algo == "HMAC_DRBG") {
3✔
708
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
1✔
709
   }
710
   #endif
711

712
   if(rng_algo == "Fixed") {
3✔
713
      if(!rng_params.empty()) {
2✔
714
         throw Test_Error("Expected empty RngParams for Fixed RNG");
×
715
      }
716
      rng = std::make_unique<Fixed_Output_RNG>();
4✔
717
   }
718

719
   if(rng) {
3✔
720
      rng->add_entropy(rng_seed.data(), rng_seed.size());
3✔
721

722
      try {
3✔
723
         auto key = Botan::create_private_key(algo_name(), *rng, key_param);
6✔
724
         const auto key_bits = key->private_key_info();
3✔
725
         result.test_eq("Generated key matched expected value", key_bits, expected_key);
6✔
726
      } catch(Botan::Exception& e) {
6✔
727
         result.test_note("failed to create key", e.what());
×
728
      }
×
729
   } else {
730
      result.test_note("Skipping test due to unavailable RNG");
×
731
   }
732

733
   result.end_timer();
3✔
734

735
   return result;
3✔
736
}
9✔
737

738
}  // namespace Botan_Tests
739

740
#endif
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