-
Notifications
You must be signed in to change notification settings - Fork 24
/
aes_kernel.cu
473 lines (408 loc) · 13.4 KB
/
aes_kernel.cu
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
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
#include "aes_core.h"
#include "aes_kernel.h"
/*******************************************************************
AES CBC kernel
******************************************************************/
__global__ void
AES_cbc_128_encrypt_kernel_SharedMem(const uint8_t *in_all,
uint8_t *out_all,
const uint32_t *pkt_offset,
const uint8_t *keys,
uint8_t *ivs,
const unsigned int num_flows,
uint8_t *checkbits = 0)
{
__shared__ uint32_t shared_Te0[256];
__shared__ uint32_t shared_Te1[256];
__shared__ uint32_t shared_Te2[256];
__shared__ uint32_t shared_Te3[256];
__shared__ uint32_t shared_Rcon[10];
/* computer the thread id */
int idx = blockDim.x * blockIdx.x + threadIdx.x;
/* initialize T boxes */
for (unsigned i = 0 ; i *blockDim.x < 256 ; i++) {
unsigned index = threadIdx.x + i * blockDim.x;
if (index >= 256)
break;
shared_Te0[index] = Te0_ConstMem[index];
shared_Te1[index] = Te1_ConstMem[index];
shared_Te2[index] = Te2_ConstMem[index];
shared_Te3[index] = Te3_ConstMem[index];
}
for(unsigned i = 0; i * blockDim.x < 10; i++){
int index = threadIdx.x + blockDim.x * i;
if(index < 10){
shared_Rcon[index] = rcon[index];
}
}
if (idx >= num_flows)
return;
/* make sure T boxes have been initialized. */
__syncthreads();
/* Locate data */
const uint8_t *in = pkt_offset[idx] + in_all;
uint8_t *out = pkt_offset[idx] + out_all;
const uint8_t *key = idx * 16 + keys;
uint8_t *ivec = idx * AES_BLOCK_SIZE + ivs;
/* Encrypt using cbc mode */
unsigned long len = pkt_offset[idx + 1] - pkt_offset[idx];
const unsigned char *iv = ivec;
while (len >= AES_BLOCK_SIZE) {
*((uint64_t*)out) = *((uint64_t*)in) ^ *((uint64_t*)iv);
*(((uint64_t*)out) + 1) = *(((uint64_t*)in) + 1) ^ *(((uint64_t*)iv) + 1);
AES_128_encrypt(out, out, key,
shared_Te0, shared_Te1, shared_Te2, shared_Te3, shared_Rcon);
iv = out;
len -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (len) {
for(unsigned n = 0; n < len; ++n)
out[n] = in[n] ^ iv[n];
for(unsigned n = len; n < AES_BLOCK_SIZE; ++n)
out[n] = iv[n];
AES_128_encrypt(out, out, key,
shared_Te0, shared_Te1, shared_Te2, shared_Te3, shared_Rcon);
iv = out;
}
*((uint4*)ivec) = *((uint4*)iv);
__syncthreads();
if (threadIdx.x == 0 && checkbits != 0)
*(checkbits + blockIdx.x) = 1;
}
__global__
void AES_cbc_128_decrypt_kernel_SharedMem(const uint8_t *in_all,
uint8_t *out_all,
uint8_t *keys,
uint8_t *ivs,
uint16_t *pkt_index,
unsigned long block_count,
uint8_t *checkbits = 0
)
{
int idx = blockDim.x * blockIdx.x + threadIdx.x;
__shared__ uint32_t shared_Td0[256];
__shared__ uint32_t shared_Td1[256];
__shared__ uint32_t shared_Td2[256];
__shared__ uint32_t shared_Td3[256];
__shared__ uint8_t shared_Td4[256];
__shared__ uint32_t shared_Rcon[10];
__shared__ uint32_t shared_Te0[256];
__shared__ uint32_t shared_Te1[256];
__shared__ uint32_t shared_Te2[256];
__shared__ uint32_t shared_Te3[256];
/* computer the thread id */
/* initialize T boxes */
for (unsigned i = 0 ; i *blockDim.x < 256 ; i++) {
unsigned index = threadIdx.x + i * blockDim.x;
if (index >= 256)
break;
shared_Te0[index] = Te0_ConstMem[index];
shared_Te1[index] = Te1_ConstMem[index];
shared_Te2[index] = Te2_ConstMem[index];
shared_Te3[index] = Te3_ConstMem[index];
shared_Td0[index] = Td0_ConstMem[index];
shared_Td1[index] = Td1_ConstMem[index];
shared_Td2[index] = Td2_ConstMem[index];
shared_Td3[index] = Td3_ConstMem[index];
shared_Td4[index] = Td4_ConstMem[index];
}
for(unsigned i = 0; i * blockDim.x < 10; i++){
int index = threadIdx.x + blockDim.x * i;
if(index < 10){
shared_Rcon[index] = rcon[index];
}
}
for (unsigned i = 0; i * blockDim.x < 10; i++) {
int index = threadIdx.x + blockDim.x * i;
if (index < 10) {
shared_Rcon[index] = rcon[index];
}
}
__syncthreads();
if (idx >= block_count)
return;
/* Locate data */
const uint8_t *in = idx * AES_BLOCK_SIZE + in_all;
uint8_t *out = idx * AES_BLOCK_SIZE + out_all;
uint16_t packet_index = pkt_index[idx];
uint32_t rk[4];
rk[0] = *((uint32_t*)(keys + 16 * packet_index));
rk[1] = *((uint32_t*)(keys + 16 * packet_index + 4));
rk[2] = *((uint32_t*)(keys + 16 * packet_index + 8));
rk[3] = *((uint32_t*)(keys + 16 * packet_index + 12));
uint8_t *ivec = packet_index * AES_BLOCK_SIZE + ivs;
/* Decrypt using cbc mode */
const unsigned char *iv;
if (idx == 0 || pkt_index[idx] != pkt_index[idx-1])
iv = ivec;
else
iv = in - AES_BLOCK_SIZE;
AES_128_decrypt(in, out, rk,
shared_Td0, shared_Td1, shared_Td2, shared_Td3, shared_Td4,
shared_Te0, shared_Te1, shared_Te2, shared_Te3, shared_Rcon);
*((uint64_t*)out) = *((uint64_t*)out) ^ *((uint64_t*)iv);
*(((uint64_t*)out) + 1) = *(((uint64_t*)out) + 1) ^ *(((uint64_t*)iv) + 1);
__syncthreads();
if (threadIdx.x == 0 && checkbits != 0)
*(checkbits + blockIdx.x) = 1;
}
/*******************************************************************
AES ECB kernel
******************************************************************/
__global__ void
AES_ecb_encrypt_kernel(const uint8_t *in_all,
uint8_t *out_all,
const uint8_t *keys,
uint16_t *pkt_index,
unsigned long block_count
)
{
__shared__ uint32_t shared_Te0[256];
__shared__ uint32_t shared_Te1[256];
__shared__ uint32_t shared_Te2[256];
__shared__ uint32_t shared_Te3[256];
__shared__ uint32_t shared_Rcon[10];
/* computer the thread id */
int idx = blockDim.x * blockIdx.x + threadIdx.x;
/* initialize T boxes, #threads in block should be larger than 256 */
for (unsigned i = 0; i * blockDim.x < 256; i++) {
unsigned index = i * blockDim.x + threadIdx.x;
if (index >= 256)
break;
shared_Te0[index] = Te0_ConstMem[index];
shared_Te1[index] = Te1_ConstMem[index];
shared_Te2[index] = Te2_ConstMem[index];
shared_Te3[index] = Te3_ConstMem[index];
}
for (unsigned i = 0; i * blockDim.x < 10; i++) {
unsigned index = threadIdx.x + blockDim.x * i;
if (index < 10) {
shared_Rcon[index] = rcon[index];
}
}
if (idx >= block_count)
return;
/* make sure T boxes have been initialized. */
__syncthreads();
/* Locate data */
const uint8_t *in = idx * AES_BLOCK_SIZE + in_all;
uint8_t *out = idx * AES_BLOCK_SIZE + out_all;
uint16_t pktIndex = pkt_index[idx];
const uint8_t *key = pktIndex * 16 + keys;
AES_128_encrypt(in, out, key,
shared_Te0, shared_Te1, shared_Te2, shared_Te3, shared_Rcon);
}
/**************************************************************************
Exported C++ function wrapper function for CUDA kernel
***************************************************************************/
void AES_cbc_128_decrypt_gpu(const uint8_t *in_d,
uint8_t *out_d,
uint8_t *keys_d,
uint8_t *ivs_d,
uint16_t *pkt_index_d,
unsigned long block_count,
uint8_t *checkbits_d,
const unsigned int threads_per_blk,
cudaStream_t stream )
{
unsigned int num_cuda_blks = (block_count+threads_per_blk - 1) / threads_per_blk;
if (stream == 0) {
AES_cbc_128_decrypt_kernel_SharedMem<<<num_cuda_blks, threads_per_blk>>>(
in_d, out_d, keys_d, ivs_d, pkt_index_d, block_count, checkbits_d);
} else {
AES_cbc_128_decrypt_kernel_SharedMem<<<num_cuda_blks, threads_per_blk, 0, stream>>>(
in_d, out_d, keys_d, ivs_d, pkt_index_d, block_count, checkbits_d);
}
}
void AES_cbc_128_encrypt_gpu(const uint8_t *in_d,
uint8_t *out_d,
const uint32_t *pkt_offset_d,
const uint8_t *keys_d,
uint8_t *ivs_d,
const unsigned int num_flows,
uint8_t *checkbits_d,
const unsigned int threads_per_blk,
cudaStream_t stream)
{
unsigned int num_cuda_blks = (num_flows+threads_per_blk - 1) / threads_per_blk;
if (stream == 0) {
AES_cbc_128_encrypt_kernel_SharedMem<<<num_cuda_blks, threads_per_blk>>>(
in_d, out_d, pkt_offset_d, keys_d, ivs_d, num_flows, checkbits_d);
} else {
AES_cbc_128_encrypt_kernel_SharedMem<<<num_cuda_blks, threads_per_blk, 0, stream>>>(
in_d, out_d, pkt_offset_d, keys_d, ivs_d, num_flows, checkbits_d);
}
}
void AES_ecb_128_encrypt_gpu(const uint8_t *in_d,
uint8_t *out_d,
const uint8_t *keys_d,
uint16_t *pkt_index_d,
unsigned long block_count,
const unsigned int threads_per_blk,
cudaStream_t stream)
{
unsigned int num_cuda_blks = (block_count + threads_per_blk - 1) / threads_per_blk;
if (stream == 0) {
AES_ecb_encrypt_kernel<<<num_cuda_blks, threads_per_blk>>>(
in_d, out_d, keys_d, pkt_index_d, block_count);
} else {
AES_ecb_encrypt_kernel<<<num_cuda_blks, threads_per_blk, 0, stream>>>(
in_d, out_d, keys_d, pkt_index_d, block_count);
}
}
/**************************************************************************
Key Setup for Decryption
***************************************************************************/
void AES_decrypt_key_prepare(uint8_t *dec_key,
const uint8_t *enc_key,
unsigned int key_bits)
{
uint32_t rk_buf[60];
uint32_t *rk = rk_buf;
int i = 0;
uint32_t temp;
rk[0] = GETU32(enc_key );
rk[1] = GETU32(enc_key + 4);
rk[2] = GETU32(enc_key + 8);
rk[3] = GETU32(enc_key + 12);
if (key_bits == 128) {
for (;;) {
temp = rk[3];
rk[4] = rk[0] ^
(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
(Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
(Te4[(temp ) & 0xff] & 0x0000ff00) ^
(Te4[(temp >> 24) ] & 0x000000ff) ^
rcon_host[i];
rk[5] = rk[1] ^ rk[4];
rk[6] = rk[2] ^ rk[5];
rk[7] = rk[3] ^ rk[6];
if (++i == 10) {
rk += 4;
goto end;
}
rk += 4;
}
}
rk[4] = GETU32(enc_key + 16);
rk[5] = GETU32(enc_key + 20);
if (key_bits == 192) {
for (;;) {
temp = rk[ 5];
rk[ 6] = rk[ 0] ^
(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
(Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
(Te4[(temp ) & 0xff] & 0x0000ff00) ^
(Te4[(temp >> 24) ] & 0x000000ff) ^
rcon_host[i];
rk[ 7] = rk[ 1] ^ rk[ 6];
rk[ 8] = rk[ 2] ^ rk[ 7];
rk[ 9] = rk[ 3] ^ rk[ 8];
if (++i == 8) {
rk += 6;
goto end;
}
rk[10] = rk[ 4] ^ rk[ 9];
rk[11] = rk[ 5] ^ rk[10];
rk += 6;
}
}
rk[6] = GETU32(enc_key + 24);
rk[7] = GETU32(enc_key + 28);
if (key_bits == 256) {
for (;;) {
temp = rk[ 7];
rk[ 8] = rk[ 0] ^
(Te4[(temp >> 16) & 0xff] & 0xff000000) ^
(Te4[(temp >> 8) & 0xff] & 0x00ff0000) ^
(Te4[(temp ) & 0xff] & 0x0000ff00) ^
(Te4[(temp >> 24) ] & 0x000000ff) ^
rcon_host[i];
rk[ 9] = rk[ 1] ^ rk[ 8];
rk[10] = rk[ 2] ^ rk[ 9];
rk[11] = rk[ 3] ^ rk[10];
if (++i == 7) {
rk += 8;
goto end;
}
temp = rk[11];
rk[12] = rk[ 4] ^
(Te4[(temp >> 24) ] & 0xff000000) ^
(Te4[(temp >> 16) & 0xff] & 0x00ff0000) ^
(Te4[(temp >> 8) & 0xff] & 0x0000ff00) ^
(Te4[(temp ) & 0xff] & 0x000000ff);
rk[13] = rk[ 5] ^ rk[12];
rk[14] = rk[ 6] ^ rk[13];
rk[15] = rk[ 7] ^ rk[14];
rk += 8;
}
}
end:
memcpy(dec_key, rk, 16);
}
/**************************************************************************
Experimental Codes
***************************************************************************/
__global__ void
AES_cbc_encrypt_kernel_SharedMem2_256(const uint8_t *in_all,
uint8_t *out_all,
const unsigned long flow_len,
const uint8_t *keys,
uint8_t *ivs)
{
__shared__ uint32_t shared_Te0[256];
__shared__ uint32_t shared_Te1[256];
__shared__ uint32_t shared_Te2[256];
__shared__ uint32_t shared_Te3[256];
__shared__ uint32_t shared_Rcon[10];
/* computer the thread id */
int idx = blockDim.x * blockIdx.x + threadIdx.x;
/* initialize T boxes */
for (unsigned i = 0 ; i *blockDim.x < 256 ; i++) {
unsigned index = threadIdx.x + i * blockDim.x;
if (index >= 256)
break;
shared_Te0[index] = Te0_ConstMem[index];
shared_Te1[index] = Te1_ConstMem[index];
shared_Te2[index] = Te2_ConstMem[index];
shared_Te3[index] = Te3_ConstMem[index];
}
for(unsigned i = 0; i * blockDim.x < 10; i++){
int index = threadIdx.x + blockDim.x * i;
if(index < 10){
shared_Rcon[index] = rcon[index];
}
}
/* make sure T boxes have been initialized. */
__syncthreads();
/* Locate data */
const uint8_t *in = idx * flow_len + in_all;
uint8_t *out = idx * flow_len + out_all;
const uint8_t *key = idx * 32 + keys;
uint8_t *ivec = idx * AES_BLOCK_SIZE + ivs;
/* Encrypt using cbc mode */
unsigned long len = flow_len;
const unsigned char *iv = ivec;
while (len >= AES_BLOCK_SIZE) {
*((uint64_t*)out) = *((uint64_t*)in) ^ *((uint64_t*)iv);
*(((uint64_t*)out)+1) = *(((uint64_t*)in)+1) ^ *(((uint64_t*)iv)+1);
AES_256_encrypt(out, out, key,
shared_Te0, shared_Te1, shared_Te2, shared_Te3, shared_Rcon);
iv = out;
len -= AES_BLOCK_SIZE;
in += AES_BLOCK_SIZE;
out += AES_BLOCK_SIZE;
}
if (len) {
for (unsigned n = 0; n < len; ++n)
out[n] = in[n] ^ iv[n];
for (unsigned n = len; n < AES_BLOCK_SIZE; ++n)
out[n] = iv[n];
AES_256_encrypt(out, out, key,
shared_Te0, shared_Te1, shared_Te2, shared_Te3, shared_Rcon);
iv = out;
}
*((uint4*)ivec) = *((uint4*)iv);
}