Botan  2.7.0
Crypto and TLS for C++11
xmss_publickey.cpp
Go to the documentation of this file.
1 /*
2  * XMSS Public Key
3  * An XMSS: Extended Hash-Based Siganture public key.
4  * The XMSS public key does not support the X509 standard. Instead the
5  * raw format described in [1] is used.
6  *
7  * [1] XMSS: Extended Hash-Based Signatures,
8  * draft-itrf-cfrg-xmss-hash-based-signatures-06
9  * Release: July 2016.
10  * https://datatracker.ietf.org/doc/
11  * draft-irtf-cfrg-xmss-hash-based-signatures/?include_text=1
12  *
13  * (C) 2016,2017 Matthias Gierlings
14  *
15  * Botan is released under the Simplified BSD License (see license.txt)
16  **/
17 
18 #include <botan/internal/xmss_verification_operation.h>
19 #include <botan/xmss_publickey.h>
20 
21 namespace Botan {
22 
23 XMSS_PublicKey::XMSS_PublicKey(const std::vector<uint8_t>& raw_key)
24  : m_xmss_params(XMSS_PublicKey::deserialize_xmss_oid(raw_key)),
25  m_wots_params(m_xmss_params.ots_oid())
26  {
27  if(raw_key.size() < size())
28  {
29  throw Integrity_Failure("Invalid XMSS public key size detected.");
30  }
31 
32  // extract & copy root from raw key.
33  m_root.clear();
35  auto begin = raw_key.begin() + sizeof(uint32_t);
36  auto end = begin + m_xmss_params.element_size();
37  std::copy(begin, end, std::back_inserter(m_root));
38 
39  // extract & copy public seed from raw key.
40  begin = end;
41  end = begin + m_xmss_params.element_size();
42  m_public_seed.clear();
44  std::copy(begin, end, std::back_inserter(m_public_seed));
45  }
46 
48 XMSS_PublicKey::deserialize_xmss_oid(const std::vector<uint8_t>& raw_key)
49  {
50  if(raw_key.size() < 4)
51  {
52  throw Integrity_Failure("XMSS signature OID missing.");
53  }
54 
55  // extract and convert algorithm id to enum type
56  uint32_t raw_id = 0;
57  for(size_t i = 0; i < 4; i++)
58  { raw_id = ((raw_id << 8) | raw_key[i]); }
59 
60  return static_cast<XMSS_Parameters::xmss_algorithm_t>(raw_id);
61  }
62 
63 std::unique_ptr<PK_Ops::Verification>
65  const std::string& provider) const
66  {
67  if(provider == "base" || provider.empty())
68  {
69  return std::unique_ptr<PK_Ops::Verification>(
70  new XMSS_Verification_Operation(*this));
71  }
72  throw Provider_Not_Found(algo_name(), provider);
73  }
74 
75 std::vector<uint8_t> XMSS_PublicKey::raw_public_key() const
76  {
77  std::vector<uint8_t> result
78  {
79  static_cast<uint8_t>(m_xmss_params.oid() >> 24),
80  static_cast<uint8_t>(m_xmss_params.oid() >> 16),
81  static_cast<uint8_t>(m_xmss_params.oid() >> 8),
82  static_cast<uint8_t>(m_xmss_params.oid())
83  };
84 
85  std::copy(m_root.begin(), m_root.end(), std::back_inserter(result));
86  std::copy(m_public_seed.begin(),
87  m_public_seed.end(),
88  std::back_inserter(result));
89 
90  return result;
91  }
92 
93 }
std::unique_ptr< PK_Ops::Verification > create_verification_op(const std::string &, const std::string &provider) const override
secure_vector< uint8_t > m_public_seed
secure_vector< uint8_t > m_root
virtual std::vector< uint8_t > raw_public_key() const
virtual size_t size() const
Definition: alg_id.cpp:13
std::string algo_name() const override
size_t element_size() const
XMSS_PublicKey(XMSS_Parameters::xmss_algorithm_t xmss_oid, RandomNumberGenerator &rng)
XMSS_Parameters m_xmss_params
xmss_algorithm_t oid() const