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

randombit / botan / 28994301380

09 Jul 2026 12:46AM UTC coverage: 89.396% (+1.7%) from 87.739%
28994301380

push

github

web-flow
Merge pull request #5712 from randombit/jack/cdp-aia-extn

Improve CDP, AIA and IDP extension decoding and handling

113303 of 126743 relevant lines covered (89.4%)

10806632.55 hits per line

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

86.41
/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 "tests.h"
9

10
#if defined(BOTAN_HAS_PUBLIC_KEY_CRYPTO)
11

12
   #include "test_pubkey.h"
13
   #include "test_rng.h"
14

15
   #include <botan/data_src.h>
16
   #include <botan/exceptn.h>
17
   #include <botan/hex.h>
18
   #include <botan/pk_algs.h>
19
   #include <botan/pkcs8.h>
20
   #include <botan/pubkey.h>
21
   #include <botan/x509_key.h>
22
   #include <botan/internal/fmt.h>
23
   #include <botan/internal/stl_util.h>
24

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

29
   #include <array>
30

31
namespace Botan_Tests {
32

33
namespace {
34

35
std::vector<std::vector<uint8_t>> generate_specific_false_signatures(const std::span<const uint8_t> correct_signature) {
2,310✔
36
   std::vector<std::vector<uint8_t>> result;
2,310✔
37
   result.push_back(std::vector<uint8_t>());
2,310✔
38
   result.push_back(std::vector<uint8_t>(1));
4,620✔
39
   result.push_back(std::vector<uint8_t>(2));
4,620✔
40
   if(correct_signature.size() > 1) {
2,310✔
41
      result.push_back(std::vector<uint8_t>(correct_signature.size() - 1));
4,620✔
42
      std::vector<uint8_t> flip_start(correct_signature.begin(), correct_signature.end());
2,310✔
43
      flip_start[0] ^= 1;
2,310✔
44
      result.push_back(flip_start);
2,310✔
45
   }
2,310✔
46

47
   return result;
2,310✔
48
}
×
49

50
void check_invalid_signatures(Test::Result& result,
2,310✔
51
                              Botan::PK_Verifier& verifier,
52
                              const std::vector<uint8_t>& message,
53
                              const std::vector<uint8_t>& signature,
54
                              Botan::RandomNumberGenerator& rng) {
55
   const size_t tests_to_run = (Test::run_long_tests() ? 24 : 9);
2,310✔
56

57
   const std::vector<uint8_t> zero_sig(signature.size());
2,310✔
58
   result.test_is_false("all zero signature invalid", verifier.verify_message(message, zero_sig));
2,310✔
59

60
   auto specific_false_sigs = generate_specific_false_signatures(signature);
2,310✔
61

62
   for(size_t i = 0; i < tests_to_run; ++i) {
57,750✔
63
      std::vector<uint8_t> bad_sig;
55,440✔
64
      if(i < specific_false_sigs.size()) {
55,440✔
65
         bad_sig = specific_false_sigs[i];
11,550✔
66
      } else {
67
         bad_sig = Test::mutate_vec(signature, rng);
43,890✔
68
      }
69

70
      try {
55,440✔
71
         if(!result.test_is_false("incorrect signature invalid", verifier.verify_message(message, bad_sig))) {
55,440✔
72
            result.test_note("Accepted invalid signature", bad_sig);
×
73
         }
74
      } catch(std::exception& e) {
×
75
         result.test_note("Modified signature", bad_sig);
×
76
         result.test_failure("Modified signature rejected with exception", e.what());
×
77
      }
×
78
      if(!result.test_is_true("correct signature valid after failed verification",
110,880✔
79
                              verifier.verify_message(message, signature))) {
55,440✔
80
         result.test_note("rejected valid signature after this invalid signature", bad_sig);
×
81
      }
82
   }
55,440✔
83
}
2,310✔
84

85
}  // namespace
86

87
// Exposed for DLIES tests
88
void check_invalid_ciphertexts(Test::Result& result,
317✔
89
                               Botan::PK_Decryptor& decryptor,
90
                               const std::vector<uint8_t>& plaintext,
91
                               const std::vector<uint8_t>& ciphertext,
92
                               Botan::RandomNumberGenerator& rng) {
93
   const size_t tests_to_run = (Test::run_long_tests() ? 20 : 5);
317✔
94

95
   size_t ciphertext_accepted = 0;
317✔
96
   size_t ciphertext_rejected = 0;
317✔
97

98
   for(size_t i = 0; i < tests_to_run; ++i) {
6,657✔
99
      const std::vector<uint8_t> bad_ctext = Test::mutate_vec(ciphertext, rng);
6,340✔
100

101
      try {
6,340✔
102
         const Botan::secure_vector<uint8_t> decrypted = decryptor.decrypt(bad_ctext);
6,340✔
103
         ++ciphertext_accepted;
3,174✔
104

105
         if(!result.test_bin_ne("incorrect ciphertext different", decrypted, plaintext)) {
3,174✔
106
            result.test_bin_eq("used corrupted ciphertext", bad_ctext, ciphertext);
×
107
         }
108
      } catch(std::exception&) {
6,340✔
109
         ++ciphertext_rejected;
3,166✔
110
      }
3,166✔
111
   }
6,340✔
112

113
   result.test_note(
317✔
114
      Botan::fmt("Accepted {} invalid ciphertexts, rejected {}", ciphertext_accepted, ciphertext_rejected));
317✔
115

116
   result.test_bin_eq("After decrypting corrupted messages, PK_Decryptor can decrypt original message",
317✔
117
                      decryptor.decrypt(ciphertext),
317✔
118
                      plaintext);
119
}
317✔
120

121
std::string PK_Test::choose_padding(const VarMap& vars, const std::string& pad_hdr) {
15,304✔
122
   if(!pad_hdr.empty()) {
15,304✔
123
      return pad_hdr;
1,840✔
124
   }
125
   return vars.get_opt_str("Padding", this->default_padding(vars));
13,464✔
126
}
127

128
std::vector<std::string> PK_Test::possible_providers(const std::string& /*params*/) {
17,544✔
129
   return Test::provider_filter({"base", "commoncrypto", "openssl", "tpm"});
17,544✔
130
}
131

132
std::unique_ptr<Botan::RandomNumberGenerator> PK_Signature_Generation_Test::test_rng(
201✔
133
   const std::vector<uint8_t>& nonce) const {
134
   return std::make_unique<Fixed_Output_RNG>(nonce);
201✔
135
}
136

137
Test::Result PK_Signature_Generation_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
1,257✔
138
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
1,257✔
139
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
1,257✔
140
   const std::string padding = choose_padding(vars, pad_hdr);
1,257✔
141

142
   std::ostringstream test_name;
1,257✔
143
   test_name << algo_name();
2,514✔
144
   if(vars.has_key("Group")) {
1,257✔
145
      test_name << "-" << vars.get_req_str("Group");
254✔
146
   }
147
   test_name << "/" << padding << " signature generation";
1,257✔
148

149
   Test::Result result(test_name.str());
1,257✔
150

151
   std::unique_ptr<Botan::Private_Key> privkey;
1,257✔
152
   try {
1,257✔
153
      privkey = load_private_key(vars);
1,257✔
154
   } catch(Botan::Lookup_Error& e) {
×
155
      result.note_missing(e.what());
×
156
      return result;
×
157
   }
×
158

159
   result.test_is_true("private key claims to support signatures",
1,257✔
160
                       privkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,257✔
161

162
   auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey->public_key()));
1,257✔
163

164
   result.test_is_true("public key claims to support signatures",
1,257✔
165
                       pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
1,257✔
166

167
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,257✔
168

169
   for(const auto& verify_provider : possible_providers(algo_name())) {
7,542✔
170
      std::unique_ptr<Botan::PK_Verifier> verifier;
5,028✔
171

172
      try {
5,028✔
173
         verifier =
5,028✔
174
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::Standard, verify_provider);
6,285✔
175
      } catch(Botan::Lookup_Error&) {
3,771✔
176
         //result.test_note("Skipping verifying", verify_provider);
177
         continue;
3,771✔
178
      }
3,771✔
179

180
      result.test_is_true("KAT signature valid", verifier->verify_message(message, signature));
1,257✔
181

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

184
      result.test_is_true("KAT signature valid (try 2)", verifier->verify_message(message, signature));
1,257✔
185

186
      verifiers.push_back(std::move(verifier));
1,257✔
187
   }
6,285✔
188

189
   for(const auto& sign_provider : possible_providers(algo_name())) {
7,542✔
190
      std::unique_ptr<Botan::PK_Signer> signer;
5,028✔
191

192
      std::vector<uint8_t> generated_signature;
5,028✔
193

194
      try {
5,028✔
195
         signer = std::make_unique<Botan::PK_Signer>(
5,028✔
196
            *privkey, this->rng(), padding, Botan::Signature_Format::Standard, sign_provider);
6,285✔
197

198
         if(vars.has_key("Nonce")) {
1,257✔
199
            auto rng = test_rng(vars.get_req_bin("Nonce"));
232✔
200
            generated_signature = signer->sign_message(message, *rng);
464✔
201
         } else {
232✔
202
            generated_signature = signer->sign_message(message, this->rng());
2,050✔
203
         }
204

205
         result.test_sz_lte(
1,257✔
206
            "Generated signature within announced bound", generated_signature.size(), signer->signature_length());
207
      } catch(Botan::Lookup_Error&) {
3,771✔
208
         //result.test_note("Skipping signing", sign_provider);
209
         continue;
3,771✔
210
      }
3,771✔
211

212
      if(sign_provider == "base") {
1,257✔
213
         result.test_bin_eq("generated signature matches KAT", generated_signature, signature);
1,257✔
214
      } else if(generated_signature != signature) {
×
215
         for(std::unique_ptr<Botan::PK_Verifier>& verifier : verifiers) {
×
216
            if(!result.test_is_true("generated signature valid",
×
217
                                    verifier->verify_message(message, generated_signature))) {
×
218
               result.test_failure("generated signature", generated_signature);
×
219
            }
220
         }
221
      }
222
   }
6,285✔
223

224
   return result;
1,257✔
225
}
3,771✔
226

227
Botan::Signature_Format PK_Signature_Verification_Test::sig_format() const {
4,944✔
228
   return Botan::Signature_Format::Standard;
4,944✔
229
}
230

231
Test::Result PK_Signature_Verification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
13,100✔
232
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
13,100✔
233
   const std::vector<uint8_t> signature = vars.get_req_bin("Signature");
13,100✔
234
   const std::string padding = choose_padding(vars, pad_hdr);
13,100✔
235

236
   const bool expected_valid = (vars.get_opt_sz("Valid", 1) == 1);
13,100✔
237

238
   auto pubkey = load_public_key(vars);
13,100✔
239

240
   std::ostringstream result_name;
13,100✔
241
   result_name << algo_name();
26,200✔
242
   if(vars.has_key("Group")) {
13,100✔
243
      result_name << "-" << vars.get_req_str("Group");
24,292✔
244
   }
245
   if(!padding.empty()) {
13,100✔
246
      result_name << "/" << padding;
13,093✔
247
   }
248
   result_name << " signature verification";
13,100✔
249
   Test::Result result(result_name.str());
13,100✔
250

251
   result.test_is_true("public key claims to support signatures",
13,100✔
252
                       pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
13,100✔
253

254
   for(const auto& verify_provider : possible_providers(algo_name())) {
78,600✔
255
      std::unique_ptr<Botan::PK_Verifier> verifier;
52,400✔
256

257
      try {
52,400✔
258
         verifier = std::make_unique<Botan::PK_Verifier>(*pubkey, padding, sig_format(), verify_provider);
61,250✔
259
      } catch(Botan::Lookup_Error&) {
43,550✔
260
         //result.test_note("Skipping verifying", verify_provider);
261
      }
43,550✔
262

263
      if(verifier) {
52,400✔
264
         try {
8,850✔
265
            const bool verified = verifier->verify_message(message, signature);
8,850✔
266

267
            if(expected_valid) {
8,850✔
268
               result.test_is_true("correct signature valid with " + verify_provider, verified);
4,526✔
269

270
               if(test_random_invalid_sigs()) {
4,526✔
271
                  check_invalid_signatures(result, *verifier, message, signature, this->rng());
1,051✔
272
               }
273
            } else {
274
               result.test_is_true("incorrect signature is rejected", verified == false);
4,324✔
275
            }
276
         } catch(std::exception& e) {
×
277
            result.test_failure("verification threw exception", e.what());
×
278
         }
×
279
      }
280
   }
65,500✔
281

282
   return result;
13,100✔
283
}
26,200✔
284

285
Test::Result PK_Signature_NonVerification_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
706✔
286
   const std::string padding = choose_padding(vars, pad_hdr);
706✔
287
   const std::vector<uint8_t> message = vars.get_req_bin("Msg");
706✔
288
   auto pubkey = load_public_key(vars);
706✔
289

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

292
   Test::Result result(algo_name() + "/" + padding + " verify invalid signature");
4,236✔
293

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

297
      try {
2,824✔
298
         verifier =
2,824✔
299
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::Standard, verify_provider);
3,530✔
300
         result.test_is_false("incorrect signature rejected", verifier->verify_message(message, invalid_signature));
706✔
301
      } catch(Botan::Lookup_Error&) {
2,118✔
302
         result.test_note("Skipping verifying", verify_provider);
2,118✔
303
      }
2,118✔
304
   }
3,530✔
305

306
   return result;
1,412✔
307
}
1,412✔
308

309
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
310
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
311
   const std::string padding = m_padding;
1✔
312

313
   auto privkey = key();
1✔
314
   if(!privkey) {
1✔
315
      return {};
×
316
   }
317
   auto pubkey = privkey->public_key();
1✔
318

319
   Test::Result result(algo_name() + "/" + padding + " signature sign/verify using DER format");
6✔
320

321
   for(const auto& provider : possible_providers(algo_name())) {
3✔
322
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
323
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
324

325
      try {
1✔
326
         signer = std::make_unique<Botan::PK_Signer>(
1✔
327
            *privkey, this->rng(), padding, Botan::Signature_Format::DerSequence, provider);
2✔
328
         verifier =
1✔
329
            std::make_unique<Botan::PK_Verifier>(*pubkey, padding, Botan::Signature_Format::DerSequence, provider);
2✔
330
      } catch(Botan::Lookup_Error& e) {
×
331
         result.test_note("Skipping sign/verify with " + provider, e.what());
×
332
      }
×
333

334
      if(signer && verifier) {
1✔
335
         try {
1✔
336
            std::vector<uint8_t> generated_signature = signer->sign_message(message, this->rng());
1✔
337
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
338

339
            result.test_is_true("correct signature valid with " + provider, verified);
1✔
340

341
            if(test_random_invalid_sigs()) {
1✔
342
               check_invalid_signatures(result, *verifier, message, generated_signature, this->rng());
1✔
343
            }
344
         } catch(std::exception& e) {
1✔
345
            result.test_failure("verification threw exception", e.what());
×
346
         }
×
347
      }
348
   }
2✔
349

350
   // Below follows a regression test for a bug introduced in #4592 that caused
351
   // an assertion in PK_Signer when setting the output format explicitly using
352
   // signer.set_output_format(Signature_Format::DerSequence)
353
   try {
1✔
354
      auto signer = Botan::PK_Signer(*privkey, this->rng(), padding /*, not setting DerSequence here */);
1✔
355
      auto verifier = Botan::PK_Verifier(*pubkey, padding /*, not setting DerSequence here */);
1✔
356

357
      // Setting the in/out formats explicitly, to ensure that PK_Signer/Verifier
358
      // handle their internal state properly and not run into an assertion.
359
      signer.set_output_format(Botan::Signature_Format::DerSequence);
1✔
360
      verifier.set_input_format(Botan::Signature_Format::DerSequence);
1✔
361

362
      const auto sig = signer.sign_message(message, this->rng());
1✔
363
      const auto verified = verifier.verify_message(message, sig);
1✔
364

365
      result.test_is_true("signature checks out", verified);
1✔
366
      if(test_random_invalid_sigs()) {
1✔
367
         check_invalid_signatures(result, verifier, message, sig, this->rng());
1✔
368
      }
369
   } catch(const Botan::Lookup_Error&) {
1✔
370
      result.test_note("Skipping sign/verify regression test");
×
371
   } catch(const std::exception& e) {
×
372
      result.test_failure("regression test verification failed", e.what());
×
373
   }
×
374

375
   return {result};
2✔
376
}
4✔
377

378
std::vector<std::string> PK_Sign_Verify_DER_Test::possible_providers(const std::string& algo_name) {
1✔
379
   const std::vector<std::string> pk_provider =
1✔
380
      Botan::probe_provider_private_key(algo_name, {"base", "commoncrypto", "openssl", "tpm"});
1✔
381
   return Test::provider_filter(pk_provider);
2✔
382
}
1✔
383

384
std::unique_ptr<Botan::RandomNumberGenerator> PK_Encryption_Decryption_Test::test_rng(
54✔
385
   const std::vector<uint8_t>& nonce) const {
386
   return std::make_unique<Fixed_Output_RNG>(nonce);
54✔
387
}
388

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

394
   Test::Result result(algo_name() + (padding.empty() ? padding : "/" + padding) + " encryption");
995✔
395

396
   auto privkey = load_private_key(vars);
199✔
397

398
   result.test_is_true("private key claims to support encryption",
199✔
399
                       privkey->supports_operation(Botan::PublicKeyOperation::Encryption));
199✔
400

401
   auto pubkey = privkey->public_key();
199✔
402

403
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
199✔
404

405
   for(const auto& dec_provider : possible_providers(algo_name())) {
1,194✔
406
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
796✔
407

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

414
      Botan::secure_vector<uint8_t> decrypted;
199✔
415
      try {
199✔
416
         decrypted = decryptor->decrypt(ciphertext);
199✔
417

418
         result.test_sz_lte(
199✔
419
            "Plaintext within length", decrypted.size(), decryptor->plaintext_length(ciphertext.size()));
199✔
420
         result.test_sz_lte(
199✔
421
            "Ciphertext within length", ciphertext.size(), decryptor->ciphertext_length(plaintext.size()));
199✔
422
      } catch(Botan::Exception& e) {
×
423
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
424
      }
×
425

426
      result.test_bin_eq(dec_provider + " decryption of KAT", decrypted, plaintext);
199✔
427
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
199✔
428
      decryptors.push_back(std::move(decryptor));
199✔
429
   }
995✔
430

431
   for(const auto& enc_provider : possible_providers(algo_name())) {
1,194✔
432
      std::unique_ptr<Botan::PK_Encryptor> encryptor;
796✔
433

434
      try {
796✔
435
         encryptor = std::make_unique<Botan::PK_Encryptor_EME>(*pubkey, this->rng(), padding, enc_provider);
796✔
436
      } catch(Botan::Lookup_Error&) {
597✔
437
         continue;
597✔
438
      }
597✔
439

440
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
199✔
441
      if(vars.has_key("Nonce")) {
199✔
442
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
59✔
443
      }
444

445
      if(padding == "Raw") {
199✔
446
         /*
447
         Hack for RSA with no padding since sometimes one more bit will fit in but maximum_input_size
448
         rounds down to nearest byte
449
         */
450
         result.test_sz_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size() + 1);
146✔
451
      } else {
452
         result.test_sz_lte("Input within accepted bounds", plaintext.size(), encryptor->maximum_input_size());
53✔
453
      }
454

455
      const std::vector<uint8_t> generated_ciphertext = encryptor->encrypt(plaintext, kat_rng ? *kat_rng : this->rng());
199✔
456

457
      result.test_sz_lte(
199✔
458
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
199✔
459

460
      if(enc_provider == "base") {
199✔
461
         result.test_bin_eq(enc_provider + " generated ciphertext matches KAT", generated_ciphertext, ciphertext);
398✔
462
      } else if(generated_ciphertext != ciphertext) {
×
463
         for(std::unique_ptr<Botan::PK_Decryptor>& dec : decryptors) {
×
464
            result.test_bin_eq("decryption of generated ciphertext", dec->decrypt(generated_ciphertext), plaintext);
×
465
         }
466
      }
467
   }
1,054✔
468

469
   return result;
199✔
470
}
597✔
471

472
Test::Result PK_Decryption_Test::run_one_test(const std::string& pad_hdr, const VarMap& vars) {
42✔
473
   const std::vector<uint8_t> plaintext = vars.get_req_bin("Msg");
42✔
474
   const std::vector<uint8_t> ciphertext = vars.get_req_bin("Ciphertext");
42✔
475
   const std::string padding = choose_padding(vars, pad_hdr);
42✔
476

477
   Test::Result result(algo_name() + (padding.empty() ? padding : "/" + padding) + " decryption");
210✔
478

479
   auto privkey = load_private_key(vars);
42✔
480

481
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
482
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
483

484
      try {
168✔
485
         decryptor = std::make_unique<Botan::PK_Decryptor_EME>(*privkey, this->rng(), padding, dec_provider);
168✔
486
      } catch(Botan::Lookup_Error&) {
126✔
487
         continue;
126✔
488
      }
126✔
489

490
      Botan::secure_vector<uint8_t> decrypted;
42✔
491
      try {
42✔
492
         decrypted = decryptor->decrypt(ciphertext);
42✔
493
         result.test_sz_lte(
42✔
494
            "Ciphertext within length", ciphertext.size(), decryptor->ciphertext_length(plaintext.size()));
42✔
495
      } catch(Botan::Exception& e) {
×
496
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
497
      }
×
498

499
      result.test_bin_eq(dec_provider + " decryption of KAT", decrypted, plaintext);
42✔
500
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
42✔
501
   }
210✔
502

503
   return result;
84✔
504
}
42✔
505

506
Test::Result PK_KEM_Test::run_one_test(const std::string& /*header*/, const VarMap& vars) {
10✔
507
   const std::vector<uint8_t> K = vars.get_req_bin("K");
10✔
508
   const std::vector<uint8_t> C0 = vars.get_req_bin("C0");
10✔
509
   const std::vector<uint8_t> salt = vars.get_opt_bin("Salt");
10✔
510
   const std::string kdf = vars.get_req_str("KDF");
10✔
511

512
   Test::Result result(algo_name() + "/" + kdf + " KEM");
60✔
513

514
   auto privkey = load_private_key(vars);
10✔
515

516
   result.test_is_true("private key claims to support KEM",
10✔
517
                       privkey->supports_operation(Botan::PublicKeyOperation::KeyEncapsulation));
10✔
518

519
   auto pubkey = privkey->public_key();
10✔
520

521
   const size_t desired_key_len = K.size();
10✔
522

523
   std::unique_ptr<Botan::PK_KEM_Encryptor> enc;
10✔
524
   try {
10✔
525
      enc = std::make_unique<Botan::PK_KEM_Encryptor>(*pubkey, kdf);
20✔
526
   } catch(Botan::Lookup_Error&) {
×
527
      result.test_note("Skipping due to missing KDF", kdf);
×
528
      return result;
×
529
   }
×
530

531
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
10✔
532

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

535
   result.test_sz_eq("encapsulated key length matches expected",
10✔
536
                     kem_result.encapsulated_shared_key().size(),
10✔
537
                     enc->encapsulated_key_length());
538

539
   result.test_sz_eq(
10✔
540
      "shared key length matches expected", kem_result.shared_key().size(), enc->shared_key_length(desired_key_len));
10✔
541

542
   result.test_bin_eq("C0 matches", kem_result.encapsulated_shared_key(), C0);
10✔
543
   result.test_bin_eq("K matches", kem_result.shared_key(), K);
10✔
544

545
   std::unique_ptr<Botan::PK_KEM_Decryptor> dec;
10✔
546
   try {
10✔
547
      dec = std::make_unique<Botan::PK_KEM_Decryptor>(*privkey, this->rng(), kdf);
20✔
548
   } catch(Botan::Lookup_Error& e) {
×
549
      result.test_note("Skipping test", e.what());
×
550
      return result;
×
551
   }
×
552

553
   result.test_sz_eq("encapsulated key length matches expected",
10✔
554
                     kem_result.encapsulated_shared_key().size(),
10✔
555
                     dec->encapsulated_key_length());
556

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

560
   result.test_sz_eq(
10✔
561
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
562

563
   result.test_bin_eq("decrypted K matches", decr_shared_key, K);
10✔
564

565
   return result;
10✔
566
}
30✔
567

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

572
   Test::Result result(algo_name() + "/" + kdf + (header.empty() ? header : " " + header) + " key agreement");
6,076✔
573

574
   auto privkey = load_our_key(header, vars);
784✔
575

576
   result.test_is_true("private key claims to support key agreement",
784✔
577
                       privkey->supports_operation(Botan::PublicKeyOperation::KeyAgreement));
784✔
578

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

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

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

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

589
         if(agreement_should_fail(header, vars)) {
784✔
590
            result.test_throws("key agreement fails", [&] { kas->derive_key(key_len, pubkey); });
50✔
591
         } else {
592
            auto derived_key = kas->derive_key(key_len, pubkey).bits_of();
1,518✔
593
            result.test_bin_eq(provider + " agreement", derived_key, shared);
759✔
594

595
            if(key_len == 0 && kdf == "Raw") {
759✔
596
               result.test_sz_eq("Expected size", derived_key.size(), kas->agreed_value_size());
755✔
597
            }
598
         }
759✔
599
      } catch(Botan::Lookup_Error&) {
2,352✔
600
         //result.test_note("Skipping key agreement", provider);
601
      }
2,352✔
602
   }
3,920✔
603

604
   return result;
784✔
605
}
1,568✔
606

607
std::vector<std::string> PK_Key_Generation_Test::possible_providers(const std::string& algo_name) {
111✔
608
   const std::vector<std::string> pk_provider =
111✔
609
      Botan::probe_provider_private_key(algo_name, {"base", "commoncrypto", "openssl", "tpm"});
111✔
610
   return Test::provider_filter(pk_provider);
222✔
611
}
111✔
612

613
namespace {
614

615
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && \
616
      (defined(BOTAN_HAS_SHA2_32) || defined(BOTAN_HAS_SCRYPT))
617
void test_pbe_roundtrip(Test::Result& result,
222✔
618
                        const Botan::Private_Key& key,
619
                        const std::string& pbe_algo,
620
                        Botan::RandomNumberGenerator& rng) {
621
   const auto pkcs8 = key.private_key_info();
222✔
622

623
   auto passphrase = Test::random_password(rng);
222✔
624

625
   try {
222✔
626
      Botan::DataSource_Memory data_src(
222✔
627
         Botan::PKCS8::PEM_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
222✔
628

629
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
222✔
630

631
      result.test_is_true("recovered private key from encrypted blob", loaded != nullptr);
222✔
632
      result.test_str_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
222✔
633
      result.test_bin_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
444✔
634
   } catch(std::exception& e) {
444✔
635
      result.test_failure("roundtrip encrypted PEM private key", e.what());
×
636
   }
×
637

638
   try {
222✔
639
      Botan::DataSource_Memory data_src(
222✔
640
         Botan::PKCS8::BER_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
444✔
641

642
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
222✔
643

644
      result.test_is_true("recovered private key from BER blob", loaded != nullptr);
222✔
645
      result.test_str_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
222✔
646
      result.test_bin_eq("reloaded key has same encoding", loaded->private_key_info(), pkcs8);
444✔
647
   } catch(std::exception& e) {
444✔
648
      result.test_failure("roundtrip encrypted BER private key", e.what());
×
649
   }
×
650
}
444✔
651
   #endif
652

653
}  // namespace
654

655
std::vector<Test::Result> PK_Key_Generation_Test::run() {
21✔
656
   std::vector<Test::Result> results;
21✔
657

658
   for(const auto& param : keygen_params()) {
132✔
659
      const auto algorithm_name = algo_name(param);
111✔
660
      const std::string report_name = Botan::fmt("{}{}", algorithm_name, (param.empty() ? param : " " + param));
115✔
661

662
      Test::Result result(report_name + " keygen");
111✔
663

664
      const std::vector<std::string> providers = possible_providers(algorithm_name);
111✔
665

666
      if(providers.empty()) {
111✔
667
         result.note_missing("provider key generation " + algorithm_name);
×
668
      }
669

670
      result.start_timer();
111✔
671
      for(auto&& prov : providers) {
222✔
672
         auto key_p = Botan::create_private_key(algorithm_name, this->rng(), param, prov);
111✔
673

674
         if(key_p == nullptr) {
111✔
675
            continue;
×
676
         }
677

678
         const Botan::Private_Key& key = *key_p;
111✔
679

680
         try {
111✔
681
            result.test_is_true("Key passes self tests", key.check_key(this->rng(), true));
111✔
682
         } catch(Botan::Lookup_Error&) {}
×
683

684
         const std::string name = key.algo_name();
111✔
685
         result.test_is_true("Key has a non-empty name", !name.empty());
111✔
686

687
         if(auto oid = Botan::OID::from_name(name)) {
111✔
688
            result.test_success("Keys name maps to an OID");
65✔
689

690
            result.test_str_eq("Keys name OID is the same as the object oid",
130✔
691
                               oid.value().to_string(),
130✔
692
                               key.object_identifier().to_string());
130✔
693
         } else {
694
            const bool exception = name == "Kyber" || name == "ML-KEM" || name == "ML-DSA" || name == "SLH-DSA" ||
40✔
695
                                   name == "FrodoKEM" || name == "SPHINCS+" || name == "ClassicMcEliece";
151✔
696
            if(!exception) {
×
697
               result.test_failure("Keys name " + name + " does not map to an OID");
×
698
            }
699
         }
×
700

701
         result.test_sz_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
111✔
702
         result.test_sz_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
111✔
703

704
         auto public_key = key.public_key();
111✔
705

706
         result.test_str_eq("public_key has same name", public_key->algo_name(), key.algo_name());
111✔
707

708
         result.test_str_eq(
222✔
709
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
222✔
710

711
         // Test generation of another key pair from a given (abstract) asymmetric key
712
         // KEX algorithms must support that (so that we can generate ephemeral keys in
713
         // an abstract fashion). For other algorithms it's a nice-to-have.
714
         try {
111✔
715
            auto sk2 = public_key->generate_another(this->rng());
111✔
716
            auto pk2 = sk2->public_key();
110✔
717

718
            result.test_str_eq("new private key has the same name", sk2->algo_name(), key.algo_name());
110✔
719
            result.test_str_eq("new public key has the same name", pk2->algo_name(), public_key->algo_name());
110✔
720
            result.test_sz_eq(
110✔
721
               "new private key has the same est. strength", sk2->estimated_strength(), key.estimated_strength());
110✔
722
            result.test_sz_eq("new public key has the same est. strength",
110✔
723
                              pk2->estimated_strength(),
110✔
724
                              public_key->estimated_strength());
110✔
725
            result.test_bin_ne("new private keys are different keys", sk2->private_key_bits(), key.private_key_bits());
330✔
726
         } catch(const Botan::Not_Implemented&) {
221✔
727
            result.test_is_true("KEX algorithms are required to implement 'generate_another'",
1✔
728
                                !public_key->supports_operation(Botan::PublicKeyOperation::KeyAgreement));
1✔
729
         }
1✔
730

731
         // Test that the raw public key can be encoded. This is not supported
732
         // by all algorithms; we expect Not_Implemented for these.
733
         const std::vector<std::string> algos_that_dont_have_a_raw_encoding = {"RSA"};
111✔
734
         try {
111✔
735
            auto raw = public_key->raw_public_key_bits();
111✔
736
            result.test_sz_ne("raw_public_key_bits is not empty", raw.size(), 0);
109✔
737

738
            if(public_key->supports_operation(Botan::PublicKeyOperation::KeyAgreement)) {
109✔
739
               // For KEX algorithms, raw_public_key_bits must be equal to the canonical
740
               // public value obtained by PK_Key_Agreement_Key::public_value().
741
               const auto* ka_key = dynamic_cast<const Botan::PK_Key_Agreement_Key*>(&key);
10✔
742
               result.require("is a key agreement private key", ka_key != nullptr);
10✔
743
               result.test_bin_eq("public_key_bits has same encoding", raw, ka_key->public_value());
10✔
744
            }
745

746
            if(auto raw_pk = public_key_from_raw(param, prov, raw)) {
109✔
747
               result.test_str_eq("public_key has same type", raw_pk->algo_name(), public_key->algo_name());
109✔
748
               result.test_bin_eq(
109✔
749
                  "public_key has same encoding", raw_pk->public_key_bits(), public_key->public_key_bits());
218✔
750
            }
×
751
         } catch(const Botan::Not_Implemented&) {
111✔
752
            if(!Botan::value_exists(algos_that_dont_have_a_raw_encoding, public_key->algo_name())) {
4✔
753
               result.test_failure("raw_public_key_bits not implemented for " + public_key->algo_name());
×
754
            } else {
755
               result.test_note("raw_public_key_bits threw Not_Implemented as expected", public_key->algo_name());
2✔
756
            }
757
         }
2✔
758

759
         // Test PEM public key round trips OK
760
         try {
111✔
761
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(*public_key));
111✔
762
            auto loaded = Botan::X509::load_key(data_src);
111✔
763

764
            result.test_is_true("recovered public key from private", loaded != nullptr);
111✔
765
            result.test_str_eq("public key has same type", loaded->algo_name(), key.algo_name());
111✔
766

767
            try {
111✔
768
               result.test_is_true("public key passes checks", loaded->check_key(this->rng(), false));
111✔
769
            } catch(Botan::Lookup_Error&) {}
×
770
         } catch(std::exception& e) {
222✔
771
            result.test_failure("roundtrip PEM public key", e.what());
×
772
         }
×
773

774
         // Test DER public key round trips OK
775
         try {
111✔
776
            const auto ber = public_key->subject_public_key();
111✔
777
            Botan::DataSource_Memory data_src(ber);
111✔
778
            auto loaded = Botan::X509::load_key(data_src);
111✔
779

780
            result.test_is_true("recovered public key from private", loaded != nullptr);
111✔
781
            result.test_str_eq("public key has same type", loaded->algo_name(), key.algo_name());
111✔
782
            result.test_bin_eq("public key has same encoding", loaded->subject_public_key(), ber);
111✔
783
         } catch(std::exception& e) {
222✔
784
            result.test_failure("roundtrip BER public key", e.what());
×
785
         }
×
786

787
         // Test PEM private key round trips OK
788
         try {
111✔
789
            const auto ber = key.private_key_info();
111✔
790
            Botan::DataSource_Memory data_src(ber);
111✔
791
            auto loaded = Botan::PKCS8::load_key(data_src);
111✔
792

793
            result.test_is_true("recovered private key from PEM blob", loaded != nullptr);
111✔
794
            result.test_str_eq("reloaded key has same type", loaded->algo_name(), key.algo_name());
111✔
795
            result.test_bin_eq("reloaded key has same encoding", loaded->private_key_info(), ber);
222✔
796
         } catch(std::exception& e) {
333✔
797
            result.test_failure("roundtrip PEM private key", e.what());
×
798
         }
×
799

800
         try {
111✔
801
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
111✔
802
            auto loaded = Botan::PKCS8::load_key(data_src);
111✔
803

804
            result.test_is_true("recovered public key from private", loaded != nullptr);
111✔
805
            result.test_str_eq("public key has same type", loaded->algo_name(), key.algo_name());
111✔
806
         } catch(std::exception& e) {
222✔
807
            result.test_failure("roundtrip BER private key", e.what());
×
808
         }
×
809

810
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
811

812
         test_pbe_roundtrip(result, key, "PBE-PKCS5v20(AES-128/CBC,SHA-256)", this->rng());
111✔
813
   #endif
814

815
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
816

817
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", this->rng());
111✔
818
   #endif
819
      }
333✔
820

821
      result.end_timer();
111✔
822

823
      results.push_back(result);
111✔
824
   }
132✔
825

826
   return results;
21✔
827
}
111✔
828

829
Test::Result PK_Key_Validity_Test::run_one_test(const std::string& header, const VarMap& vars) {
9✔
830
   Test::Result result(algo_name() + " key validity");
27✔
831

832
   if(header != "Valid" && header != "Invalid") {
9✔
833
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
834
   }
835

836
   const bool expected_valid = (header == "Valid");
9✔
837
   auto pubkey = load_public_key(vars);
9✔
838

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

841
   result.test_bool_eq("Expected validation result", tested_valid, expected_valid);
9✔
842

843
   return result;
9✔
844
}
9✔
845

846
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
847
                                                                   const std::string& test_src) :
2✔
848
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
4✔
849

850
Test::Result PK_Key_Generation_Stability_Test::run_one_test(const std::string& /*header*/, const VarMap& vars) {
4✔
851
   const std::string key_param = vars.get_opt_str("KeyParams", "");
4✔
852
   const std::string rng_algo = vars.get_req_str("Rng");
4✔
853
   const std::string rng_params = vars.get_opt_str("RngParams", "");
4✔
854
   const std::vector<uint8_t> rng_seed = vars.get_req_bin("RngSeed");
4✔
855
   const std::vector<uint8_t> expected_key = vars.get_req_bin("Key");
4✔
856

857
   std::ostringstream report_name;
4✔
858

859
   report_name << algo_name();
8✔
860
   if(!key_param.empty()) {
4✔
861
      report_name << " " << key_param;
4✔
862
   }
863
   report_name << " keygen stability";
4✔
864

865
   Test::Result result(report_name.str());
4✔
866

867
   result.start_timer();
4✔
868

869
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
4✔
870

871
   #if defined(BOTAN_HAS_HMAC_DRBG)
872
   if(rng_algo == "HMAC_DRBG") {
4✔
873
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
2✔
874
   }
875
   #endif
876

877
   if(rng_algo == "Fixed") {
4✔
878
      if(!rng_params.empty()) {
2✔
879
         throw Test_Error("Expected empty RngParams for Fixed RNG");
×
880
      }
881
      rng = std::make_unique<Fixed_Output_RNG>();
4✔
882
   }
883

884
   if(rng) {
4✔
885
      rng->add_entropy(rng_seed.data(), rng_seed.size());
4✔
886

887
      try {
4✔
888
         auto key = Botan::create_private_key(algo_name(), *rng, key_param);
8✔
889
         if(key) {
4✔
890
            const auto key_bits = key->private_key_info();
4✔
891
            result.test_bin_eq("Generated key matched expected value", key_bits, expected_key);
4✔
892
         }
4✔
893
      } catch(Botan::Exception& e) {
4✔
894
         result.test_note("failed to create key", e.what());
×
895
      }
×
896
   } else {
897
      result.test_note("Skipping test due to unavailable RNG");
×
898
   }
899

900
   result.end_timer();
4✔
901

902
   return result;
4✔
903
}
4✔
904

905
namespace {
906

907
/**
908
 * @brief Some general tests for minimal API sanity for signing/verification.
909
 */
910
class PK_API_Sign_Test : public Text_Based_Test {
×
911
   public:
912
      PK_API_Sign_Test() : Text_Based_Test("pubkey/api_sign.vec", "AlgoParams,SigParams", "Provider") {}
2✔
913

914
   protected:
915
      Test::Result run_one_test(const std::string& algorithm, const VarMap& vars) final {
14✔
916
         const std::string algo_params = vars.get_req_str("AlgoParams");
14✔
917
         const std::string sig_params = vars.get_req_str("SigParams");
14✔
918
         const std::string verify_params = vars.get_opt_str("VerifyParams", sig_params);
14✔
919
         const std::string provider = vars.get_opt_str("Provider", "base");
14✔
920

921
         std::ostringstream test_name;
14✔
922
         test_name << "Sign/verify API tests " << algorithm;
14✔
923
         if(!algo_params.empty()) {
14✔
924
            test_name << '(' << algo_params << ')';
12✔
925
         }
926
         if(!sig_params.empty()) {
14✔
927
            test_name << '/' << sig_params;
11✔
928
         }
929
         Test::Result result(test_name.str());
14✔
930

931
         auto privkey = [&]() -> std::unique_ptr<Botan::Private_Key> {
42✔
932
            try {
14✔
933
               return Botan::create_private_key(algorithm, this->rng(), algo_params, provider);
14✔
934
            } catch(Botan::Not_Implemented&) {}
×
935

936
            return nullptr;
×
937
         }();
14✔
938

939
         if(!privkey) {
14✔
940
            result.test_note(Botan::fmt(
×
941
               "Skipping Sign/verify API tests for {}({}) with provider {}", algorithm, algo_params, provider));
942
            return result;
×
943
         }
944

945
         auto pubkey = Botan::X509::load_key(Botan::X509::BER_encode(*privkey->public_key()));
14✔
946
         result.test_is_true("Storing and loading public key works", pubkey != nullptr);
14✔
947

948
         result.test_is_true("private key claims to support signatures",
14✔
949
                             privkey->supports_operation(Botan::PublicKeyOperation::Signature));
14✔
950
         result.test_is_true("public key claims to support signatures",
14✔
951
                             pubkey->supports_operation(Botan::PublicKeyOperation::Signature));
14✔
952
         result.test_sz_gt("Public key length must be greater than 0", pubkey->key_length(), 0);
14✔
953
         if(privkey->stateful_operation()) {
14✔
954
            result.test_is_true("A stateful key reports the number of remaining operations",
2✔
955
                                privkey->remaining_operations().has_value());
2✔
956
         } else {
957
            result.test_is_true("A stateless key has an unlimited number of remaining operations",
12✔
958
                                !privkey->remaining_operations().has_value());
12✔
959
         }
960

961
         auto [signer, verifier] = [&] {
14✔
962
            try {
14✔
963
               return std::make_pair(std::make_unique<Botan::PK_Signer>(
14✔
964
                                        *privkey, this->rng(), sig_params, Botan::Signature_Format::Standard, provider),
14✔
965
                                     std::make_unique<Botan::PK_Verifier>(
14✔
966
                                        *pubkey, verify_params, Botan::Signature_Format::Standard, provider));
42✔
967
            } catch(Botan::Algorithm_Not_Found&) {}
×
968

969
            return std::pair<std::unique_ptr<Botan::PK_Signer>, std::unique_ptr<Botan::PK_Verifier>>{};
×
970
         }();
14✔
971

972
         if(!signer || !verifier) {
14✔
973
            result.test_note(Botan::fmt(
×
974
               "Skipping Sign/verify API tests for {}({}) with provider {}", algorithm, algo_params, provider));
975
            return result;
×
976
         }
977

978
         result.test_is_true("Creating PK_Signer works", signer != nullptr);
14✔
979
         result.test_is_true("Creating PK_Signer works", verifier != nullptr);
14✔
980

981
         result.test_str_not_empty("PK_Signer should report some hash", signer->hash_function());
14✔
982
         result.test_str_not_empty("PK_Verifier should report some hash", verifier->hash_function());
14✔
983

984
         result.test_str_eq(
14✔
985
            "PK_Signer and PK_Verifier report the same hash", signer->hash_function(), verifier->hash_function());
28✔
986

987
         pubkey.reset();
14✔
988
         privkey.reset();
14✔
989
         const std::array<uint8_t, 4> msg{0xde, 0xad, 0xbe, 0xef};
14✔
990
         const auto sig = signer->sign_message(msg, this->rng());
14✔
991
         result.test_sz_gt("Signer should still work if no one else hold a reference to the key", sig.size(), 0);
14✔
992
         result.test_is_true("Verifier should still work if no one else hold a reference to the key",
14✔
993
                             verifier->verify_message(msg, sig));
14✔
994

995
         return result;
14✔
996
      }
42✔
997

998
      bool skip_this_test([[maybe_unused]] const std::string& header, const VarMap& /*vars*/) override {
14✔
999
   #if !defined(BOTAN_HAS_SLH_DSA_WITH_SHA2)
1000
         if(header == "SLH-DSA") {
1001
            return true;
1002
         }
1003
   #endif
1004
         return false;
14✔
1005
      }
1006
};
1007

1008
BOTAN_REGISTER_TEST("pubkey", "pk_api_sign", PK_API_Sign_Test);
1009

1010
/**
1011
 * @brief Testing PK key decoding
1012
 */
1013
class PK_Key_Decoding_Test : public Text_Based_Test {
×
1014
   public:
1015
      PK_Key_Decoding_Test() : Text_Based_Test("pubkey/key_encoding.vec", "Key") {}
2✔
1016

1017
   protected:
1018
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) final {
1✔
1019
         const auto key = vars.get_req_bin("Key");
1✔
1020

1021
         Test::Result result("PK Key Decoding");
1✔
1022

1023
         try {
1✔
1024
            auto k = Botan::PKCS8::load_key(key);
1✔
1025
            result.test_success("Was able to deserialize the key");
1✔
1026
         } catch(Botan::Not_Implemented&) {
1✔
1027
            result.test_note("Skipping test due to to algorithm being unavailable");
×
1028
         } catch(Botan::Exception& e) {
×
1029
            if(std::string(e.what()).starts_with("Unknown or unavailable public key algorithm")) {
×
1030
               result.test_note("Skipping test due to to algorithm being unavailable");
×
1031
            } else {
1032
               result.test_failure("Failed to deserialize key", e.what());
×
1033
            }
1034
         }
×
1035

1036
         return result;
1✔
1037
      }
1✔
1038
};
1039

1040
BOTAN_REGISTER_TEST("pubkey", "pk_key_decoding", PK_Key_Decoding_Test);
1041

1042
}  // namespace
1043

1044
}  // namespace Botan_Tests
1045

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

© 2026 Coveralls, Inc