• 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

95.65
/src/tests/test_sphincsplus_wots.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)
11

12
   #include <botan/hash.h>
13
   #include <botan/hex.h>
14

15
   #include <botan/assert.h>
16
   #include <botan/sp_parameters.h>
17
   #include <botan/internal/loadstor.h>
18
   #include <botan/internal/sp_address.h>
19
   #include <botan/internal/sp_hash.h>
20
   #include <botan/internal/sp_wots.h>
21

22
namespace Botan_Tests {
23

24
class SPHINCS_Plus_WOTS_Test final : public Text_Based_Test {
×
25
   private:
26
      static std::pair<Botan::Sphincs_Address, Botan::TreeNodeIndex> read_address_and_leaf_idx(
12✔
27
         std::span<const uint8_t> address_buffer) {
28
         BOTAN_ASSERT_NOMSG(address_buffer.size() == 32);
12✔
29

30
         std::array<uint32_t, 8> adrs;
31
         for(size_t i = 0; i < 8; ++i) {
108✔
32
            adrs[i] = Botan::load_be<uint32_t>(address_buffer.data(), i);
96✔
33
         }
34

35
         return std::make_pair(Botan::Sphincs_Address(adrs), Botan::TreeNodeIndex(adrs[5]));
12✔
36
      }
37

38
   public:
39
      SPHINCS_Plus_WOTS_Test() :
1✔
40
            Text_Based_Test("pubkey/sphincsplus_wots.vec",
41
                            "SphincsParameterSet,Address,SecretSeed,PublicSeed,HashedWotsPk,Msg,HashedWotsSig") {}
2✔
42

43
      bool skip_this_test(const std::string&, const VarMap& vars) override {
12✔
44
         [[maybe_unused]] auto params = Botan::Sphincs_Parameters::create(vars.get_req_str("SphincsParameterSet"));
24✔
45
         return !params.is_available();
12✔
46
      }
47

48
      Test::Result run_one_test(const std::string&, const VarMap& vars) final {
12✔
49
         Test::Result result("SLH-DSA's WOTS+");
12✔
50

51
         auto params = Botan::Sphincs_Parameters::create(vars.get_req_str("SphincsParameterSet"));
24✔
52

53
         auto [address, leaf_idx] = read_address_and_leaf_idx(vars.get_req_bin("Address"));
24✔
54
         const auto secret_seed = Botan::SphincsSecretSeed(vars.get_req_bin("SecretSeed"));
24✔
55
         const auto public_seed = Botan::SphincsPublicSeed(vars.get_req_bin("PublicSeed"));
12✔
56
         auto hashed_pk_ref = Botan::SphincsTreeNode(vars.get_req_bin("HashedWotsPk"));
12✔
57
         const auto root_to_sign = Botan::SphincsTreeNode(vars.get_req_bin("Msg"));
12✔
58
         const auto hashed_wots_sig_ref = Botan::WotsSignature(vars.get_req_bin("HashedWotsSig"));
24✔
59

60
         auto hashes = Botan::Sphincs_Hash_Functions::create(params, public_seed);
12✔
61

62
         // Depending on the SLH-DSA's configuration the resulting WOTS+ signature is
63
         // hashed either with SHA-3 or SHA-256 to reduce the inner dependencies
64
         // on other hash function modules.
65
         auto hash_algo_spec = [&]() -> std::string {
×
66
            if(params.hash_type() == Botan::Sphincs_Hash_Type::Shake256) {
12✔
67
               return "SHA-3(256)";
6✔
68
            } else {
69
               return "SHA-256";
6✔
70
            }
71
         }();
12✔
72
         auto hash = Botan::HashFunction::create(hash_algo_spec);
12✔
73

74
         // Addresses used for signing
75
         auto leaf_addr = Botan::Sphincs_Address::as_subtree_from(address);
12✔
76
         auto pk_addr_sign_and_pkgen = Botan::Sphincs_Address::as_subtree_from(address).set_type(
12✔
77
            Botan::Sphincs_Address_Type::WotsPublicKeyCompression);
12✔
78

79
         // Address used for hashing the WOTS+ public key
80
         auto pk_addr_pk_from_sig = Botan::Sphincs_Address::as_subtree_from(address).set_type(
12✔
81
            Botan::Sphincs_Address_Type::WotsPublicKeyCompression);
12✔
82
         pk_addr_pk_from_sig.set_keypair_address(leaf_idx);
12✔
83

84
         // Prepare the message
85
         auto wots_steps = Botan::chain_lengths(root_to_sign, params);
12✔
86

87
         // Test: WOTS+ Signature and Public Key Generation
88
         Botan::WotsSignature sig_out(params.n() * params.wots_len());
12✔
89
         Botan::SphincsTreeNode hashed_pk_out(params.n());
12✔
90
         wots_sign_and_pkgen(Botan::StrongSpan<Botan::WotsSignature>(sig_out),
12✔
91
                             Botan::StrongSpan<Botan::SphincsTreeNode>(hashed_pk_out),
92
                             secret_seed,
93
                             leaf_idx,
94
                             leaf_idx,
95
                             wots_steps,
96
                             leaf_addr,
97
                             pk_addr_sign_and_pkgen,
98
                             params,
99
                             *hashes);
12✔
100

101
         result.test_is_eq("WOTS+ signature generation", hash->process(sig_out), hashed_wots_sig_ref.get());
24✔
102
         result.test_is_eq("WOTS+ public key generation", hashed_pk_out, hashed_pk_ref);
12✔
103

104
         // Test: Create PK from signature (Verification)
105
         Botan::WotsPublicKey wots_pk_from_sig =
12✔
106
            Botan::wots_public_key_from_signature(root_to_sign, sig_out, address, params, *hashes);
12✔
107

108
         // The WOTS+ PK is hashed like for creating a leaf.
109
         result.test_is_eq("WOTS+ public key from signature",
24✔
110
                           hashes->T<Botan::SphincsTreeNode>(pk_addr_pk_from_sig, wots_pk_from_sig),
12✔
111
                           hashed_pk_ref);
112

113
         return result;
12✔
114
      }
132✔
115
};
116

117
BOTAN_REGISTER_TEST("pubkey", "sphincsplus_wots", SPHINCS_Plus_WOTS_Test);
118

119
}  // namespace Botan_Tests
120

121
#endif  // BOTAN_HAS_SPHINCS_PLUS_COMMON
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