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

randombit / botan / 5520856989

11 Jul 2023 01:54PM UTC coverage: 91.738% (+0.003%) from 91.735%
5520856989

Pull #3611

github

web-flow
Merge a9bdf7311 into 054e13f3d
Pull Request #3611: API modernization PK_KEM_Encryptor/Decryptor

78273 of 85322 relevant lines covered (91.74%)

12409274.59 hits per line

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

86.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

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,936✔
64

65
         if(!result.test_ne("incorrect ciphertext different", decrypted, plaintext)) {
5,808✔
66
            result.test_eq("used corrupted ciphertext", bad_ctext, ciphertext);
×
67
         }
68
      } catch(std::exception&) {
5,040✔
69
         ++ciphertext_rejected;
3,104✔
70
      }
3,104✔
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 {
3,072✔
179
   return Botan::Signature_Format::Standard;
3,072✔
180
}
181

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

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

189
   auto pubkey = load_public_key(vars);
12,632✔
190

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

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

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

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

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

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

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

233
   return result;
25,264✔
234
}
50,537✔
235

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

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

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

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

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

257
   return result;
702✔
258
}
2,247✔
259

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

264
   auto privkey = key();
1✔
265

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

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

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

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

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

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

297
   return {result};
2✔
298
}
3✔
299

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

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

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

313
   auto privkey = load_private_key(vars);
134✔
314

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

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

322
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
134✔
323

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

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

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

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

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

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

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

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

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

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

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

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

384
   return result;
268✔
385
}
536✔
386

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

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

394
   auto privkey = load_private_key(vars);
42✔
395

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

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

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

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

416
   return result;
42✔
417
}
151✔
418

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

425
   Test::Result result(algo_name() + "/" + kdf + " KEM");
20✔
426

427
   auto privkey = load_private_key(vars);
10✔
428

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

432
   const Botan::Public_Key& pubkey = *privkey;
10✔
433

434
   const size_t desired_key_len = K.size();
10✔
435

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

444
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
20✔
445

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

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

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

455
   result.test_eq("C0 matches", kem_result.encapsulated_shared_key(), C0);
10✔
456
   result.test_eq("K matches", kem_result.shared_key(), K);
10✔
457

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

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

469
   result.test_eq(
10✔
470
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
471

472
   result.test_eq("decrypted K matches", decr_shared_key, K);
10✔
473

474
   return result;
10✔
475
}
55✔
476

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

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

483
   auto privkey = load_our_key(header, vars);
268✔
484

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

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

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

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

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

498
         auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
268✔
499
         result.test_eq(provider, "agreement", derived_key, shared);
268✔
500

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

509
   return result;
268✔
510
}
804✔
511

512
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo) {
54✔
513
   std::vector<std::string> pk_provider =
54✔
514
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
324✔
515
   return Test::provider_filter(pk_provider);
108✔
516
}
54✔
517

518
namespace {
519

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

528
   try {
108✔
529
      Botan::DataSource_Memory data_src(
108✔
530
         Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
108✔
531

532
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
108✔
533

534
      result.confirm("recovered private key from encrypted blob", loaded != nullptr);
216✔
535
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
238✔
536
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
324✔
537
   } catch(std::exception& e) {
216✔
538
      result.test_failure("roundtrip encrypted PEM private key", e.what());
×
539
   }
×
540

541
   try {
108✔
542
      Botan::DataSource_Memory data_src(
108✔
543
         Botan::PKCS8::BER_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
216✔
544

545
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
108✔
546

547
      result.confirm("recovered private key from BER blob", loaded != nullptr);
216✔
548
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
238✔
549
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
324✔
550
   } catch(std::exception& e) {
216✔
551
      result.test_failure("roundtrip encrypted BER private key", e.what());
×
552
   }
×
553
}
108✔
554
   #endif
555

556
}  // namespace
557

558
std::vector<Test::Result> PK_Key_Generation_Test::run() {
16✔
559
   std::vector<Test::Result> results;
16✔
560

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

564
      Test::Result result(report_name + " keygen");
54✔
565

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

568
      if(providers.empty()) {
54✔
569
         result.note_missing("provider key generation " + algo_name());
×
570
      }
571

572
      result.start_timer();
54✔
573
      for(auto&& prov : providers) {
108✔
574
         auto key_p = Botan::create_private_key(algo_name(), Test::rng(), param, prov);
54✔
575

576
         if(key_p == nullptr) {
54✔
577
            result.test_failure("create_private_key returned null, should throw instead");
×
578
            continue;
×
579
         }
580

581
         const Botan::Private_Key& key = *key_p;
54✔
582

583
         try {
54✔
584
            result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
162✔
585
         } catch(Botan::Lookup_Error&) {}
×
586

587
         result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
54✔
588
         result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
54✔
589

590
         auto public_key = key.public_key();
54✔
591

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

594
         result.test_eq(
162✔
595
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
108✔
596

597
         // Test PEM public key round trips OK
598
         try {
54✔
599
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
54✔
600
            auto loaded = Botan::X509::load_key(data_src);
54✔
601

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

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

612
         // Test DER public key round trips OK
613
         try {
54✔
614
            const auto ber = key.subject_public_key();
54✔
615
            Botan::DataSource_Memory data_src(ber);
54✔
616
            auto loaded = Botan::X509::load_key(data_src);
54✔
617

618
            result.confirm("recovered public key from private", loaded != nullptr);
108✔
619
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
119✔
620
            result.test_eq("public key has same encoding", loaded->subject_public_key(), ber);
162✔
621
         } catch(std::exception& e) {
162✔
622
            result.test_failure("roundtrip BER public key", e.what());
×
623
         }
×
624

625
         // Test PEM private key round trips OK
626
         try {
54✔
627
            const auto ber = key.private_key_info();
54✔
628
            Botan::DataSource_Memory data_src(ber);
54✔
629
            auto loaded = Botan::PKCS8::load_key(data_src);
54✔
630

631
            result.confirm("recovered private key from PEM blob", loaded != nullptr);
108✔
632
            result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
119✔
633
            result.test_eq("reloaded key has same encoding", loaded->private_key_info(), ber);
162✔
634
         } catch(std::exception& e) {
162✔
635
            result.test_failure("roundtrip PEM private key", e.what());
×
636
         }
×
637

638
         try {
54✔
639
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
54✔
640
            auto loaded = Botan::PKCS8::load_key(data_src);
54✔
641

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

648
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
649

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

653
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
654

655
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", Test::random_password());
152✔
656
   #endif
657
      }
108✔
658

659
      result.end_timer();
54✔
660

661
      results.push_back(result);
54✔
662
   }
110✔
663

664
   return results;
16✔
665
}
×
666

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

670
   if(header != "Valid" && header != "Invalid") {
9✔
671
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
672
   }
673

674
   const bool expected_valid = (header == "Valid");
9✔
675
   auto pubkey = load_public_key(vars);
9✔
676

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

679
   result.test_eq("Expected validation result", expected_valid, tested_valid);
9✔
680

681
   return result;
9✔
682
}
9✔
683

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

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

695
   std::ostringstream report_name;
3✔
696

697
   report_name << algo_name();
6✔
698
   if(!key_param.empty()) {
3✔
699
      report_name << " " << key_param;
3✔
700
   }
701
   report_name << " keygen stability";
3✔
702

703
   Test::Result result(report_name.str());
3✔
704

705
   result.start_timer();
3✔
706

707
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
3✔
708

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

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

722
   if(rng) {
3✔
723
      rng->add_entropy(rng_seed.data(), rng_seed.size());
3✔
724

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

736
   result.end_timer();
3✔
737

738
   return result;
3✔
739
}
9✔
740

741
}  // namespace Botan_Tests
742

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