MCKL
Monte Carlo Kernel Library
aes.hpp
Go to the documentation of this file.
1 //============================================================================
2 // MCKL/include/mckl/random/aes.hpp
3 //----------------------------------------------------------------------------
4 // MCKL: Monte Carlo Kernel Library
5 //----------------------------------------------------------------------------
6 // Copyright (c) 2013-2018, Yan Zhou
7 // All rights reserved.
8 //
9 // Redistribution and use in source and binary forms, with or without
10 // modification, are permitted provided that the following conditions are met:
11 //
12 // Redistributions of source code must retain the above copyright notice,
13 // this list of conditions and the following disclaimer.
14 //
15 // Redistributions in binary form must reproduce the above copyright notice,
16 // this list of conditions and the following disclaimer in the documentation
17 // and/or other materials provided with the distribution.
18 //
19 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS AS IS
20 // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23 // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 // POSSIBILITY OF SUCH DAMAGE.
30 //============================================================================
31 
32 #ifndef MCKL_RANDOM_AES_HPP
33 #define MCKL_RANDOM_AES_HPP
34 
39 #include <mckl/random/counter.hpp>
41 
42 #if MCKL_HAS_AESNI
44 #else
45 #endif
46 
47 MCKL_PUSH_GCC_WARNING("-Wignored-attributes")
48 
49 #ifndef MCKL_AES128_ROUNDS
52 #define MCKL_AES128_ROUNDS 10
53 #endif
54 
57 #ifndef MCKL_AES192_ROUNDS
58 #define MCKL_AES192_ROUNDS 12
59 #endif
60 
63 #ifndef MCKL_AES256_ROUNDS
64 #define MCKL_AES256_ROUNDS 14
65 #endif
66 
69 #ifndef MCKL_ARS_ROUNDS
70 #define MCKL_ARS_ROUNDS 5
71 #endif
72 
73 namespace mckl {
74 
75 namespace internal {
76 
77 #if MCKL_USE_AESNI
78 
82 
83 template <typename Constants>
85 
86 template <typename KeySeqType>
88 
89 #else // MCKL_USE_AESNI
90 
94 
95 template <typename Constants>
97 
98 template <typename KeySeqType>
100 
101 #endif // MCKL_USE_AESNI
102 
103 } // namespace internal
104 
107 template <std::size_t Rounds = MCKL_AES128_ROUNDS>
108 using AES128KeySeq =
110 
113 template <std::size_t Rounds = MCKL_AES192_ROUNDS>
114 using AES192KeySeq =
116 
119 template <std::size_t Rounds = MCKL_AES256_ROUNDS>
120 using AES256KeySeq =
122 
128 template <std::size_t Rounds = MCKL_ARS_ROUNDS,
129  typename Constants = ARSConstants>
130 using ARSKeySeq =
132 
135 template <typename KeySeqType>
137 {
138  static_assert(KeySeqType::rounds() != 0,
139  "**AESGenerate** used with KeySeqType::rounds() equal to zero");
140 
141  public:
143  using key_type = typename KeySeqType::key_type;
144 
145  static constexpr std::size_t size() { return sizeof(std::uint32_t) * 4; }
146 
147  key_type key() const { return key_seq_.key(); }
148 
149  void reset(const key_type &key) { key_seq_.set(key); }
150 
151  void operator()(const void *plain, void *cipher) const
152  {
153  internal::AESGeneratorImpl<KeySeqType>::eval(plain, cipher, key_seq_);
154  }
155 
156  template <typename ResultType>
157  void operator()(ctr_type &ctr, ResultType *r) const
158  {
160  }
161 
162  template <typename ResultType>
163  void operator()(ctr_type &ctr, std::size_t n, ResultType *r) const
164  {
166  }
167 
168  friend bool operator==(const AESGenerator<KeySeqType> &gen1,
169  const AESGenerator<KeySeqType> &gen2)
170  {
171  return gen1.key_seq_ == gen2.key_seq_;
172  }
173 
174  friend bool operator!=(const AESGenerator<KeySeqType> &gen1,
175  const AESGenerator<KeySeqType> &gen2)
176  {
177  return !(gen1 == gen2);
178  }
179 
180  template <typename CharT, typename Traits>
181  friend std::basic_ostream<CharT, Traits> &operator<<(
182  std::basic_ostream<CharT, Traits> &os,
183  const AESGenerator<KeySeqType> &gen)
184  {
185  if (!os) {
186  return os;
187  }
188 
189  os << gen.key_seq_;
190 
191  return os;
192  }
193 
194  template <typename CharT, typename Traits>
195  friend std::basic_istream<CharT, Traits> &operator>>(
196  std::basic_istream<CharT, Traits> &is, AESGenerator<KeySeqType> &gen)
197  {
198  if (!is) {
199  return is;
200  }
201 
202  AESGenerator<KeySeqType> gen_tmp;
203  is >> std::ws >> gen_tmp.key_seq_;
204 
205  if (is) {
206  gen = std::move(gen_tmp);
207  }
208 
209  return is;
210  }
211 
212  private:
213  KeySeqType key_seq_;
214 }; // class AESGenerator
215 
218 template <typename ResultType, typename KeySeqType>
220 
223 template <typename ResultType, std::size_t Rounds = MCKL_AES128_ROUNDS>
225 
228 template <typename ResultType, std::size_t Rounds = MCKL_AES192_ROUNDS>
230 
233 template <typename ResultType, std::size_t Rounds = MCKL_AES256_ROUNDS>
235 
238 template <typename ResultType, std::size_t Rounds = MCKL_ARS_ROUNDS,
239  typename Constants = ARSConstants>
241 
245 
249 
253 
257 
261 
265 
269 
273 
274 } // namespace mckl
275 
277 
278 #endif // MCKL_RANDOM_AES_HPP
void operator()(const void *plain, void *cipher) const
Definition: aes.hpp:151
Counter based RNG engine.
Definition: counter.hpp:62
RNG generator using AES round functions.
Definition: aes.hpp:136
Default ARS constants.
#define MCKL_PUSH_GCC_WARNING(warning)
Definition: compiler.h:78
key_type key() const
Definition: aes.hpp:147
uint uint32_t
Definition: opencl.h:41
friend bool operator==(const AESGenerator< KeySeqType > &gen1, const AESGenerator< KeySeqType > &gen2)
Definition: aes.hpp:168
typename internal::CounterImpl< T, K >::type Counter
A counter type with the same width as std::array<T, K> but with possibly fewer elements.
Definition: increment.hpp:104
friend std::basic_istream< CharT, Traits > & operator>>(std::basic_istream< CharT, Traits > &is, AESGenerator< KeySeqType > &gen)
Definition: aes.hpp:195
void operator()(ctr_type &ctr, ResultType *r) const
Definition: aes.hpp:157
void operator()(ctr_type &ctr, std::size_t n, ResultType *r) const
Definition: aes.hpp:163
static void eval(const void *plain, void *cipher, const KeySeqType &ks)
Definition: aes_aesni.hpp:54
static constexpr std::size_t size()
Definition: aes.hpp:145
void reset(const key_type &key)
Definition: aes.hpp:149
Definition: mcmc.hpp:40
friend std::basic_ostream< CharT, Traits > & operator<<(std::basic_ostream< CharT, Traits > &os, const AESGenerator< KeySeqType > &gen)
Definition: aes.hpp:181
typename KeySeqType::key_type key_type
Definition: aes.hpp:143
#define MCKL_ARS_ROUNDS
ARSEngine default rounds.
Definition: aes.hpp:70
Counter< std::uint32_t, 4 > ctr_type
Definition: aes.hpp:142
#define MCKL_POP_GCC_WARNING
Definition: compiler.h:79
friend bool operator!=(const AESGenerator< KeySeqType > &gen1, const AESGenerator< KeySeqType > &gen2)
Definition: aes.hpp:174