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

randombit / botan / 11331525401

14 Oct 2024 04:29PM UTC coverage: 91.093% (-0.03%) from 91.12%
11331525401

Pull #4291

github

web-flow
Merge f5ffe99f5 into ed74c9542
Pull Request #4291: PQC: SLH-DSA

90346 of 99180 relevant lines covered (91.09%)

9678761.99 hits per line

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

93.02
/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/pk_algs.h>
16
   #include <botan/pubkey.h>
17
   #include <botan/secmem.h>
18
   #include <botan/sp_parameters.h>
19
   #include <botan/sphincsplus.h>
20
   #include <botan/internal/loadstor.h>
21
   #include <botan/internal/sp_hash.h>
22

23
   #include "test_pubkey.h"
24

25
namespace Botan_Tests {
26

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

148
         // Verification with generated Keypair
149

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

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

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

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

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

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

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

187
         return result;
48✔
188
      }
240✔
189
};
190

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

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

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

229
      std::string algo_name() const override { return "SLH-DSA"; }
72✔
230

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

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

245
      bool clear_between_callbacks() const override { return false; }
1✔
246

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

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

258
         return sk;
1✔
259
      }
3✔
260

261
      std::string default_padding(const VarMap&) const override { return ""; }
1✔
262

263
      bool skip_this_test(const std::string&, const VarMap& vars) override {
1✔
264
         return !Botan::Sphincs_Parameters::create(vars.get_req_str("Instance")).is_available();
1✔
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") {}
2✔
273

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

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

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

283
      std::string default_padding(const VarMap&) const override { return ""; }
1✔
284

285
      bool skip_this_test(const std::string&, const VarMap& vars) override {
1✔
286
         return !Botan::Sphincs_Parameters::create(vars.get_req_str("Instance")).is_available();
1✔
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

© 2025 Coveralls, Inc