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

randombit / botan / 11345152100

15 Oct 2024 11:31AM UTC coverage: 91.146% (+0.02%) from 91.131%
11345152100

Pull #4270

github

web-flow
Merge 041fb0a91 into 7dd590598
Pull Request #4270: PQC: ML-DSA

90747 of 99562 relevant lines covered (91.15%)

9029457.78 hits per line

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

87.81
/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
   #include <botan/internal/stl_util.h>
22

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

27
   #include <array>
28

29
namespace Botan_Tests {
30

31
namespace {
32

33
void check_invalid_signatures(Test::Result& result,
1,976✔
34
                              Botan::PK_Verifier& verifier,
35
                              const std::vector<uint8_t>& message,
36
                              const std::vector<uint8_t>& signature,
37
                              Botan::RandomNumberGenerator& rng) {
38
   const size_t tests_to_run = (Test::run_long_tests() ? 20 : 5);
1,976✔
39

40
   const std::vector<uint8_t> zero_sig(signature.size());
1,976✔
41
   result.test_eq("all zero signature invalid", verifier.verify_message(message, zero_sig), false);
1,976✔
42

43
   for(size_t i = 0; i < tests_to_run; ++i) {
41,496✔
44
      const std::vector<uint8_t> bad_sig = Test::mutate_vec(signature, rng);
39,520✔
45

46
      try {
39,520✔
47
         if(!result.test_eq("incorrect signature invalid", verifier.verify_message(message, bad_sig), false)) {
39,520✔
48
            result.test_note("Accepted invalid signature " + Botan::hex_encode(bad_sig));
×
49
         }
50
      } catch(std::exception& e) {
×
51
         result.test_note("Accepted invalid signature " + Botan::hex_encode(bad_sig));
×
52
         result.test_failure("Modified signature rejected with exception", e.what());
×
53
      }
×
54
   }
39,520✔
55
}
1,976✔
56

57
}  // namespace
58

59
// Exposed for DLIES tests
60
void check_invalid_ciphertexts(Test::Result& result,
252✔
61
                               Botan::PK_Decryptor& decryptor,
62
                               const std::vector<uint8_t>& plaintext,
63
                               const std::vector<uint8_t>& ciphertext,
64
                               Botan::RandomNumberGenerator& rng) {
65
   const size_t tests_to_run = (Test::run_long_tests() ? 20 : 5);
252✔
66

67
   size_t ciphertext_accepted = 0, ciphertext_rejected = 0;
252✔
68

69
   for(size_t i = 0; i < tests_to_run; ++i) {
5,292✔
70
      const std::vector<uint8_t> bad_ctext = Test::mutate_vec(ciphertext, rng);
5,040✔
71

72
      try {
5,040✔
73
         const Botan::secure_vector<uint8_t> decrypted = decryptor.decrypt(bad_ctext);
5,040✔
74
         ++ciphertext_accepted;
1,933✔
75

76
         if(!result.test_ne("incorrect ciphertext different", decrypted, plaintext)) {
3,866✔
77
            result.test_eq("used corrupted ciphertext", bad_ctext, ciphertext);
×
78
         }
79
      } catch(std::exception&) {
5,040✔
80
         ++ciphertext_rejected;
3,107✔
81
      }
3,107✔
82
   }
5,040✔
83

84
   result.test_note("Accepted " + std::to_string(ciphertext_accepted) + " invalid ciphertexts, rejected " +
1,008✔
85
                    std::to_string(ciphertext_rejected));
252✔
86
}
252✔
87

88
std::string PK_Test::choose_padding(const VarMap& vars, const std::string& pad_hdr) {
14,852✔
89
   if(!pad_hdr.empty()) {
14,852✔
90
      return pad_hdr;
1,637✔
91
   }
92
   return vars.get_opt_str("Padding", this->default_padding(vars));
26,430✔
93
}
94

95
std::vector<std::string> PK_Test::possible_providers(const std::string& /*params*/) {
16,952✔
96
   return Test::provider_filter({"base", "commoncrypto", "openssl", "tpm"});
16,952✔
97
}
98

99
Test::Result PK_Signature_Generation_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
1,182✔
100
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
1,182✔
101
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
1,182✔
102
   const std::string padding = choose_padding(vars, pad_hdr);
1,182✔
103

104
   std::ostringstream test_name;
1,182✔
105
   test_name << algo_name();
2,364✔
106
   if(vars.has_key("Group")) {
2,364✔
107
      test_name << "-" << vars.get_req_str("Group");
254✔
108
   }
109
   test_name << "/" << padding << " signature generation";
1,182✔
110

111
   Test::Result result(test_name.str());
1,182✔
112

113
   std::unique_ptr<Botan::Private_Key> privkey;
1,182✔
114
   try {
1,182✔
115
      privkey = load_private_key(vars);
1,182✔
116
   } catch(Botan::Lookup_Error& e) {
×
117
      result.note_missing(e.what());
×
118
      return result;
×
119
   }
×
120

121
   result.confirm("private key claims to support signatures",
2,364✔
122
                  privkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,182✔
123

124
   auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
1,182✔
125

126
   result.confirm("public key claims to support signatures",
2,364✔
127
                  pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,182✔
128

129
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,182✔
130

131
   for(const auto& verify_provider : possible_providers(algo_name())) {
7,092✔
132
      std::unique_ptr<Botan::PK_Verifier> verifier;
4,728✔
133

134
      try {
4,728✔
135
         verifier =
4,728✔
136
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::Standard, verify_provider);
5,910✔
137
      } catch(Botan::Lookup_Error&) {
3,546✔
138
         //result.test_note("Skipping verifying with " + verify_provider);
139
         continue;
3,546✔
140
      }
3,546✔
141

142
      result.test_eq("KAT signature valid", verifier->verify_message(message, signature), true);
1,182✔
143

144
      check_invalid_signatures(result, *verifier, message, signature, this->rng());
1,182✔
145

146
      result.test_eq("KAT signature valid (try 2)", verifier->verify_message(message, signature), true);
1,182✔
147

148
      verifiers.push_back(std::move(verifier));
1,182✔
149
   }
5,910✔
150

151
   for(const auto& sign_provider : possible_providers(algo_name())) {
7,092✔
152
      std::unique_ptr<Botan::PK_Signer> signer;
4,728✔
153

154
      std::vector<uint8_t> generated_signature;
4,728✔
155

156
      try {
4,728✔
157
         signer = std::make_unique<Botan::PK_Signer>(
4,728✔
158
            *privkey, this->rng(), padding, Botan::Signature_Format::Standard, sign_provider);
5,910✔
159

160
         if(vars.has_key("Nonce")) {
2,364✔
161
            auto rng = test_rng(vars.get_req_bin("Nonce"));
446✔
162
            generated_signature = signer->sign_message(message, *rng);
446✔
163
         } else {
223✔
164
            generated_signature = signer->sign_message(message, this->rng());
1,918✔
165
         }
166

167
         result.test_lte(
1,182✔
168
            "Generated signature within announced bound", generated_signature.size(), signer->signature_length());
169
      } catch(Botan::Lookup_Error&) {
3,546✔
170
         //result.test_note("Skipping signing with " + sign_provider);
171
         continue;
3,546✔
172
      }
3,546✔
173

174
      if(sign_provider == "base") {
1,182✔
175
         result.test_eq("generated signature matches KAT", generated_signature, signature);
2,364✔
176
      } else if(generated_signature != signature) {
×
177
         for(std::unique_ptr<Botan::PK_Verifier>& verifier : verifiers) {
×
178
            if(!result.test_eq(
×
179
                  "generated signature valid", verifier->verify_message(message, generated_signature), true)) {
×
180
               result.test_failure("generated signature", generated_signature);
×
181
            }
182
         }
183
      }
184
   }
5,910✔
185

186
   return result;
1,182✔
187
}
5,910✔
188

189
Botan::Signature_Format PK_Signature_Verification_Test::sig_format() const {
3,696✔
190
   return Botan::Signature_Format::Standard;
3,696✔
191
}
192

193
Test::Result PK_Signature_Verification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
12,788✔
194
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
12,788✔
195
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
12,788✔
196
   const std::string padding = choose_padding(vars, pad_hdr);
12,788✔
197

198
   const bool expected_valid = (vars.get_opt_sz("Valid", 1) == 1);
12,788✔
199

200
   auto pubkey = load_public_key(vars);
12,788✔
201

202
   std::ostringstream result_name;
12,788✔
203
   result_name << algo_name();
25,576✔
204
   if(vars.has_key("Group")) {
25,576✔
205
      result_name << "-" << vars.get_req_str("Group");
24,260✔
206
   }
207
   if(!padding.empty()) {
12,788✔
208
      result_name << "/" << padding;
12,783✔
209
   }
210
   result_name << " signature verification";
12,788✔
211
   Test::Result result(result_name.str());
12,788✔
212

213
   result.confirm("public key claims to support signatures",
25,576✔
214
                  pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
12,788✔
215

216
   for(const auto& verify_provider : possible_providers(algo_name())) {
76,728✔
217
      std::unique_ptr<Botan::PK_Verifier> verifier;
51,152✔
218

219
      try {
51,152✔
220
         verifier = std::make_unique<Botan::PK_Verifier>(*pubkey, padding, sig_format(), verify_provider);
59,690✔
221
      } catch(Botan::Lookup_Error&) {
42,614✔
222
         //result.test_note("Skipping verifying with " + verify_provider);
223
      }
42,614✔
224

225
      if(verifier) {
51,152✔
226
         try {
8,538✔
227
            const bool verified = verifier->verify_message(message, signature);
8,538✔
228

229
            if(expected_valid) {
8,538✔
230
               result.test_eq("correct signature valid with " + verify_provider, verified, true);
4,268✔
231

232
               if(test_random_invalid_sigs()) {
4,268✔
233
                  check_invalid_signatures(result, *verifier, message, signature, this->rng());
793✔
234
               }
235
            } else {
236
               result.confirm("incorrect signature is rejected", verified == false);
8,540✔
237
            }
238
         } catch(std::exception& e) {
×
239
            result.test_failure("verification threw exception", e.what());
×
240
         }
×
241
      }
242
   }
63,940✔
243

244
   return result;
25,576✔
245
}
51,152✔
246

247
Test::Result PK_Signature_NonVerification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
706✔
248
   const std::string padding = choose_padding(vars, pad_hdr);
706✔
249
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
706✔
250
   auto pubkey = load_public_key(vars);
706✔
251

252
   const std::vector<uint8_t> invalid_signature = vars.get_req_bin("InvalidSignature");
706✔
253

254
   Test::Result result(algo_name() + "/" + padding + " verify invalid signature");
3,530✔
255

256
   for(const auto& verify_provider : possible_providers(algo_name())) {
4,236✔
257
      std::unique_ptr<Botan::PK_Verifier> verifier;
2,824✔
258

259
      try {
2,824✔
260
         verifier =
2,824✔
261
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::Standard, verify_provider);
3,530✔
262
         result.test_eq("incorrect signature rejected", verifier->verify_message(message, invalid_signature), false);
1,412✔
263
      } catch(Botan::Lookup_Error&) {
2,118✔
264
         result.test_note("Skipping verifying with " + verify_provider);
2,118✔
265
      }
2,118✔
266
   }
3,530✔
267

268
   return result;
706✔
269
}
2,118✔
270

271
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
272
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
273
   const std::string padding = m_padding;
1✔
274

275
   auto privkey = key();
1✔
276

277
   Test::Result result(algo_name() + "/" + padding + " signature sign/verify using DER format");
5✔
278

279
   for(const auto& provider : possible_providers(algo_name())) {
3✔
280
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
281
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
282

283
      try {
1✔
284
         signer = std::make_unique<Botan::PK_Signer>(
1✔
285
            *privkey, this->rng(), padding, Botan::Signature_Format::DerSequence, provider);
2✔
286
         verifier =
1✔
287
            std::make_unique<Botan::PK_Verifier>(*privkey, padding, Botan::Signature_Format::DerSequence, provider);
2✔
288
      } catch(Botan::Lookup_Error& e) {
×
289
         result.test_note("Skipping sign/verify with " + provider, e.what());
×
290
      }
×
291

292
      if(signer && verifier) {
1✔
293
         try {
1✔
294
            std::vector<uint8_t> generated_signature = signer->sign_message(message, this->rng());
1✔
295
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
296

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

299
            if(test_random_invalid_sigs()) {
1✔
300
               check_invalid_signatures(result, *verifier, message, generated_signature, this->rng());
1✔
301
            }
302
         } catch(std::exception& e) {
1✔
303
            result.test_failure("verification threw exception", e.what());
×
304
         }
×
305
      }
306
   }
2✔
307

308
   return {result};
2✔
309
}
4✔
310

311
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo) {
1✔
312
   std::vector<std::string> pk_provider =
1✔
313
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
1✔
314
   return Test::provider_filter(pk_provider);
2✔
315
}
1✔
316

317
Test::Result PK_Encryption_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
134✔
318
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
134✔
319
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
134✔
320
   const std::string padding = choose_padding(vars, pad_hdr);
134✔
321

322
   Test::Result result(algo_name() + (padding.empty() ? padding : "/" + padding) + " encryption");
536✔
323

324
   auto privkey = load_private_key(vars);
134✔
325

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

329
   // instead slice the private key to work around elgamal test inputs
330
   //auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
331
   Botan::Public_Key* pubkey = privkey.get();
134✔
332

333
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
134✔
334

335
   for(const auto& dec_provider : possible_providers(algo_name())) {
804✔
336
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
536✔
337

338
      try {
536✔
339
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, this->rng(), padding, dec_provider);
536✔
340
      } catch(Botan::Lookup_Error&) {
402✔
341
         continue;
402✔
342
      }
402✔
343

344
      Botan::secure_vector<uint8_t> decrypted;
134✔
345
      try {
134✔
346
         decrypted = decryptor->decrypt(ciphertext);
134✔
347

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

353
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
134✔
354
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
134✔
355
   }
670✔
356

357
   for(const auto& enc_provider : possible_providers(algo_name())) {
804✔
358
      std::unique_ptr<Botan::PK_Encryptor> encryptor;
536✔
359

360
      try {
536✔
361
         encryptor = std::make_unique<Botan::PK_Encryptor_EME>(*pubkey, this->rng(), padding, enc_provider);
536✔
362
      } catch(Botan::Lookup_Error&) {
402✔
363
         continue;
402✔
364
      }
402✔
365

366
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
134✔
367
      if(vars.has_key("Nonce")) {
268✔
368
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
118✔
369
      }
370

371
      if(padding == "Raw") {
134✔
372
         /*
373
         Hack for RSA with no padding since sometimes one more bit will fit in but maximum_input_size
374
         rounds down to nearest byte
375
         */
376
         result.test_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size() + 1);
162✔
377
      } else {
378
         result.test_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size());
106✔
379
      }
380

381
      const std::vector<uint8_t> generated_ciphertext = encryptor->encrypt(plaintext, kat_rng ? *kat_rng : this->rng());
134✔
382

383
      result.test_lte(
134✔
384
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
134✔
385

386
      if(enc_provider == "base") {
134✔
387
         result.test_eq(enc_provider, "generated ciphertext matches KAT", generated_ciphertext, ciphertext);
268✔
388
      } else if(generated_ciphertext != ciphertext) {
×
389
         for(std::unique_ptr<Botan::PK_Decryptor>& dec : decryptors) {
×
390
            result.test_eq("decryption of generated ciphertext", dec->decrypt(generated_ciphertext), plaintext);
×
391
         }
392
      }
393
   }
729✔
394

395
   return result;
268✔
396
}
536✔
397

398
Test::Result PK_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
42✔
399
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
42✔
400
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
42✔
401
   const std::string padding = choose_padding(vars, pad_hdr);
42✔
402

403
   Test::Result result(algo_name() + (padding.empty() ? padding : "/" + padding) + " decryption");
168✔
404

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

407
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
408
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
409

410
      try {
168✔
411
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, this->rng(), padding, dec_provider);
168✔
412
      } catch(Botan::Lookup_Error&) {
126✔
413
         continue;
126✔
414
      }
126✔
415

416
      Botan::secure_vector<uint8_t> decrypted;
42✔
417
      try {
42✔
418
         decrypted = decryptor->decrypt(ciphertext);
42✔
419
      } catch(Botan::Exception& e) {
×
420
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
421
      }
×
422

423
      result.test_eq(dec_provider, "decryption of KAT", decrypted, plaintext);
42✔
424
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
42✔
425
   }
210✔
426

427
   return result;
42✔
428
}
126✔
429

430
Test::Result PK_KEM_Test::run_one_test(const std::string& /*header*/, const VarMap& vars) {
10✔
431
   const std::vector<uint8_t> K = vars.get_req_bin("K");
10✔
432
   const std::vector<uint8_t> C0 = vars.get_req_bin("C0");
10✔
433
   const std::vector<uint8_t> salt = vars.get_opt_bin("Salt");
10✔
434
   const std::string kdf = vars.get_req_str("KDF");
10✔
435

436
   Test::Result result(algo_name() + "/" + kdf + " KEM");
50✔
437

438
   auto privkey = load_private_key(vars);
10✔
439

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

443
   const Botan::Public_Key& pubkey = *privkey;
10✔
444

445
   const size_t desired_key_len = K.size();
10✔
446

447
   std::unique_ptr<Botan::PK_KEM_Encryptor> enc;
10✔
448
   try {
10✔
449
      enc = std::make_unique<Botan::PK_KEM_Encryptor>(pubkey, kdf);
20✔
450
   } catch(Botan::Lookup_Error&) {
×
451
      result.test_note("Skipping due to missing KDF: " + kdf);
×
452
      return result;
×
453
   }
×
454

455
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
20✔
456

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

459
   result.test_eq("encapsulated key length matches expected",
20✔
460
                  kem_result.encapsulated_shared_key().size(),
10✔
461
                  enc->encapsulated_key_length());
462

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

466
   result.test_eq("C0 matches", kem_result.encapsulated_shared_key(), C0);
10✔
467
   result.test_eq("K matches", kem_result.shared_key(), K);
10✔
468

469
   std::unique_ptr<Botan::PK_KEM_Decryptor> dec;
10✔
470
   try {
10✔
471
      dec = std::make_unique<Botan::PK_KEM_Decryptor>(*privkey, this->rng(), kdf);
20✔
472
   } catch(Botan::Lookup_Error& e) {
×
473
      result.test_note("Skipping test", e.what());
×
474
      return result;
×
475
   }
×
476

477
   result.test_eq("encapsulated key length matches expected",
20✔
478
                  kem_result.encapsulated_shared_key().size(),
10✔
479
                  dec->encapsulated_key_length());
480

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

484
   result.test_eq(
10✔
485
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
486

487
   result.test_eq("decrypted K matches", decr_shared_key, K);
10✔
488

489
   return result;
10✔
490
}
40✔
491

492
Test::Result PK_Key_Agreement_Test::run_one_test(const std::string& header, const VarMap& vars) {
784✔
493
   const std::vector<uint8_t> shared = vars.get_req_bin("K");
784✔
494
   const std::string kdf = vars.get_opt_str("KDF", default_kdf(vars));
1,568✔
495

496
   Test::Result result(algo_name() + "/" + kdf + (header.empty() ? header : " " + header) + " key agreement");
5,292✔
497

498
   auto privkey = load_our_key(header, vars);
784✔
499

500
   result.confirm("private key claims to support key agreement",
1,568✔
501
                  privkey->supports_operation(Botan::PublicKeyOperation::KeyAgreement));
784✔
502

503
   const std::vector<uint8_t> pubkey = load_their_key(header, vars);
784✔
504

505
   const size_t key_len = vars.get_opt_sz("OutLen", 0);
784✔
506

507
   for(const auto& provider : possible_providers(algo_name())) {
4,704✔
508
      std::unique_ptr<Botan::PK_Key_Agreement> kas;
3,136✔
509

510
      try {
3,136✔
511
         kas = std::make_unique<Botan::PK_Key_Agreement>(*privkey, this->rng(), kdf, provider);
3,920✔
512

513
         auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
1,568✔
514
         result.test_eq(provider, "agreement", derived_key, shared);
784✔
515

516
         if(key_len == 0 && kdf == "Raw") {
784✔
517
            result.test_eq("Expected size", derived_key.size(), kas->agreed_value_size());
1,560✔
518
         }
519
      } catch(Botan::Lookup_Error&) {
3,136✔
520
         //result.test_note("Skipping key agreement with with " + provider);
521
      }
2,352✔
522
   }
3,920✔
523

524
   return result;
784✔
525
}
2,352✔
526

527
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo) {
97✔
528
   std::vector<std::string> pk_provider =
97✔
529
      Botan::probe_provider_private_key(algo, {"base", "commoncrypto", "openssl", "tpm"});
97✔
530
   return Test::provider_filter(pk_provider);
194✔
531
}
97✔
532

533
namespace {
534

535
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && \
536
      (defined(BOTAN_HAS_SHA2_32) || defined(BOTAN_HAS_SCRYPT))
537
void test_pbe_roundtrip(Test::Result& result,
194✔
538
                        const Botan::Private_Key& key,
539
                        const std::string& pbe_algo,
540
                        Botan::RandomNumberGenerator& rng) {
541
   const auto pkcs8 = key.private_key_info();
194✔
542

543
   auto passphrase = Test::random_password(rng);
194✔
544

545
   try {
194✔
546
      Botan::DataSource_Memory data_src(
194✔
547
         Botan::PKCS8::PEM_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
194✔
548

549
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
194✔
550

551
      result.confirm("recovered private key from encrypted blob", loaded != nullptr);
388✔
552
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
388✔
553
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
582✔
554
   } catch(std::exception& e) {
388✔
555
      result.test_failure("roundtrip encrypted PEM private key", e.what());
×
556
   }
×
557

558
   try {
194✔
559
      Botan::DataSource_Memory data_src(
194✔
560
         Botan::PKCS8::BER_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
388✔
561

562
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
194✔
563

564
      result.confirm("recovered private key from BER blob", loaded != nullptr);
388✔
565
      result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
388✔
566
      result.test_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
582✔
567
   } catch(std::exception& e) {
388✔
568
      result.test_failure("roundtrip encrypted BER private key", e.what());
×
569
   }
×
570
}
388✔
571
   #endif
572

573
}  // namespace
574

575
std::vector<Test::Result> PK_Key_Generation_Test::run() {
20✔
576
   std::vector<Test::Result> results;
20✔
577

578
   for(const auto& param : keygen_params()) {
117✔
579
      const auto algo = algo_name(param);
97✔
580
      const std::string report_name = Botan::fmt("{}{}", algo, (param.empty() ? param : " " + param));
101✔
581

582
      Test::Result result(report_name + " keygen");
97✔
583

584
      const std::vector<std::string> providers = possible_providers(algo);
97✔
585

586
      if(providers.empty()) {
97✔
587
         result.note_missing("provider key generation " + algo);
×
588
      }
589

590
      result.start_timer();
97✔
591
      for(auto&& prov : providers) {
194✔
592
         auto key_p = Botan::create_private_key(algo, this->rng(), param, prov);
97✔
593

594
         if(key_p == nullptr) {
97✔
595
            result.test_failure("create_private_key returned null, should throw instead");
×
596
            continue;
×
597
         }
598

599
         const Botan::Private_Key& key = *key_p;
97✔
600

601
         try {
97✔
602
            result.confirm("Key passes self tests", key.check_key(this->rng(), true));
194✔
603
         } catch(Botan::Lookup_Error&) {}
×
604

605
         const std::string name = key.algo_name();
97✔
606
         result.confirm("Key has a non-empty name", !name.empty());
194✔
607

608
         if(auto oid = Botan::OID::from_name(name)) {
97✔
609
            result.test_success("Keys name maps to an OID");
65✔
610

611
            result.test_eq("Keys name OID is the same as the object oid",
130✔
612
                           oid.value().to_string(),
130✔
613
                           key.object_identifier().to_string());
130✔
614
         } else {
615
            const bool exception =
32✔
616
               name == "Kyber" || name == "ML-KEM" || name == "ML-DSA" || name == "FrodoKEM" || name == "SPHINCS+";
97✔
617

618
            if(!exception) {
×
619
               result.test_failure("Keys name " + name + " does not map to an OID");
×
620
            }
621
         }
×
622

623
         result.test_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
97✔
624
         result.test_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
97✔
625

626
         auto public_key = key.public_key();
97✔
627

628
         result.test_eq("public_key has same name", public_key->algo_name(), key.algo_name());
194✔
629

630
         result.test_eq(
291✔
631
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
194✔
632

633
         // Test generation of another key pair from a given (abstract) asymmetric key
634
         // KEX algorithms must support that (so that we can generate ephemeral keys in
635
         // an abstract fashion). For other algorithms it's a nice-to-have.
636
         try {
97✔
637
            auto sk2 = public_key->generate_another(this->rng());
97✔
638
            auto pk2 = sk2->public_key();
96✔
639

640
            result.test_eq("new private key has the same name", sk2->algo_name(), key.algo_name());
192✔
641
            result.test_eq("new public key has the same name", pk2->algo_name(), public_key->algo_name());
192✔
642
            result.test_eq(
96✔
643
               "new private key has the same est. strength", sk2->estimated_strength(), key.estimated_strength());
96✔
644
            result.test_eq("new public key has the same est. strength",
96✔
645
                           pk2->estimated_strength(),
96✔
646
                           public_key->estimated_strength());
96✔
647
            result.test_ne("new private keys are different keys", sk2->private_key_bits(), key.private_key_bits());
384✔
648
         } catch(const Botan::Not_Implemented&) {
193✔
649
            result.confirm("KEX algorithms are required to implement 'generate_another'",
2✔
650
                           !public_key->supports_operation(Botan::PublicKeyOperation::KeyAgreement));
1✔
651
         }
1✔
652

653
         // Test that the raw public key can be encoded. This is not supported
654
         // by all algorithms; we expect Not_Implemented for these.
655
         const std::vector<std::string> algos_that_dont_have_a_raw_encoding = {"RSA"};
97✔
656
         try {
97✔
657
            auto raw = public_key->raw_public_key_bits();
97✔
658
            result.test_ne("raw_public_key_bits is not empty", raw.size(), 0);
95✔
659

660
            if(public_key->supports_operation(Botan::PublicKeyOperation::KeyAgreement)) {
95✔
661
               // For KEX algorithms, raw_public_key_bits must be equal to the canonical
662
               // public value obtained by PK_Key_Agreement_Key::public_value().
663
               const auto* ka_key = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&key);
10✔
664
               result.require("is a key agreement private key", ka_key != nullptr);
10✔
665
               result.test_eq("public_key_bits has same encoding", raw, ka_key->public_value());
30✔
666
            }
667

668
            if(auto raw_pk = public_key_from_raw(param, prov, raw)) {
95✔
669
               result.test_eq("public_key has same type", raw_pk->algo_name(), public_key->algo_name());
190✔
670
               result.test_eq("public_key has same encoding", raw_pk->public_key_bits(), public_key->public_key_bits());
380✔
671
            }
95✔
672
         } catch(const Botan::Not_Implemented&) {
97✔
673
            if(!Botan::value_exists(algos_that_dont_have_a_raw_encoding, public_key->algo_name())) {
2✔
674
               result.test_failure("raw_public_key_bits not implemented for " + public_key->algo_name());
×
675
            } else {
676
               result.test_note("raw_public_key_bits threw Not_Implemented as expected for " + public_key->algo_name());
6✔
677
            }
678
         }
2✔
679

680
         // Test PEM public key round trips OK
681
         try {
97✔
682
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(key));
97✔
683
            auto loaded = Botan::X509::load_key(data_src);
97✔
684

685
            result.confirm("recovered public key from private", loaded != nullptr);
194✔
686
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
194✔
687

688
            try {
97✔
689
               result.test_eq("public key passes checks", loaded->check_key(this->rng(), false), true);
194✔
690
            } catch(Botan::Lookup_Error&) {}
×
691
         } catch(std::exception& e) {
194✔
692
            result.test_failure("roundtrip PEM public key", e.what());
×
693
         }
×
694

695
         // Test DER public key round trips OK
696
         try {
97✔
697
            const auto ber = key.subject_public_key();
97✔
698
            Botan::DataSource_Memory data_src(ber);
97✔
699
            auto loaded = Botan::X509::load_key(data_src);
97✔
700

701
            result.confirm("recovered public key from private", loaded != nullptr);
194✔
702
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
194✔
703
            result.test_eq("public key has same encoding", loaded->subject_public_key(), ber);
291✔
704
         } catch(std::exception& e) {
291✔
705
            result.test_failure("roundtrip BER public key", e.what());
×
706
         }
×
707

708
         // Test PEM private key round trips OK
709
         try {
97✔
710
            const auto ber = key.private_key_info();
97✔
711
            Botan::DataSource_Memory data_src(ber);
97✔
712
            auto loaded = Botan::PKCS8::load_key(data_src);
97✔
713

714
            result.confirm("recovered private key from PEM blob", loaded != nullptr);
194✔
715
            result.test_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
194✔
716
            result.test_eq("reloaded key has same encoding", loaded->private_key_info(), ber);
291✔
717
         } catch(std::exception& e) {
291✔
718
            result.test_failure("roundtrip PEM private key", e.what());
×
719
         }
×
720

721
         try {
97✔
722
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
97✔
723
            auto loaded = Botan::PKCS8::load_key(data_src);
97✔
724

725
            result.confirm("recovered public key from private", loaded != nullptr);
194✔
726
            result.test_eq("public key has same type", loaded->algo_name(), key.algo_name());
194✔
727
         } catch(std::exception& e) {
194✔
728
            result.test_failure("roundtrip BER private key", e.what());
×
729
         }
×
730

731
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
732

733
         test_pbe_roundtrip(result, key, "PBE-PKCS5v20(AES-128/CBC,SHA-256)", this->rng());
97✔
734
   #endif
735

736
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
737

738
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", this->rng());
97✔
739
   #endif
740
      }
291✔
741

742
      result.end_timer();
97✔
743

744
      results.push_back(result);
97✔
745
   }
117✔
746

747
   return results;
20✔
748
}
×
749

750
Test::Result PK_Key_Validity_Test::run_one_test(const std::string& header, const VarMap& vars) {
9✔
751
   Test::Result result(algo_name() + " key validity");
18✔
752

753
   if(header != "Valid" && header != "Invalid") {
9✔
754
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
755
   }
756

757
   const bool expected_valid = (header == "Valid");
9✔
758
   auto pubkey = load_public_key(vars);
9✔
759

760
   const bool tested_valid = pubkey->check_key(this->rng(), true);
9✔
761

762
   result.test_eq("Expected validation result", expected_valid, tested_valid);
9✔
763

764
   return result;
9✔
765
}
9✔
766

767
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
768
                                                                   const std::string& test_src) :
2✔
769
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
4✔
770

771
Test::Result PK_Key_Generation_Stability_Test::run_one_test(const std::string&, const VarMap& vars) {
3✔
772
   const std::string key_param = vars.get_opt_str("KeyParams", "");
6✔
773
   const std::string rng_algo = vars.get_req_str("Rng");
3✔
774
   const std::string rng_params = vars.get_opt_str("RngParams", "");
6✔
775
   const std::vector<uint8_t> rng_seed = vars.get_req_bin("RngSeed");
3✔
776
   const std::vector<uint8_t> expected_key = vars.get_req_bin("Key");
3✔
777

778
   std::ostringstream report_name;
3✔
779

780
   report_name << algo_name();
6✔
781
   if(!key_param.empty()) {
3✔
782
      report_name << " " << key_param;
3✔
783
   }
784
   report_name << " keygen stability";
3✔
785

786
   Test::Result result(report_name.str());
3✔
787

788
   result.start_timer();
3✔
789

790
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
3✔
791

792
   #if defined(BOTAN_HAS_HMAC_DRBG)
793
   if(rng_algo == "HMAC_DRBG") {
3✔
794
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
1✔
795
   }
796
   #endif
797

798
   if(rng_algo == "Fixed") {
3✔
799
      if(!rng_params.empty()) {
2✔
800
         throw Test_Error("Expected empty RngParams for Fixed RNG");
×
801
      }
802
      rng = std::make_unique<Fixed_Output_RNG>();
4✔
803
   }
804

805
   if(rng) {
3✔
806
      rng->add_entropy(rng_seed.data(), rng_seed.size());
3✔
807

808
      try {
3✔
809
         auto key = Botan::create_private_key(algo_name(), *rng, key_param);
6✔
810
         const auto key_bits = key->private_key_info();
3✔
811
         result.test_eq("Generated key matched expected value", key_bits, expected_key);
6✔
812
      } catch(Botan::Exception& e) {
6✔
813
         result.test_note("failed to create key", e.what());
×
814
      }
×
815
   } else {
816
      result.test_note("Skipping test due to unavailable RNG");
×
817
   }
818

819
   result.end_timer();
3✔
820

821
   return result;
3✔
822
}
9✔
823

824
/**
825
 * @brief Some general tests for minimal API sanity for signing/verification.
826
 */
827
class PK_API_Sign_Test : public Text_Based_Test {
828
   public:
829
      PK_API_Sign_Test() : Text_Based_Test("pubkey/api_sign.vec", "AlgoParams,SigParams", "Provider") {}
2✔
830

831
   protected:
832
      Test::Result run_one_test(const std::string& algorithm, const VarMap& vars) final {
14✔
833
         const std::string algo_params = vars.get_req_str("AlgoParams");
14✔
834
         const std::string sig_params = vars.get_req_str("SigParams");
14✔
835
         const std::string verify_params = vars.get_opt_str("VerifyParams", sig_params);
14✔
836
         const std::string provider = vars.get_opt_str("Provider", "base");
28✔
837

838
         std::ostringstream test_name;
14✔
839
         test_name << "Sign/verify API tests " << algorithm;
14✔
840
         if(!algo_params.empty()) {
14✔
841
            test_name << '(' << algo_params << ')';
12✔
842
         }
843
         if(!sig_params.empty()) {
14✔
844
            test_name << '/' << sig_params;
11✔
845
         }
846
         Test::Result result(test_name.str());
14✔
847

848
         auto privkey = Botan::create_private_key(algorithm, this->rng(), algo_params, provider);
14✔
849
         if(!privkey) {
14✔
850
            result.test_note(Botan::fmt(
×
851
               "Skipping Sign/verify API tests for {}({}) with provider {}", algorithm, algo_params, provider));
852
            return result;
×
853
         }
854
         auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey));
14✔
855
         result.confirm("Storing and loading public key works", pubkey != nullptr);
28✔
856

857
         result.confirm("private key claims to support signatures",
28✔
858
                        privkey->supports_operation(Botan::PublicKeyOperation::Signature));
14✔
859
         result.confirm("public key claims to support signatures",
28✔
860
                        pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
14✔
861
         result.test_gt("Public key length must be greater than 0", privkey->key_length(), 0);
14✔
862
         if(privkey->stateful_operation()) {
14✔
863
            result.confirm("A stateful key reports the number of remaining operations",
4✔
864
                           privkey->remaining_operations().has_value());
4✔
865
         } else {
866
            result.confirm("A stateless key has an unlimited number of remaining operations",
24✔
867
                           !privkey->remaining_operations().has_value());
24✔
868
         }
869

870
         auto signer = std::make_unique<Botan::PK_Signer>(
14✔
871
            *privkey, this->rng(), sig_params, Botan::Signature_Format::Standard, provider);
14✔
872
         auto verifier =
14✔
873
            std::make_unique<Botan::PK_Verifier>(*pubkey, verify_params, Botan::Signature_Format::Standard, provider);
14✔
874
         result.confirm("Creating PK_Signer works", signer != nullptr);
28✔
875
         result.confirm("Creating PK_Signer works", verifier != nullptr);
28✔
876

877
         result.test_is_nonempty("PK_Signer should report some hash", signer->hash_function());
28✔
878
         result.test_is_nonempty("PK_Verifier should report some hash", verifier->hash_function());
28✔
879

880
         result.test_eq(
42✔
881
            "PK_Signer and PK_Verifier report the same hash", signer->hash_function(), verifier->hash_function());
28✔
882

883
         pubkey.reset();
14✔
884
         privkey.reset();
14✔
885
         const std::array<uint8_t, 4> msg{0xde, 0xad, 0xbe, 0xef};
14✔
886
         const auto sig = signer->sign_message(msg, this->rng());
14✔
887
         result.test_gt("Signer should still work if no one else hold a reference to the key", sig.size(), 0);
14✔
888
         result.test_eq("Verifier should still work if no one else hold a reference to the key",
28✔
889
                        verifier->verify_message(msg, sig),
14✔
890
                        true);
891

892
         return result;
14✔
893
      }
28✔
894
};
895

896
BOTAN_REGISTER_TEST("pubkey", "pk_api_sign", PK_API_Sign_Test);
897

898
}  // namespace Botan_Tests
899

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