Coverage Report

Created: 2026-09-03 07:16

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/botan/src/lib/pubkey/xmss/xmss_signature.cpp
Line
Count
Source
1
/*
2
 * XMSS Signature
3
 * (C) 2016,2017,2018 Matthias Gierlings
4
 *
5
 * Botan is released under the Simplified BSD License (see license.txt)
6
 **/
7
8
#include <botan/internal/xmss_signature.h>
9
#include <iterator>
10
11
namespace Botan {
12
13
XMSS_Signature::XMSS_Signature(XMSS_Parameters::xmss_algorithm_t oid, std::span<const uint8_t> raw_sig) :
14
0
      m_leaf_idx(0), m_randomness(0, 0x00) {
15
0
   const auto params = XMSS_Parameters::from_id(oid);
16
17
0
   if(raw_sig.size() != (params.len() + params.tree_height() + 1) * params.element_size() + sizeof(uint32_t)) {
18
0
      throw Decoding_Error("XMSS signature size invalid.");
19
0
   }
20
21
0
   for(size_t i = 0; i < 4; i++) {
22
0
      m_leaf_idx = ((m_leaf_idx << 8) | raw_sig[i]);
23
0
   }
24
25
0
   if(m_leaf_idx >= params.total_number_of_signatures()) {
26
0
      throw Decoding_Error("XMSS signature leaf index out of bounds.");
27
0
   }
28
29
0
   auto begin = raw_sig.begin() + sizeof(uint32_t);
30
0
   auto end = begin + params.element_size();
31
0
   std::copy(begin, end, std::back_inserter(m_randomness));
32
33
0
   for(size_t i = 0; i < params.len(); i++) {
34
0
      begin = end;
35
0
      end = begin + params.element_size();
36
0
      m_tree_sig.ots_signature.push_back(secure_vector<uint8_t>(0));
37
0
      m_tree_sig.ots_signature.back().reserve(params.element_size());
38
0
      std::copy(begin, end, std::back_inserter(m_tree_sig.ots_signature.back()));
39
0
   }
40
41
0
   for(size_t i = 0; i < params.tree_height(); i++) {
42
0
      begin = end;
43
0
      end = begin + params.element_size();
44
0
      m_tree_sig.authentication_path.push_back(secure_vector<uint8_t>(0));
45
0
      m_tree_sig.authentication_path.back().reserve(params.element_size());
46
0
      std::copy(begin, end, std::back_inserter(m_tree_sig.authentication_path.back()));
47
0
   }
48
0
}
49
50
0
std::vector<uint8_t> XMSS_Signature::bytes() const {
51
0
   std::vector<uint8_t> result{static_cast<uint8_t>(m_leaf_idx >> 24U),
52
0
                               static_cast<uint8_t>(m_leaf_idx >> 16U),
53
0
                               static_cast<uint8_t>(m_leaf_idx >> 8U),
54
0
                               static_cast<uint8_t>(m_leaf_idx)};
55
56
0
   std::copy(m_randomness.begin(), m_randomness.end(), std::back_inserter(result));
57
58
0
   for(const auto& sig : tree().ots_signature) {
59
0
      std::copy(sig.begin(), sig.end(), std::back_inserter(result));
60
0
   }
61
62
0
   for(const auto& auth : tree().authentication_path) {
63
0
      std::copy(auth.begin(), auth.end(), std::back_inserter(result));
64
0
   }
65
0
   return result;
66
0
}
67
68
}  // namespace Botan