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

randombit / botan / 23225340130

18 Mar 2026 01:53AM UTC coverage: 89.677% (-0.001%) from 89.678%
23225340130

push

github

web-flow
Merge pull request #5456 from randombit/jack/clang-tidy-22

Fix various warnings from clang-tidy 22

104438 of 116460 relevant lines covered (89.68%)

11819947.55 hits per line

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

86.33
/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,229✔
104

105
         if(!result.test_bin_ne("incorrect ciphertext different", decrypted, plaintext)) {
3,229✔
106
            result.test_bin_eq("used corrupted ciphertext", bad_ctext, ciphertext);
×
107
         }
108
      } catch(std::exception&) {
6,340✔
109
         ++ciphertext_rejected;
3,111✔
110
      }
3,111✔
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,302✔
122
   if(!pad_hdr.empty()) {
15,302✔
123
      return pad_hdr;
1,838✔
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,542✔
129
   return Test::provider_filter({"base", "commoncrypto", "openssl", "tpm"});
17,542✔
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,936✔
228
   return Botan::Signature_Format::Standard;
4,936✔
229
}
230

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

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

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

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

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

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

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

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

267
            if(expected_valid) {
8,848✔
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,322✔
275
            }
276
         } catch(std::exception& e) {
×
277
            result.test_failure("verification threw exception", e.what());
×
278
         }
×
279
      }
280
   }
65,490✔
281

282
   return result;
13,098✔
283
}
26,196✔
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
      } catch(Botan::Exception& e) {
×
421
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
422
      }
×
423

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

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

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

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

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

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

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

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

467
   return result;
199✔
468
}
597✔
469

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

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

477
   auto privkey = load_private_key(vars);
42✔
478

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

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

488
      Botan::secure_vector<uint8_t> decrypted;
42✔
489
      try {
42✔
490
         decrypted = decryptor->decrypt(ciphertext);
42✔
491
      } catch(Botan::Exception& e) {
×
492
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
493
      }
×
494

495
      result.test_bin_eq(dec_provider + " decryption of KAT", decrypted, plaintext);
42✔
496
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
42✔
497
   }
210✔
498

499
   return result;
84✔
500
}
42✔
501

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

508
   Test::Result result(algo_name() + "/" + kdf + " KEM");
60✔
509

510
   auto privkey = load_private_key(vars);
10✔
511

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

515
   auto pubkey = privkey->public_key();
10✔
516

517
   const size_t desired_key_len = K.size();
10✔
518

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

527
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
10✔
528

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

531
   result.test_sz_eq("encapsulated key length matches expected",
10✔
532
                     kem_result.encapsulated_shared_key().size(),
10✔
533
                     enc->encapsulated_key_length());
534

535
   result.test_sz_eq(
10✔
536
      "shared key length matches expected", kem_result.shared_key().size(), enc->shared_key_length(desired_key_len));
10✔
537

538
   result.test_bin_eq("C0 matches", kem_result.encapsulated_shared_key(), C0);
10✔
539
   result.test_bin_eq("K matches", kem_result.shared_key(), K);
10✔
540

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

549
   result.test_sz_eq("encapsulated key length matches expected",
10✔
550
                     kem_result.encapsulated_shared_key().size(),
10✔
551
                     dec->encapsulated_key_length());
552

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

556
   result.test_sz_eq(
10✔
557
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
558

559
   result.test_bin_eq("decrypted K matches", decr_shared_key, K);
10✔
560

561
   return result;
10✔
562
}
30✔
563

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

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

570
   auto privkey = load_our_key(header, vars);
784✔
571

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

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

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

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

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

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

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

600
   return result;
784✔
601
}
1,568✔
602

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

609
namespace {
610

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

619
   auto passphrase = Test::random_password(rng);
222✔
620

621
   try {
222✔
622
      Botan::DataSource_Memory data_src(
222✔
623
         Botan::PKCS8::PEM_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
222✔
624

625
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
222✔
626

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

634
   try {
222✔
635
      Botan::DataSource_Memory data_src(
222✔
636
         Botan::PKCS8::BER_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
444✔
637

638
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
222✔
639

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

649
}  // namespace
650

651
std::vector<Test::Result> PK_Key_Generation_Test::run() {
21✔
652
   std::vector<Test::Result> results;
21✔
653

654
   for(const auto& param : keygen_params()) {
132✔
655
      const auto algorithm_name = algo_name(param);
111✔
656
      const std::string report_name = Botan::fmt("{}{}", algorithm_name, (param.empty() ? param : " " + param));
115✔
657

658
      Test::Result result(report_name + " keygen");
111✔
659

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

662
      if(providers.empty()) {
111✔
663
         result.note_missing("provider key generation " + algorithm_name);
×
664
      }
665

666
      result.start_timer();
111✔
667
      for(auto&& prov : providers) {
222✔
668
         auto key_p = Botan::create_private_key(algorithm_name, this->rng(), param, prov);
111✔
669

670
         if(key_p == nullptr) {
111✔
671
            continue;
×
672
         }
673

674
         const Botan::Private_Key& key = *key_p;
111✔
675

676
         try {
111✔
677
            result.test_is_true("Key passes self tests", key.check_key(this->rng(), true));
111✔
678
         } catch(Botan::Lookup_Error&) {}
×
679

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

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

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

697
         result.test_sz_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
111✔
698
         result.test_sz_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
111✔
699

700
         auto public_key = key.public_key();
111✔
701

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

704
         result.test_str_eq(
222✔
705
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
222✔
706

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

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

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

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

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

755
         // Test PEM public key round trips OK
756
         try {
111✔
757
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(*public_key));
111✔
758
            auto loaded = Botan::X509::load_key(data_src);
111✔
759

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

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

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

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

783
         // Test PEM private key round trips OK
784
         try {
111✔
785
            const auto ber = key.private_key_info();
111✔
786
            Botan::DataSource_Memory data_src(ber);
111✔
787
            auto loaded = Botan::PKCS8::load_key(data_src);
111✔
788

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

796
         try {
111✔
797
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
111✔
798
            auto loaded = Botan::PKCS8::load_key(data_src);
111✔
799

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

806
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
807

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

811
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
812

813
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", this->rng());
111✔
814
   #endif
815
      }
333✔
816

817
      result.end_timer();
111✔
818

819
      results.push_back(result);
111✔
820
   }
132✔
821

822
   return results;
21✔
823
}
111✔
824

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

828
   if(header != "Valid" && header != "Invalid") {
9✔
829
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
830
   }
831

832
   const bool expected_valid = (header == "Valid");
9✔
833
   auto pubkey = load_public_key(vars);
9✔
834

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

837
   result.test_bool_eq("Expected validation result", tested_valid, expected_valid);
9✔
838

839
   return result;
9✔
840
}
9✔
841

842
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
843
                                                                   const std::string& test_src) :
2✔
844
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
4✔
845

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

853
   std::ostringstream report_name;
4✔
854

855
   report_name << algo_name();
8✔
856
   if(!key_param.empty()) {
4✔
857
      report_name << " " << key_param;
4✔
858
   }
859
   report_name << " keygen stability";
4✔
860

861
   Test::Result result(report_name.str());
4✔
862

863
   result.start_timer();
4✔
864

865
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
4✔
866

867
   #if defined(BOTAN_HAS_HMAC_DRBG)
868
   if(rng_algo == "HMAC_DRBG") {
4✔
869
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
2✔
870
   }
871
   #endif
872

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

880
   if(rng) {
4✔
881
      rng->add_entropy(rng_seed.data(), rng_seed.size());
4✔
882

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

896
   result.end_timer();
4✔
897

898
   return result;
4✔
899
}
4✔
900

901
namespace {
902

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

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

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

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

932
            return nullptr;
×
933
         }();
14✔
934

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

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

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

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

965
            return std::pair<std::unique_ptr<Botan::PK_Signer>, std::unique_ptr<Botan::PK_Verifier>>{};
×
966
         }();
14✔
967

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

974
         result.test_is_true("Creating PK_Signer works", signer != nullptr);
14✔
975
         result.test_is_true("Creating PK_Signer works", verifier != nullptr);
14✔
976

977
         result.test_str_not_empty("PK_Signer should report some hash", signer->hash_function());
14✔
978
         result.test_str_not_empty("PK_Verifier should report some hash", verifier->hash_function());
14✔
979

980
         result.test_str_eq(
14✔
981
            "PK_Signer and PK_Verifier report the same hash", signer->hash_function(), verifier->hash_function());
28✔
982

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

991
         return result;
14✔
992
      }
42✔
993

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

1004
BOTAN_REGISTER_TEST("pubkey", "pk_api_sign", PK_API_Sign_Test);
1005

1006
/**
1007
 * @brief Testing PK key decoding
1008
 */
1009
class PK_Key_Decoding_Test : public Text_Based_Test {
×
1010
   public:
1011
      PK_Key_Decoding_Test() : Text_Based_Test("pubkey/key_encoding.vec", "Key") {}
2✔
1012

1013
   protected:
1014
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) final {
1✔
1015
         const auto key = vars.get_req_bin("Key");
1✔
1016

1017
         Test::Result result("PK Key Decoding");
1✔
1018

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

1032
         return result;
1✔
1033
      }
1✔
1034
};
1035

1036
BOTAN_REGISTER_TEST("pubkey", "pk_key_decoding", PK_Key_Decoding_Test);
1037

1038
}  // namespace
1039

1040
}  // namespace Botan_Tests
1041

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