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

randombit / botan / 5079590438

25 May 2023 12:28PM UTC coverage: 92.228% (+0.5%) from 91.723%
5079590438

Pull #3502

github

Pull Request #3502: Apply clang-format to the codebase

75589 of 81959 relevant lines covered (92.23%)

12139530.51 hits per line

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

90.99
/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,937✔
64

65
         if(!result.test_ne("incorrect ciphertext different", decrypted, plaintext)) {
5,811✔
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
   return vars.get_opt_str("Padding", this->default_padding(vars));
26,449✔
79
}
80

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

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

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

96
   Test::Result result(test_name.str());
1,161✔
97

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

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

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

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

114
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,161✔
115

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

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

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

129
      check_invalid_signatures(result, *verifier, message, signature);
1,161✔
130

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

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

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

139
      std::vector<uint8_t> generated_signature;
4,644✔
140

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

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

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

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

171
   return result;
1,161✔
172
}
5,988✔
173

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

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

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

183
   auto pubkey = load_public_key(vars);
12,632✔
184

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

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

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

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

206
      if(verifier) {
50,528✔
207
         try {
8,382✔
208
            const bool verified = verifier->verify_message(message, signature);
8,382✔
209

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

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

223
   return result;
25,264✔
224
}
50,537✔
225

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

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

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

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

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

245
   return result;
702✔
246
}
2,247✔
247

248
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
249
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
250
   const std::string padding = m_padding;
1✔
251

252
   auto privkey = key();
1✔
253

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

256
   for(const auto& provider : possible_providers(algo_name())) {
3✔
257
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
258
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
259

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

267
      if(signer && verifier) {
1✔
268
         try {
1✔
269
            std::vector<uint8_t> generated_signature = signer->sign_message(message, Test::rng());
1✔
270
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
271

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

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

281
   return {result};
2✔
282
}
3✔
283

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

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

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

297
   auto privkey = load_private_key(vars);
134✔
298

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

302
   // instead slice the private key to work around elgamal test inputs
303
   //auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
304
   Botan::Public_Key* pubkey = privkey.get();
134✔
305

306
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
134✔
307

308
   for(const auto& dec_provider : possible_providers(algo_name())) {
804✔
309
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
536✔
310

311
      try {
536✔
312
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
536✔
313
      } catch(Botan::Lookup_Error&) { continue; }
402✔
314

315
      Botan::secure_vector<uint8_t> decrypted;
134✔
316
      try {
134✔
317
         decrypted = decryptor->decrypt(ciphertext);
134✔
318

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

322
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
134✔
323
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
134✔
324
   }
804✔
325

326
   for(const auto& enc_provider : possible_providers(algo_name())) {
804✔
327
      std::unique_ptr<Botan::PK_Encryptor> encryptor;
536✔
328

329
      try {
536✔
330
         encryptor = std::make_unique<Botan::PK_Encryptor_EME>(*pubkey, Test::rng(), padding, enc_provider);
536✔
331
      } catch(Botan::Lookup_Error&) { continue; }
402✔
332

333
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
134✔
334
      if(vars.has_key("Nonce")) {
268✔
335
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
118✔
336
      }
337

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

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

350
      result.test_lte(
134✔
351
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
134✔
352

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

362
   return result;
268✔
363
}
536✔
364

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

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

372
   auto privkey = load_private_key(vars);
42✔
373

374
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
375
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
376

377
      try {
168✔
378
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, Test::rng(), padding, dec_provider);
168✔
379
      } catch(Botan::Lookup_Error&) { continue; }
126✔
380

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

386
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
42✔
387
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext);
42✔
388
   }
252✔
389

390
   return result;
42✔
391
}
151✔
392

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

399
   Test::Result result(algo_name() + "/" + kdf + " KEM");
20✔
400

401
   auto privkey = load_private_key(vars);
10✔
402

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

406
   const Botan::Public_Key& pubkey = *privkey;
10✔
407

408
   const size_t desired_key_len = K.size();
409

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

418
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
20✔
419

420
   Botan::secure_vector<uint8_t> produced_encap_key, shared_key;
10✔
421
   enc->encrypt(produced_encap_key, shared_key, desired_key_len, fixed_output_rng, salt);
10✔
422

423
   result.test_eq(
10✔
424
      "encapsulated key length matches expected", produced_encap_key.size(), enc->encapsulated_key_length());
425

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

428
   result.test_eq("C0 matches", produced_encap_key, C0);
10✔
429
   result.test_eq("K matches", shared_key, K);
10✔
430

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

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

442
   result.test_eq(
10✔
443
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
444

445
   result.test_eq("decrypted K matches", decr_shared_key, K);
10✔
446

447
   return result;
10✔
448
}
75✔
449

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

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

456
   auto privkey = load_our_key(header, vars);
268✔
457

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

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

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

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

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

471
         auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
268✔
472
         result.test_eq(provider, "agreement", derived_key, shared);
268✔
473

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

482
   return result;
268✔
483
}
804✔
484

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

491
namespace {
492

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

501
   try {
84✔
502
      Botan::DataSource_Memory data_src(
84✔
503
         Botan::PKCS8::PEM_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
84✔
504

505
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
84✔
506

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

512
   try {
84✔
513
      Botan::DataSource_Memory data_src(
84✔
514
         Botan::PKCS8::BER_encode(key, Test::rng(), passphrase, std::chrono::milliseconds(1), pbe_algo));
168✔
515

516
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
84✔
517

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

525
}
526

527
std::vector<Test::Result> PK_Key_Generation_Test::run() {
15✔
528
   std::vector<Test::Result> results;
15✔
529

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

533
      Test::Result result(report_name + " keygen");
42✔
534

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

537
      if(providers.empty()) {
42✔
538
         result.note_missing("provider key generation " + algo_name());
×
539
      }
540

541
      result.start_timer();
42✔
542
      for(auto&& prov : providers) {
84✔
543
         auto key_p = Botan::create_private_key(algo_name(), Test::rng(), param, prov);
42✔
544

545
         if(key_p == nullptr) {
42✔
546
            result.test_failure("create_private_key returned null, should throw instead");
×
547
            continue;
×
548
         }
549

550
         const Botan::Private_Key& key = *key_p;
42✔
551

552
         try {
42✔
553
            result.confirm("Key passes self tests", key.check_key(Test::rng(), true));
126✔
554
         } catch(Botan::Lookup_Error&) {}
×
555

556
         result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
42✔
557
         result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
42✔
558

559
         auto public_key = key.public_key();
42✔
560

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

563
         result.test_eq(
126✔
564
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
84✔
565

566
         // Test PEM public key round trips OK
567
         try {
42✔
568
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
42✔
569
            auto loaded = Botan::X509::load_key(data_src);
42✔
570

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

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

579
         // Test DER public key round trips OK
580
         try {
42✔
581
            const auto ber = key.subject_public_key();
42✔
582
            Botan::DataSource_Memory data_src(ber);
42✔
583
            auto loaded = Botan::X509::load_key(data_src);
42✔
584

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

590
         // Test PEM private key round trips OK
591
         try {
42✔
592
            const auto ber = key.private_key_info();
42✔
593
            Botan::DataSource_Memory data_src(ber);
42✔
594
            auto loaded = Botan::PKCS8::load_key(data_src);
42✔
595

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

601
         try {
42✔
602
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
42✔
603
            auto loaded = Botan::PKCS8::load_key(data_src);
42✔
604

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

609
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
610

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

614
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
615

616
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", Test::random_password());
119✔
617
   #endif
618
      }
84✔
619

620
      result.end_timer();
42✔
621

622
      results.push_back(result);
42✔
623
   }
85✔
624

625
   return results;
15✔
626
}
×
627

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

631
   if(header != "Valid" && header != "Invalid")
9✔
632
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
633

634
   const bool expected_valid = (header == "Valid");
9✔
635
   auto pubkey = load_public_key(vars);
9✔
636

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

639
   result.test_eq("Expected validation result", expected_valid, tested_valid);
9✔
640

641
   return result;
9✔
642
}
9✔
643

644
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
645
                                                                   const std::string& test_src) :
2✔
646
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
6✔
647

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

655
   std::ostringstream report_name;
3✔
656

657
   report_name << algo_name();
6✔
658
   if(!key_param.empty())
3✔
659
      report_name << " " << key_param;
3✔
660
   report_name << " keygen stability";
3✔
661

662
   Test::Result result(report_name.str());
3✔
663

664
   result.start_timer();
3✔
665

666
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
3✔
667

668
   #if defined(BOTAN_HAS_HMAC_DRBG)
669
   if(rng_algo == "HMAC_DRBG") {
3✔
670
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
1✔
671
   }
672
   #endif
673

674
   if(rng_algo == "Fixed") {
3✔
675
      if(!rng_params.empty())
2✔
676
         throw Test_Error("Expected empty RngParams for Fixed RNG");
×
677
      rng = std::make_unique<Fixed_Output_RNG>();
4✔
678
   }
679

680
   if(rng) {
3✔
681
      rng->add_entropy(rng_seed.data(), rng_seed.size());
3✔
682

683
      try {
3✔
684
         auto key = Botan::create_private_key(algo_name(), *rng, key_param);
6✔
685
         const auto key_bits = key->private_key_info();
3✔
686
         result.test_eq("Generated key matched expected value", key_bits, expected_key);
6✔
687
      } catch(Botan::Exception& e) { result.test_note("failed to create key", e.what()); }
6✔
688
   } else {
689
      result.test_note("Skipping test due to unavailable RNG");
×
690
   }
691

692
   result.end_timer();
3✔
693

694
   return result;
3✔
695
}
9✔
696

697
}
698

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