File: aes.h

package info (click to toggle)
librandom123 1.09%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 5,284 kB
  • sloc: cpp: 4,849; ansic: 4,407; perl: 108; sh: 37; makefile: 3
file content (344 lines) | stat: -rw-r--r-- 11,986 bytes parent folder | download | duplicates (5)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
/*
Copyright 2010-2011, D. E. Shaw Research.
All rights reserved.

Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are
met:

* Redistributions of source code must retain the above copyright
  notice, this list of conditions, and the following disclaimer.

* Redistributions in binary form must reproduce the above copyright
  notice, this list of conditions, and the following disclaimer in the
  documentation and/or other materials provided with the distribution.

* Neither the name of D. E. Shaw Research nor the names of its
  contributors may be used to endorse or promote products derived from
  this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef __Random123_aes_dot_hpp__
#define __Random123_aes_dot_hpp__

#include "features/compilerfeatures.h"
#include "array.h"

/* Implement a bona fide AES block cipher.  It's minimally
// checked against the test vector in FIPS-197 in ut_aes.cpp. */
#if R123_USE_AES_NI

/** @ingroup AESNI */
typedef struct r123array1xm128i aesni1xm128i_ctr_t;
/** @ingroup AESNI */
typedef struct r123array1xm128i aesni1xm128i_ukey_t;
/** @ingroup AESNI */
typedef struct r123array4x32 aesni4x32_ukey_t;
/** @ingroup AESNI */
enum r123_enum_aesni1xm128i { aesni1xm128i_rounds = 10 };

/** \cond HIDDEN_FROM_DOXYGEN */
R123_STATIC_INLINE __m128i AES_128_ASSIST (__m128i temp1, __m128i temp2) { 
    __m128i temp3; 
    temp2 = _mm_shuffle_epi32 (temp2 ,0xff); 
    temp3 = _mm_slli_si128 (temp1, 0x4);
    temp1 = _mm_xor_si128 (temp1, temp3);
    temp3 = _mm_slli_si128 (temp3, 0x4);
    temp1 = _mm_xor_si128 (temp1, temp3);
    temp3 = _mm_slli_si128 (temp3, 0x4);
    temp1 = _mm_xor_si128 (temp1, temp3);
    temp1 = _mm_xor_si128 (temp1, temp2); 
    return temp1; 
}

R123_STATIC_INLINE void aesni1xm128iexpand(aesni1xm128i_ukey_t uk, __m128i ret[11])
{
    __m128i rkey = uk.v[0].m;
    __m128i tmp2;

    ret[0] = rkey;
    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x1);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[1] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x2);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[2] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x4);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[3] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x8);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[4] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x10);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[5] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x20);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[6] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x40);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[7] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x80);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[8] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x1b);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[9] = rkey;

    tmp2 = _mm_aeskeygenassist_si128(rkey, 0x36);
    rkey = AES_128_ASSIST(rkey, tmp2);
    ret[10] = rkey;
}
/** \endcond */
    
#ifdef __cplusplus
/** @ingroup AESNI */
struct aesni1xm128i_key_t{ 
    __m128i k[11]; 
    aesni1xm128i_key_t(){
        aesni1xm128i_ukey_t uk;
        uk.v[0].m = _mm_setzero_si128();
        aesni1xm128iexpand(uk, k);
    }
    aesni1xm128i_key_t(const aesni1xm128i_ukey_t& uk){
        aesni1xm128iexpand(uk, k);
    }
    aesni1xm128i_key_t(const aesni4x32_ukey_t& uk){
        aesni1xm128i_ukey_t uk128;
        uk128.v[0].m = _mm_set_epi32(uk.v[3], uk.v[2], uk.v[1], uk.v[0]);
        aesni1xm128iexpand(uk128, k);
    }
    aesni1xm128i_key_t& operator=(const aesni1xm128i_ukey_t& uk){
        aesni1xm128iexpand(uk, k);
        return *this;
    }
    aesni1xm128i_key_t& operator=(const aesni4x32_ukey_t& uk){
        aesni1xm128i_ukey_t uk128;
        uk128.v[0].m = _mm_set_epi32(uk.v[3], uk.v[2], uk.v[1], uk.v[0]);
        aesni1xm128iexpand(uk128, k);
        return *this;
    }
};
#else
typedef struct { 
    __m128i k[11]; 
}aesni1xm128i_key_t;

/** @ingroup AESNI */
R123_STATIC_INLINE aesni1xm128i_key_t aesni1xm128ikeyinit(aesni1xm128i_ukey_t uk){
    aesni1xm128i_key_t ret;
    aesni1xm128iexpand(uk, ret.k);
    return ret;
}
#endif

/** @ingroup AESNI */
R123_STATIC_INLINE aesni1xm128i_ctr_t aesni1xm128i(aesni1xm128i_ctr_t in, aesni1xm128i_key_t k) {
    __m128i x = _mm_xor_si128(k.k[0], in.v[0].m);
    x = _mm_aesenc_si128(x, k.k[1]);
    x = _mm_aesenc_si128(x, k.k[2]);
    x = _mm_aesenc_si128(x, k.k[3]);
    x = _mm_aesenc_si128(x, k.k[4]);
    x = _mm_aesenc_si128(x, k.k[5]);
    x = _mm_aesenc_si128(x, k.k[6]);
    x = _mm_aesenc_si128(x, k.k[7]);
    x = _mm_aesenc_si128(x, k.k[8]);
    x = _mm_aesenc_si128(x, k.k[9]);
    x = _mm_aesenclast_si128(x, k.k[10]);
    {
      aesni1xm128i_ctr_t ret;
      ret.v[0].m = x;
      return ret;
    }
}

/** @ingroup AESNI */
R123_STATIC_INLINE aesni1xm128i_ctr_t aesni1xm128i_R(unsigned R, aesni1xm128i_ctr_t in, aesni1xm128i_key_t k){
    R123_ASSERT(R==10);
    return aesni1xm128i(in, k);
}


/** @ingroup AESNI */
typedef struct r123array4x32 aesni4x32_ctr_t;
/** @ingroup AESNI */
typedef aesni1xm128i_key_t aesni4x32_key_t;
/** @ingroup AESNI */
enum r123_enum_aesni4x32 { aesni4x32_rounds = 10 };
/** @ingroup AESNI */
R123_STATIC_INLINE aesni4x32_key_t aesni4x32keyinit(aesni4x32_ukey_t uk){
    aesni1xm128i_ukey_t uk128;
    aesni4x32_key_t ret;
    uk128.v[0].m = _mm_set_epi32(uk.v[3], uk.v[2], uk.v[1], uk.v[0]);
    aesni1xm128iexpand(uk128, ret.k);
    return ret;
}

/** @ingroup AESNI */
/** The aesni4x32_R function provides a C API to the @ref AESNI "AESNI" CBRNG, allowing the number of rounds to be specified explicitly **/
R123_STATIC_INLINE aesni4x32_ctr_t aesni4x32_R(unsigned int Nrounds, aesni4x32_ctr_t c, aesni4x32_key_t k){
    aesni1xm128i_ctr_t c128;
    c128.v[0].m = _mm_set_epi32(c.v[3], c.v[2], c.v[1], c.v[0]);
    c128 = aesni1xm128i_R(Nrounds, c128, k);
    _mm_storeu_si128((__m128i*)&c.v[0], c128.v[0].m);
    return c;
}

#define aesni4x32_rounds aesni1xm128i_rounds

/** The aesni4x32 macro provides a C API to the @ref AESNI "AESNI" CBRNG, uses the default number of rounds i.e. \c aesni4x32_rounds **/
/** @ingroup AESNI */
#define aesni4x32(c,k) aesni4x32_R(aesni4x32_rounds, c, k)

#ifdef __cplusplus
namespace r123{
/** 
@defgroup AESNI ARS and AESNI Classes and Typedefs

The ARS4x32, ARS1xm128i, AESNI4x32 and AESNI1xm128i classes export the member functions, typedefs and
operator overloads required by a @ref CBRNG "CBRNG" class.

ARS1xm128i and AESNI1xm128i are based on the AES block cipher and rely on the AES-NI hardware instructions
available on some some new (2011) CPUs.

The ARS1xm128i CBRNG and the use of AES for random number generation are described in 
<a href="http://dl.acm.org/citation.cfm?doid=2063405"><i>Parallel Random Numbers:  As Easy as 1, 2, 3</i> </a>.
Although it uses some cryptographic primitives, ARS1xm128i uses a cryptographically weak key schedule and is \b not suitable for cryptographic use.

@class AESNI1xm128i
@ingroup AESNI
AESNI exports the member functions, typedefs and operator overloads required by a @ref CBRNG class.

AESNI1xm128i uses the crypotgraphic AES round function, including the cryptographic key schedule.

In contrast to the other CBRNGs in the Random123 library, the AESNI1xm128i_R::key_type is opaque
and is \b not identical to the AESNI1xm128i_R::ukey_type.  Creating a key_type, using either the constructor
or assignment operator, is significantly more time-consuming than running the bijection (hundreds
of clock cycles vs. tens of clock cycles).

AESNI1xm128i is only available when the feature-test macro R123_USE_AES_NI is true, which
should occur only when the compiler is configured to generate AES-NI instructions (or
when defaults are overridden by compile-time, compiler-command-line options).

As of September 2011, the authors know of no statistical flaws with AESNI1xm128i.  It
would be an event of major cryptographic note if any such flaws were ever found.
*/
struct AESNI1xm128i{
    typedef aesni1xm128i_ctr_t ctr_type;
    typedef aesni1xm128i_ukey_t ukey_type;
    typedef aesni1xm128i_key_t key_type;
    static const unsigned int rounds=10;
    ctr_type operator()(ctr_type ctr, key_type key) const{
        return aesni1xm128i(ctr, key);
    }
};

/* @class AESNI4x32 */
struct AESNI4x32{
    typedef aesni4x32_ctr_t ctr_type;
    typedef aesni4x32_ukey_t ukey_type;
    typedef aesni4x32_key_t key_type;
    static const unsigned int rounds=10;
    ctr_type operator()(ctr_type ctr, key_type key) const{
        return aesni4x32(ctr, key);
    }
};

/** @ingroup AESNI
    @class AESNI1xm128i_R

AESNI1xm128i_R is provided for completeness, but is only instantiable with ROUNDS=10, in
which case it is identical to AESNI1xm128i */
template <unsigned ROUNDS=10> 
struct AESNI1xm128i_R : public AESNI1xm128i{
    R123_STATIC_ASSERT(ROUNDS==10, "AESNI1xm128i_R<R> is only valid with R=10");
};

/** @class AESNI4x32_R **/
template <unsigned ROUNDS=10> 
struct AESNI4x32_R : public AESNI4x32{
    R123_STATIC_ASSERT(ROUNDS==10, "AESNI4x32_R<R> is only valid with R=10");
};
} // namespace r123
#endif /* __cplusplus */

#endif /* R123_USE_AES_NI */

#if R123_USE_AES_OPENSSL
#include <openssl/aes.h>
typedef struct r123array16x8 aesopenssl16x8_ctr_t;
typedef struct r123array16x8 aesopenssl16x8_ukey_t;
#ifdef __cplusplus
struct aesopenssl16x8_key_t{
    AES_KEY k;
    aesopenssl16x8_key_t(){
        aesopenssl16x8_ukey_t ukey={{}};
        AES_set_encrypt_key((const unsigned char *)&ukey.v[0], 128, &k);
    }
    aesopenssl16x8_key_t(const aesopenssl16x8_ukey_t& ukey){
        AES_set_encrypt_key((const unsigned char *)&ukey.v[0], 128, &k);
    }
    aesopenssl16x8_key_t& operator=(const aesopenssl16x8_ukey_t& ukey){
        AES_set_encrypt_key((const unsigned char *)&ukey.v[0], 128, &k);
        return *this;
    }
};
#else
typedef struct aesopenssl16x8_key_t{
    AES_KEY k;
}aesopenssl16x8_key_t;
R123_STATIC_INLINE struct aesopenssl16x8_key_t aesopenssl16x8keyinit(aesopenssl16x8_ukey_t uk){
    aesopenssl16x8_key_t ret;
    AES_set_encrypt_key((const unsigned char *)&uk.v[0], 128, &ret.k);
    return ret;
}
#endif

R123_STATIC_INLINE R123_FORCE_INLINE(aesopenssl16x8_ctr_t aesopenssl16x8_R(aesopenssl16x8_ctr_t ctr, aesopenssl16x8_key_t key));
R123_STATIC_INLINE
aesopenssl16x8_ctr_t aesopenssl16x8_R(aesopenssl16x8_ctr_t ctr, aesopenssl16x8_key_t key){
    aesopenssl16x8_ctr_t ret;
    AES_encrypt((const unsigned char*)&ctr.v[0], (unsigned char *)&ret.v[0], &key.k);
    return ret;
}

#define aesopenssl16x8_rounds aesni4x32_rounds
#define aesopenssl16x8(c,k) aesopenssl16x8_R(aesopenssl16x8_rounds)

#ifdef __cplusplus
namespace r123{
struct AESOpenSSL16x8{
    typedef aesopenssl16x8_ctr_t ctr_type;
    typedef aesopenssl16x8_key_t key_type;
    typedef aesopenssl16x8_ukey_t ukey_type;
    static const unsigned int rounds=10;
    ctr_type operator()(const ctr_type& in, const key_type& k){
        ctr_type out;
        AES_encrypt((const unsigned char *)&in[0], (unsigned char *)&out[0], &k.k);
        return out;
    }
};
} // namespace r123
#endif /* __cplusplus */
#endif /* R123_USE_AES_OPENSSL */

#endif