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

randombit / botan / 19012754211

02 Nov 2025 01:10PM UTC coverage: 90.677% (+0.006%) from 90.671%
19012754211

push

github

web-flow
Merge pull request #5137 from randombit/jack/clang-tidy-includes

Remove various unused includes flagged by clang-tidy misc-include-cleaner

100457 of 110786 relevant lines covered (90.68%)

12189873.8 hits per line

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

93.08
/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 "test_rng.h"
9
#include "tests.h"
10

11
#if defined(BOTAN_HAS_SPHINCS_PLUS_COMMON) && defined(BOTAN_HAS_AES)
12

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

21
   #include "test_pubkey.h"
22

23
namespace Botan_Tests {
24

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

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

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

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

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

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

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

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

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

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

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

118
         result.test_is_eq("public key bits", priv_key.public_key_bits(), pk_ref);
48✔
119
         result.test_is_eq("private key bits", unlock(priv_key.private_key_bits()), sk_ref);
96✔
120

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

125
         result.test_is_eq("signature creation randomized", unlock(hash->process(signature_rand)), sig_rand_hash);
96✔
126

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

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

139
            result.test_is_eq("signature creation deterministic", unlock(hash->process(signature_det)), *sig_det_hash);
48✔
140

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

146
         // Verification with generated Keypair
147

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

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

161
            result.test_is_eq("signature creation after deserialization",
12✔
162
                              unlock(hash->process(deserialized_signature)),
12✔
163
                              sig_rand_hash);
164

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

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

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

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

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

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

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

199
class SPHINCS_Plus_Keygen_Tests final : public PK_Key_Generation_Test {
×
200
   public:
201
      std::vector<std::string> keygen_params() const override {
1✔
202
         const std::vector<std::string> short_test_params = {
1✔
203
            "SphincsPlus-shake-128s-r3.1",
204
            "SLH-DSA-SHAKE-128s",
205
            "SphincsPlus-sha2-128f-r3.1",
206
            "SLH-DSA-SHA2-128f",
207
         };
1✔
208
         const std::vector<std::string> all_params = {
1✔
209
            "SphincsPlus-shake-128s-r3.1", "SphincsPlus-shake-128f-r3.1", "SphincsPlus-shake-192s-r3.1",
210
            "SphincsPlus-shake-192f-r3.1", "SphincsPlus-shake-256s-r3.1", "SphincsPlus-shake-256f-r3.1",
211
            "SLH-DSA-SHAKE-128s",          "SLH-DSA-SHAKE-128f",          "SLH-DSA-SHAKE-192s",
212
            "SLH-DSA-SHAKE-192f",          "SLH-DSA-SHAKE-256s",          "SLH-DSA-SHAKE-256f",
213
            "SphincsPlus-sha2-128s-r3.1",  "SphincsPlus-sha2-128f-r3.1",  "SphincsPlus-sha2-192s-r3.1",
214
            "SphincsPlus-sha2-192f-r3.1",  "SphincsPlus-sha2-256s-r3.1",  "SphincsPlus-sha2-256f-r3.1",
215
            "SLH-DSA-SHA2-128s",           "SLH-DSA-SHA2-128f",           "SLH-DSA-SHA2-192s",
216
            "SLH-DSA-SHA2-192f",           "SLH-DSA-SHA2-256s",           "SLH-DSA-SHA2-256f",
217
         };
1✔
218
         const auto& tested_params = Test::run_long_tests() ? all_params : short_test_params;
1✔
219
         std::vector<std::string> available_params;
1✔
220
         std::copy_if(tested_params.begin(),
1✔
221
                      tested_params.end(),
222
                      std::back_inserter(available_params),
223
                      [](const std::string& param) { return Botan::Sphincs_Parameters::create(param).is_available(); });
24✔
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";
6✔
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