Botan  2.15.0
Crypto and TLS for C++11
xmss_tools.h
Go to the documentation of this file.
1 /*
2  * XMSS Tools
3  * (C) 2016,2017 Matthias Gierlings
4  *
5  * Botan is released under the Simplified BSD License (see license.txt)
6  **/
7 
8 #ifndef BOTAN_XMSS_TOOLS_H_
9 #define BOTAN_XMSS_TOOLS_H_
10 
11 #include <botan/cpuid.h>
12 #include <botan/secmem.h>
13 #include <iterator>
14 #include <type_traits>
15 
16 //BOTAN_FUTURE_INTERNAL_HEADER(xmss_tools.h)
17 
18 namespace Botan {
19 
20 /**
21  * Helper tools for low level byte operations required
22  * for the XMSS implementation.
23  **/
25  {
26  public:
27  XMSS_Tools(const XMSS_Tools&) = delete;
28  void operator=(const XMSS_Tools&) = delete;
29 
30  /**
31  * Concatenates the byte representation in big-endian order of any
32  * integral value to a secure_vector.
33  *
34  * @param target Vector to concatenate the byte representation of the
35  * integral value to.
36  * @param src integral value to concatenate.
37  **/
38  template<typename T,
39  typename U = typename std::enable_if<std::is_integral<T>::value,
40  void>::type>
41  static void concat(secure_vector<uint8_t>& target, const T& src);
42 
43  /**
44  * Concatenates the last n bytes of the byte representation in big-endian
45  * order of any integral value to a to a secure_vector.
46  *
47  * @param target Vector to concatenate the byte representation of the
48  * integral value to.
49  * @param src Integral value to concatenate.
50  * @param len number of bytes to concatenate. This value must be smaller
51  * or equal to the size of type T.
52  **/
53  template <typename T,
54  typename U = typename std::enable_if<std::is_integral<T>::value,
55  void>::type>
56  static void concat(secure_vector<uint8_t>& target, const T& src, size_t len);
57 
58  private:
59  XMSS_Tools();
60  };
61 
62 template <typename T, typename U>
63 void XMSS_Tools::concat(secure_vector<uint8_t>& target, const T& src)
64  {
65  const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
67  {
68  std::reverse_copy(src_bytes,
69  src_bytes + sizeof(src),
70  std::back_inserter(target));
71  }
72  else
73  {
74  std::copy(src_bytes,
75  src_bytes + sizeof(src),
76  std::back_inserter(target));
77  }
78  }
79 
80 
81 template <typename T, typename U>
83  const T& src,
84  size_t len)
85  {
86  size_t c = static_cast<size_t>(std::min(len, sizeof(src)));
87  if(len > sizeof(src))
88  {
89  target.resize(target.size() + len - sizeof(src), 0);
90  }
91 
92  const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
94  {
95  std::reverse_copy(src_bytes,
96  src_bytes + c,
97  std::back_inserter(target));
98  }
99  else
100  {
101  std::copy(src_bytes + sizeof(src) - c,
102  src_bytes + sizeof(src),
103  std::back_inserter(target));
104  }
105  }
106 }
107 
108 #endif
void operator=(const XMSS_Tools &)=delete
int(* final)(unsigned char *, CTX *)
MechanismType type
static bool is_little_endian()
Definition: cpuid.h:73
Definition: alg_id.cpp:13
static void concat(secure_vector< uint8_t > &target, const T &src)
Definition: xmss_tools.h:63
fe T
Definition: ge.cpp:37
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:65