Botan 3.0.0
Crypto and TLS for C&
xmss_address.h
Go to the documentation of this file.
1/*
2 * XMSS Address
3 * (C) 2016 Matthias Gierlings
4 *
5 * Botan is released under the Simplified BSD License (see license.txt)
6 **/
7
8#ifndef BOTAN_XMSS_ADDRESS_H_
9#define BOTAN_XMSS_ADDRESS_H_
10
11#include <botan/types.h>
12#include <botan/secmem.h>
13
14namespace Botan {
15
16/**
17 * Generic XMSS Address type holding 256 Bits of data. Properties
18 * of all three address formats L-Tree-Address, Hash-Tree-Address,
19 * OTS-Hash-Address can be called depending on the type currently
20 * assigned to the XMSS address using set_type().
21 **/
23 {
24 public:
25 /**
26 * Distinct types an XMSS_Address can represent. The available types
27 * are specified in [1] - 2.5 Hash Function Address Scheme.
28 **/
29 enum class Type : uint8_t
30 {
31 None = 255,
33 LTree_Address = 1,
35 };
36
37 /**
38 * The available modes for an XMSS Address:
39 * - Key_Mode: Used to generate the key.
40 * - Mask_Mode: Sets the n-byte bitmask (OTS-Hash-Address)
41 * - Mask_MSB_Mode: Used to generate the b most significant bytes of
42 * the 2n-byte bitmask (LTree Address and Hash Tree Address).
43 * - Mask_LSB_Mode: Used to generated the b least significant bytes
44 * of the 2n-byte bitmask. (LTree Address and Hash Tree Address).
45 **/
46 enum class Key_Mask : uint8_t
47 {
48 Key_Mode = 0,
49 Mask_Mode = 1,
50 Mask_MSB_Mode = 1,
52 };
53
54 /**
55 * Layer Address for XMSS is constantly zero and can not be changed this
56 * property is only of relevance to XMSS_MT.
57 *
58 * @return Layer address, which is constant 0 for XMSS.
59 **/
60 uint8_t get_layer_addr() const { return 0; }
61
62 /**
63 * Layer Address for XMSS is constantly zero and can not be changed this
64 * property is only of relevance to XMSS_MT. Calling this method for
65 * XMSS will result in an error.
66 **/
68 {
69 BOTAN_ASSERT(false, "Only available in XMSS_MT.");
70 }
71
72 /**
73 * Tree Address for XMSS is constantly zero and can not be changed this
74 * property is only of relevance to XMSS_MT.
75 *
76 * @return Tree address, which is constant 0 for XMSS.
77 **/
78 uint64_t get_tree_addr() const { return 0; }
79
80 /**
81 * Tree Address for XMSS is constantly zero and can not be changed this
82 * property is only of relevance to XMSS_MT. Calling this method for
83 * XMSS will result in an error.
84 **/
86 {
87 BOTAN_ASSERT(false, "Only available in XMSS_MT.");
88 }
89
90 /**
91 * retrieves the logical type currently assigned to the XMSS Address
92 * instance.
93 *
94 * @return Type of the address (OTS_Hash_Address, LTree_Address or
95 * Hash_Tree_Address)
96 **/
97 Type get_type() const
98 {
99 return static_cast<Type>(m_data[15]);
100 }
101
102 /**
103 * Changes the logical type currently assigned to the XMSS Address
104 * instance. Please note that changing the type will automatically
105 * reset the 128 LSBs of the Address to zero. This affects the
106 * key_mask_mode property as well as all properties identified by
107 * XMSS_Address::Property.
108 *
109 * @param type Type that shall be assigned to the address
110 * (OTS_Hash_Address, LTree_Address or Hash_Tree_Address)
111 **/
112 void set_type(Type type)
113 {
114 m_data[15] = static_cast<uint8_t>(type);
115 std::fill(m_data.begin() + 16, m_data.end(), static_cast<uint8_t>(0));
116 }
117
118 /**
119 * Retrieves the mode the address is currently set to. (See
120 * XMSS_Address::Key_Mask for details.)
121 *
122 * @return currently active mode
123 **/
125 {
126 return Key_Mask(m_data[31]);
127 }
128
129 /**
130 * Changes the mode the address currently used address mode.
131 * (XMSS_Address::Key_Mask for details.)
132 *
133 * @param value Target mode.
134 **/
136 {
139 "Invalid Key_Mask for current XMSS_Address::Type.");
140 m_data[31] = static_cast<uint8_t>(value);
141 }
142
143 /**
144 * Retrieve the index of the OTS key pair within the tree. A call to
145 * this method is only valid, if the address type is set to
146 * Type::OTS_Hash_Address.
147 *
148 * @return index of OTS key pair.
149 **/
150 uint32_t get_ots_address() const
151 {
153 "get_ots_address() requires XMSS_Address::Type::"
154 "OTS_Hash_Address.");
155 return get_hi32(2);
156 }
157
158 /**
159 * Sets the index of the OTS key pair within the tree. A call to this
160 * method is only valid, if the address type is set to
161 * Type::OTS_Hash_Address.
162 *
163 * @param value index of OTS key pair.
164 **/
165 void set_ots_address(uint32_t value)
166 {
168 "set_ots_address() requires XMSS_Address::Type::"
169 "OTS_Hash_Address.");
170 set_hi32(2, value);
171 }
172
173 /**
174 * Retrieves the index of the leaf computed with this LTree. A call to
175 * this method is only valid, if the address type is set to
176 * Type::LTree_Address.
177 *
178 * @return index of the leaf.
179 **/
180 uint32_t get_ltree_address() const
181 {
183 "set_ltree_address() requires XMSS_Address::Type::"
184 "LTree_Address.");
185 return get_hi32(2);
186 }
187
188 /**
189 * Sets the index of the leaf computed with this LTree. A call to this
190 * method is only valid, if the address type is set to
191 * Type::LTree_Address.
192 *
193 * @param value index of the leaf.
194 **/
195 void set_ltree_address(uint32_t value)
196 {
198 "set_ltree_address() requires XMSS_Address::Type::"
199 "LTree_Address.");
200 set_hi32(2, value);
201 }
202
203 /**
204 * Retrieve the chain address. A call to this method is only valid, if
205 * the address type is set to Type::OTS_Hash_Address.
206 *
207 * @return chain address.
208 **/
209 uint32_t get_chain_address() const
210 {
212 "get_chain_address() requires XMSS_Address::Type::"
213 "OTS_Hash_Address.");
214 return get_lo32(2);
215 }
216
217 /**
218 * Set the chain address. A call to this method is only valid, if
219 * the address type is set to Type::OTS_Hash_Address.
220 **/
221 void set_chain_address(uint32_t value)
222 {
224 "set_chain_address() requires XMSS_Address::Type::"
225 "OTS_Hash_Address.");
226 set_lo32(2, value);
227 }
228
229 /**
230 * Retrieves the height of the tree node to be computed within the
231 * tree. A call to this method is only valid, if the address type is
232 * set to Type::LTree_Address or Type::Hash_Tree_Address.
233 *
234 * @return height of the tree node.
235 **/
236 uint32_t get_tree_height() const
237 {
240 "get_tree_height() requires XMSS_Address::Type::"
241 "LTree_Address or XMSS_Address::Type::Hash_Tree_Address.");
242 return get_lo32(2);
243 }
244
245 /**
246 * Sets the height of the tree node to be computed within the
247 * tree. A call to this method is only valid, if the address type is
248 * set to Type::LTree_Address or Type::Hash_Tree_Address.
249 *
250 * @param value height of the tree node.
251 **/
252 void set_tree_height(uint32_t value)
253 {
256 "set_tree_height() requires XMSS_Address::Type::"
257 "LTree_Address or XMSS_Address::Type::Hash_Tree_Address.");
258 set_lo32(2, value);
259 }
260
261 /**
262 * Retrieves the address of the hash function call within the chain.
263 * A call to this method is only valid, if the address type is
264 * set to Type::OTS_Hash_Address.
265 *
266 * @return address of the hash function call within chain.
267 **/
268 uint32_t get_hash_address() const
269 {
271 "get_hash_address() requires XMSS_Address::Type::"
272 "OTS_Hash_Address.");
273 return get_hi32(3);
274 }
275
276 /**
277 * Sets the address of the hash function call within the chain.
278 * A call to this method is only valid, if the address type is
279 * set to Type::OTS_Hash_Address.
280 *
281 * @param value address of the hash function call within chain.
282 **/
283 void set_hash_address(uint32_t value)
284 {
286 "set_hash_address() requires XMSS_Address::Type::"
287 "OTS_Hash_Address.");
288 set_hi32(3, value);
289 }
290
291 /**
292 * Retrieves the index of the tree node at current tree height in the
293 * tree. A call to this method is only valid, if the address type is
294 * set to Type::LTree_Address or Type::Hash_Tree_Address.
295 *
296 * @return index of the tree node at current height.
297 **/
298 uint32_t get_tree_index() const
299 {
302 "get_tree_index() requires XMSS_Address::Type::"
303 "LTree_Address or XMSS_Address::Type::Hash_Tree_Address.");
304 return get_hi32(3);
305 }
306
307 /**
308 * Sets the index of the tree node at current tree height in the
309 * tree. A call to this method is only valid, if the address type is
310 * set to Type::LTree_Address or Type::Hash_Tree_Address.
311 *
312 * @param value index of the tree node at current height.
313 **/
314 void set_tree_index(uint32_t value)
315 {
318 "set_tree_index() requires XMSS_Address::Type::"
319 "LTree_Address or XMSS_Address::Type::Hash_Tree_Address.");
320 set_hi32(3, value);
321 }
322
324 {
325 return m_data;
326 }
327
329 {
330 return m_data;
331 }
332
333 /**
334 * @return the size of an XMSS_Address
335 **/
336 size_t size() const
337 {
338 return m_data.size();
339 }
340
342 : m_data(m_address_size)
343 {
345 }
346
348 : m_data(m_address_size)
349 {
350 set_type(type);
351 }
352
354 {
355 BOTAN_ASSERT(m_data.size() == m_address_size,
356 "XMSS_Address must be of 256 bits size.");
357 }
358
359 protected:
361
362 private:
363 static const size_t m_address_size = 32;
364
365 inline uint32_t get_hi32(size_t offset) const
366 {
367 return ((0x000000FF & m_data[8 * offset + 3]) |
368 (0x000000FF & m_data[8 * offset + 2]) << 8 |
369 (0x000000FF & m_data[8 * offset + 1]) << 16 |
370 (0x000000FF & m_data[8 * offset ]) << 24);
371 }
372
373 inline void set_hi32(size_t offset, uint32_t value)
374 {
375 m_data[offset * 8 ] = ((value >> 24) & 0xFF);
376 m_data[offset * 8 + 1] = ((value >> 16) & 0xFF);
377 m_data[offset * 8 + 2] = ((value >> 8) & 0xFF);
378 m_data[offset * 8 + 3] = ((value ) & 0xFF);
379 }
380
381 inline uint32_t get_lo32(size_t offset) const
382 {
383 return ((0x000000FF & m_data[8 * offset + 7]) |
384 (0x000000FF & m_data[8 * offset + 6]) << 8 |
385 (0x000000FF & m_data[8 * offset + 5]) << 16 |
386 (0x000000FF & m_data[8 * offset + 4]) << 24);
387 }
388
389 inline void set_lo32(size_t offset, uint32_t value)
390 {
391 m_data[offset * 8 + 4] = ((value >> 24) & 0xFF);
392 m_data[offset * 8 + 5] = ((value >> 16) & 0xFF);
393 m_data[offset * 8 + 6] = ((value >> 8) & 0xFF);
394 m_data[offset * 8 + 7] = ((value ) & 0xFF);
395 }
396 };
397
398}
399
400#endif
#define BOTAN_ASSERT(expr, assertion_made)
Definition: assert.h:54
void set_hash_address(uint32_t value)
Definition: xmss_address.h:283
void set_key_mask_mode(Key_Mask value)
Definition: xmss_address.h:135
secure_vector< uint8_t > m_data
Definition: xmss_address.h:360
void set_ots_address(uint32_t value)
Definition: xmss_address.h:165
Type get_type() const
Definition: xmss_address.h:97
size_t size() const
Definition: xmss_address.h:336
uint32_t get_tree_index() const
Definition: xmss_address.h:298
uint32_t get_ots_address() const
Definition: xmss_address.h:150
void set_type(Type type)
Definition: xmss_address.h:112
uint32_t get_ltree_address() const
Definition: xmss_address.h:180
void set_chain_address(uint32_t value)
Definition: xmss_address.h:221
uint32_t get_tree_height() const
Definition: xmss_address.h:236
void set_tree_height(uint32_t value)
Definition: xmss_address.h:252
void set_tree_index(uint32_t value)
Definition: xmss_address.h:314
uint64_t get_tree_addr() const
Definition: xmss_address.h:78
uint8_t get_layer_addr() const
Definition: xmss_address.h:60
XMSS_Address(Type type)
Definition: xmss_address.h:347
uint32_t get_chain_address() const
Definition: xmss_address.h:209
const secure_vector< uint8_t > & bytes() const
Definition: xmss_address.h:323
secure_vector< uint8_t > & bytes()
Definition: xmss_address.h:328
void set_ltree_address(uint32_t value)
Definition: xmss_address.h:195
XMSS_Address(secure_vector< uint8_t > data)
Definition: xmss_address.h:353
uint32_t get_hash_address() const
Definition: xmss_address.h:268
Key_Mask get_key_mask_mode() const
Definition: xmss_address.h:124
int(* final)(unsigned char *, CTX *)
Definition: alg_id.cpp:12
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:64
Definition: bigint.h:1092