32 #ifndef __Random123_aes_dot_hpp__
33 #define __Random123_aes_dot_hpp__
53 R123_STATIC_INLINE __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2) {
55 temp2 = _mm_shuffle_epi32 (temp2 ,0xff);
56 temp3 = _mm_slli_si128 (temp1, 0x4);
57 temp1 = _mm_xor_si128 (temp1, temp3);
58 temp3 = _mm_slli_si128 (temp3, 0x4);
59 temp1 = _mm_xor_si128 (temp1, temp3);
60 temp3 = _mm_slli_si128 (temp3, 0x4);
61 temp1 = _mm_xor_si128 (temp1, temp3);
62 temp1 = _mm_xor_si128 (temp1, temp2);
68 __m128i rkey = uk.v[0].m;
72 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x1);
73 rkey = AES_128_ASSIST(rkey, tmp2);
76 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x2);
77 rkey = AES_128_ASSIST(rkey, tmp2);
80 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x4);
81 rkey = AES_128_ASSIST(rkey, tmp2);
84 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x8);
85 rkey = AES_128_ASSIST(rkey, tmp2);
88 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x10);
89 rkey = AES_128_ASSIST(rkey, tmp2);
92 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x20);
93 rkey = AES_128_ASSIST(rkey, tmp2);
96 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x40);
97 rkey = AES_128_ASSIST(rkey, tmp2);
100 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x80);
101 rkey = AES_128_ASSIST(rkey, tmp2);
104 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x1b);
105 rkey = AES_128_ASSIST(rkey, tmp2);
108 tmp2 = _mm_aeskeygenassist_si128(rkey, 0x36);
109 rkey = AES_128_ASSIST(rkey, tmp2);
120 uk.v[0].m = _mm_setzero_si128();
121 aesni1xm128iexpand(uk,
k);
124 aesni1xm128iexpand(uk,
k);
128 uk128.v[0].m = _mm_set_epi32(uk.v[3], uk.v[2], uk.v[1], uk.v[0]);
129 aesni1xm128iexpand(uk128,
k);
132 aesni1xm128iexpand(uk,
k);
137 uk128.v[0].m = _mm_set_epi32(uk.v[3], uk.v[2], uk.v[1], uk.v[0]);
138 aesni1xm128iexpand(uk128,
k);
142 for(
int i=0; i<11; ++i){
146 if( li != ri )
return false;
151 return !(*
this == rhs);
155 for(
int i=0; i<10; ++i){
164 for(
int i=0; i<11; ++i){
179 aesni1xm128iexpand(uk, ret.
k);
186 __m128i x = _mm_xor_si128(k.
k[0], in.v[0].m);
187 x = _mm_aesenc_si128(x, k.
k[1]);
188 x = _mm_aesenc_si128(x, k.
k[2]);
189 x = _mm_aesenc_si128(x, k.
k[3]);
190 x = _mm_aesenc_si128(x, k.
k[4]);
191 x = _mm_aesenc_si128(x, k.
k[5]);
192 x = _mm_aesenc_si128(x, k.
k[6]);
193 x = _mm_aesenc_si128(x, k.
k[7]);
194 x = _mm_aesenc_si128(x, k.
k[8]);
195 x = _mm_aesenc_si128(x, k.
k[9]);
196 x = _mm_aesenclast_si128(x, k.
k[10]);
221 uk128.v[0].m = _mm_set_epi32(uk.v[3], uk.v[2], uk.v[1], uk.v[0]);
222 aesni1xm128iexpand(uk128, ret.
k);
230 c128.v[0].m = _mm_set_epi32(c.v[3], c.v[2], c.v[1], c.v[0]);
232 _mm_storeu_si128((__m128i*)&c.v[0], c128.v[0].m);
236 #define aesni4x32_rounds aesni1xm128i_rounds
240 #define aesni4x32(c,k) aesni4x32_R(aesni4x32_rounds, c, k)
301 template <
unsigned ROUNDS=10>
303 R123_STATIC_ASSERT(ROUNDS==10,
"AESNI1xm128i_R<R> is only valid with R=10");
307 template <
unsigned ROUNDS=10>
309 R123_STATIC_ASSERT(ROUNDS==10,
"AESNI4x32_R<R> is only valid with R=10");
316 #if R123_USE_AES_OPENSSL
317 #include <openssl/aes.h>
318 typedef struct r123array16x8 aesopenssl16x8_ctr_t;
319 typedef struct r123array16x8 aesopenssl16x8_ukey_t;
321 struct aesopenssl16x8_key_t{
323 aesopenssl16x8_key_t(){
324 aesopenssl16x8_ukey_t ukey={{}};
325 AES_set_encrypt_key((
const unsigned char *)&ukey.v[0], 128, &k);
327 aesopenssl16x8_key_t(
const aesopenssl16x8_ukey_t& ukey){
328 AES_set_encrypt_key((
const unsigned char *)&ukey.v[0], 128, &k);
330 aesopenssl16x8_key_t& operator=(
const aesopenssl16x8_ukey_t& ukey){
331 AES_set_encrypt_key((
const unsigned char *)&ukey.v[0], 128, &k);
334 bool operator==(
const aesopenssl16x8_key_t& rhs)
const{
335 return (k.rounds == rhs.k.rounds) && 0==::memcmp(&k.rd_key[0], &rhs.k.rd_key[0], (k.rounds+1) * 4 *
sizeof(uint32_t));
337 bool operator!=(
const aesopenssl16x8_key_t& rhs)
const{
338 return !(*
this == rhs);
340 friend std::ostream&
operator<<(std::ostream& os,
const aesopenssl16x8_key_t& v){
342 const unsigned int *p = &v.k.rd_key[0];
343 for(
int i=0; i<(v.k.rounds+1); ++i){
344 os <<
" " << p[0] <<
" " << p[1] <<
" " << p[2] <<
" " << p[3];
349 friend std::istream&
operator>>(std::istream& is, aesopenssl16x8_key_t& v){
351 unsigned int *p = &v.k.rd_key[0];
352 for(
int i=0; i<(v.k.rounds+1); ++i){
353 is >> p[0] >> p[1] >> p[2] >> p[3];
360 typedef struct aesopenssl16x8_key_t{
362 }aesopenssl16x8_key_t;
363 R123_STATIC_INLINE
struct aesopenssl16x8_key_t aesopenssl16x8keyinit(aesopenssl16x8_ukey_t uk){
364 aesopenssl16x8_key_t ret;
365 AES_set_encrypt_key((
const unsigned char *)&uk.v[0], 128, &ret.k);
370 R123_STATIC_INLINE R123_FORCE_INLINE(aesopenssl16x8_ctr_t aesopenssl16x8_R(aesopenssl16x8_ctr_t ctr, aesopenssl16x8_key_t key));
372 aesopenssl16x8_ctr_t aesopenssl16x8_R(aesopenssl16x8_ctr_t ctr, aesopenssl16x8_key_t key){
373 aesopenssl16x8_ctr_t ret;
374 AES_encrypt((
const unsigned char*)&ctr.v[0], (
unsigned char *)&ret.v[0], &key.k);
378 #define aesopenssl16x8_rounds aesni4x32_rounds
379 #define aesopenssl16x8(c,k) aesopenssl16x8_R(aesopenssl16x8_rounds)
383 struct AESOpenSSL16x8{
384 typedef aesopenssl16x8_ctr_t ctr_type;
385 typedef aesopenssl16x8_key_t key_type;
386 typedef aesopenssl16x8_ukey_t ukey_type;
387 static const unsigned int rounds=10;
388 ctr_type operator()(
const ctr_type& in,
const key_type& k){
390 AES_encrypt((
const unsigned char *)&in[0], (
unsigned char *)&out[0], &k.k);
static std::ostream & operator<<(std::ostream &os, const r123m128i &m)
Definition: sse.h:241
r123_enum_aesni4x32
Definition: aes.h:216
aesni1xm128i_key_t & operator=(const aesni1xm128i_ukey_t &uk)
Definition: aes.h:131
__m128i k[11]
Definition: aes.h:117
static bool operator==(const r123m128i &lhs, const r123m128i &rhs)
Definition: sse.h:233
struct r123array4x32 aesni4x32_ukey_t
Definition: aes.h:48
static aesni1xm128i_ctr_t aesni1xm128i(aesni1xm128i_ctr_t in, aesni1xm128i_key_t k)
Definition: aes.h:185
aesni1xm128i_key_t aesni4x32_key_t
Definition: aes.h:214
#define aesni4x32_rounds
Definition: aes.h:236
bool operator!=(const aesni1xm128i_key_t &rhs) const
Definition: aes.h:150
aesni4x32_key_t key_type
Definition: aes.h:289
aesni1xm128i_key_t(const aesni1xm128i_ukey_t &uk)
Definition: aes.h:123
static bool operator!=(const r123m128i &lhs, const r123m128i &rhs)
Definition: sse.h:235
static std::istream & operator>>(std::istream &is, r123m128i &m)
Definition: sse.h:250
aesni1xm128i_key_t & operator=(const aesni4x32_ukey_t &uk)
Definition: aes.h:135
static const unsigned int rounds
Definition: aes.h:290
aesni4x32_ctr_t ctr_type
Definition: aes.h:287
static aesni4x32_key_t aesni4x32keyinit(aesni4x32_ukey_t uk)
Definition: aes.h:218
aesni1xm128i_key_t key_type
Definition: aes.h:278
ctr_type operator()(ctr_type ctr, key_type key) const
Definition: aes.h:291
friend std::ostream & operator<<(std::ostream &os, const aesni1xm128i_key_t &v)
Definition: aes.h:153
struct r123array1xm128i aesni1xm128i_ukey_t
Definition: aes.h:46
bool operator==(const aesni1xm128i_key_t &rhs) const
Definition: aes.h:141
struct r123array4x32 aesni4x32_ctr_t
Definition: aes.h:212
struct r123array1xm128i aesni1xm128i_ctr_t
Definition: aes.h:44
static aesni1xm128i_ctr_t aesni1xm128i_R(unsigned R, aesni1xm128i_ctr_t in, aesni1xm128i_key_t k)
Definition: aes.h:205
aesni1xm128i_ukey_t ukey_type
Definition: aes.h:277
aesni1xm128i_ctr_t ctr_type
Definition: aes.h:276
friend std::istream & operator>>(std::istream &is, aesni1xm128i_key_t &v)
Definition: aes.h:162
aesni1xm128i_key_t(const aesni4x32_ukey_t &uk)
Definition: aes.h:126
#define aesni4x32(c, k)
Definition: aes.h:240
ctr_type operator()(ctr_type ctr, key_type key) const
Definition: aes.h:280
static const unsigned int rounds
Definition: aes.h:279
aesni1xm128i_key_t()
Definition: aes.h:118
static aesni4x32_ctr_t aesni4x32_R(unsigned int Nrounds, aesni4x32_ctr_t c, aesni4x32_key_t k)
Definition: aes.h:228
aesni4x32_ukey_t ukey_type
Definition: aes.h:288
r123_enum_aesni1xm128i
Definition: aes.h:50
__m128i m
Definition: sse.h:149