Botan 3.12.0
Crypto and TLS for C&
xmss.h
Go to the documentation of this file.
1/*
2 * XMSS Keys
3 * (C) 2016,2017 Matthias Gierlings
4 * (C) 2019 René Korthaus, Rohde & Schwarz Cybersecurity
5 *
6 * Botan is released under the Simplified BSD License (see license.txt)
7 **/
8
9#ifndef BOTAN_XMSS_H_
10#define BOTAN_XMSS_H_
11
12#include <botan/pk_keys.h>
13#include <botan/xmss_parameters.h>
14#include <memory>
15#include <span>
16
17namespace Botan {
18
20class XMSS_Address;
21class XMSS_Hash;
22class XMSS_PublicKey_Internal;
23class XMSS_PrivateKey_Internal;
27
28/**
29 * An XMSS: Extended Hash-Based Signature public key.
30 *
31 * [1] XMSS: Extended Hash-Based Signatures,
32 * Request for Comments: 8391
33 * Release: May 2018.
34 * https://datatracker.ietf.org/doc/rfc8391/
35 **/
36class BOTAN_PUBLIC_API(2, 0) XMSS_PublicKey : public virtual Public_Key {
37 public:
38 /**
39 * Creates a new XMSS public key for the chosen XMSS signature method.
40 * New public and prf seeds are generated using rng. The appropriate WOTS
41 * signature method will be automatically set based on the chosen XMSS
42 * signature method.
43 *
44 * @param xmss_oid Identifier for the selected XMSS signature method.
45 * @param rng A random number generator to use for key generation.
46 **/
48
49 /**
50 * Loads a public key.
51 *
52 * Public key must be encoded as in RFC
53 * draft-vangeest-x509-hash-sigs-03.
54 *
55 * @param key_bits DER encoded public key bits
56 */
57 BOTAN_FUTURE_EXPLICIT XMSS_PublicKey(std::span<const uint8_t> key_bits);
58
59 /**
60 * Creates a new XMSS public key for a chosen XMSS signature method as
61 * well as pre-computed root node and public_seed values.
62 *
63 * @param xmss_oid Identifier for the selected XMSS signature method.
64 * @param root Root node value.
65 * @param public_seed Public seed value.
66 **/
70
71 std::string algo_name() const override { return "XMSS"; }
72
76
77 bool check_key(RandomNumberGenerator& rng, bool strong) const override;
78
79 size_t estimated_strength() const override;
80
81 size_t key_length() const override;
82
83 /**
84 * Generates a byte sequence representing the XMSS
85 * public key, as defined in [1] (p. 23, "XMSS Public Key")
86 *
87 * @return 4-byte OID, followed by n-byte root node, followed by
88 * public seed.
89 **/
90 std::vector<uint8_t> raw_public_key_bits() const override;
91
92 /**
93 * Returns the encoded public key as defined in RFC
94 * draft-vangeest-x509-hash-sigs-03.
95 *
96 * @return encoded public key bits
97 **/
98 std::vector<uint8_t> public_key_bits() const override;
99
100 BOTAN_DEPRECATED("Use raw_public_key_bits()") std::vector<uint8_t> raw_public_key() const;
101
102 std::unique_ptr<Private_Key> generate_another(RandomNumberGenerator& rng) const final;
103
104 bool supports_operation(PublicKeyOperation op) const override { return (op == PublicKeyOperation::Signature); }
105
106 std::unique_ptr<PK_Ops::Verification> create_verification_op(std::string_view params,
107 std::string_view provider) const override;
108
109 std::unique_ptr<PK_Ops::Verification> create_x509_verification_op(const AlgorithmIdentifier& alg_id,
110 std::string_view provider) const override;
111
112 protected:
114
115 const secure_vector<uint8_t>& public_seed() const;
116
117 const secure_vector<uint8_t>& root() const;
118
119 const XMSS_Parameters& xmss_parameters() const;
120
122
123 private:
124 std::shared_ptr<XMSS_PublicKey_Internal> m_public_key;
125};
126
127template <typename>
128class Atomic;
129
130class XMSS_Index_Registry;
131
132/**
133 * Determines how WOTS+ private keys are derived from the XMSS private key
134 */
135enum class WOTS_Derivation_Method : uint8_t {
136 /// This roughly followed the suggestions in RFC 8391 but is vulnerable
137 /// to a multi-target attack. For new private keys, we recommend using
138 /// the derivation as suggested in NIST SP.800-208.
139 /// Private keys generated with Botan 2.x will need to stay with this mode,
140 /// otherwise they won't be able to generate valid signatures any longer.
142
143 /// Derivation as specified in NIST SP.800-208 to avoid a multi-target attack
144 /// on the WOTS+ key derivation suggested in RFC 8391. New private keys
145 /// should use this mode.
147};
148
149/**
150 * An XMSS: Extended Hash-Based Signature private key.
151 * The XMSS private key does not support the X509 and PKCS7 standard. Instead
152 * the raw format described in [1] is used.
153 *
154 * [1] XMSS: Extended Hash-Based Signatures,
155 * Request for Comments: 8391
156 * Release: May 2018.
157 * https://datatracker.ietf.org/doc/rfc8391/
158 **/
159
162
163class BOTAN_PUBLIC_API(2, 0) XMSS_PrivateKey final : public virtual XMSS_PublicKey,
164 public virtual Private_Key {
165 public:
166 /**
167 * Creates a new XMSS private key for the chosen XMSS signature method.
168 * New seeds for public/private key and pseudo random function input are
169 * generated using the provided RNG. The appropriate WOTS signature method
170 * will be automatically set based on the chosen XMSS signature method.
171 *
172 * @param xmss_algo_id Identifier for the selected XMSS signature method.
173 * @param rng A random number generator to use for key generation.
174 * @param wots_derivation_method The method used to derive WOTS+ private keys
175 **/
179
180 /**
181 * Creates an XMSS_PrivateKey from a byte sequence produced by
182 * raw_private_key().
183 *
184 * @param raw_key An XMSS private key serialized using raw_private_key().
185 **/
186 BOTAN_FUTURE_EXPLICIT XMSS_PrivateKey(std::span<const uint8_t> raw_key);
187
188 /**
189 * Creates a new XMSS private key for the chosen XMSS signature method
190 * using precomputed seeds for public/private keys and pseudo random
191 * function input. The appropriate WOTS signature method will be
192 * automatically set, based on the chosen XMSS signature method.
193 *
194 * @param xmss_algo_id Identifier for the selected XMSS signature method.
195 * @param idx_leaf Index of the next unused leaf.
196 * @param wots_priv_seed A seed to generate a Winternitz-One-Time-
197 * Signature private key from.
198 * @param prf a secret n-byte key sourced from a secure source
199 * of uniformly random data.
200 * @param root Root node of the binary hash tree.
201 * @param public_seed The public seed.
202 * @param wots_derivation_method The method used to derive WOTS+ private keys
203 **/
205 size_t idx_leaf,
206 secure_vector<uint8_t> wots_priv_seed,
211
212 bool stateful_operation() const override { return true; }
213
214 std::unique_ptr<Public_Key> public_key() const override;
215
216 /**
217 * Retrieves the last unused leaf index of the private key. Reusing a leaf
218 * by utilizing leaf indices lower than the last unused leaf index will
219 * compromise security.
220 *
221 * @return Index of the last unused leaf.
222 **/
223 BOTAN_DEPRECATED("Use remaining_operations()") size_t unused_leaf_index() const;
224
225 /**
226 * Retrieves the number of remaining signatures for this private key.
227 */
228 BOTAN_DEPRECATED("Use remaining_operations()") size_t remaining_signatures() const;
229
230 std::optional<uint64_t> remaining_operations() const override;
231
232 std::unique_ptr<PK_Ops::Signature> create_signature_op(RandomNumberGenerator& rng,
233 std::string_view params,
234 std::string_view provider) const override;
235
236 secure_vector<uint8_t> private_key_bits() const override;
237
238 /**
239 * Generates a non standardized byte sequence representing the XMSS
240 * private key.
241 *
242 * @return byte sequence consisting of the following elements in order:
243 * 4-byte OID, n-byte root node, n-byte public seed,
244 * 4-byte unused leaf index, n-byte prf seed, n-byte private seed.
245 * At last 1-byte that encodes the WOTS+ key derivation method.
246 **/
247 secure_vector<uint8_t> raw_private_key() const;
248
249 WOTS_Derivation_Method wots_derivation_method() const;
250
251 private:
253
254 size_t reserve_unused_leaf_index();
255
256 const secure_vector<uint8_t>& prf_value() const;
257
258 XMSS_WOTS_PublicKey wots_public_key_for(const XMSS_Address& adrs, XMSS_Hash& hash) const;
259 XMSS_WOTS_PrivateKey wots_private_key_for(const XMSS_Address& adrs, XMSS_Hash& hash) const;
260
261 /**
262 * Algorithm 9: "treeHash"
263 * Computes the internal n-byte nodes of a Merkle tree.
264 *
265 * @param start_idx The start index.
266 * @param target_node_height Height of the target node.
267 * @param adrs Address of the tree containing the target node.
268 * @param hash The hash function to use
269 *
270 * @return The root node of a tree of height target_node height with the
271 * leftmost leaf being the hash of the WOTS+ pk with index
272 * start_idx.
273 **/
274 secure_vector<uint8_t> tree_hash(size_t start_idx,
275 size_t target_node_height,
276 const XMSS_Address& adrs,
277 XMSS_Hash& hash) const;
278
279 void tree_hash_subtree(secure_vector<uint8_t>& result,
280 size_t start_idx,
281 size_t target_node_height,
282 XMSS_Address& adrs,
283 XMSS_Hash& hash) const;
284
285 std::shared_ptr<XMSS_PrivateKey_Internal> m_private;
286};
287
289
290} // namespace Botan
291
292#endif
#define BOTAN_DIAGNOSTIC_POP
Definition api.h:122
#define BOTAN_DIAGNOSTIC_PUSH
Definition api.h:119
#define BOTAN_DIAGNOSTIC_IGNORE_INHERITED_VIA_DOMINANCE
Definition api.h:121
#define BOTAN_PUBLIC_API(maj, min)
Definition api.h:21
#define BOTAN_DEPRECATED(msg)
Definition api.h:73
#define BOTAN_FUTURE_EXPLICIT
Definition api.h:52
virtual OID object_identifier() const
Definition pk_keys.cpp:22
bool stateful_operation() const override
Definition xmss.h:212
friend class XMSS_Signature_Operation
Definition xmss.h:252
WOTS_Derivation_Method wots_derivation_method() const
XMSS_PrivateKey(XMSS_Parameters::xmss_algorithm_t xmss_algo_id, RandomNumberGenerator &rng, WOTS_Derivation_Method wots_derivation_method=WOTS_Derivation_Method::NIST_SP800_208)
const secure_vector< uint8_t > & root() const
const secure_vector< uint8_t > & public_seed() const
const XMSS_Parameters & xmss_parameters() const
bool supports_operation(PublicKeyOperation op) const override
Definition xmss.h:104
void set_root(secure_vector< uint8_t > root)
friend class XMSS_Verification_Operation
Definition xmss.h:113
std::string algo_name() const override
Definition xmss.h:71
AlgorithmIdentifier algorithm_identifier() const override
Definition xmss.h:73
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, RandomNumberGenerator &rng)
PublicKeyOperation
Definition pk_keys.h:46
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:68
WOTS_Derivation_Method
Definition xmss.h:135