/src/botan/build/include/botan/internal/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/internal/cpuid.h> |
12 | | #include <botan/secmem.h> |
13 | | #include <iterator> |
14 | | #include <type_traits> |
15 | | |
16 | | namespace Botan { |
17 | | |
18 | | /** |
19 | | * Helper tools for low level byte operations required |
20 | | * for the XMSS implementation. |
21 | | **/ |
22 | | class XMSS_Tools final |
23 | | { |
24 | | public: |
25 | | XMSS_Tools(const XMSS_Tools&) = delete; |
26 | | void operator=(const XMSS_Tools&) = delete; |
27 | | |
28 | | /** |
29 | | * Concatenates the byte representation in big-endian order of any |
30 | | * integral value to a secure_vector. |
31 | | * |
32 | | * @param target Vector to concatenate the byte representation of the |
33 | | * integral value to. |
34 | | * @param src integral value to concatenate. |
35 | | **/ |
36 | | template<typename T, |
37 | | typename U = typename std::enable_if<std::is_integral<T>::value, |
38 | | void>::type> |
39 | | static void concat(secure_vector<uint8_t>& target, const T& src); |
40 | | |
41 | | /** |
42 | | * Concatenates the last n bytes of the byte representation in big-endian |
43 | | * order of any integral value to a to a secure_vector. |
44 | | * |
45 | | * @param target Vector to concatenate the byte representation of the |
46 | | * integral value to. |
47 | | * @param src Integral value to concatenate. |
48 | | * @param len number of bytes to concatenate. This value must be smaller |
49 | | * or equal to the size of type T. |
50 | | **/ |
51 | | template <typename T, |
52 | | typename U = typename std::enable_if<std::is_integral<T>::value, |
53 | | void>::type> |
54 | | static void concat(secure_vector<uint8_t>& target, const T& src, size_t len); |
55 | | |
56 | | private: |
57 | | XMSS_Tools(); |
58 | | }; |
59 | | |
60 | | template <typename T, typename U> |
61 | | void XMSS_Tools::concat(secure_vector<uint8_t>& target, const T& src) |
62 | | { |
63 | | const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src); |
64 | | if(CPUID::is_little_endian()) |
65 | | { |
66 | | std::reverse_copy(src_bytes, |
67 | | src_bytes + sizeof(src), |
68 | | std::back_inserter(target)); |
69 | | } |
70 | | else |
71 | | { |
72 | | std::copy(src_bytes, |
73 | | src_bytes + sizeof(src), |
74 | | std::back_inserter(target)); |
75 | | } |
76 | | } |
77 | | |
78 | | |
79 | | template <typename T, typename U> |
80 | | void XMSS_Tools::concat(secure_vector<uint8_t>& target, |
81 | | const T& src, |
82 | | size_t len) |
83 | 0 | { |
84 | 0 | size_t c = static_cast<size_t>(std::min(len, sizeof(src))); |
85 | 0 | if(len > sizeof(src)) |
86 | 0 | { |
87 | 0 | target.resize(target.size() + len - sizeof(src), 0); |
88 | 0 | } |
89 | |
|
90 | 0 | const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src); |
91 | 0 | if(CPUID::is_little_endian()) |
92 | 0 | { |
93 | 0 | std::reverse_copy(src_bytes, |
94 | 0 | src_bytes + c, |
95 | 0 | std::back_inserter(target)); |
96 | 0 | } |
97 | 0 | else |
98 | 0 | { |
99 | 0 | std::copy(src_bytes + sizeof(src) - c, |
100 | 0 | src_bytes + sizeof(src), |
101 | 0 | std::back_inserter(target)); |
102 | 0 | } |
103 | 0 | } 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) 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) |
104 | | } |
105 | | |
106 | | #endif |