Botan  2.4.0
Crypto and TLS for C++11
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/cpuid.h>
12 #include <botan/secmem.h>
13 #include <iterator>
14 #include <type_traits>
15 
16 #if defined(BOTAN_TARGET_OS_HAS_THREADS)
17  #include <thread>
18  #include <chrono>
19  #include <botan/xmss_hash.h>
20 #endif
21 
22 namespace Botan {
23 
24 /**
25  * Helper tools for low level byte operations required
26  * for the XMSS implementation.
27  **/
28 class XMSS_Tools final
29  {
30  public:
31  XMSS_Tools(const XMSS_Tools&) = delete;
32  void operator=(const XMSS_Tools&) = delete;
33 
34  /**
35  * Concatenates the byte representation in big-endian order of any
36  * integral value to a secure_vector.
37  *
38  * @param target Vector to concatenate the byte representation of the
39  * integral value to.
40  * @param src integral value to concatenate.
41  **/
42  template<typename T,
43  typename U = typename std::enable_if<std::is_integral<T>::value,
44  void>::type>
45  static void concat(secure_vector<uint8_t>& target, const T& src);
46 
47  /**
48  * Concatenates the last n bytes of the byte representation in big-endian
49  * order of any integral value to a to a secure_vector.
50  *
51  * @param target Vector to concatenate the byte representation of the
52  * integral value to.
53  * @param src Integral value to concatenate.
54  * @param len number of bytes to concatenate. This value must be smaller
55  * or equal to the size of type T.
56  **/
57  template <typename T,
58  typename U = typename std::enable_if<std::is_integral<T>::value,
59  void>::type>
60  static void concat(secure_vector<uint8_t>& target, const T& src, size_t len);
61 
62  /**
63  * Not a public API function - will be removed in a future release.
64  *
65  * Determines the maximum number of threads to be used
66  * efficiently, based on runtime timining measurements. Ideally the
67  * result will correspond to the physical number of cores. On systems
68  * supporting simultaneous multi threading (SMT)
69  * std::thread::hardware_concurrency() usually reports a supported
70  * number of threads which is bigger (typically by a factor of 2) than
71  * the number of physical cores available. Using more threads than
72  * physically available cores for computationally intesive tasks
73  * resulted in slowdowns compared to using a number of threads equal to
74  * the number of physical cores on test systems. This function is a
75  * temporary workaround to prevent performance degradation due to
76  * overstressing the CPU with too many threads.
77  *
78  * @return Presumed number of physical cores based on timing measurements.
79  **/
80  static size_t max_threads(); // TODO: Remove max_threads() and use
81  // Botan::CPUID once proper plattform
82  // independent detection of physical cores is
83  // available.
84 
85  private:
86  XMSS_Tools();
87  /**
88  * Measures the time t1 it takes to calculate hashes using
89  * std::thread::hardware_concurrency() many threads and the time t2
90  * calculating the same number of hashes using
91  * std::thread::hardware_concurrency() / 2 threads.
92  *
93  * @return std::thread::hardware_concurrency() if t1 < t2
94  * std::thread::hardware_concurrency() / 2 otherwise.
95  **/
96  static size_t bench_threads(); // TODO: Remove bench_threads() and use
97  // Botan::CPUID once proper plattform
98  // independent detection of physical cores
99  // is //available.
100  };
101 
102 template <typename T, typename U>
103 void XMSS_Tools::concat(secure_vector<uint8_t>& target, const T& src)
104  {
105  const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
107  {
108  std::reverse_copy(src_bytes,
109  src_bytes + sizeof(src),
110  std::back_inserter(target));
111  }
112  else
113  {
114  std::copy(src_bytes,
115  src_bytes + sizeof(src),
116  std::back_inserter(target));
117  }
118  }
119 
120 
121 template <typename T, typename U>
123  const T& src,
124  size_t len)
125  {
126  size_t c = static_cast<size_t>(std::min(len, sizeof(src)));
127  if(len > sizeof(src))
128  {
129  target.resize(target.size() + len - sizeof(src), 0);
130  }
131 
132  const uint8_t* src_bytes = reinterpret_cast<const uint8_t*>(&src);
134  {
135  std::reverse_copy(src_bytes,
136  src_bytes + c,
137  std::back_inserter(target));
138  }
139  else
140  {
141  std::copy(src_bytes + sizeof(src) - c,
142  src_bytes + sizeof(src),
143  std::back_inserter(target));
144  }
145  }
146 }
147 
148 #endif
static size_t max_threads()
void operator=(const XMSS_Tools &)=delete
MechanismType type
static bool is_little_endian()
Definition: cpuid.h:75
Definition: alg_id.cpp:13
static void concat(secure_vector< uint8_t > &target, const T &src)
Definition: xmss_tools.h:103
fe T
Definition: ge.cpp:37
std::vector< T, secure_allocator< T > > secure_vector
Definition: secmem.h:88