Botan 3.6.1
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
20enum class Sphincs_Address_Type : uint32_t {
21 WotsHash = 0,
23 HashTree = 2,
24 ForsTree = 3,
28};
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
50 m_address.fill(0);
51 set_type(type);
52 }
53
54 Sphincs_Address(std::array<uint32_t, 8> address) { std::copy(address.begin(), address.end(), m_address.begin()); }
55
56 /* Setter member functions as specified in FIPS 205, Section 4.3 */
57
59 m_address[layer_offset] = layer.get();
60 return *this;
61 }
62
64 m_address[tree_offset + 0] = 0; // not required by all current instances
65 m_address[tree_offset + 1] = static_cast<uint32_t>(tree.get() >> 32);
66 m_address[tree_offset + 2] = static_cast<uint32_t>(tree.get());
67 return *this;
68 }
69
70 /*
71 * Sets the type without clearing the other fields (contrary to the specs setTypeAndClear).
72 * This adaption is used for optimization purposes.
73 */
75 m_address[type_offset] = static_cast<uint32_t>(type);
76 return *this;
77 }
78
80 m_address[keypair_offset] = keypair.get();
81 return *this;
82 }
83
85 m_address[chain_offset] = chain.get();
86 return *this;
87 }
88
90 m_address[tree_height_offset] = tree_height.get();
91 return *this;
92 }
93
95 m_address[hash_offset] = hash.get();
96 return *this;
97 }
98
100 m_address[tree_index_offset] = tree_index.get();
101 return *this;
102 }
103
104 /* Custom helper member functions */
105
107 m_address[layer_offset] = other.m_address[layer_offset];
108 m_address[tree_offset + 0] = other.m_address[tree_offset + 0];
109 m_address[tree_offset + 1] = other.m_address[tree_offset + 1];
110 m_address[tree_offset + 2] = other.m_address[tree_offset + 2];
111
112 return *this;
113 }
114
116 auto result = Sphincs_Address({0, 0, 0, 0, 0, 0, 0, 0});
117 result.copy_subtree_from(other);
118 return result;
119 }
120
122 m_address[layer_offset] = other.m_address[layer_offset];
123 m_address[tree_offset + 0] = other.m_address[tree_offset + 0];
124 m_address[tree_offset + 1] = other.m_address[tree_offset + 1];
125 m_address[tree_offset + 2] = other.m_address[tree_offset + 2];
126 m_address[keypair_offset] = other.m_address[keypair_offset];
127
128 return *this;
129 }
130
132 Sphincs_Address result({0, 0, 0, 0, 0, 0, 0, 0});
133 result.copy_keypair_from(other);
134 return result;
135 }
136
137 Sphincs_Address_Type get_type() const { return Sphincs_Address_Type(m_address[type_offset]); }
138
139 std::array<uint8_t, 32> to_bytes() const {
140 std::array<uint8_t, sizeof(m_address)> result;
141 for(unsigned int i = 0; i < m_address.size(); ++i) {
142 store_be(m_address[i], result.data() + (i * 4));
143 }
144 return result;
145 }
146
147 std::array<uint8_t, 22> to_bytes_compressed() const {
148 std::array<uint8_t, 22> result;
149
150 result[0] = static_cast<uint8_t>(m_address[layer_offset]);
151 store_be(m_address[tree_offset + 1], &result[1]);
152 store_be(m_address[tree_offset + 2], &result[5]);
153 result[9] = static_cast<uint8_t>(m_address[type_offset]);
154 store_be(m_address[keypair_offset], &result[10]);
155 store_be(m_address[chain_offset], &result[14]);
156 store_be(m_address[hash_offset], &result[18]);
157
158 return result;
159 }
160
161 private:
162 std::array<uint32_t, 8> m_address;
163};
164
165} // namespace Botan
166
167#endif
Sphincs_Address_Type get_type() const
Definition sp_address.h:137
Sphincs_Address & set_layer_address(HypertreeLayerIndex layer)
Definition sp_address.h:58
Sphincs_Address & set_tree_address(XmssTreeIndexInLayer tree)
Definition sp_address.h:63
Sphincs_Address & set_chain_address(WotsChainIndex chain)
Definition sp_address.h:84
Sphincs_Address & copy_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:106
std::array< uint8_t, 22 > to_bytes_compressed() const
Definition sp_address.h:147
Sphincs_Address & set_hash_address(WotsHashIndex hash)
Definition sp_address.h:94
Sphincs_Address & set_tree_height(TreeLayerIndex tree_height)
Definition sp_address.h:89
Sphincs_Address & set_tree_index(TreeNodeIndex tree_index)
Definition sp_address.h:99
static Sphincs_Address as_subtree_from(const Sphincs_Address &other)
Definition sp_address.h:115
Sphincs_Address & set_keypair_address(TreeNodeIndex keypair)
Definition sp_address.h:79
Sphincs_Address(Sphincs_Address_Type type)
Definition sp_address.h:49
Sphincs_Address & copy_keypair_from(const Sphincs_Address other)
Definition sp_address.h:121
Sphincs_Address(std::array< uint32_t, 8 > address)
Definition sp_address.h:54
Sphincs_Address & set_type(Sphincs_Address_Type type)
Definition sp_address.h:74
static Sphincs_Address as_keypair_from(const Sphincs_Address &other)
Definition sp_address.h:131
std::array< uint8_t, 32 > to_bytes() const
Definition sp_address.h:139
constexpr T & get() &
Definition strong_type.h:50
int(* final)(unsigned char *, CTX *)
#define BOTAN_TEST_API
Definition compiler.h:51
Sphincs_Address_Type
Definition sp_address.h:20
constexpr auto store_be(ParamTs &&... params)
Definition loadstor.h:773