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

randombit / botan / 22074725248

16 Feb 2026 07:06PM UTC coverage: 90.046% (+0.002%) from 90.044%
22074725248

Pull #5350

github

web-flow
Merge d4af29038 into cff0123e7
Pull Request #5350: IWYU - Clean up unused includes and forward declarations

102337 of 113650 relevant lines covered (90.05%)

11509204.97 hits per line

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

93.94
/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
   #include "test_pubkey.h"
20
   #include "test_rng.h"
21

22
namespace Botan_Tests {
23

24
/**
25
 * Test all implemented SLH-DSA instances using the data of the KAT files.
26
 */
27
class SPHINCS_Plus_Test_Base : public Text_Based_Test {
×
28
   public:
29
      explicit SPHINCS_Plus_Test_Base(std::string_view kat_path) :
2✔
30
            Text_Based_Test(std::string(kat_path), "SphincsParameterSet,seed,pk,sk,msg,HashSigRand", "HashSigDet") {}
6✔
31

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

35
         if(!params.is_available()) {
24✔
36
            return true;
37
         }
38

39
   #if not defined(BOTAN_HAS_SHA3)
40
         // The SLH-DSA shake-based signatures are hashed with SHA-3 in the test file.
41
         if(params.hash_type() == Botan::Sphincs_Hash_Type::Shake256) {
42
            return true;
43
         }
44
   #endif
45

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

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

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

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

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

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

114
         // Generate Keypair
115
         const Botan::SphincsPlus_PrivateKey priv_key(fixed_rng, params);
24✔
116

117
         result.test_bin_eq("public key bits", priv_key.public_key_bits(), pk_ref);
24✔
118
         result.test_bin_eq("private key bits", priv_key.private_key_bits(), sk_ref);
24✔
119

120
         // Signature roundtrip (Randomized mode)
121
         auto signer_rand = Botan::PK_Signer(priv_key, fixed_rng, "Randomized");
24✔
122
         auto signature_rand = signer_rand.sign_message(msg_ref.data(), msg_ref.size(), fixed_rng);
24✔
123

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

126
         Botan::PK_Verifier verifier(*priv_key.public_key(), params.algorithm_identifier());
48✔
127
         const bool verify_success =
24✔
128
            verifier.verify_message(msg_ref.data(), msg_ref.size(), signature_rand.data(), signature_rand.size());
24✔
129
         result.test_is_true("verification of valid randomized signature", verify_success);
24✔
130

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

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

140
            auto verify_success_det =
12✔
141
               verifier.verify_message(msg_ref.data(), msg_ref.size(), signature_det.data(), signature_det.size());
12✔
142
            result.test_is_true("verification of valid deterministic signature", verify_success_det);
12✔
143
         }
12✔
144

145
         // Verification with generated Keypair
146

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

156
            // Signature with deserialized Keypair
157
            auto deserialized_signer = Botan::PK_Signer(deserialized_priv_key, fixed_rng, "Randomized");
4✔
158
            auto deserialized_signature = deserialized_signer.sign_message(msg_ref.data(), msg_ref.size(), fixed_rng);
4✔
159

160
            result.test_bin_eq(
4✔
161
               "signature creation after deserialization", hash->process(deserialized_signature), sig_rand_hash);
4✔
162

163
            // Verification with deserialized Keypair
164
            Botan::PK_Verifier deserialized_verifier(deserialized_pub_key, params.algorithm_identifier());
4✔
165
            const bool verify_success_deserialized = deserialized_verifier.verify_message(
4✔
166
               msg_ref.data(), msg_ref.size(), signature_rand.data(), signature_rand.size());
4✔
167
            result.test_is_true("verification of valid signature after deserialization", verify_success_deserialized);
4✔
168

169
            // Verification of invalid signature
170
            auto broken_sig = Test::mutate_vec(deserialized_signature, this->rng());
4✔
171
            const bool verify_fail = deserialized_verifier.verify_message(
4✔
172
               msg_ref.data(), msg_ref.size(), broken_sig.data(), broken_sig.size());
4✔
173
            result.test_is_true("verification of invalid signature", !verify_fail);
4✔
174

175
            const bool verify_success_after_fail = deserialized_verifier.verify_message(
4✔
176
               msg_ref.data(), msg_ref.size(), signature_rand.data(), signature_rand.size());
4✔
177
            result.test_is_true("verification of valid signature after broken signature", verify_success_after_fail);
4✔
178
         }
8✔
179

180
         // Misc
181
         result.test_is_true("parameter serialization works",
48✔
182
                             params.to_string() == vars.get_req_str("SphincsParameterSet"));
48✔
183

184
         return result;
48✔
185
      }
240✔
186
};
187

188
class SPHINCS_Plus_Test final : public SPHINCS_Plus_Test_Base {
×
189
   public:
190
      SPHINCS_Plus_Test() : SPHINCS_Plus_Test_Base("pubkey/sphincsplus.vec") {}
1✔
191
};
192

193
class SLH_DSA_Test final : public SPHINCS_Plus_Test_Base {
×
194
   public:
195
      SLH_DSA_Test() : SPHINCS_Plus_Test_Base("pubkey/slh_dsa.vec") {}
1✔
196
};
197

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

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

229
      std::unique_ptr<Botan::Public_Key> public_key_from_raw(std::string_view keygen_params,
24✔
230
                                                             std::string_view /* provider */,
231
                                                             std::span<const uint8_t> raw_pk) const override {
232
         return std::make_unique<Botan::SphincsPlus_PublicKey>(raw_pk,
24✔
233
                                                               Botan::Sphincs_Parameters::create(keygen_params));
24✔
234
      }
235
};
236

237
class Generic_SlhDsa_Signature_Tests final : public PK_Signature_Generation_Test {
238
   public:
239
      Generic_SlhDsa_Signature_Tests() :
1✔
240
            PK_Signature_Generation_Test(
241
               "SLH-DSA", "pubkey/slh_dsa_generic.vec", "Instance,Msg,PrivateKey,PublicKey,Valid,Signature", "Nonce") {}
2✔
242

243
      bool clear_between_callbacks() const override { return false; }
2✔
244

245
      std::unique_ptr<Botan::Private_Key> load_private_key(const VarMap& vars) override {
2✔
246
         const std::string instance = vars.get_req_str("Instance");
2✔
247
         const std::vector<uint8_t> privkey = vars.get_req_bin("PrivateKey");
2✔
248
         const std::vector<uint8_t> pubkey = vars.get_req_bin("PublicKey");
2✔
249
         auto sk =
2✔
250
            std::make_unique<Botan::SphincsPlus_PrivateKey>(privkey, Botan::Sphincs_Parameters::create(instance));
2✔
251

252
         if(sk->public_key_bits() != pubkey) {
4✔
253
            throw Test_Error("Invalid SLH-DSA key in test data");
×
254
         }
255

256
         return sk;
2✔
257
      }
6✔
258

259
      std::string default_padding(const VarMap& vars) const override {
2✔
260
         return vars.has_key("Nonce") ? "Randomized" : "Deterministic";
5✔
261
      }
262

263
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
2✔
264
         return !Botan::Sphincs_Parameters::create(vars.get_req_str("Instance")).is_available();
2✔
265
      }
266
};
267

268
class Generic_SlhDsa_Verification_Tests final : public PK_Signature_Verification_Test {
269
   public:
270
      Generic_SlhDsa_Verification_Tests() :
1✔
271
            PK_Signature_Verification_Test(
272
               "SLH-DSA", "pubkey/slh_dsa_generic.vec", "Instance,Msg,PrivateKey,PublicKey,Valid,Signature", "Nonce") {}
2✔
273

274
      bool clear_between_callbacks() const override { return false; }
2✔
275

276
      std::unique_ptr<Botan::Public_Key> load_public_key(const VarMap& vars) override {
2✔
277
         const std::string instance = vars.get_req_str("Instance");
2✔
278
         const std::vector<uint8_t> pubkey = vars.get_req_bin("PublicKey");
2✔
279

280
         return std::make_unique<Botan::SphincsPlus_PublicKey>(pubkey, Botan::Sphincs_Parameters::create(instance));
4✔
281
      }
2✔
282

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

285
      bool skip_this_test(const std::string& /*header*/, const VarMap& vars) override {
2✔
286
         return !Botan::Sphincs_Parameters::create(vars.get_req_str("Instance")).is_available();
2✔
287
      }
288
};
289

290
BOTAN_REGISTER_TEST("pubkey", "sphincsplus", SPHINCS_Plus_Test);
291
BOTAN_REGISTER_TEST("pubkey", "slh_dsa", SLH_DSA_Test);
292
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_keygen", SPHINCS_Plus_Keygen_Tests);
293
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_sign_generic", Generic_SlhDsa_Signature_Tests);
294
BOTAN_REGISTER_TEST("pubkey", "slh_dsa_verify_generic", Generic_SlhDsa_Verification_Tests);
295

296
}  // namespace Botan_Tests
297

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