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

randombit / botan / 23194039994

17 Mar 2026 12:24PM UTC coverage: 89.676% (-0.005%) from 89.681%
23194039994

push

github

web-flow
Merge pull request #5455 from randombit/jack/fix-ecdsa-der-good-after-bad

Fix a bug in handling invalid DER ECDSA signatures

104434 of 116457 relevant lines covered (89.68%)

11745253.89 hits per line

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

86.55
/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,234✔
104

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

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

117
std::string PK_Test::choose_padding(const VarMap& vars, const std::string& pad_hdr) {
15,302✔
118
   if(!pad_hdr.empty()) {
15,302✔
119
      return pad_hdr;
1,838✔
120
   }
121
   return vars.get_opt_str("Padding", this->default_padding(vars));
13,464✔
122
}
123

124
std::vector<std::string> PK_Test::possible_providers(const std::string& /*params*/) {
17,542✔
125
   return Test::provider_filter({"base", "commoncrypto", "openssl", "tpm"});
17,542✔
126
}
127

128
std::unique_ptr<Botan::RandomNumberGenerator> PK_Signature_Generation_Test::test_rng(
201✔
129
   const std::vector<uint8_t>& nonce) const {
130
   return std::make_unique<Fixed_Output_RNG>(nonce);
201✔
131
}
132

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

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

145
   Test::Result result(test_name.str());
1,257✔
146

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

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

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

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

163
   std::vector<std::unique_ptr<Botan::PK_Verifier>> verifiers;
1,257✔
164

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

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

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

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

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

182
      verifiers.push_back(std::move(verifier));
1,257✔
183
   }
6,285✔
184

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

188
      std::vector<uint8_t> generated_signature;
5,028✔
189

190
      try {
5,028✔
191
         signer = std::make_unique<Botan::PK_Signer>(
5,028✔
192
            *privkey, this->rng(), padding, Botan::Signature_Format::Standard, sign_provider);
6,285✔
193

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

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

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

220
   return result;
1,257✔
221
}
3,771✔
222

223
Botan::Signature_Format PK_Signature_Verification_Test::sig_format() const {
4,936✔
224
   return Botan::Signature_Format::Standard;
4,936✔
225
}
226

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

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

234
   auto pubkey = load_public_key(vars);
13,098✔
235

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

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

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

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

259
      if(verifier) {
52,392✔
260
         try {
8,848✔
261
            const bool verified = verifier->verify_message(message, signature);
8,848✔
262

263
            if(expected_valid) {
8,848✔
264
               result.test_is_true("correct signature valid with " + verify_provider, verified);
4,526✔
265

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

278
   return result;
13,098✔
279
}
26,196✔
280

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

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

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

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

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

302
   return result;
1,412✔
303
}
1,412✔
304

305
std::vector<Test::Result> PK_Sign_Verify_DER_Test::run() {
1✔
306
   const std::vector<uint8_t> message = {'f', 'o', 'o', 'b', 'a', 'r'};
1✔
307
   const std::string padding = m_padding;
1✔
308

309
   auto privkey = key();
1✔
310
   if(!privkey) {
1✔
311
      return {};
×
312
   }
313
   auto pubkey = privkey->public_key();
1✔
314

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

317
   for(const auto& provider : possible_providers(algo_name())) {
3✔
318
      std::unique_ptr<Botan::PK_Signer> signer;
1✔
319
      std::unique_ptr<Botan::PK_Verifier> verifier;
1✔
320

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

330
      if(signer && verifier) {
1✔
331
         try {
1✔
332
            std::vector<uint8_t> generated_signature = signer->sign_message(message, this->rng());
1✔
333
            const bool verified = verifier->verify_message(message, generated_signature);
1✔
334

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

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

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

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

358
      const auto sig = signer.sign_message(message, this->rng());
1✔
359
      const auto verified = verifier.verify_message(message, sig);
1✔
360

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

371
   return {result};
2✔
372
}
4✔
373

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

380
std::unique_ptr<Botan::RandomNumberGenerator> PK_Encryption_Decryption_Test::test_rng(
54✔
381
   const std::vector<uint8_t>& nonce) const {
382
   return std::make_unique<Fixed_Output_RNG>(nonce);
54✔
383
}
384

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

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

392
   auto privkey = load_private_key(vars);
199✔
393

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

397
   auto pubkey = privkey->public_key();
199✔
398

399
   std::vector<std::unique_ptr<Botan::PK_Decryptor>> decryptors;
199✔
400

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

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

410
      Botan::secure_vector<uint8_t> decrypted;
199✔
411
      try {
199✔
412
         decrypted = decryptor->decrypt(ciphertext);
199✔
413

414
         result.test_sz_lte(
199✔
415
            "Plaintext within length", decrypted.size(), decryptor->plaintext_length(ciphertext.size()));
199✔
416
      } catch(Botan::Exception& e) {
×
417
         result.test_failure("Failed to decrypt KAT ciphertext", e.what());
×
418
      }
×
419

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

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

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

434
      std::unique_ptr<Botan::RandomNumberGenerator> kat_rng;
199✔
435
      if(vars.has_key("Nonce")) {
199✔
436
         kat_rng = test_rng(vars.get_req_bin("Nonce"));
59✔
437
      }
438

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

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

451
      result.test_sz_lte(
199✔
452
         "Ciphertext within length", generated_ciphertext.size(), encryptor->ciphertext_length(plaintext.size()));
199✔
453

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

463
   return result;
199✔
464
}
597✔
465

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

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

473
   auto privkey = load_private_key(vars);
42✔
474

475
   for(const auto& dec_provider : possible_providers(algo_name())) {
252✔
476
      std::unique_ptr<Botan::PK_Decryptor> decryptor;
168✔
477

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

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

491
      result.test_bin_eq(dec_provider + " decryption of KAT", decrypted, plaintext);
42✔
492
      check_invalid_ciphertexts(result, *decryptor, plaintext, ciphertext, this->rng());
42✔
493
   }
210✔
494

495
   return result;
84✔
496
}
42✔
497

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

504
   Test::Result result(algo_name() + "/" + kdf + " KEM");
60✔
505

506
   auto privkey = load_private_key(vars);
10✔
507

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

511
   auto pubkey = privkey->public_key();
10✔
512

513
   const size_t desired_key_len = K.size();
10✔
514

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

523
   Fixed_Output_RNG fixed_output_rng(vars.get_req_bin("R"));
10✔
524

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

527
   result.test_sz_eq("encapsulated key length matches expected",
10✔
528
                     kem_result.encapsulated_shared_key().size(),
10✔
529
                     enc->encapsulated_key_length());
530

531
   result.test_sz_eq(
10✔
532
      "shared key length matches expected", kem_result.shared_key().size(), enc->shared_key_length(desired_key_len));
10✔
533

534
   result.test_bin_eq("C0 matches", kem_result.encapsulated_shared_key(), C0);
10✔
535
   result.test_bin_eq("K matches", kem_result.shared_key(), K);
10✔
536

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

545
   result.test_sz_eq("encapsulated key length matches expected",
10✔
546
                     kem_result.encapsulated_shared_key().size(),
10✔
547
                     dec->encapsulated_key_length());
548

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

552
   result.test_sz_eq(
10✔
553
      "shared key length matches expected", decr_shared_key.size(), dec->shared_key_length(desired_key_len));
554

555
   result.test_bin_eq("decrypted K matches", decr_shared_key, K);
10✔
556

557
   return result;
10✔
558
}
30✔
559

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

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

566
   auto privkey = load_our_key(header, vars);
784✔
567

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

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

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

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

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

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

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

596
   return result;
784✔
597
}
1,568✔
598

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

605
namespace {
606

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

615
   auto passphrase = Test::random_password(rng);
222✔
616

617
   try {
222✔
618
      Botan::DataSource_Memory data_src(
222✔
619
         Botan::PKCS8::PEM_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
222✔
620

621
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
222✔
622

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

630
   try {
222✔
631
      Botan::DataSource_Memory data_src(
222✔
632
         Botan::PKCS8::BER_encode(key, rng, passphrase, std::chrono::milliseconds(1), pbe_algo));
444✔
633

634
      auto loaded = Botan::PKCS8::load_key(data_src, passphrase);
222✔
635

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

645
}  // namespace
646

647
std::vector<Test::Result> PK_Key_Generation_Test::run() {
21✔
648
   std::vector<Test::Result> results;
21✔
649

650
   for(const auto& param : keygen_params()) {
132✔
651
      const auto algorithm_name = algo_name(param);
111✔
652
      const std::string report_name = Botan::fmt("{}{}", algorithm_name, (param.empty() ? param : " " + param));
115✔
653

654
      Test::Result result(report_name + " keygen");
111✔
655

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

658
      if(providers.empty()) {
111✔
659
         result.note_missing("provider key generation " + algorithm_name);
×
660
      }
661

662
      result.start_timer();
111✔
663
      for(auto&& prov : providers) {
222✔
664
         auto key_p = Botan::create_private_key(algorithm_name, this->rng(), param, prov);
111✔
665

666
         if(key_p == nullptr) {
111✔
667
            continue;
×
668
         }
669

670
         const Botan::Private_Key& key = *key_p;
111✔
671

672
         try {
111✔
673
            result.test_is_true("Key passes self tests", key.check_key(this->rng(), true));
111✔
674
         } catch(Botan::Lookup_Error&) {}
×
675

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

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

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

693
         result.test_sz_gte("Key has reasonable estimated strength (lower)", key.estimated_strength(), 64);
111✔
694
         result.test_sz_lt("Key has reasonable estimated strength (upper)", key.estimated_strength(), 512);
111✔
695

696
         auto public_key = key.public_key();
111✔
697

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

700
         result.test_str_eq(
222✔
701
            "public_key has same encoding", Botan::X509::PEM_encode(key), Botan::X509::PEM_encode(*public_key));
222✔
702

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

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

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

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

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

751
         // Test PEM public key round trips OK
752
         try {
111✔
753
            Botan::DataSource_Memory data_src(Botan::X509::PEM_encode(*public_key));
111✔
754
            auto loaded = Botan::X509::load_key(data_src);
111✔
755

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

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

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

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

779
         // Test PEM private key round trips OK
780
         try {
111✔
781
            const auto ber = key.private_key_info();
111✔
782
            Botan::DataSource_Memory data_src(ber);
111✔
783
            auto loaded = Botan::PKCS8::load_key(data_src);
111✔
784

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

792
         try {
111✔
793
            Botan::DataSource_Memory data_src(Botan::PKCS8::BER_encode(key));
111✔
794
            auto loaded = Botan::PKCS8::load_key(data_src);
111✔
795

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

802
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SHA2_32)
803

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

807
   #if defined(BOTAN_HAS_PKCS5_PBES2) && defined(BOTAN_HAS_AES) && defined(BOTAN_HAS_SCRYPT)
808

809
         test_pbe_roundtrip(result, key, "PBES2(AES-128/CBC,Scrypt)", this->rng());
111✔
810
   #endif
811
      }
333✔
812

813
      result.end_timer();
111✔
814

815
      results.push_back(result);
111✔
816
   }
132✔
817

818
   return results;
21✔
819
}
111✔
820

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

824
   if(header != "Valid" && header != "Invalid") {
9✔
825
      throw Test_Error("Unexpected header for PK_Key_Validity_Test");
×
826
   }
827

828
   const bool expected_valid = (header == "Valid");
9✔
829
   auto pubkey = load_public_key(vars);
9✔
830

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

833
   result.test_bool_eq("Expected validation result", tested_valid, expected_valid);
9✔
834

835
   return result;
9✔
836
}
9✔
837

838
PK_Key_Generation_Stability_Test::PK_Key_Generation_Stability_Test(const std::string& algo,
2✔
839
                                                                   const std::string& test_src) :
2✔
840
      PK_Test(algo, test_src, "Rng,RngSeed,Key", "KeyParams,RngParams") {}
4✔
841

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

849
   std::ostringstream report_name;
4✔
850

851
   report_name << algo_name();
8✔
852
   if(!key_param.empty()) {
4✔
853
      report_name << " " << key_param;
4✔
854
   }
855
   report_name << " keygen stability";
4✔
856

857
   Test::Result result(report_name.str());
4✔
858

859
   result.start_timer();
4✔
860

861
   std::unique_ptr<Botan::RandomNumberGenerator> rng;
4✔
862

863
   #if defined(BOTAN_HAS_HMAC_DRBG)
864
   if(rng_algo == "HMAC_DRBG") {
4✔
865
      rng = std::make_unique<Botan::HMAC_DRBG>(rng_params);
2✔
866
   }
867
   #endif
868

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

876
   if(rng) {
4✔
877
      rng->add_entropy(rng_seed.data(), rng_seed.size());
4✔
878

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

892
   result.end_timer();
4✔
893

894
   return result;
4✔
895
}
4✔
896

897
/**
898
 * @brief Some general tests for minimal API sanity for signing/verification.
899
 */
900
class PK_API_Sign_Test : public Text_Based_Test {
901
   public:
902
      PK_API_Sign_Test() : Text_Based_Test("pubkey/api_sign.vec", "AlgoParams,SigParams", "Provider") {}
2✔
903

904
   protected:
905
      Test::Result run_one_test(const std::string& algorithm, const VarMap& vars) final {
14✔
906
         const std::string algo_params = vars.get_req_str("AlgoParams");
14✔
907
         const std::string sig_params = vars.get_req_str("SigParams");
14✔
908
         const std::string verify_params = vars.get_opt_str("VerifyParams", sig_params);
14✔
909
         const std::string provider = vars.get_opt_str("Provider", "base");
14✔
910

911
         std::ostringstream test_name;
14✔
912
         test_name << "Sign/verify API tests " << algorithm;
14✔
913
         if(!algo_params.empty()) {
14✔
914
            test_name << '(' << algo_params << ')';
12✔
915
         }
916
         if(!sig_params.empty()) {
14✔
917
            test_name << '/' << sig_params;
11✔
918
         }
919
         Test::Result result(test_name.str());
14✔
920

921
         auto privkey = [&]() -> std::unique_ptr<Botan::Private_Key> {
42✔
922
            try {
14✔
923
               return Botan::create_private_key(algorithm, this->rng(), algo_params, provider);
14✔
924
            } catch(Botan::Not_Implemented&) {}
×
925

926
            return nullptr;
×
927
         }();
14✔
928

929
         if(!privkey) {
14✔
930
            result.test_note(Botan::fmt(
×
931
               "Skipping Sign/verify API tests for {}({}) with provider {}", algorithm, algo_params, provider));
932
            return result;
×
933
         }
934

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

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

951
         auto [signer, verifier] = [&] {
14✔
952
            try {
14✔
953
               return std::make_pair(std::make_unique<Botan::PK_Signer>(
14✔
954
                                        *privkey, this->rng(), sig_params, Botan::Signature_Format::Standard, provider),
14✔
955
                                     std::make_unique<Botan::PK_Verifier>(
14✔
956
                                        *pubkey, verify_params, Botan::Signature_Format::Standard, provider));
42✔
957
            } catch(Botan::Algorithm_Not_Found&) {}
×
958

959
            return std::pair<std::unique_ptr<Botan::PK_Signer>, std::unique_ptr<Botan::PK_Verifier>>{};
×
960
         }();
14✔
961

962
         if(!signer || !verifier) {
14✔
963
            result.test_note(Botan::fmt(
×
964
               "Skipping Sign/verify API tests for {}({}) with provider {}", algorithm, algo_params, provider));
965
            return result;
×
966
         }
967

968
         result.test_is_true("Creating PK_Signer works", signer != nullptr);
14✔
969
         result.test_is_true("Creating PK_Signer works", verifier != nullptr);
14✔
970

971
         result.test_str_not_empty("PK_Signer should report some hash", signer->hash_function());
14✔
972
         result.test_str_not_empty("PK_Verifier should report some hash", verifier->hash_function());
14✔
973

974
         result.test_str_eq(
14✔
975
            "PK_Signer and PK_Verifier report the same hash", signer->hash_function(), verifier->hash_function());
28✔
976

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

985
         return result;
14✔
986
      }
42✔
987

988
      bool skip_this_test([[maybe_unused]] const std::string& header, const VarMap& /*vars*/) override {
14✔
989
   #if !defined(BOTAN_HAS_SLH_DSA_WITH_SHA2)
990
         if(header == "SLH-DSA") {
991
            return true;
992
         }
993
   #endif
994
         return false;
14✔
995
      }
996
};
997

998
BOTAN_REGISTER_TEST("pubkey", "pk_api_sign", PK_API_Sign_Test);
999

1000
/**
1001
 * @brief Testing PK key decoding
1002
 */
1003
class PK_Key_Decoding_Test : public Text_Based_Test {
1004
   public:
1005
      PK_Key_Decoding_Test() : Text_Based_Test("pubkey/key_encoding.vec", "Key") {}
2✔
1006

1007
   protected:
1008
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) final {
1✔
1009
         const auto key = vars.get_req_bin("Key");
1✔
1010

1011
         Test::Result result("PK Key Decoding");
1✔
1012

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

1026
         return result;
1✔
1027
      }
1✔
1028
};
1029

1030
BOTAN_REGISTER_TEST("pubkey", "pk_key_decoding", PK_Key_Decoding_Test);
1031

1032
}  // namespace Botan_Tests
1033

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