Botan 2.1.0 Crypto and TLS for C++11

- Main Page
- Namespaces
- Classes
- Files

- File List
- File Members

xmss_tools.h
Go to the documentation of this file.
1 /*
2 * XMSS Address
3 * (C) 2016 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 <stdint.h>
12 #include <iterator>
13 #include <type_traits>
14 #include <botan/cpuid.h>
15 #include <botan/types.h>
16 #include <botan/secmem.h>
17
18 namespace Botan {
19
20 /**
21 * Helper tools for low level byte operations required
22 * for the XMSS implementation.
23 **/
24 class XMSS_Tools
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);
66 if(CPUID::is_little_endian())
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>
82 void XMSS_Tools::concat(secure_vector<uint8_t>& target,
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);
93 if(CPUID::is_little_endian())
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
Botan::XMSS_Tools::operator=
void operator=(const XMSS_Tools &)=delete
type
MechanismType type
Definition: p11_mechanism.cpp:43
Botan::secure_vector
std::vector< T, secure_allocator< T >> secure_vector
Definition: secmem.h:121
Botan::CPUID::is_little_endian
static bool is_little_endian()
Definition: cpuid.h:74
Botan
Definition: alg_id.cpp:13
Botan::XMSS_Tools::concat
static void concat(secure_vector< uint8_t > &target, const T &src)
Definition: xmss_tools.h:63
Botan::CT::min
T min(T a, T b)
Definition: ct_utils.h:180
Botan::XMSS_Tools
Definition: xmss_tools.h:24