Botan 3.9.0
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 <concepts>
15#include <iterator>
16
17namespace Botan {
18
19/**
20* Concatenates the byte representation in big-endian order of any
21* integral value to a secure_vector.
22*
23* @param target Vector to concatenate the byte representation of the
24* integral value to.
25* @param src integral value to concatenate.
26**/
27template <std::unsigned_integral T>
28void xmss_concat(secure_vector<uint8_t>& target, const T& src) {
29 const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
30 if constexpr(std::endian::native == std::endian::little) {
31 std::reverse_copy(src_bytes, src_bytes + sizeof(src), std::back_inserter(target));
32 } else {
33 std::copy(src_bytes, src_bytes + sizeof(src), std::back_inserter(target));
34 }
35}
36
37/**
38* Concatenates the last n bytes of the byte representation in big-endian
39* order of any integral value to a to a secure_vector.
40*
41* @param target Vector to concatenate the byte representation of the
42* integral value to.
43* @param src Integral value to concatenate.
44* @param len number of bytes to concatenate. This value must be smaller
45* or equal to the size of type T.
46**/
47template <std::unsigned_integral T>
48void xmss_concat(secure_vector<uint8_t>& target, const T& src, size_t len) {
49 size_t c = static_cast<size_t>(std::min(len, sizeof(src)));
50 if(len > sizeof(src)) {
51 target.resize(target.size() + len - sizeof(src), 0);
52 }
53
54 const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
55 if constexpr(std::endian::native == std::endian::little) {
56 std::reverse_copy(src_bytes, src_bytes + c, std::back_inserter(target));
57 } else {
58 std::copy(src_bytes + sizeof(src) - c, src_bytes + sizeof(src), std::back_inserter(target));
59 }
60}
61
62} // namespace Botan
63
64#endif
void xmss_concat(secure_vector< uint8_t > &target, const T &src)
Definition xmss_tools.h:28
std::vector< T, secure_allocator< T > > secure_vector
Definition secmem.h:69