Botan 3.9.0
Crypto and TLS for C&
xmss_verification_operation.cpp
Go to the documentation of this file.
1/*
2 * XMSS Verification Operation
3 * Provides signature verification capabilities for Extended Hash-Based
4 * Signatures (XMSS).
5 *
6 * (C) 2016,2017 Matthias Gierlings
7 *
8 * Botan is released under the Simplified BSD License (see license.txt)
9 **/
10
11#include <botan/internal/xmss_verification_operation.h>
12
13#include <botan/internal/xmss_common_ops.h>
14#include <botan/internal/xmss_tools.h>
15#include <array>
16
17namespace Botan {
18
20 m_pub_key(public_key), m_hash(public_key.xmss_parameters()), m_msg_buf(0) {}
21
22secure_vector<uint8_t> XMSS_Verification_Operation::root_from_signature(const XMSS_Signature& sig,
23 const secure_vector<uint8_t>& msg,
24 XMSS_Address& adrs,
25 const secure_vector<uint8_t>& seed) {
26 const auto& params = m_pub_key.xmss_parameters();
27
28 const uint32_t next_index = static_cast<uint32_t>(sig.unused_leaf_index());
30 adrs.set_ots_address(next_index);
31
32 XMSS_WOTS_Parameters wots_params(params.ots_oid());
33 XMSS_WOTS_PublicKey pub_key_ots(wots_params, seed, sig.tree().ots_signature, msg, adrs, m_hash);
34
36 adrs.set_ltree_address(next_index);
37
38 std::array<secure_vector<uint8_t>, 2> node;
39 XMSS_Common_Ops::create_l_tree(node[0], pub_key_ots.key_data(), adrs, seed, m_hash, params);
40
42 adrs.set_tree_index(next_index);
43
44 for(size_t k = 0; k < params.tree_height(); k++) {
45 adrs.set_tree_height(static_cast<uint32_t>(k));
46 if(((next_index / (static_cast<size_t>(1) << k)) & 0x01) == 0) {
47 adrs.set_tree_index(adrs.get_tree_index() >> 1);
49 node[1], node[0], sig.tree().authentication_path[k], adrs, seed, m_hash, params);
50 } else {
51 adrs.set_tree_index((adrs.get_tree_index() - 1) >> 1);
53 node[1], sig.tree().authentication_path[k], node[0], adrs, seed, m_hash, params);
54 }
55 node[0] = node[1];
56 }
57 return node[0];
58}
59
60bool XMSS_Verification_Operation::verify(const XMSS_Signature& sig,
61 const secure_vector<uint8_t>& msg,
62 const XMSS_PublicKey& public_key) {
63 XMSS_Address adrs;
64 secure_vector<uint8_t> index_bytes;
65 xmss_concat(index_bytes, sig.unused_leaf_index(), m_pub_key.xmss_parameters().element_size());
66 secure_vector<uint8_t> msg_digest = m_hash.h_msg(sig.randomness(), public_key.root(), index_bytes, msg);
67
68 secure_vector<uint8_t> node = root_from_signature(sig, msg_digest, adrs, public_key.public_seed());
69
70 return (node == public_key.root());
71}
72
73// FIXME: XMSS signature verification requires the "randomness" parameter out
74// of the XMSS signature, which is part of the prefix that is hashed before
75// msg. Since the signature is unknown till sign() is called all message
76// content has to be buffered. For large messages this can be inconvenient or
77// impossible.
78// Possible solution: Change PK_Ops::Verification interface to take the
79// signature as constructor argument, make sign a parameterless member call.
80void XMSS_Verification_Operation::update(std::span<const uint8_t> input) {
81 m_msg_buf.insert(m_msg_buf.end(), input.begin(), input.end());
82}
83
84bool XMSS_Verification_Operation::is_valid_signature(std::span<const uint8_t> sig) {
85 try {
86 XMSS_Signature signature(m_pub_key.xmss_parameters().oid(), sig);
87 bool result = verify(signature, m_msg_buf, m_pub_key);
88 m_msg_buf.clear();
89 return result;
90 } catch(...) {
91 m_msg_buf.clear();
92 return false;
93 }
94}
95
96} // namespace Botan
void set_ots_address(uint32_t value)
uint32_t get_tree_index() const
void set_type(Type type)
void set_tree_height(uint32_t value)
void set_tree_index(uint32_t value)
void set_ltree_address(uint32_t value)
static void create_l_tree(secure_vector< uint8_t > &result, wots_keysig_t pk, XMSS_Address &adrs, const secure_vector< uint8_t > &seed, XMSS_Hash &hash, const XMSS_Parameters &params)
static void randomize_tree_hash(secure_vector< uint8_t > &result, const secure_vector< uint8_t > &left, const secure_vector< uint8_t > &right, XMSS_Address &adrs, const secure_vector< uint8_t > &seed, XMSS_Hash &hash, const XMSS_Parameters &params)
const XMSS_Parameters & xmss_parameters() const
Definition xmss.h:120
const XMSS_Signature::TreeSignature & tree() const
size_t unused_leaf_index() const
bool is_valid_signature(std::span< const uint8_t > sign) override
XMSS_Verification_Operation(const XMSS_PublicKey &public_key)
void update(std::span< const uint8_t > input) override
void xmss_concat(secure_vector< uint8_t > &target, const T &src)
Definition xmss_tools.h:28
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69