Botan 3.9.0
Crypto and TLS for C&
sp_address.h
Go to the documentation of this file.
1/*
2 * SLH-DSA Address
3 * (C) 2023 Jack Lloyd
4 * 2023 Fabian Albert, René Meusel, Amos Treiber - Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 */
8
9#ifndef BOTAN_SPHINCS_PLUS_ADDRESS_H_
10#define BOTAN_SPHINCS_PLUS_ADDRESS_H_
11
12#include <array>
13
14#include <botan/hash.h>
15#include <botan/internal/loadstor.h>
16#include <botan/internal/sp_types.h>
17
18namespace Botan {
19
29
30/**
31 * Representation of a SLH-DSA hash function address as specified in
32 * FIPS 205, Section 4.2
33 */
35 private:
36 // Offsets of the address fields in the address array. Counted in 32-bit words.
37 static constexpr size_t layer_offset = 0;
38 static constexpr size_t tree_offset = 1; // tree address is 3 words wide
39 static constexpr size_t type_offset = 4;
40 static constexpr size_t keypair_offset = 5;
41 static constexpr size_t chain_offset = 6;
42 static constexpr size_t hash_offset = 7;
43 static constexpr size_t tree_height_offset = chain_offset;
44 static constexpr size_t tree_index_offset = hash_offset;
45
46 public:
47 using enum Sphincs_Address_Type;
48
49 explicit Sphincs_Address(Sphincs_Address_Type type) : m_address{} { set_type(type); }
50
51 explicit Sphincs_Address(std::array<uint32_t, 8> address) : m_address{} {
52 std::copy(address.begin(), address.end(), m_address.begin());
53 }
54
55 /* Setter member functions as specified in FIPS 205, Section 4.3 */
56
58 m_address[layer_offset] = layer.get();
59 return *this;
60 }
61
63 m_address[tree_offset + 0] = 0; // not required by all current instances
64 m_address[tree_offset + 1] = static_cast<uint32_t>(tree.get() >> 32);
65 m_address[tree_offset + 2] = static_cast<uint32_t>(tree.get());
66 return *this;
67 }
68
69 /*
70 * Sets the type without clearing the other fields (contrary to the specs setTypeAndClear).
71 * This adaption is used for optimization purposes.
72 */
74 m_address[type_offset] = static_cast<uint32_t>(type);
75 return *this;
76 }
77
79 m_address[keypair_offset] = keypair.get();
80 return *this;
81 }
82
84 m_address[chain_offset] = chain.get();
85 return *this;
86 }
87
89 m_address[tree_height_offset] = tree_height.get();
90 return *this;
91 }
92
94 m_address[hash_offset] = hash.get();
95 return *this;
96 }
97
99 m_address[tree_index_offset] = tree_index.get();
100 return *this;
101 }
102
103 /* Custom helper member functions */
104
106 m_address[layer_offset] = other.m_address[layer_offset];
107 m_address[tree_offset + 0] = other.m_address[tree_offset + 0];
108 m_address[tree_offset + 1] = other.m_address[tree_offset + 1];
109 m_address[tree_offset + 2] = other.m_address[tree_offset + 2];
110
111 return *this;
112 }
113
115 auto result = Sphincs_Address({0, 0, 0, 0, 0, 0, 0, 0});
116 result.copy_subtree_from(other);
117 return result;
118 }
119
121 m_address[layer_offset] = other.m_address[layer_offset];
122 m_address[tree_offset + 0] = other.m_address[tree_offset + 0];
123 m_address[tree_offset + 1] = other.m_address[tree_offset + 1];
124 m_address[tree_offset + 2] = other.m_address[tree_offset + 2];
125 m_address[keypair_offset] = other.m_address[keypair_offset];
126
127 return *this;
128 }
129
131 Sphincs_Address result({0, 0, 0, 0, 0, 0, 0, 0});
132 result.copy_keypair_from(other);
133 return result;
134 }
135
136 Sphincs_Address_Type get_type() const { return Sphincs_Address_Type(m_address[type_offset]); }
137
138 std::array<uint8_t, 32> to_bytes() const {
139 std::array<uint8_t, sizeof(m_address)> result{};
140 for(unsigned int i = 0; i < m_address.size(); ++i) {
141 store_be(m_address[i], result.data() + (i * 4));
142 }
143 return result;
144 }
145
146 std::array<uint8_t, 22> to_bytes_compressed() const {
147 std::array<uint8_t, 22> result{};
148
149 result[0] = static_cast<uint8_t>(m_address[layer_offset]);
150 store_be(m_address[tree_offset + 1], &result[1]);
151 store_be(m_address[tree_offset + 2], &result[5]);
152 result[9] = static_cast<uint8_t>(m_address[type_offset]);
153 store_be(m_address[keypair_offset], &result[10]);
154 store_be(m_address[chain_offset], &result[14]);
155 store_be(m_address[hash_offset], &result[18]);
156
157 return result;
158 }
159
160 private:
161 std::array<uint32_t, 8> m_address;
162};
163
164} // namespace Botan
165
166#endif
#define BOTAN_TEST_API
Definition api.h:41
Sphincs_Address_Type get_type() const
Definition sp_address.h:136
Sphincs_Address & set_layer_address(HypertreeLayerIndex layer)
Definition sp_address.h:57
Sphincs_Address & set_tree_address(XmssTreeIndexInLayer tree)
Definition sp_address.h:62
Sphincs_Address & set_chain_address(WotsChainIndex chain)
Definition sp_address.h:83
Sphincs_Address & copy_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:105
std::array< uint8_t, 22 > to_bytes_compressed() const
Definition sp_address.h:146
Sphincs_Address & set_hash_address(WotsHashIndex hash)
Definition sp_address.h:93
Sphincs_Address & set_tree_height(TreeLayerIndex tree_height)
Definition sp_address.h:88
Sphincs_Address & set_tree_index(TreeNodeIndex tree_index)
Definition sp_address.h:98
static Sphincs_Address as_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:114
Sphincs_Address & set_keypair_address(TreeNodeIndex keypair)
Definition sp_address.h:78
Sphincs_Address(Sphincs_Address_Type type)
Definition sp_address.h:49
Sphincs_Address & copy_keypair_from(const Sphincs_Address other)
Definition sp_address.h:120
Sphincs_Address(std::array< uint32_t, 8 > address)
Definition sp_address.h:51
Sphincs_Address & set_type(Sphincs_Address_Type type)
Definition sp_address.h:73
static Sphincs_Address as_keypair_from(const Sphincs_Address &other)
Definition sp_address.h:130
std::array< uint8_t, 32 > to_bytes() const
Definition sp_address.h:138
constexpr T & get() &
Definition strong_type.h:52
Sphincs_Address_Type
Definition sp_address.h:20
Strong< uint64_t, struct XmssTreeIndexInLayer_, EnableArithmeticWithPlainNumber > XmssTreeIndexInLayer
Index of an XMSS tree (unique for just the local hyper-tree layer)
Definition sp_types.h:89
Strong< uint8_t, struct WotsHashIndex_, EnableArithmeticWithPlainNumber > WotsHashIndex
Index of a hash application inside a single WOTS chain (integers in "base_w")
Definition sp_types.h:98
Strong< uint32_t, struct WotsChainIndex_ > WotsChainIndex
Index of a WOTS chain within a single usage of WOTS.
Definition sp_types.h:95
Strong< uint32_t, struct TreeNodeIndex_, EnableArithmeticWithPlainNumber > TreeNodeIndex
Index of an individual node inside an XMSS or FORS tree.
Definition sp_types.h:92
Strong< uint32_t, struct TreeLayerIndex_, EnableArithmeticWithPlainNumber > TreeLayerIndex
Index of the layer within a FORS/XMSS tree.
Definition sp_types.h:83
Strong< uint32_t, struct HypertreeLayerIndex_ > HypertreeLayerIndex
Index of a layer in the XMSS hyper-tree.
Definition sp_types.h:86
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:745