Coverage Report

Created: 2020-08-01 06:18

/src/botan/src/lib/pubkey/xmss/xmss_publickey.cpp
Line
Count
Source (jump to first uncovered line)
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
 *     Request for Comments: 8391
9
 *     Release: May 2018.
10
 *     https://datatracker.ietf.org/doc/rfc8391/
11
 *
12
 * (C) 2016,2017 Matthias Gierlings
13
 *
14
 * Botan is released under the Simplified BSD License (see license.txt)
15
 **/
16
17
#include <botan/internal/xmss_verification_operation.h>
18
#include <botan/xmss_publickey.h>
19
#include <botan/der_enc.h>
20
#include <botan/ber_dec.h>
21
22
namespace Botan {
23
24
namespace {
25
26
// fall back to raw decoding for previous versions, which did not encode an OCTET STRING
27
std::vector<uint8_t> extract_raw_key(const std::vector<uint8_t>& key_bits)
28
0
   {
29
0
   std::vector<uint8_t> raw_key;
30
0
   try
31
0
      {
32
0
      BER_Decoder(key_bits).decode(raw_key, OCTET_STRING);
33
0
      }
34
0
   catch(Decoding_Error&)
35
0
      {
36
0
      raw_key = key_bits;
37
0
      }
38
0
   return raw_key;
39
0
   }
40
41
}
42
43
XMSS_PublicKey::XMSS_PublicKey(const std::vector<uint8_t>& key_bits)
44
   : m_raw_key(extract_raw_key(key_bits)),
45
     m_xmss_params(XMSS_PublicKey::deserialize_xmss_oid(m_raw_key)),
46
     m_wots_params(m_xmss_params.ots_oid())
47
0
   {
48
0
   if(m_raw_key.size() < XMSS_PublicKey::size())
49
0
      {
50
0
      throw Decoding_Error("Invalid XMSS public key size detected");
51
0
      }
52
0
53
0
   // extract & copy root from raw key
54
0
   m_root.clear();
55
0
   m_root.reserve(m_xmss_params.element_size());
56
0
   auto begin = m_raw_key.begin() + sizeof(uint32_t);
57
0
   auto end = begin + m_xmss_params.element_size();
58
0
   std::copy(begin, end, std::back_inserter(m_root));
59
0
60
0
   // extract & copy public seed from raw key
61
0
   begin = end;
62
0
   end = begin + m_xmss_params.element_size();
63
0
   m_public_seed.clear();
64
0
   m_public_seed.reserve(m_xmss_params.element_size());
65
0
   std::copy(begin, end, std::back_inserter(m_public_seed));
66
0
   }
Unexecuted instantiation: Botan::XMSS_PublicKey::XMSS_PublicKey(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
Unexecuted instantiation: Botan::XMSS_PublicKey::XMSS_PublicKey(std::__1::vector<unsigned char, std::__1::allocator<unsigned char> > const&)
67
68
XMSS_Parameters::xmss_algorithm_t
69
XMSS_PublicKey::deserialize_xmss_oid(const std::vector<uint8_t>& raw_key)
70
0
   {
71
0
   if(raw_key.size() < 4)
72
0
      {
73
0
      throw Decoding_Error("XMSS signature OID missing.");
74
0
      }
75
0
76
0
   // extract and convert algorithm id to enum type
77
0
   uint32_t raw_id = 0;
78
0
   for(size_t i = 0; i < 4; i++)
79
0
      { raw_id = ((raw_id << 8) | raw_key[i]); }
80
0
81
0
   return static_cast<XMSS_Parameters::xmss_algorithm_t>(raw_id);
82
0
   }
83
84
std::unique_ptr<PK_Ops::Verification>
85
XMSS_PublicKey::create_verification_op(const std::string&,
86
                                       const std::string& provider) const
87
0
   {
88
0
   if(provider == "base" || provider.empty())
89
0
      {
90
0
      return std::unique_ptr<PK_Ops::Verification>(
91
0
                new XMSS_Verification_Operation(*this));
92
0
      }
93
0
   throw Provider_Not_Found(algo_name(), provider);
94
0
   }
95
96
std::vector<uint8_t> XMSS_PublicKey::raw_public_key() const
97
0
   {
98
0
   std::vector<uint8_t> result
99
0
      {
100
0
      static_cast<uint8_t>(m_xmss_params.oid() >> 24),
101
0
      static_cast<uint8_t>(m_xmss_params.oid() >> 16),
102
0
      static_cast<uint8_t>(m_xmss_params.oid() >>  8),
103
0
      static_cast<uint8_t>(m_xmss_params.oid())
104
0
      };
105
0
106
0
   std::copy(m_root.begin(), m_root.end(), std::back_inserter(result));
107
0
   std::copy(m_public_seed.begin(),
108
0
             m_public_seed.end(),
109
0
             std::back_inserter(result));
110
0
111
0
   return result;
112
0
   }
113
114
}