Botan 2.13.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

xmss_signature_operation.cpp
Go to the documentation of this file.
1 /*
2 * XMSS Signature Operation
3 * Signature generation operation for Extended Hash-Based Signatures (XMSS) as
4 * defined in:
5 *
6 * [1] XMSS: Extended Hash-Based Signatures,
7 * Request for Comments: 8391
8 * Release: May 2018.
9 * https://datatracker.ietf.org/doc/rfc8391/
10 *
11 * (C) 2016,2017,2018 Matthias Gierlings
12 *
13 * Botan is released under the Simplified BSD License (see license.txt)
14 **/
15
16 #include <botan/internal/xmss_signature_operation.h>
17
18 namespace Botan {
19
20 XMSS_Signature_Operation::XMSS_Signature_Operation(
21 const XMSS_PrivateKey& private_key)
22 : XMSS_Common_Ops(private_key.xmss_oid()),
23 m_priv_key(private_key),
24 m_randomness(0),
25 m_leaf_idx(0),
26 m_is_initialized(false)
27 {}
28
29 XMSS_WOTS_PublicKey::TreeSignature
30 XMSS_Signature_Operation::generate_tree_signature(const secure_vector<uint8_t>& msg,
31 XMSS_PrivateKey& xmss_priv_key,
32 XMSS_Address& adrs)
33 {
34
35 wots_keysig_t auth_path = build_auth_path(xmss_priv_key, adrs);
36 adrs.set_type(XMSS_Address::Type::OTS_Hash_Address);
37 adrs.set_ots_address(m_leaf_idx);
38
39 wots_keysig_t sig_ots = xmss_priv_key.wots_private_key().sign(msg, adrs);
40 return XMSS_WOTS_PublicKey::TreeSignature(sig_ots, auth_path);
41 }
42
43 XMSS_Signature
44 XMSS_Signature_Operation::sign(const secure_vector<uint8_t>& msg_hash,
45 XMSS_PrivateKey& xmss_priv_key)
46 {
47 XMSS_Address adrs;
48 XMSS_Signature sig(m_leaf_idx,
49 m_randomness,
50 generate_tree_signature(msg_hash, xmss_priv_key,adrs));
51 return sig;
52 }
53
54 size_t XMSS_Signature_Operation::signature_length() const
55 {
56 return sizeof(uint64_t) + // size of leaf index
57 m_xmss_params.element_size() +
58 m_xmss_params.len() * m_xmss_params.element_size() +
59 m_xmss_params.tree_height() * m_xmss_params.element_size();
60 }
61
62 wots_keysig_t
63 XMSS_Signature_Operation::build_auth_path(XMSS_PrivateKey& priv_key,
64 XMSS_Address& adrs)
65 {
66 wots_keysig_t auth_path(m_xmss_params.tree_height());
67 adrs.set_type(XMSS_Address::Type::Hash_Tree_Address);
68
69 for(size_t j = 0; j < m_xmss_params.tree_height(); j++)
70 {
71 size_t k = (m_leaf_idx / (1ULL << j)) ^ 0x01;
72 auth_path[j] = priv_key.tree_hash(k * (1ULL << j), j, adrs);
73 }
74
75 return auth_path;
76 }
77
78 void XMSS_Signature_Operation::update(const uint8_t msg[], size_t msg_len)
79 {
80 initialize();
81 m_hash.h_msg_update(msg, msg_len);
82 }
83
84 secure_vector<uint8_t>
85 XMSS_Signature_Operation::sign(RandomNumberGenerator&)
86 {
87 initialize();
88 secure_vector<uint8_t> signature(sign(m_hash.h_msg_final(),
89 m_priv_key).bytes());
90 m_is_initialized = false;
91 return signature;
92 }
93
94 void XMSS_Signature_Operation::initialize()
95 {
96 // return if we already initialized and reserved a leaf index for signing.
97 if(m_is_initialized)
98 { return; }
99
100 secure_vector<uint8_t> index_bytes;
101 // reserve leaf index so it can not be reused in by another signature
102 // operation using the same private key.
103 m_leaf_idx = static_cast<uint32_t>(m_priv_key.reserve_unused_leaf_index());
104
105 // write prefix for message hashing into buffer.
106 XMSS_Tools::concat(index_bytes, m_leaf_idx, 32);
107 m_randomness = m_hash.prf(m_priv_key.prf(), index_bytes);
108 index_bytes.clear();
109 XMSS_Tools::concat(index_bytes, m_leaf_idx,
110 m_priv_key.xmss_parameters().element_size());
111 m_hash.h_msg_init(m_randomness,
112 m_priv_key.root(),
113 index_bytes);
114 m_is_initialized = true;
115 }
116
117 }
118
Botan::XMSS_Signature_Operation::signature_length
size_t signature_length() const override
Definition: xmss_signature_operation.cpp:54
Botan::XMSS_Common_Ops
Definition: xmss_common_ops.h:26
Botan::XMSS_PrivateKey::tree_hash
secure_vector< uint8_t > tree_hash(size_t start_idx, size_t target_node_height, XMSS_Address &adrs)
Definition: xmss_privatekey.cpp:115
Botan::XMSS_Parameters::element_size
size_t element_size() const
Definition: xmss_parameters.h:67
Botan::XMSS_Hash::h_msg_final
secure_vector< uint8_t > h_msg_final()
Definition: xmss_hash.cpp:64
Botan::RandomNumberGenerator
Definition: rng.h:24
Botan::XMSS_Address::set_ots_address
void set_ots_address(uint32_t value)
Definition: xmss_address.h:164
Botan::XMSS_Signature_Operation::XMSS_Signature_Operation
XMSS_Signature_Operation(const XMSS_PrivateKey &private_key)
Definition: xmss_signature_operation.cpp:20
Botan::XMSS_Hash::h_msg_update
void h_msg_update(const uint8_t data[], size_t size)
Definition: xmss_hash.cpp:59
Botan::XMSS_Address::Type::OTS_Hash_Address
Botan::XMSS_WOTS_PrivateKey::sign
wots_keysig_t sign(const secure_vector< uint8_t > &msg, XMSS_Address &adrs)
Definition: xmss_wots_privatekey.h:236
Botan::XMSS_Parameters::tree_height
size_t tree_height() const
Definition: xmss_parameters.h:72
Botan::XMSS_Address
Definition: xmss_address.h:21
Botan::XMSS_PrivateKey::reserve_unused_leaf_index
size_t reserve_unused_leaf_index()
Definition: xmss_privatekey.h:137
Botan::XMSS_PublicKey::xmss_parameters
const XMSS_Parameters & xmss_parameters() const
Definition: xmss_publickey.h:120
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:65
Botan::XMSS_Hash::prf
void prf(secure_vector< uint8_t > &result, const secure_vector< uint8_t > &key, const secure_vector< uint8_t > &data)
Definition: xmss_hash.h:35
Botan::XMSS_Address::set_type
void set_type(Type type)
Definition: xmss_address.h:111
Botan::XMSS_PrivateKey
Definition: xmss_privatekey.h:37
Botan::XMSS_PublicKey::root
secure_vector< uint8_t > & root()
Definition: xmss_publickey.h:147
Botan::XMSS_Common_Ops::m_xmss_params
XMSS_Parameters m_xmss_params
Definition: xmss_common_ops.h:121
Botan::XMSS_WOTS_PublicKey::TreeSignature
Definition: xmss_wots_publickey.h:35
Botan::XMSS_Common_Ops::m_hash
XMSS_Hash m_hash
Definition: xmss_common_ops.h:122
Botan
Definition: alg_id.cpp:13
Botan::wots_keysig_t
std::vector< secure_vector< uint8_t > > wots_keysig_t
Definition: xmss_common_ops.h:21
Botan::XMSS_Tools::concat
static void concat(secure_vector< uint8_t > &target, const T &src)
Definition: xmss_tools.h:63
Botan::XMSS_Hash::h_msg_init
void h_msg_init(const secure_vector< uint8_t > &randomness, const secure_vector< uint8_t > &root, const secure_vector< uint8_t > &index_bytes)
Definition: xmss_hash.cpp:47
Botan::XMSS_PrivateKey::prf
const secure_vector< uint8_t > & prf() const
Definition: xmss_privatekey.h:170
Botan::XMSS_Address::Type::Hash_Tree_Address
Botan::XMSS_Parameters::len
size_t len() const
Definition: xmss_parameters.h:82
Botan::XMSS_Signature_Operation::sign
secure_vector< uint8_t > sign(RandomNumberGenerator &) override
Definition: xmss_signature_operation.cpp:85
Botan::XMSS_Signature_Operation::update
void update(const uint8_t msg[], size_t msg_len) override
Definition: xmss_signature_operation.cpp:78
Botan::XMSS_PrivateKey::wots_private_key
const XMSS_WOTS_PrivateKey & wots_private_key() const
Definition: xmss_privatekey.h:154