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

randombit / botan / 5123321399

30 May 2023 04:06PM UTC coverage: 92.213% (+0.004%) from 92.209%
5123321399

Pull #3558

github

web-flow
Merge dd72f7389 into 057bcbc35
Pull Request #3558: Add braces around all if/else statements

75602 of 81986 relevant lines covered (92.21%)

11859779.3 hits per line

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

91.01
/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,935✔
64

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

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

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

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

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

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

98
   Test::Result result(test_name.str());
1,161✔
99

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

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

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

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

116
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,161✔
117

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

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

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

131
      check_invalid_signatures(result, *verifier, message, signature);
1,161✔
132

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

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

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

141
      std::vector<uint8_t> generated_signature;
4,644✔
142

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

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

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

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

173
   return result;
1,161✔
174
}
5,805✔
175

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

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

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

185
   auto pubkey = load_public_key(vars);
12,632✔
186

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

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

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

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

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

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

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

227
   return result;
25,264✔
228
}
50,537✔
229

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

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

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

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

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

249
   return result;
702✔
250
}
2,247✔
251

252
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
253
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
254
   const std::string padding = m_padding;
1✔
255

256
   auto privkey = key();
1✔
257

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

260
   for(const auto& provider : possible_providers(algo_name())) {
3✔
261
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
262
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
263

264
      try {
1✔
265
         signer = std::make_unique<Botan::PK_Signer>(
1✔
266
            *privkey, Test::rng(), padding, Botan::Signature_Format::DerSequence, provider);
2✔
267
         verifier =
1✔
268
            std::make_unique<Botan::PK_Verifier>(*privkey, padding, Botan::Signature_Format::DerSequence, provider);
2✔
269
      } catch(Botan::Lookup_Error& e) { result.test_note("Skipping sign/verify with " + provider, e.what()); }
×
270

271
      if(signer && verifier) {
1✔
272
         try {
1✔
273
            std::vector<uint8_t> generated_signature = signer->sign_message(message, Test::rng());
1✔
274
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
275

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

278
            if(test_random_invalid_sigs()) {
1✔
279
               check_invalid_signatures(result, *verifier, message, generated_signature);
1✔
280
            }
281
         } catch(std::exception& e) { result.test_failure("verification threw exception", e.what()); }
1✔
282
      }
283
   }
2✔
284

285
   return {result};
2✔
286
}
3✔
287

288
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo) {
1✔
289
   std::vector<std::string> pk_provider =
1✔
290
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
6✔
291
   return Test::provider_filter(pk_provider);
2✔
292
}
1✔
293

294
Test::Result PK_Encryption_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
134✔
295
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
134✔
296
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
134✔
297
   const std::string padding = choose_padding(vars, pad_hdr);
134✔
298

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

301
   auto privkey = load_private_key(vars);
134✔
302

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

306
   // instead slice the private key to work around elgamal test inputs
307
   //auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
308
   Botan::Public_Key* pubkey = privkey.get();
134✔
309

310
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
134✔
311

312
   for(const auto& dec_provider : possible_providers(algo_name())) {
804✔
313
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
536✔
314

315
      try {
536✔
316
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
536✔
317
      } catch(Botan::Lookup_Error&) { continue; }
402✔
318

319
      Botan::secure_vector<uint8_t> decrypted;
134✔
320
      try {
134✔
321
         decrypted = decryptor->decrypt(ciphertext);
134✔
322

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

326
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
134✔
327
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
134✔
328
   }
804✔
329

330
   for(const auto& enc_provider : possible_providers(algo_name())) {
804✔
331
      std::unique_ptr<Botan::PK_Encryptor> encryptor;
536✔
332

333
      try {
536✔
334
         encryptor = std::make_unique<Botan::PK_Encryptor_EME>(*pubkey, Test::rng(), padding, enc_provider);
536✔
335
      } catch(Botan::Lookup_Error&) { continue; }
402✔
336

337
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
134✔
338
      if(vars.has_key("Nonce")) {
268✔
339
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
118✔
340
      }
341

342
      if(padding == "Raw") {
134✔
343
         /*
344
         Hack for RSA with no padding since sometimes one more bit will fit in but maximum_input_size
345
         rounds down to nearest byte
346
         */
347
         result.test_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size() + 1);
162✔
348
      } else {
349
         result.test_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size());
106✔
350
      }
351

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

354
      result.test_lte(
134✔
355
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
134✔
356

357
      if(enc_provider == "base") {
134✔
358
         result.test_eq(enc_provider, "generated ciphertext matches KAT", generated_ciphertext, ciphertext);
268✔
359
      } else if(generated_ciphertext != ciphertext) {
×
360
         for(std::unique_ptr<Botan::PK_Decryptor>& dec : decryptors) {
×
361
            result.test_eq("decryption of generated ciphertext", dec->decrypt(generated_ciphertext), plaintext);
×
362
         }
363
      }
364
   }
729✔
365

366
   return result;
268✔
367
}
536✔
368

369
Test::Result PK_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
42✔
370
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
42✔
371
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
42✔
372
   const std::string padding = choose_padding(vars, pad_hdr);
42✔
373

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

376
   auto privkey = load_private_key(vars);
42✔
377

378
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
379
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
380

381
      try {
168✔
382
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
168✔
383
      } catch(Botan::Lookup_Error&) { continue; }
126✔
384

385
      Botan::secure_vector<uint8_t> decrypted;
42✔
386
      try {
42✔
387
         decrypted = decryptor->decrypt(ciphertext);
42✔
388
      } catch(Botan::Exception& e) { result.test_failure("Failed to decrypt KAT ciphertext", e.what()); }
×
389

390
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
42✔
391
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
42✔
392
   }
252✔
393

394
   return result;
42✔
395
}
151✔
396

397
Test::Result PK_KEM_Test::run_one_test(const std::string& /*header*/, const VarMap& vars) {
10✔
398
   const std::vector<uint8_t> K = vars.get_req_bin("K");
10✔
399
   const std::vector<uint8_t> C0 = vars.get_req_bin("C0");
10✔
400
   const std::vector<uint8_t> salt = vars.get_opt_bin("Salt");
10✔
401
   const std::string kdf = vars.get_req_str("KDF");
10✔
402

403
   Test::Result result(algo_name() + "/" + kdf + " KEM");
20✔
404

405
   auto privkey = load_private_key(vars);
10✔
406

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

410
   const Botan::Public_Key& pubkey = *privkey;
10✔
411

412
   const size_t desired_key_len = K.size();
10✔
413

414
   std::unique_ptr<Botan::PK_KEM_Encryptor> enc;
10✔
415
   try {
10✔
416
      enc = std::make_unique<Botan::PK_KEM_Encryptor>(pubkey, kdf);
20✔
417
   } catch(Botan::Lookup_Error&) {
×
418
      result.test_note("Skipping due to missing KDF: " + kdf);
×
419
      return result;
×
420
   }
×
421

422
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
20✔
423

424
   Botan::secure_vector<uint8_t> produced_encap_key, shared_key;
10✔
425
   enc->encrypt(produced_encap_key, shared_key, desired_key_len, fixed_output_rng, salt);
10✔
426

427
   result.test_eq(
10✔
428
      "encapsulated key length matches expected", produced_encap_key.size(), enc->encapsulated_key_length());
429

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

432
   result.test_eq("C0 matches", produced_encap_key, C0);
10✔
433
   result.test_eq("K matches", shared_key, K);
10✔
434

435
   std::unique_ptr<Botan::PK_KEM_Decryptor> dec;
10✔
436
   try {
10✔
437
      dec = std::make_unique<Botan::PK_KEM_Decryptor>(*privkey, Test::rng(), kdf);
20✔
438
   } catch(Botan::Lookup_Error& e) {
×
439
      result.test_note("Skipping test", e.what());
×
440
      return result;
×
441
   }
×
442

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

446
   result.test_eq(
10✔
447
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
448

449
   result.test_eq("decrypted K matches", decr_shared_key, K);
10✔
450

451
   return result;
10✔
452
}
75✔
453

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

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

460
   auto privkey = load_our_key(header, vars);
268✔
461

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

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

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

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

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

475
         auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
268✔
476
         result.test_eq(provider, "agreement", derived_key, shared);
268✔
477

478
         if(key_len == 0 && kdf == "Raw") {
268✔
479
            result.test_eq("Expected size", derived_key.size(), kas->agreed_value_size());
528✔
480
         }
481
      } catch(Botan::Lookup_Error&) {
1,072✔
482
         //result.test_note("Skipping key agreement with with " + provider);
483
      }
804✔
484
   }
1,340✔
485

486
   return result;
268✔
487
}
804✔
488

489
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo) {
42✔
490
   std::vector<std::string> pk_provider =
42✔
491
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
252✔
492
   return Test::provider_filter(pk_provider);
84✔
493
}
42✔
494

495
namespace {
496

497
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && \
498
      (defined(BOTAN_HAS_SHA2_32) || defined(BOTAN_HAS_SCRYPT))
499
void test_pbe_roundtrip(Test::Result& result,
84✔
500
                        const Botan::Private_Key& key,
501
                        const std::string& pbe_algo,
502
                        const std::string& passphrase) {
503
   const auto pkcs8 = key.private_key_info();
84✔
504

505
   try {
84✔
506
      Botan::DataSource_Memory data_src(
84✔
507
         Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
84✔
508

509
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
84✔
510

511
      result.confirm("recovered private key from encrypted blob", loaded != nullptr);
168✔
512
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
190✔
513
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
252✔
514
   } catch(std::exception& e) { result.test_failure("roundtrip encrypted PEM private key", e.what()); }
168✔
515

516
   try {
84✔
517
      Botan::DataSource_Memory data_src(
84✔
518
         Botan::PKCS8::BER_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
168✔
519

520
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
84✔
521

522
      result.confirm("recovered private key from BER blob", loaded != nullptr);
168✔
523
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
190✔
524
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
252✔
525
   } catch(std::exception& e) { result.test_failure("roundtrip encrypted BER private key", e.what()); }
168✔
526
}
84✔
527
   #endif
528

529
}  // namespace
530

531
std::vector<Test::Result> PK_Key_Generation_Test::run() {
15✔
532
   std::vector<Test::Result> results;
15✔
533

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

537
      Test::Result result(report_name + " keygen");
42✔
538

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

541
      if(providers.empty()) {
42✔
542
         result.note_missing("provider key generation " + algo_name());
×
543
      }
544

545
      result.start_timer();
42✔
546
      for(auto&& prov : providers) {
84✔
547
         auto key_p = Botan::create_private_key(algo_name(), Test::rng(), param, prov);
42✔
548

549
         if(key_p == nullptr) {
42✔
550
            result.test_failure("create_private_key returned null, should throw instead");
×
551
            continue;
×
552
         }
553

554
         const Botan::Private_Key& key = *key_p;
42✔
555

556
         try {
42✔
557
            result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
126✔
558
         } catch(Botan::Lookup_Error&) {}
×
559

560
         result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
42✔
561
         result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
42✔
562

563
         auto public_key = key.public_key();
42✔
564

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

567
         result.test_eq(
126✔
568
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
84✔
569

570
         // Test PEM public key round trips OK
571
         try {
42✔
572
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
42✔
573
            auto loaded = Botan::X509::load_key(data_src);
42✔
574

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

578
            try {
42✔
579
               result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
84✔
580
            } catch(Botan::Lookup_Error&) {}
×
581
         } catch(std::exception& e) { result.test_failure("roundtrip PEM public key", e.what()); }
84✔
582

583
         // Test DER public key round trips OK
584
         try {
42✔
585
            const auto ber = key.subject_public_key();
42✔
586
            Botan::DataSource_Memory data_src(ber);
42✔
587
            auto loaded = Botan::X509::load_key(data_src);
42✔
588

589
            result.confirm("recovered public key from private", loaded != nullptr);
84✔
590
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
95✔
591
            result.test_eq("public key has same encoding", loaded->subject_public_key(), ber);
126✔
592
         } catch(std::exception& e) { result.test_failure("roundtrip BER public key", e.what()); }
126✔
593

594
         // Test PEM private key round trips OK
595
         try {
42✔
596
            const auto ber = key.private_key_info();
42✔
597
            Botan::DataSource_Memory data_src(ber);
42✔
598
            auto loaded = Botan::PKCS8::load_key(data_src);
42✔
599

600
            result.confirm("recovered private key from PEM blob", loaded != nullptr);
84✔
601
            result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
95✔
602
            result.test_eq("reloaded key has same encoding", loaded->private_key_info(), ber);
126✔
603
         } catch(std::exception& e) { result.test_failure("roundtrip PEM private key", e.what()); }
126✔
604

605
         try {
42✔
606
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
42✔
607
            auto loaded = Botan::PKCS8::load_key(data_src);
42✔
608

609
            result.confirm("recovered public key from private", loaded != nullptr);
84✔
610
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
106✔
611
         } catch(std::exception& e) { result.test_failure("roundtrip BER private key", e.what()); }
84✔
612

613
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
614

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

618
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
619

620
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", Test::random_password());
122✔
621
   #endif
622
      }
84✔
623

624
      result.end_timer();
42✔
625

626
      results.push_back(result);
42✔
627
   }
85✔
628

629
   return results;
15✔
630
}
×
631

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

635
   if(header != "Valid" && header != "Invalid") {
9✔
636
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
637
   }
638

639
   const bool expected_valid = (header == "Valid");
9✔
640
   auto pubkey = load_public_key(vars);
9✔
641

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

644
   result.test_eq("Expected validation result", expected_valid, tested_valid);
9✔
645

646
   return result;
9✔
647
}
9✔
648

649
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
650
                                                                   const std::string& test_src) :
2✔
651
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
6✔
652

653
Test::Result PK_Key_Generation_Stability_Test::run_one_test(const std::string&, const VarMap& vars) {
3✔
654
   const std::string key_param = vars.get_opt_str("KeyParams", "");
6✔
655
   const std::string rng_algo = vars.get_req_str("Rng");
3✔
656
   const std::string rng_params = vars.get_opt_str("RngParams", "");
6✔
657
   const std::vector<uint8_t> rng_seed = vars.get_req_bin("RngSeed");
3✔
658
   const std::vector<uint8_t> expected_key = vars.get_req_bin("Key");
3✔
659

660
   std::ostringstream report_name;
3✔
661

662
   report_name << algo_name();
6✔
663
   if(!key_param.empty()) {
3✔
664
      report_name << " " << key_param;
3✔
665
   }
666
   report_name << " keygen stability";
3✔
667

668
   Test::Result result(report_name.str());
3✔
669

670
   result.start_timer();
3✔
671

672
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
3✔
673

674
   #if defined(BOTAN_HAS_HMAC_DRBG)
675
   if(rng_algo == "HMAC_DRBG") {
3✔
676
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
1✔
677
   }
678
   #endif
679

680
   if(rng_algo == "Fixed") {
3✔
681
      if(!rng_params.empty()) {
2✔
682
         throw Test_Error("Expected empty RngParams for Fixed RNG");
×
683
      }
684
      rng = std::make_unique<Fixed_Output_RNG>();
4✔
685
   }
686

687
   if(rng) {
3✔
688
      rng->add_entropy(rng_seed.data(), rng_seed.size());
3✔
689

690
      try {
3✔
691
         auto key = Botan::create_private_key(algo_name(), *rng, key_param);
6✔
692
         const auto key_bits = key->private_key_info();
3✔
693
         result.test_eq("Generated key matched expected value", key_bits, expected_key);
6✔
694
      } catch(Botan::Exception& e) { result.test_note("failed to create key", e.what()); }
6✔
695
   } else {
696
      result.test_note("Skipping test due to unavailable RNG");
×
697
   }
698

699
   result.end_timer();
3✔
700

701
   return result;
3✔
702
}
9✔
703

704
}  // namespace Botan_Tests
705

706
#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