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

randombit / botan / 29069405686

09 Jul 2026 02:56PM UTC coverage: 89.393% (-0.003%) from 89.396%
29069405686

push

github

web-flow
Merge pull request #5716 from falko-strenzke/slh-dsa-cert-test

Add RFC 9909 test certificate for SLH-DSA

113307 of 126752 relevant lines covered (89.39%)

10739422.74 hits per line

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

93.62
/src/tests/test_sphincsplus.cpp
1
/*
2
* (C) 2023 Jack Lloyd
3
*     2023 Fabian Albert, René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
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_SPHINCS_PLUS_COMMON) && defined(BOTAN_HAS_AES)
11

12
   #include <botan/assert.h>
13
   #include <botan/hash.h>
14
   #include <botan/pubkey.h>
15
   #include <botan/secmem.h>
16
   #include <botan/sp_parameters.h>
17
   #include <botan/sphincsplus.h>
18

19
   #if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
20
      #include <botan/x509cert.h>
21
   #endif
22

23
   #include "test_pubkey.h"
24
   #include "test_rng.h"
25

26
namespace Botan_Tests {
27

28
namespace {
29

30
/**
31
 * Test all implemented SLH-DSA instances using the data of the KAT files.
32
 */
33
class SPHINCS_Plus_Test_Base : public Text_Based_Test {
×
34
   public:
35
      explicit SPHINCS_Plus_Test_Base(std::string_view kat_path) :
2✔
36
            Text_Based_Test(std::string(kat_path), "SphincsParameterSet,seed,pk,sk,msg,HashSigRand", "HashSigDet") {}
6✔
37

38
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
24✔
39
         auto params = Botan::Sphincs_Parameters::create(vars.get_req_str("SphincsParameterSet"));
24✔
40

41
         if(!params.is_available()) {
24✔
42
            return true;
43
         }
44

45
   #if not defined(BOTAN_HAS_SHA3)
46
         // The SLH-DSA shake-based signatures are hashed with SHA-3 in the test file.
47
         if(params.hash_type() == Botan::Sphincs_Hash_Type::Shake256) {
48
            return true;
49
         }
50
   #endif
51

52
         // Execute the small (slow) instances only with --run-long-tests
53
         switch(params.parameter_set()) {
24✔
54
            case Botan::Sphincs_Parameter_Set::Sphincs128Fast:
55
            case Botan::Sphincs_Parameter_Set::Sphincs192Fast:
56
            case Botan::Sphincs_Parameter_Set::Sphincs256Fast:
57
            case Botan::Sphincs_Parameter_Set::SLHDSA128Fast:
58
            case Botan::Sphincs_Parameter_Set::SLHDSA192Fast:
59
            case Botan::Sphincs_Parameter_Set::SLHDSA256Fast:
60
               return false;
61
            case Botan::Sphincs_Parameter_Set::Sphincs128Small:
12✔
62
            case Botan::Sphincs_Parameter_Set::Sphincs192Small:
12✔
63
            case Botan::Sphincs_Parameter_Set::Sphincs256Small:
12✔
64
            case Botan::Sphincs_Parameter_Set::SLHDSA128Small:
12✔
65
            case Botan::Sphincs_Parameter_Set::SLHDSA192Small:
12✔
66
            case Botan::Sphincs_Parameter_Set::SLHDSA256Small:
12✔
67
               return !Test::run_long_tests();
12✔
68
         }
69
         BOTAN_ASSERT_UNREACHABLE();
×
70
      }
71

72
      Test::Result run_one_test(const std::string& /*header*/, const VarMap& vars) final {
24✔
73
         auto params = Botan::Sphincs_Parameters::create(vars.get_req_str("SphincsParameterSet"));
24✔
74
         Test::Result result(params.is_slh_dsa() ? "SLH-DSA" : "SPHINCS+");
36✔
75

76
         const std::vector<uint8_t> seed_ref = vars.get_req_bin("seed");
24✔
77
         const std::vector<uint8_t> msg_ref = vars.get_req_bin("msg");
24✔
78
         const std::vector<uint8_t> pk_ref = vars.get_req_bin("pk");
24✔
79
         const std::vector<uint8_t> sk_ref = vars.get_req_bin("sk");
24✔
80
         const std::vector<uint8_t> sig_rand_hash = vars.get_req_bin("HashSigRand");
24✔
81
         const auto sig_det_hash = [&]() -> std::optional<std::vector<uint8_t>> {
×
82
            if(vars.has_key("HashSigDet")) {
24✔
83
               return vars.get_opt_bin("HashSigDet");
12✔
84
            } else {
85
               return std::nullopt;
12✔
86
            }
87
         }();
24✔
88

89
         // Depending on the SLH-DSA configuration the resulting signature is
90
         // hashed either with SHA-3 or SHA-256 to reduce the inner dependencies
91
         // on other hash function modules.
92
         auto hash_algo_spec = [&]() -> std::string {
×
93
            if(params.hash_type() == Botan::Sphincs_Hash_Type::Shake256) {
24✔
94
               return "SHA-3(256)";
12✔
95
            } else {
96
               return "SHA-256";
12✔
97
            }
98
         }();
24✔
99
         auto hash = Botan::HashFunction::create_or_throw(hash_algo_spec);
24✔
100

101
         /*
102
          * To get sk_seed || sk_prf || pk_seed and opt_rand from the given seed
103
          * (from KAT), we create a CTR_DRBG_AES256 rng and "simulate" the
104
          * invocation-pattern in the reference. We feed the created randomness
105
          * to the fixed output rng, to allow for our (slightly different)
106
          * invocation-pattern. Be careful, some KATs are generated by 3 separate
107
          * invocations of dbrg.random_vals which is different.
108
          */
109
         auto kat_rng = CTR_DRBG_AES256(seed_ref);
24✔
110
         Fixed_Output_RNG fixed_rng;
24✔
111
         fixed_rng.add_entropy(kat_rng.random_vec<std::vector<uint8_t>>(3 * params.n()));
24✔
112

113
         // push the entropy used for signing twice, as we want to perform two
114
         // signing operations
115
         const auto entropy_for_signing = kat_rng.random_vec<std::vector<uint8_t>>(1 * params.n());
24✔
116
         // Depending on the configuration, up to 2 signatures with 'Randomized' are created
117
         fixed_rng.add_entropy(entropy_for_signing);
24✔
118
         fixed_rng.add_entropy(entropy_for_signing);
24✔
119

120
         // Generate Keypair
121
         const Botan::SphincsPlus_PrivateKey priv_key(fixed_rng, params);
24✔
122

123
         result.test_bin_eq("public key bits", priv_key.public_key_bits(), pk_ref);
24✔
124
         result.test_bin_eq("private key bits", priv_key.private_key_bits(), sk_ref);
24✔
125

126
         // Signature roundtrip (Randomized mode)
127
         auto signer_rand = Botan::PK_Signer(priv_key, fixed_rng, "Randomized");
24✔
128
         auto signature_rand = signer_rand.sign_message(msg_ref.data(), msg_ref.size(), fixed_rng);
24✔
129

130
         result.test_bin_eq("signature creation randomized", hash->process(signature_rand), sig_rand_hash);
24✔
131

132
         Botan::PK_Verifier verifier(*priv_key.public_key(), params.algorithm_identifier());
48✔
133
         const bool verify_success =
24✔
134
            verifier.verify_message(msg_ref.data(), msg_ref.size(), signature_rand.data(), signature_rand.size());
24✔
135
         result.test_is_true("verification of valid randomized signature", verify_success);
24✔
136

137
         // Signature roundtrip (Deterministic mode) - not available for all parameter sets
138
         // For testing time reasons we only test this for some tests if not --run-long-tests
139
         if(sig_det_hash.has_value() &&
24✔
140
            (run_long_tests() || params.parameter_set() == Botan::Sphincs_Parameter_Set::SLHDSA128Fast)) {
×
141
            auto signer_det = Botan::PK_Signer(priv_key, fixed_rng, "Deterministic");
12✔
142
            auto signature_det = signer_det.sign_message(msg_ref.data(), msg_ref.size(), fixed_rng);
12✔
143

144
            result.test_bin_eq("signature creation deterministic", hash->process(signature_det), *sig_det_hash);
12✔
145

146
            auto verify_success_det =
12✔
147
               verifier.verify_message(msg_ref.data(), msg_ref.size(), signature_det.data(), signature_det.size());
12✔
148
            result.test_is_true("verification of valid deterministic signature", verify_success_det);
12✔
149
         }
12✔
150

151
         // Verification with generated Keypair
152

153
         // Run additional parsing and re-verification tests on one parameter
154
         // set only to save test runtime. Given the current test vector we will
155
         // run this exactly once per hash function.
156
         if(params.parameter_set() == Botan::Sphincs_Parameter_Set::Sphincs128Fast ||
24✔
157
            params.parameter_set() == Botan::Sphincs_Parameter_Set::SLHDSA128Fast) {
22✔
158
            // Deserialization of Keypair from test vector
159
            const Botan::SphincsPlus_PrivateKey deserialized_priv_key(sk_ref, params);
4✔
160
            const Botan::SphincsPlus_PublicKey deserialized_pub_key(pk_ref, params);
4✔
161

162
            // Signature with deserialized Keypair
163
            auto deserialized_signer = Botan::PK_Signer(deserialized_priv_key, fixed_rng, "Randomized");
4✔
164
            auto deserialized_signature = deserialized_signer.sign_message(msg_ref.data(), msg_ref.size(), fixed_rng);
4✔
165

166
            result.test_bin_eq(
4✔
167
               "signature creation after deserialization", hash->process(deserialized_signature), sig_rand_hash);
4✔
168

169
            // Verification with deserialized Keypair
170
            Botan::PK_Verifier deserialized_verifier(deserialized_pub_key, params.algorithm_identifier());
4✔
171
            const bool verify_success_deserialized = deserialized_verifier.verify_message(
4✔
172
               msg_ref.data(), msg_ref.size(), signature_rand.data(), signature_rand.size());
4✔
173
            result.test_is_true("verification of valid signature after deserialization", verify_success_deserialized);
4✔
174

175
            // Verification of invalid signature
176
            auto broken_sig = Test::mutate_vec(deserialized_signature, this->rng());
4✔
177
            const bool verify_fail = deserialized_verifier.verify_message(
4✔
178
               msg_ref.data(), msg_ref.size(), broken_sig.data(), broken_sig.size());
4✔
179
            result.test_is_true("verification of invalid signature", !verify_fail);
4✔
180

181
            const bool verify_success_after_fail = deserialized_verifier.verify_message(
4✔
182
               msg_ref.data(), msg_ref.size(), signature_rand.data(), signature_rand.size());
4✔
183
            result.test_is_true("verification of valid signature after broken signature", verify_success_after_fail);
4✔
184
         }
8✔
185

186
         // Misc
187
         result.test_is_true("parameter serialization works",
48✔
188
                             params.to_string() == vars.get_req_str("SphincsParameterSet"));
48✔
189

190
         return result;
48✔
191
      }
240✔
192
};
193

194
class SPHINCS_Plus_Test final : public SPHINCS_Plus_Test_Base {
×
195
   public:
196
      SPHINCS_Plus_Test() : SPHINCS_Plus_Test_Base("pubkey/sphincsplus.vec") {}
1✔
197
};
198

199
class SLH_DSA_Test final : public SPHINCS_Plus_Test_Base {
×
200
   public:
201
      SLH_DSA_Test() : SPHINCS_Plus_Test_Base("pubkey/slh_dsa.vec") {}
1✔
202
};
203

204
class SPHINCS_Plus_Keygen_Tests final : public PK_Key_Generation_Test {
1✔
205
   public:
206
      std::vector<std::string> keygen_params() const override {
1✔
207
         const std::vector<std::string> short_test_params = {
1✔
208
            "SphincsPlus-shake-128s-r3.1",
209
            "SLH-DSA-SHAKE-128s",
210
            "SphincsPlus-sha2-128f-r3.1",
211
            "SLH-DSA-SHA2-128f",
212
         };
1✔
213
         const std::vector<std::string> all_params = {
1✔
214
            "SphincsPlus-shake-128s-r3.1", "SphincsPlus-shake-128f-r3.1", "SphincsPlus-shake-192s-r3.1",
215
            "SphincsPlus-shake-192f-r3.1", "SphincsPlus-shake-256s-r3.1", "SphincsPlus-shake-256f-r3.1",
216
            "SLH-DSA-SHAKE-128s",          "SLH-DSA-SHAKE-128f",          "SLH-DSA-SHAKE-192s",
217
            "SLH-DSA-SHAKE-192f",          "SLH-DSA-SHAKE-256s",          "SLH-DSA-SHAKE-256f",
218
            "SphincsPlus-sha2-128s-r3.1",  "SphincsPlus-sha2-128f-r3.1",  "SphincsPlus-sha2-192s-r3.1",
219
            "SphincsPlus-sha2-192f-r3.1",  "SphincsPlus-sha2-256s-r3.1",  "SphincsPlus-sha2-256f-r3.1",
220
            "SLH-DSA-SHA2-128s",           "SLH-DSA-SHA2-128f",           "SLH-DSA-SHA2-192s",
221
            "SLH-DSA-SHA2-192f",           "SLH-DSA-SHA2-256s",           "SLH-DSA-SHA2-256f",
222
         };
1✔
223
         const auto& tested_params = Test::run_long_tests() ? all_params : short_test_params;
1✔
224
         std::vector<std::string> available_params;
1✔
225
         for(const auto& param : tested_params) {
25✔
226
            if(Botan::Sphincs_Parameters::create(param).is_available()) {
24✔
227
               available_params.push_back(param);
24✔
228
            }
229
         }
230
         return available_params;
1✔
231
      }
1✔
232

233
      std::string algo_name() const override { return "SLH-DSA"; }
24✔
234

235
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
24✔
236
                                                             std::string_view /* provider */,
237
                                                             std::span<const uint8_t> raw_pk) const override {
238
         return std::make_unique<Botan::SphincsPlus_PublicKey>(raw_pk,
24✔
239
                                                               Botan::Sphincs_Parameters::create(keygen_params));
24✔
240
      }
241
};
242

243
class Generic_SlhDsa_Signature_Tests final : public PK_Signature_Generation_Test {
244
   public:
245
      Generic_SlhDsa_Signature_Tests() :
1✔
246
            PK_Signature_Generation_Test(
247
               "SLH-DSA", "pubkey/slh_dsa_generic.vec", "Instance,Msg,PrivateKey,PublicKey,Valid,Signature", "Nonce") {}
2✔
248

249
      bool clear_between_callbacks() const override { return false; }
2✔
250

251
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
2✔
252
         const std::string instance = vars.get_req_str("Instance");
2✔
253
         const std::vector<uint8_t> privkey = vars.get_req_bin("PrivateKey");
2✔
254
         const std::vector<uint8_t> pubkey = vars.get_req_bin("PublicKey");
2✔
255
         auto sk =
2✔
256
            std::make_unique<Botan::SphincsPlus_PrivateKey>(privkey, Botan::Sphincs_Parameters::create(instance));
2✔
257

258
         if(sk->public_key_bits() != pubkey) {
4✔
259
            throw Test_Error("Invalid SLH-DSA key in test data");
×
260
         }
261

262
         return sk;
2✔
263
      }
6✔
264

265
      std::string default_padding(const VarMap& vars) const override {
2✔
266
         return vars.has_key("Nonce") ? "Randomized" : "Deterministic";
3✔
267
      }
268

269
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
2✔
270
         return !Botan::Sphincs_Parameters::create(vars.get_req_str("Instance")).is_available();
2✔
271
      }
272
};
273

274
class Generic_SlhDsa_Verification_Tests final : public PK_Signature_Verification_Test {
275
   public:
276
      Generic_SlhDsa_Verification_Tests() :
1✔
277
            PK_Signature_Verification_Test(
278
               "SLH-DSA", "pubkey/slh_dsa_generic.vec", "Instance,Msg,PrivateKey,PublicKey,Valid,Signature", "Nonce") {}
2✔
279

280
      bool clear_between_callbacks() const override { return false; }
2✔
281

282
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
2✔
283
         const std::string instance = vars.get_req_str("Instance");
2✔
284
         const std::vector<uint8_t> pubkey = vars.get_req_bin("PublicKey");
2✔
285

286
         return std::make_unique<Botan::SphincsPlus_PublicKey>(pubkey, Botan::Sphincs_Parameters::create(instance));
4✔
287
      }
2✔
288

289
      std::string default_padding(const VarMap& /*vars*/) const override { return ""; }
2✔
290

291
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
2✔
292
         return !Botan::Sphincs_Parameters::create(vars.get_req_str("Instance")).is_available();
2✔
293
      }
294
};
295

296
   #if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
297
class SLH_DSA_X509_Tests : public Test {
×
298
   public:
299
      SLH_DSA_X509_Tests() = default;
1✔
300

301
      std::vector<Test::Result> run() override {
1✔
302
         Test::Result result("SLH-DSA-X.509");
1✔
303
         const Botan::X509_Certificate cert(Test::data_file("x509/slh-dsa/slh-dsa-rfc-9909-cert.pem"));
2✔
304
         auto ver_res = cert.verify_signature(*cert.subject_public_key());
1✔
305
         result.test_is_true("signature of certificate verifies", ver_res.first == Botan::Certificate_Status_Code::OK);
1✔
306
         return std::vector<Test::Result>({result});
4✔
307
      }
2✔
308
};
309
   #endif
310

311
BOTAN_REGISTER_TEST("pubkey", "sphincsplus", SPHINCS_Plus_Test);
312
BOTAN_REGISTER_TEST("pubkey", "slh_dsa", SLH_DSA_Test);
313
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_keygen", SPHINCS_Plus_Keygen_Tests);
314
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_sign_generic", Generic_SlhDsa_Signature_Tests);
315
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_verify_generic", Generic_SlhDsa_Verification_Tests);
316
   #if defined(BOTAN_HAS_X509_CERTIFICATES) && defined(BOTAN_TARGET_OS_HAS_FILESYSTEM)
317
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_x509", SLH_DSA_X509_Tests);
318
   #endif
319

320
}  // namespace
321

322
}  // namespace Botan_Tests
323

324
#endif  // BOTAN_HAS_SPHINCS_PLUS
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