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