/src/botan/build/include/botan/xmss_tools.h
Line | Count | Source (jump to first uncovered line) |
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 | | **/ |
24 | | class XMSS_Tools final |
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 | 0 | { |
86 | 0 | size_t c = static_cast<size_t>(std::min(len, sizeof(src))); |
87 | 0 | if(len > sizeof(src)) |
88 | 0 | { |
89 | 0 | target.resize(target.size() + len - sizeof(src), 0); |
90 | 0 | } |
91 | 0 |
|
92 | 0 | const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src); |
93 | 0 | if(CPUID::is_little_endian()) |
94 | 0 | { |
95 | 0 | std::reverse_copy(src_bytes, |
96 | 0 | src_bytes + c, |
97 | 0 | std::back_inserter(target)); |
98 | 0 | } |
99 | 0 | else |
100 | 0 | { |
101 | 0 | std::copy(src_bytes + sizeof(src) - c, |
102 | 0 | src_bytes + sizeof(src), |
103 | 0 | std::back_inserter(target)); |
104 | 0 | } |
105 | 0 | } Unexecuted instantiation: void Botan::XMSS_Tools::concat<unsigned long, void>(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned long const&, unsigned long) Unexecuted instantiation: void Botan::XMSS_Tools::concat<unsigned int, void>(std::__1::vector<unsigned char, Botan::secure_allocator<unsigned char> >&, unsigned int const&, unsigned long) |
106 | | } |
107 | | |
108 | | #endif |