Botan  2.4.0
Crypto and TLS for C++11
xmss_publickey.h
Go to the documentation of this file.
1 /*
2  * XMSS Public Key
3  * (C) 2016,2017 Matthias Gierlings
4  *
5  * Botan is released under the Simplified BSD License (see license.txt)
6  **/
7 
8 #ifndef BOTAN_XMSS_PUBLICKEY_H_
9 #define BOTAN_XMSS_PUBLICKEY_H_
10 
11 #include <cstddef>
12 #include <iterator>
13 #include <memory>
14 #include <string>
15 #include <botan/alg_id.h>
16 #include <botan/asn1_oid.h>
17 #include <botan/der_enc.h>
18 #include <botan/exceptn.h>
19 #include <botan/rng.h>
20 #include <botan/types.h>
21 #include <botan/pk_keys.h>
22 #include <botan/xmss_parameters.h>
23 #include <botan/xmss_wots_parameters.h>
24 #include <botan/pk_ops.h>
25 
26 namespace Botan {
27 
28 class XMSS_Verification_Operation;
29 
30 /**
31  * An XMSS: Extended Hash-Based Signature public key.
32  * The XMSS public key does not support the X509 standard. Instead the
33  * raw format described in [1] is used.
34  *
35  * [1] XMSS: Extended Hash-Based Signatures,
36  * draft-itrf-cfrg-xmss-hash-based-signatures-06
37  * Release: July 2016.
38  * https://datatracker.ietf.org/doc/
39  * draft-irtf-cfrg-xmss-hash-based-signatures/?include_text=1
40  **/
41 class BOTAN_PUBLIC_API(2,0) XMSS_PublicKey : public virtual Public_Key
42  {
43  public:
44  /**
45  * Creates a new XMSS public key for the chosen XMSS signature method.
46  * New public and prf seeds are generated using rng. The appropriate WOTS
47  * signature method will be automatically set based on the chosen XMSS
48  * signature method.
49  *
50  * @param xmss_oid Identifier for the selected XMSS signature method.
51  * @param rng A random number generator to use for key generation.
52  **/
55  : m_xmss_params(xmss_oid), m_wots_params(m_xmss_params.ots_oid()),
56  m_root(m_xmss_params.element_size()),
57  m_public_seed(rng.random_vec(m_xmss_params.element_size())) {}
58 
59  /**
60  * Creates an XMSS public key from a byte sequence produced by
61  * raw_private_key().
62  **/
63  XMSS_PublicKey(const std::vector<uint8_t>& raw_key);
64 
65  /**
66  * Creates a new XMSS public key for a chosen XMSS signature method as
67  * well as pre-computed root node and public_seed values.
68  *
69  * @param xmss_oid Identifier for the selected XMSS signature method.
70  * @param root Root node value.
71  * @param public_seed Public seed value.
72  **/
74  const secure_vector<uint8_t>& root,
75  const secure_vector<uint8_t>& public_seed)
76  : m_xmss_params(xmss_oid), m_wots_params(m_xmss_params.ots_oid()),
77  m_root(root), m_public_seed(public_seed) {}
78 
79  /**
80  * Creates a new XMSS public key for a chosen XMSS signature method as
81  * well as pre-computed root node and public_seed values.
82  *
83  * @param xmss_oid Identifier for the selected XMSS signature method.
84  * @param root Root node value.
85  * @param public_seed Public seed value.
86  **/
89  secure_vector<uint8_t>&& public_seed)
90  : m_xmss_params(xmss_oid), m_wots_params(m_xmss_params.ots_oid()),
91  m_root(std::move(root)), m_public_seed(std::move(public_seed)) {}
92 
93  /**
94  * Retrieves the chosen XMSS signature method.
95  *
96  * @return XMSS signature method identifier.
97  **/
99  {
100  return m_xmss_params.oid();
101  }
102 
103  /**
104  * Sets the chosen XMSS signature method
105  **/
107  {
108  m_xmss_params = XMSS_Parameters(xmss_oid);
109  m_wots_params = XMSS_WOTS_Parameters(m_xmss_params.ots_oid());
110  }
111 
112  /**
113  * Retrieves the XMSS parameters determined by the chosen XMSS Signature
114  * method.
115  *
116  * @return XMSS parameters.
117  **/
119  {
120  return m_xmss_params;
121  }
122 
123  /**
124  * Retrieves the Winternitz One Time Signature (WOTS) method,
125  * corrseponding to the chosen XMSS signature method.
126  *
127  * @return XMSS WOTS signature method identifier.
128  **/
130  {
131  return m_wots_params.oid();
132  }
133 
134  /**
135  * Retrieves the Winternitz One Time Signature (WOTS) parameters
136  * corresponding to the chosen XMSS signature method.
137  *
138  * @return XMSS WOTS signature method parameters.
139  **/
141  {
142  return m_wots_params;
143  }
144 
146  {
147  return m_root;
148  }
149 
151  {
152  m_root = root;
153  }
154 
156  {
157  m_root = std::move(root);
158  }
159 
161  {
162  return m_root;
163  }
164 
166  {
167  return m_public_seed;
168  }
169 
170  virtual void set_public_seed(const secure_vector<uint8_t>& public_seed)
171  {
172  m_public_seed = public_seed;
173  }
174 
175  virtual void set_public_seed(secure_vector<uint8_t>&& public_seed)
176  {
177  m_public_seed = std::move(public_seed);
178  }
179 
180  virtual const secure_vector<uint8_t>& public_seed() const
181  {
182  return m_public_seed;
183  }
184 
185  std::string algo_name() const override
186  {
187  return "XMSS";
188  }
189 
191  {
193  }
194 
195  bool check_key(RandomNumberGenerator&, bool) const override
196  {
197  return true;
198  }
199 
200  std::unique_ptr<PK_Ops::Verification>
201  create_verification_op(const std::string&,
202  const std::string& provider) const override;
203 
204  size_t estimated_strength() const override
205  {
206  return m_xmss_params.estimated_strength();
207  }
208 
209  size_t key_length() const override
210  {
211  return m_xmss_params.estimated_strength();
212  }
213 
214  /**
215  * Returns a raw byte sequence as defined in [1].
216  * This method acts as an alias for raw_public_key().
217  *
218  * @return raw public key bits.
219  **/
220  std::vector<uint8_t> public_key_bits() const override
221  {
222  return raw_public_key();
223  }
224 
225  /**
226  * Size in bytes of the serialized XMSS public key produced by
227  * raw_public_key().
228  *
229  * @return size in bytes of serialized Public Key.
230  **/
231  virtual size_t size() const
232  {
233  return sizeof(uint32_t) + 2 * m_xmss_params.element_size();
234  }
235 
236  /**
237  * Generates a non standardized byte sequence representing the XMSS
238  * public key, as defined in [1] (p. 23, "XMSS Public Key")
239  *
240  * @return 4-byte OID, followed by n-byte root node, followed by
241  * public seed.
242  **/
243  virtual std::vector<uint8_t> raw_public_key() const;
244 
245  protected:
250 
251  private:
252  XMSS_Parameters::xmss_algorithm_t deserialize_xmss_oid(
253  const std::vector<uint8_t>& raw_key);
254  };
255 
256 }
257 
258 #endif
secure_vector< uint8_t > m_public_seed
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, const secure_vector< uint8_t > &root, const secure_vector< uint8_t > &public_seed)
virtual void set_public_seed(const secure_vector< uint8_t > &public_seed)
secure_vector< uint8_t > m_root
#define BOTAN_PUBLIC_API(maj, min)
Definition: compiler.h:27
void set_root(const secure_vector< uint8_t > &root)
const secure_vector< uint8_t > & root() const
Definition: bigint.h:635
XMSS_Parameters::xmss_algorithm_t xmss_oid() const
void set_root(secure_vector< uint8_t > &&root)
virtual size_t size() const
virtual void set_public_seed(secure_vector< uint8_t > &&public_seed)
const XMSS_WOTS_Parameters & wots_parameters() const
void set_xmss_oid(XMSS_Parameters::xmss_algorithm_t xmss_oid)
const XMSS_Parameters & xmss_parameters() const
virtual const secure_vector< uint8_t > & public_seed() const
secure_vector< uint8_t > & root()
size_t key_length() const override
XMSS_WOTS_Parameters::ots_algorithm_t wots_oid() const
Definition: alg_id.cpp:13
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, secure_vector< uint8_t > &&root, secure_vector< uint8_t > &&public_seed)
std::vector< uint8_t > public_key_bits() const override
AlgorithmIdentifier algorithm_identifier() const override
std::string algo_name() const override
size_t estimated_strength() const override
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, RandomNumberGenerator &rng)
XMSS_WOTS_Parameters m_wots_params
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88
virtual secure_vector< uint8_t > & public_seed()
XMSS_Parameters m_xmss_params
bool check_key(RandomNumberGenerator &, bool) const override