Repository navigation
Expand file tree
/
Copy pathaesctr.c
More file actions
202 lines (173 loc) · 6.77 KB
/
Copy pathaesctr.c
File metadata and controls
202 lines (173 loc) · 6.77 KB
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
/* Based heavily on public-domain code by Romain Dolbeau
* Different handling of nonce+counter than original version using
* separated 64-bit nonce and internal 64-bit counter, starting from zero
* Public Domain */
#include <stddef.h>
#include <stdint.h>
#include "simd.h"
#include "aesctr.h"
static inline void vaesni_encrypt8(uint8_t out[512], __m128i *n, const __m128i *rkeys, int rounds)
{
int i;
__m512i f0,f1,f2,f3,f4,f5,f6,f7,k;
const __m512i off0 = _mm512_set_epi64(3,0,2,0,1,0,0,0);
const __m512i off1 = _mm512_broadcast_i64x2(_mm_set_epi64x(4,0));
const __m512i off2 = _mm512_broadcast_i64x2(_mm_set_epi64x(8,0));
const __m512i off3 = _mm512_broadcast_i64x2(_mm_set_epi64x(12,0));
/* Increase counter in 8 consecutive blocks */
f0 = _mm512_broadcast_i32x4(_mm_load_si128(n));
f0 = _mm512_add_epi64(f0,off0);
f1 = _mm512_add_epi64(f0,off1);
f2 = _mm512_add_epi64(f0,off2);
f3 = _mm512_add_epi64(f0,off3);
f4 = _mm512_add_epi64(f1,off3);
f5 = _mm512_add_epi64(f2,off3);
f6 = _mm512_add_epi64(f3,off3);
f7 = _mm512_add_epi64(f4,off3);
/* Write counter for next iteration, increased by 32 */
_mm_store_si128(n,_mm_add_epi64(_mm512_castsi512_si128(f5),_mm512_castsi512_si128(off3)));
/* Actual AES encryption, 5x interleaved */
k = _mm512_broadcast_i32x4(_mm_load_si128(rkeys));
f0 = _mm512_xor_si512(f0,k);
f1 = _mm512_xor_si512(f1,k);
f2 = _mm512_xor_si512(f2,k);
f3 = _mm512_xor_si512(f3,k);
f4 = _mm512_xor_si512(f4,k);
f5 = _mm512_xor_si512(f5,k);
f6 = _mm512_xor_si512(f6,k);
f7 = _mm512_xor_si512(f7,k);
for(i=1;i<rounds;i++) {
k = _mm512_broadcast_i32x4(_mm_load_si128(rkeys+i));
f0 = _mm512_aesenc_epi128(f0,k);
f1 = _mm512_aesenc_epi128(f1,k);
f2 = _mm512_aesenc_epi128(f2,k);
f3 = _mm512_aesenc_epi128(f3,k);
f4 = _mm512_aesenc_epi128(f4,k);
f5 = _mm512_aesenc_epi128(f5,k);
f6 = _mm512_aesenc_epi128(f6,k);
f7 = _mm512_aesenc_epi128(f7,k);
}
k = _mm512_broadcast_i64x2(_mm_load_si128(rkeys+rounds));
f0 = _mm512_aesenclast_epi128(f0,k);
f1 = _mm512_aesenclast_epi128(f1,k);
f2 = _mm512_aesenclast_epi128(f2,k);
f3 = _mm512_aesenclast_epi128(f3,k);
f4 = _mm512_aesenclast_epi128(f4,k);
f5 = _mm512_aesenclast_epi128(f5,k);
f6 = _mm512_aesenclast_epi128(f6,k);
f7 = _mm512_aesenclast_epi128(f7,k);
/* Write results */
_mm512_storeu_si512((__m512i*)(out+ 0),f0);
_mm512_storeu_si512((__m512i*)(out+ 64),f1);
_mm512_storeu_si512((__m512i*)(out+128),f2);
_mm512_storeu_si512((__m512i*)(out+192),f3);
_mm512_storeu_si512((__m512i*)(out+256),f4);
_mm512_storeu_si512((__m512i*)(out+320),f5);
_mm512_storeu_si512((__m512i*)(out+384),f6);
_mm512_storeu_si512((__m512i*)(out+448),f7);
}
void aes128ctr_init(aes128ctr_ctx *state, const uint8_t key[16], uint64_t nonce)
{
__m128i temp0, temp1, temp2;
const __m128i zero = _mm_setzero_si128();
int idx = 0;
_mm_store_si128(&state->n,_mm_loadl_epi64((__m128i *)&nonce));
#define BLOCK1(IMM) \
temp1 = _mm_aeskeygenassist_si128(temp0, IMM); \
temp2 = (__m128i)_mm_shuffle_ps((__m128)zero, (__m128)temp0, 0x10); \
temp0 = _mm_xor_si128(temp0, temp2); \
temp2 = (__m128i)_mm_shuffle_ps((__m128)temp2, (__m128)temp0, 0x8c); \
temp0 = _mm_xor_si128(temp0, temp2); \
temp1 = (__m128i)_mm_shuffle_ps((__m128)temp1, (__m128)temp1, 0xff); \
temp0 = _mm_xor_si128(temp0, temp1)
temp0 = _mm_loadu_si128((__m128i *)key);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x01);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x02);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x04);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x08);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x10);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x20);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x40);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x80);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x1b);
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x36);
_mm_store_si128(&state->rkeys[idx++],temp0);
#undef BLOCK1
}
void aes256ctr_init(aes256ctr_ctx *state, const uint8_t key[32], uint64_t nonce)
{
__m128i key0, key1, temp0, temp1, temp2, temp4;
int idx = 0;
key0 = _mm_loadu_si128((__m128i *)(key+ 0));
key1 = _mm_loadu_si128((__m128i *)(key+16));
_mm_store_si128(&state->n,_mm_loadl_epi64((__m128i *)&nonce));
#define BLOCK1(IMM) \
temp1 = _mm_aeskeygenassist_si128(temp2, IMM); \
_mm_store_si128(&state->rkeys[idx++],temp2); \
temp4 = (__m128i)_mm_shuffle_ps((__m128)temp4, (__m128)temp0, 0x10); \
temp0 = _mm_xor_si128(temp0, temp4); \
temp4 = (__m128i)_mm_shuffle_ps((__m128)temp4, (__m128)temp0, 0x8c); \
temp0 = _mm_xor_si128(temp0, temp4); \
temp1 = (__m128i)_mm_shuffle_ps((__m128)temp1, (__m128)temp1, 0xff); \
temp0 = _mm_xor_si128(temp0, temp1)
#define BLOCK2(IMM) \
temp1 = _mm_aeskeygenassist_si128(temp0, IMM); \
_mm_store_si128(&state->rkeys[idx++],temp0); \
temp4 = (__m128i)_mm_shuffle_ps((__m128)temp4, (__m128)temp2, 0x10); \
temp2 = _mm_xor_si128(temp2, temp4); \
temp4 = (__m128i)_mm_shuffle_ps((__m128)temp4, (__m128)temp2, 0x8c); \
temp2 = _mm_xor_si128(temp2, temp4); \
temp1 = (__m128i)_mm_shuffle_ps((__m128)temp1, (__m128)temp1, 0xaa); \
temp2 = _mm_xor_si128(temp2, temp1)
temp0 = key0;
temp2 = key1;
temp4 = _mm_setzero_si128();
_mm_store_si128(&state->rkeys[idx++],temp0);
BLOCK1(0x01);
BLOCK2(0x01);
BLOCK1(0x02);
BLOCK2(0x02);
BLOCK1(0x04);
BLOCK2(0x04);
BLOCK1(0x08);
BLOCK2(0x08);
BLOCK1(0x10);
BLOCK2(0x10);
BLOCK1(0x20);
BLOCK2(0x20);
BLOCK1(0x40);
_mm_store_si128(&state->rkeys[idx++],temp0);
#undef BLOCK1
}
void aes128ctr_select(aes128ctr_ctx *state, uint64_t nonce) {
_mm_store_si128(&state->n,_mm_loadl_epi64((__m128i *)&nonce));
}
void aes256ctr_select(aes256ctr_ctx *state, uint64_t nonce) {
_mm_store_si128(&state->n,_mm_loadl_epi64((__m128i *)&nonce));
}
void aes128ctr_squeezeblocks(uint8_t *out, size_t nblocks, aes128ctr_ctx *state)
{
size_t i;
for(i=0;i<nblocks;i++) {
vaesni_encrypt8(out, &state->n, state->rkeys, 10);
out += AES128CTR_BLOCKBYTES;
}
}
void aes256ctr_squeezeblocks(uint8_t *out, size_t nblocks, aes256ctr_ctx *state)
{
size_t i;
for(i=0;i<nblocks;i++) {
vaesni_encrypt8(out, &state->n, state->rkeys, 14);
out += AES256CTR_BLOCKBYTES;
}
}