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

randombit / botan / 6642806734

25 Oct 2023 03:40PM UTC coverage: 91.687% (+0.01%) from 91.677%
6642806734

push

github

web-flow
Merge pull request #3770 from Rohde-Schwarz/feature/pubkey_create_another

Feature: `AsymmetricKey::generate_another()`

80170 of 87439 relevant lines covered (91.69%)

8623809.04 hits per line

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

87.6
/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
   #include <botan/internal/fmt.h>
21

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

26
   #include <array>
27

28
namespace Botan_Tests {
29

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

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

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

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

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

59
   size_t ciphertext_accepted = 0, ciphertext_rejected = 0;
252✔
60

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

64
      try {
5,040✔
65
         const Botan::secure_vector<uint8_t> decrypted = decryptor.decrypt(bad_ctext);
5,040✔
66
         ++ciphertext_accepted;
1,934✔
67

68
         if(!result.test_ne("incorrect ciphertext different", decrypted, plaintext)) {
5,802✔
69
            result.test_eq("used corrupted ciphertext", bad_ctext, ciphertext);
×
70
         }
71
      } catch(std::exception&) {
5,040✔
72
         ++ciphertext_rejected;
3,106✔
73
      }
3,106✔
74
   }
5,040✔
75

76
   result.test_note("Accepted " + std::to_string(ciphertext_accepted) + " invalid ciphertexts, rejected " +
504✔
77
                    std::to_string(ciphertext_rejected));
252✔
78
}
252✔
79

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

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

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

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

103
   Test::Result result(test_name.str());
1,161✔
104

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

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

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

118
   result.confirm("public key claims to support signatures",
2,322✔
119
                  pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,161✔
120

121
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,161✔
122

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

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

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

136
      check_invalid_signatures(result, *verifier, message, signature);
1,161✔
137

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

140
      verifiers.push_back(std::move(verifier));
1,161✔
141
   }
5,805✔
142

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

146
      std::vector<uint8_t> generated_signature;
4,644✔
147

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

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

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

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

178
   return result;
1,161✔
179
}
5,988✔
180

181
Botan::Signature_Format PK_Signature_Verification_Test::sig_format() const {
3,072✔
182
   return Botan::Signature_Format::Standard;
3,072✔
183
}
184

185
Test::Result PK_Signature_Verification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
12,632✔
186
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
12,632✔
187
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
12,632✔
188
   const std::string padding = choose_padding(vars, pad_hdr);
12,632✔
189

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

192
   auto pubkey = load_public_key(vars);
12,632✔
193

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

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

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

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

217
      if(verifier) {
50,528✔
218
         try {
8,382✔
219
            const bool verified = verifier->verify_message(message, signature);
8,382✔
220

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

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

236
   return result;
25,264✔
237
}
50,537✔
238

239
Test::Result PK_Signature_NonVerification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
702✔
240
   const std::string padding = choose_padding(vars, pad_hdr);
702✔
241
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
702✔
242
   auto pubkey = load_public_key(vars);
702✔
243

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

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

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

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

260
   return result;
702✔
261
}
2,247✔
262

263
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
264
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
265
   const std::string padding = m_padding;
1✔
266

267
   auto privkey = key();
1✔
268

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

271
   for(const auto& provider : possible_providers(algo_name())) {
3✔
272
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
273
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
274

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

284
      if(signer && verifier) {
1✔
285
         try {
1✔
286
            std::vector<uint8_t> generated_signature = signer->sign_message(message, Test::rng());
1✔
287
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
288

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

291
            if(test_random_invalid_sigs()) {
1✔
292
               check_invalid_signatures(result, *verifier, message, generated_signature);
1✔
293
            }
294
         } catch(std::exception& e) {
1✔
295
            result.test_failure("verification threw exception", e.what());
×
296
         }
×
297
      }
298
   }
2✔
299

300
   return {result};
2✔
301
}
3✔
302

303
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo) {
1✔
304
   std::vector<std::string> pk_provider =
1✔
305
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
6✔
306
   return Test::provider_filter(pk_provider);
2✔
307
}
1✔
308

309
Test::Result PK_Encryption_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
134✔
310
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
134✔
311
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
134✔
312
   const std::string padding = choose_padding(vars, pad_hdr);
134✔
313

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

316
   auto privkey = load_private_key(vars);
134✔
317

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

321
   // instead slice the private key to work around elgamal test inputs
322
   //auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
323
   Botan::Public_Key* pubkey = privkey.get();
134✔
324

325
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
134✔
326

327
   for(const auto& dec_provider : possible_providers(algo_name())) {
804✔
328
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
536✔
329

330
      try {
536✔
331
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
536✔
332
      } catch(Botan::Lookup_Error&) {
402✔
333
         continue;
402✔
334
      }
402✔
335

336
      Botan::secure_vector<uint8_t> decrypted;
134✔
337
      try {
134✔
338
         decrypted = decryptor->decrypt(ciphertext);
134✔
339

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

345
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
134✔
346
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
134✔
347
   }
804✔
348

349
   for(const auto& enc_provider : possible_providers(algo_name())) {
804✔
350
      std::unique_ptr<Botan::PK_Encryptor> encryptor;
536✔
351

352
      try {
536✔
353
         encryptor = std::make_unique<Botan::PK_Encryptor_EME>(*pubkey, Test::rng(), padding, enc_provider);
536✔
354
      } catch(Botan::Lookup_Error&) {
402✔
355
         continue;
402✔
356
      }
402✔
357

358
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
134✔
359
      if(vars.has_key("Nonce")) {
268✔
360
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
118✔
361
      }
362

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

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

375
      result.test_lte(
134✔
376
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
134✔
377

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

387
   return result;
268✔
388
}
536✔
389

390
Test::Result PK_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
42✔
391
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
42✔
392
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
42✔
393
   const std::string padding = choose_padding(vars, pad_hdr);
42✔
394

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

397
   auto privkey = load_private_key(vars);
42✔
398

399
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
400
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
401

402
      try {
168✔
403
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
168✔
404
      } catch(Botan::Lookup_Error&) {
126✔
405
         continue;
126✔
406
      }
126✔
407

408
      Botan::secure_vector<uint8_t> decrypted;
42✔
409
      try {
42✔
410
         decrypted = decryptor->decrypt(ciphertext);
42✔
411
      } catch(Botan::Exception& e) {
×
412
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
413
      }
×
414

415
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
42✔
416
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
42✔
417
   }
252✔
418

419
   return result;
42✔
420
}
151✔
421

422
Test::Result PK_KEM_Test::run_one_test(const std::string& /*header*/, const VarMap& vars) {
10✔
423
   const std::vector<uint8_t> K = vars.get_req_bin("K");
10✔
424
   const std::vector<uint8_t> C0 = vars.get_req_bin("C0");
10✔
425
   const std::vector<uint8_t> salt = vars.get_opt_bin("Salt");
10✔
426
   const std::string kdf = vars.get_req_str("KDF");
10✔
427

428
   Test::Result result(algo_name() + "/" + kdf + " KEM");
20✔
429

430
   auto privkey = load_private_key(vars);
10✔
431

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

435
   const Botan::Public_Key& pubkey = *privkey;
10✔
436

437
   const size_t desired_key_len = K.size();
10✔
438

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

447
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
20✔
448

449
   const auto kem_result = enc->encrypt(fixed_output_rng, desired_key_len, salt);
10✔
450

451
   result.test_eq("encapsulated key length matches expected",
20✔
452
                  kem_result.encapsulated_shared_key().size(),
10✔
453
                  enc->encapsulated_key_length());
454

455
   result.test_eq(
20✔
456
      "shared key length matches expected", kem_result.shared_key().size(), enc->shared_key_length(desired_key_len));
10✔
457

458
   result.test_eq("C0 matches", kem_result.encapsulated_shared_key(), C0);
10✔
459
   result.test_eq("K matches", kem_result.shared_key(), K);
10✔
460

461
   std::unique_ptr<Botan::PK_KEM_Decryptor> dec;
10✔
462
   try {
10✔
463
      dec = std::make_unique<Botan::PK_KEM_Decryptor>(*privkey, Test::rng(), kdf);
20✔
464
   } catch(Botan::Lookup_Error& e) {
×
465
      result.test_note("Skipping test", e.what());
×
466
      return result;
×
467
   }
×
468

469
   result.test_eq("encapsulated key length matches expected",
20✔
470
                  kem_result.encapsulated_shared_key().size(),
10✔
471
                  dec->encapsulated_key_length());
472

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

476
   result.test_eq(
10✔
477
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
478

479
   result.test_eq("decrypted K matches", decr_shared_key, K);
10✔
480

481
   return result;
10✔
482
}
55✔
483

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

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

490
   auto privkey = load_our_key(header, vars);
268✔
491

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

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

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

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

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

505
         auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
268✔
506
         result.test_eq(provider, "agreement", derived_key, shared);
268✔
507

508
         if(key_len == 0 && kdf == "Raw") {
268✔
509
            result.test_eq("Expected size", derived_key.size(), kas->agreed_value_size());
528✔
510
         }
511
      } catch(Botan::Lookup_Error&) {
1,072✔
512
         //result.test_note("Skipping key agreement with with " + provider);
513
      }
804✔
514
   }
1,340✔
515

516
   return result;
268✔
517
}
804✔
518

519
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo) {
54✔
520
   std::vector<std::string> pk_provider =
54✔
521
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
324✔
522
   return Test::provider_filter(pk_provider);
108✔
523
}
54✔
524

525
namespace {
526

527
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && \
528
      (defined(BOTAN_HAS_SHA2_32) || defined(BOTAN_HAS_SCRYPT))
529
void test_pbe_roundtrip(Test::Result& result,
108✔
530
                        const Botan::Private_Key& key,
531
                        const std::string& pbe_algo,
532
                        const std::string& passphrase) {
533
   const auto pkcs8 = key.private_key_info();
108✔
534

535
   try {
108✔
536
      Botan::DataSource_Memory data_src(
108✔
537
         Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
108✔
538

539
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
108✔
540

541
      result.confirm("recovered private key from encrypted blob", loaded != nullptr);
216✔
542
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
238✔
543
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
324✔
544
   } catch(std::exception& e) {
216✔
545
      result.test_failure("roundtrip encrypted PEM private key", e.what());
×
546
   }
×
547

548
   try {
108✔
549
      Botan::DataSource_Memory data_src(
108✔
550
         Botan::PKCS8::BER_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
216✔
551

552
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
108✔
553

554
      result.confirm("recovered private key from BER blob", loaded != nullptr);
216✔
555
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
238✔
556
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
324✔
557
   } catch(std::exception& e) {
216✔
558
      result.test_failure("roundtrip encrypted BER private key", e.what());
×
559
   }
×
560
}
108✔
561
   #endif
562

563
}  // namespace
564

565
std::vector<Test::Result> PK_Key_Generation_Test::run() {
16✔
566
   std::vector<Test::Result> results;
16✔
567

568
   for(const auto& param : keygen_params()) {
70✔
569
      const std::string report_name = algo_name() + (param.empty() ? param : " " + param);
110✔
570

571
      Test::Result result(report_name + " keygen");
54✔
572

573
      const std::vector<std::string> providers = possible_providers(algo_name());
54✔
574

575
      if(providers.empty()) {
54✔
576
         result.note_missing("provider key generation " + algo_name());
×
577
      }
578

579
      result.start_timer();
54✔
580
      for(auto&& prov : providers) {
108✔
581
         auto key_p = Botan::create_private_key(algo_name(), Test::rng(), param, prov);
54✔
582

583
         if(key_p == nullptr) {
54✔
584
            result.test_failure("create_private_key returned null, should throw instead");
×
585
            continue;
×
586
         }
587

588
         const Botan::Private_Key& key = *key_p;
54✔
589

590
         try {
54✔
591
            result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
162✔
592
         } catch(Botan::Lookup_Error&) {}
×
593

594
         result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
54✔
595
         result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
54✔
596

597
         auto public_key = key.public_key();
54✔
598

599
         result.test_eq("public_key has same name", public_key->algo_name(), key.algo_name());
119✔
600

601
         result.test_eq(
162✔
602
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
108✔
603

604
         // Test generation of another key pair from a given (abstract) asymmetric key
605
         auto sk2 = public_key->generate_another(Test::rng());
54✔
606
         auto pk2 = sk2->public_key();
54✔
607

608
         result.test_eq("new private key has the same name", sk2->algo_name(), key.algo_name());
119✔
609
         result.test_eq("new public key has the same name", pk2->algo_name(), public_key->algo_name());
119✔
610
         result.test_eq(
54✔
611
            "new private key has the same est. strength", sk2->estimated_strength(), key.estimated_strength());
54✔
612
         result.test_eq(
54✔
613
            "new public key has the same est. strength", pk2->estimated_strength(), public_key->estimated_strength());
54✔
614
         result.test_ne("new private keys are different keys", sk2->private_key_bits(), key.private_key_bits());
216✔
615

616
         // Test PEM public key round trips OK
617
         try {
54✔
618
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
54✔
619
            auto loaded = Botan::X509::load_key(data_src);
54✔
620

621
            result.confirm("recovered public key from private", loaded != nullptr);
108✔
622
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
119✔
623

624
            try {
54✔
625
               result.test_eq("public key passes checks", loaded->check_key(Test::rng(), false), true);
108✔
626
            } catch(Botan::Lookup_Error&) {}
×
627
         } catch(std::exception& e) {
108✔
628
            result.test_failure("roundtrip PEM public key", e.what());
×
629
         }
×
630

631
         // Test DER public key round trips OK
632
         try {
54✔
633
            const auto ber = key.subject_public_key();
54✔
634
            Botan::DataSource_Memory data_src(ber);
54✔
635
            auto loaded = Botan::X509::load_key(data_src);
54✔
636

637
            result.confirm("recovered public key from private", loaded != nullptr);
108✔
638
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
119✔
639
            result.test_eq("public key has same encoding", loaded->subject_public_key(), ber);
162✔
640
         } catch(std::exception& e) {
162✔
641
            result.test_failure("roundtrip BER public key", e.what());
×
642
         }
×
643

644
         // Test PEM private key round trips OK
645
         try {
54✔
646
            const auto ber = key.private_key_info();
54✔
647
            Botan::DataSource_Memory data_src(ber);
54✔
648
            auto loaded = Botan::PKCS8::load_key(data_src);
54✔
649

650
            result.confirm("recovered private key from PEM blob", loaded != nullptr);
108✔
651
            result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
119✔
652
            result.test_eq("reloaded key has same encoding", loaded->private_key_info(), ber);
162✔
653
         } catch(std::exception& e) {
162✔
654
            result.test_failure("roundtrip PEM private key", e.what());
×
655
         }
×
656

657
         try {
54✔
658
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
54✔
659
            auto loaded = Botan::PKCS8::load_key(data_src);
54✔
660

661
            result.confirm("recovered public key from private", loaded != nullptr);
108✔
662
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
130✔
663
         } catch(std::exception& e) {
108✔
664
            result.test_failure("roundtrip BER private key", e.what());
×
665
         }
×
666

667
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
668

669
         test_pbe_roundtrip(result, key, "PBE-PKCS5v20(AES-128/CBC,SHA-256)", Test::random_password());
108✔
670
   #endif
671

672
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
673

674
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", Test::random_password());
145✔
675
   #endif
676
      }
216✔
677

678
      result.end_timer();
54✔
679

680
      results.push_back(result);
54✔
681
   }
110✔
682

683
   return results;
16✔
684
}
×
685

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

689
   if(header != "Valid" && header != "Invalid") {
9✔
690
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
691
   }
692

693
   const bool expected_valid = (header == "Valid");
9✔
694
   auto pubkey = load_public_key(vars);
9✔
695

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

698
   result.test_eq("Expected validation result", expected_valid, tested_valid);
9✔
699

700
   return result;
9✔
701
}
9✔
702

703
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
704
                                                                   const std::string& test_src) :
2✔
705
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
6✔
706

707
Test::Result PK_Key_Generation_Stability_Test::run_one_test(const std::string&, const VarMap& vars) {
3✔
708
   const std::string key_param = vars.get_opt_str("KeyParams", "");
6✔
709
   const std::string rng_algo = vars.get_req_str("Rng");
3✔
710
   const std::string rng_params = vars.get_opt_str("RngParams", "");
6✔
711
   const std::vector<uint8_t> rng_seed = vars.get_req_bin("RngSeed");
3✔
712
   const std::vector<uint8_t> expected_key = vars.get_req_bin("Key");
3✔
713

714
   std::ostringstream report_name;
3✔
715

716
   report_name << algo_name();
6✔
717
   if(!key_param.empty()) {
3✔
718
      report_name << " " << key_param;
3✔
719
   }
720
   report_name << " keygen stability";
3✔
721

722
   Test::Result result(report_name.str());
3✔
723

724
   result.start_timer();
3✔
725

726
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
3✔
727

728
   #if defined(BOTAN_HAS_HMAC_DRBG)
729
   if(rng_algo == "HMAC_DRBG") {
3✔
730
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
1✔
731
   }
732
   #endif
733

734
   if(rng_algo == "Fixed") {
3✔
735
      if(!rng_params.empty()) {
2✔
736
         throw Test_Error("Expected empty RngParams for Fixed RNG");
×
737
      }
738
      rng = std::make_unique<Fixed_Output_RNG>();
4✔
739
   }
740

741
   if(rng) {
3✔
742
      rng->add_entropy(rng_seed.data(), rng_seed.size());
3✔
743

744
      try {
3✔
745
         auto key = Botan::create_private_key(algo_name(), *rng, key_param);
6✔
746
         const auto key_bits = key->private_key_info();
3✔
747
         result.test_eq("Generated key matched expected value", key_bits, expected_key);
6✔
748
      } catch(Botan::Exception& e) {
6✔
749
         result.test_note("failed to create key", e.what());
×
750
      }
×
751
   } else {
752
      result.test_note("Skipping test due to unavailable RNG");
×
753
   }
754

755
   result.end_timer();
3✔
756

757
   return result;
3✔
758
}
9✔
759

760
/**
761
 * @brief Some general tests for minimal API sanity for signing/verification.
762
 */
763
class PK_API_Sign_Test : public Text_Based_Test {
764
   public:
765
      PK_API_Sign_Test() : Text_Based_Test("pubkey/api_sign.vec", "AlgoParams,SigParams", "Provider") {}
3✔
766

767
   protected:
768
      Test::Result run_one_test(const std::string& algorithm, const VarMap& vars) final {
13✔
769
         const std::string algo_params = vars.get_req_str("AlgoParams");
13✔
770
         const std::string sig_params = vars.get_req_str("SigParams");
13✔
771
         const std::string verify_params = vars.get_opt_str("VerifyParams", sig_params);
13✔
772
         const std::string provider = vars.get_opt_str("Provider", "base");
26✔
773

774
         std::ostringstream test_name;
13✔
775
         test_name << "Sign/verify API tests " << algorithm;
13✔
776
         if(!algo_params.empty()) {
13✔
777
            test_name << '(' << algo_params << ')';
11✔
778
         }
779
         if(!sig_params.empty()) {
13✔
780
            test_name << '/' << sig_params;
11✔
781
         }
782
         Test::Result result(test_name.str());
13✔
783

784
         auto privkey = Botan::create_private_key(algorithm, Test::rng(), algo_params, provider);
13✔
785
         if(!privkey) {
13✔
786
            result.test_note(Botan::fmt(
×
787
               "Skipping Sign/verify API tests for {}({}) with provider {}", algorithm, algo_params, provider));
788
            return result;
×
789
         }
790
         auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
13✔
791
         result.confirm("Storing and loading public key works", pubkey != nullptr);
26✔
792

793
         result.confirm("private key claims to support signatures",
26✔
794
                        privkey->supports_operation(Botan::PublicKeyOperation::Signature));
13✔
795
         result.confirm("public key claims to support signatures",
26✔
796
                        pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
13✔
797
         result.test_gt("Public key length must be greater than 0", privkey->key_length(), 0);
13✔
798

799
         auto signer = std::make_unique<Botan::PK_Signer>(
13✔
800
            *privkey, Test::rng(), sig_params, Botan::Signature_Format::Standard, provider);
13✔
801
         auto verifier =
13✔
802
            std::make_unique<Botan::PK_Verifier>(*pubkey, verify_params, Botan::Signature_Format::Standard, provider);
13✔
803
         result.confirm("Creating PK_Signer works", signer != nullptr);
26✔
804
         result.confirm("Creating PK_Signer works", verifier != nullptr);
26✔
805

806
         result.test_is_nonempty("PK_Signer should report some hash", signer->hash_function());
26✔
807
         result.test_is_nonempty("PK_Verifier should report some hash", verifier->hash_function());
26✔
808

809
         result.test_eq(
39✔
810
            "PK_Signer and PK_Verifier report the same hash", signer->hash_function(), verifier->hash_function());
26✔
811

812
         pubkey.reset();
13✔
813
         privkey.reset();
13✔
814
         const std::array<uint8_t, 4> msg{0xde, 0xad, 0xbe, 0xef};
13✔
815
         const auto sig = signer->sign_message(msg, Test::rng());
13✔
816
         result.test_gt("Signer should still work if no one else hold a reference to the key", sig.size(), 0);
13✔
817
         result.test_eq("Verifier should still work if no one else hold a reference to the key",
26✔
818
                        verifier->verify_message(msg, sig),
13✔
819
                        true);
820

821
         return result;
13✔
822
      }
33✔
823
};
824

825
BOTAN_REGISTER_TEST("pubkey", "pk_api_sign", PK_API_Sign_Test);
826

827
}  // namespace Botan_Tests
828

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