Botan 3.8.1
Crypto and TLS for C&
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/secmem.h>
12#include <algorithm>
13#include <bit>
14#include <iterator>
15#include <type_traits>
16
17namespace Botan {
18
19/**
20 * Helper tools for low level byte operations required
21 * for the XMSS implementation.
22 **/
23class XMSS_Tools final {
24 public:
25 XMSS_Tools() = delete;
26 XMSS_Tools(const XMSS_Tools&) = delete;
27 void operator=(const XMSS_Tools&) = delete;
28
29 /**
30 * Concatenates the byte representation in big-endian order of any
31 * integral value to a secure_vector.
32 *
33 * @param target Vector to concatenate the byte representation of the
34 * integral value to.
35 * @param src integral value to concatenate.
36 **/
37 template <typename T, typename U = typename std::enable_if<std::is_integral<T>::value, void>::type>
38 static void concat(secure_vector<uint8_t>& target, const T& src);
39
40 /**
41 * Concatenates the last n bytes of the byte representation in big-endian
42 * order of any integral value to a to a secure_vector.
43 *
44 * @param target Vector to concatenate the byte representation of the
45 * integral value to.
46 * @param src Integral value to concatenate.
47 * @param len number of bytes to concatenate. This value must be smaller
48 * or equal to the size of type T.
49 **/
50 template <typename T, typename U = typename std::enable_if<std::is_integral<T>::value, void>::type>
51 static void concat(secure_vector<uint8_t>& target, const T& src, size_t len);
52};
53
54template <typename T, typename U>
55void XMSS_Tools::concat(secure_vector<uint8_t>& target, const T& src) {
56 const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
57 if constexpr(std::endian::native == std::endian::little) {
58 std::reverse_copy(src_bytes, src_bytes + sizeof(src), std::back_inserter(target));
59 } else {
60 std::copy(src_bytes, src_bytes + sizeof(src), std::back_inserter(target));
61 }
62}
63
64template <typename T, typename U>
65void XMSS_Tools::concat(secure_vector<uint8_t>& target, const T& src, size_t len) {
66 size_t c = static_cast<size_t>(std::min(len, sizeof(src)));
67 if(len > sizeof(src)) {
68 target.resize(target.size() + len - sizeof(src), 0);
69 }
70
71 const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
72 if constexpr(std::endian::native == std::endian::little) {
73 std::reverse_copy(src_bytes, src_bytes + c, std::back_inserter(target));
74 } else {
75 std::copy(src_bytes + sizeof(src) - c, src_bytes + sizeof(src), std::back_inserter(target));
76 }
77}
78} // namespace Botan
79
80#endif
void operator=(const XMSS_Tools &)=delete
XMSS_Tools(const XMSS_Tools &)=delete
static void concat(secure_vector< uint8_t > &target, const T &src)
Definition xmss_tools.h:55
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:65