-
Notifications
You must be signed in to change notification settings - Fork 1
/
vecCrypt.cu
568 lines (495 loc) · 16 KB
/
vecCrypt.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
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
/*
* Copyright 1993-2010 NVIDIA Corporation. All rights reserved.
*
* Please refer to the NVIDIA end user license agreement (EULA) associated
* with this source code for terms and conditions that govern your use of
* this software. Any use, reproduction, disclosure, or distribution of
* this software and related documentation outside the terms of the EULA
* is strictly prohibited.
*
*/
/*
* This software contains source code provided by NVIDIA Corporation.
*
* GPU accelerated Salsa20 Vector crypto core function.
*
* This sample demonstrates an implementation of the core Salsa20 crypto function
* in CTR mode accelerated using CUDA.
*/
// Includes
#include <inttypes.h>
#include <stdio.h>
#include <unistd.h>
#include <time.h>
// includes CUDA
#include <cuda_runtime.h>
#include "common.h"
extern "C" int crypto_stream_salsa20_amd64_xmm6_xor(unsigned char *c, unsigned char *m,
unsigned long long mlen, unsigned char *n, unsigned char *k);
__constant__ unsigned char MY_ALIGN(sizeof (uint32_t)) key[XSALSA20_CRYPTO_KEYBYTES * THREADS_PER_BLOCK];
__constant__ unsigned char MY_ALIGN(sizeof (uint32_t)) sigma[16];
const unsigned char hsigma[17] = "expand 32-byte k";
unsigned char h_nonce[XSALSA20_CRYPTO_NONCEBYTES];
int pinned = 0;
__host__ __device__ static inline uint32_t
rotate(uint32_t u,int c)
{
return (u << c) | (u >> (32 - c));
}
__host__ __device__ static inline uint32_t
load_littleendian(const unsigned char *x)
{
return
(uint32_t) (x[0]) \
| (((uint32_t) (x[1])) << 8) \
| (((uint32_t) (x[2])) << 16) \
| (((uint32_t) (x[3])) << 24)
;
}
__host__ __device__ static inline void
store_littleendian(unsigned char *x, uint32_t u)
{
x[0] = u; u >>= 8;
x[1] = u; u >>= 8;
x[2] = u; u >>= 8;
x[3] = u;
}
__host__ static inline uint32_t
load_littleendian64(const unsigned char *x)
{
return
(uint64_t) (x[0]) \
| (((uint64_t) (x[1])) << 8) \
| (((uint64_t) (x[2])) << 16) \
| (((uint64_t) (x[3])) << 24) \
| (((uint64_t) (x[4])) << 32) \
| (((uint64_t) (x[5])) << 40) \
| (((uint64_t) (x[6])) << 48) \
| (((uint64_t) (x[7])) << 56)
;
}
__host__ static int
crypto_core(
unsigned char *out,
const unsigned char *in,
const unsigned char *k,
const unsigned char *c
)
{
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
int i;
j0 = x0 = load_littleendian(c + 0);
j1 = x1 = load_littleendian(k + 0);
j2 = x2 = load_littleendian(k + 4);
j3 = x3 = load_littleendian(k + 8);
j4 = x4 = load_littleendian(k + 12);
j5 = x5 = load_littleendian(c + 4);
j6 = x6 = load_littleendian(in + 0);
j7 = x7 = load_littleendian(in + 4);
j8 = x8 = load_littleendian(in + 8);
j9 = x9 = load_littleendian(in + 12);
j10 = x10 = load_littleendian(c + 8);
j11 = x11 = load_littleendian(k + 16);
j12 = x12 = load_littleendian(k + 20);
j13 = x13 = load_littleendian(k + 24);
j14 = x14 = load_littleendian(k + 28);
j15 = x15 = load_littleendian(c + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
x8 ^= rotate( x4+ x0, 9);
x12 ^= rotate( x8+ x4,13);
x0 ^= rotate(x12+ x8,18);
x9 ^= rotate( x5+ x1, 7);
x13 ^= rotate( x9+ x5, 9);
x1 ^= rotate(x13+ x9,13);
x5 ^= rotate( x1+x13,18);
x14 ^= rotate(x10+ x6, 7);
x2 ^= rotate(x14+x10, 9);
x6 ^= rotate( x2+x14,13);
x10 ^= rotate( x6+ x2,18);
x3 ^= rotate(x15+x11, 7);
x7 ^= rotate( x3+x15, 9);
x11 ^= rotate( x7+ x3,13);
x15 ^= rotate(x11+ x7,18);
x1 ^= rotate( x0+ x3, 7);
x2 ^= rotate( x1+ x0, 9);
x3 ^= rotate( x2+ x1,13);
x0 ^= rotate( x3+ x2,18);
x6 ^= rotate( x5+ x4, 7);
x7 ^= rotate( x6+ x5, 9);
x4 ^= rotate( x7+ x6,13);
x5 ^= rotate( x4+ x7,18);
x11 ^= rotate(x10+ x9, 7);
x8 ^= rotate(x11+x10, 9);
x9 ^= rotate( x8+x11,13);
x10 ^= rotate( x9+ x8,18);
x12 ^= rotate(x15+x14, 7);
x13 ^= rotate(x12+x15, 9);
x14 ^= rotate(x13+x12,13);
x15 ^= rotate(x14+x13,18);
}
x0 += j0;
x1 += j1;
x2 += j2;
x3 += j3;
x4 += j4;
x5 += j5;
x6 += j6;
x7 += j7;
x8 += j8;
x9 += j9;
x10 += j10;
x11 += j11;
x12 += j12;
x13 += j13;
x14 += j14;
x15 += j15;
store_littleendian(out + 0,x0);
store_littleendian(out + 4,x1);
store_littleendian(out + 8,x2);
store_littleendian(out + 12,x3);
store_littleendian(out + 16,x4);
store_littleendian(out + 20,x5);
store_littleendian(out + 24,x6);
store_littleendian(out + 28,x7);
store_littleendian(out + 32,x8);
store_littleendian(out + 36,x9);
store_littleendian(out + 40,x10);
store_littleendian(out + 44,x11);
store_littleendian(out + 48,x12);
store_littleendian(out + 52,x13);
store_littleendian(out + 56,x14);
store_littleendian(out + 60,x15);
return 0;
}
// Variables
unsigned char* h_A = NULL;
unsigned char* h_B = NULL;
unsigned char* d_A = NULL;
bool noprompt = false;
// Functions
void CleanupResources(void);
void Init(unsigned char*, size_t);
void ParseArguments(int, char**);
////////////////////////////////////////////////////////////////////////////////
// These are CUDA Helper functions
// This will output the proper CUDA error strings in the event that a CUDA host call returns an error
#define checkCudaErrors(err) __checkCudaErrors (err, __FILE__, __LINE__)
inline void __checkCudaErrors(cudaError err, const char *file, const int line )
{
if(cudaSuccess != err)
{
fprintf(stderr, "%s(%i) : CUDA Runtime API error %d: %s.\n",file, line, (int)err, cudaGetErrorString( err ) );
CleanupResources();
exit(-1);
}
}
// This will output the proper error string when calling cudaGetLastError
#define getLastCudaError(msg) __getLastCudaError (msg, __FILE__, __LINE__)
inline void __getLastCudaError(const char *errorMessage, const char *file, const int line )
{
cudaError_t err = cudaGetLastError();
if (cudaSuccess != err)
{
fprintf(stderr, "%s(%i) : getLastCudaError() CUDA error : %s : (%d) %s.\n",
file, line, errorMessage, (int)err, cudaGetErrorString( err ) );
CleanupResources();
exit(-1);
}
}
// end of CUDA Helper Functions
// Device code
__global__ void VecCrypt(unsigned char* A, unsigned int N, uint64_t nblocks, uint64_t p_nonce, int blks_per_chunk)
{
uint64_t i = THREADS_PER_BLOCK * blockIdx.x + threadIdx.x;
if (i < N) {
int k, tot;
uint32_t *mem;
uint32_t x0, x1, x2, x3, x4, x5, x6, x7, x8, x9, x10, x11, x12, x13, x14, x15;
uint32_t j0, j1, j2, j3, j4, j5, j6, j7, j8, j9, j10, j11, j12, j13, j14, j15;
uint64_t blockno;
blockno = i*blks_per_chunk;
tot = (nblocks - blockno > blks_per_chunk) ? blks_per_chunk:(nblocks - blockno);
for (k = 0; k < tot; k++) {
j0 = x0 = load_littleendian(sigma + 0);
j1 = x1 = load_littleendian(key + 0);
j2 = x2 = load_littleendian(key + 4);
j3 = x3 = load_littleendian(key + 8);
j4 = x4 = load_littleendian(key + 12);
j5 = x5 = load_littleendian(sigma + 4);
j6 = x6 = p_nonce;
j7 = x7 = p_nonce >> 32;
j8 = x8 = blockno;
j9 = x9 = blockno >> 32;
j10 = x10 = load_littleendian(sigma + 8);
j11 = x11 = load_littleendian(key + 16);
j12 = x12 = load_littleendian(key + 20);
j13 = x13 = load_littleendian(key + 24);
j14 = x14 = load_littleendian(key + 28);
j15 = x15 = load_littleendian(sigma + 12);
for (i = ROUNDS;i > 0;i -= 2) {
x4 ^= rotate( x0+x12, 7);
x8 ^= rotate( x4+ x0, 9);
x12 ^= rotate( x8+ x4,13);
x0 ^= rotate(x12+ x8,18);
x9 ^= rotate( x5+ x1, 7);
x13 ^= rotate( x9+ x5, 9);
x1 ^= rotate(x13+ x9,13);
x5 ^= rotate( x1+x13,18);
x14 ^= rotate(x10+ x6, 7);
x2 ^= rotate(x14+x10, 9);
x6 ^= rotate( x2+x14,13);
x10 ^= rotate( x6+ x2,18);
x3 ^= rotate(x15+x11, 7);
x7 ^= rotate( x3+x15, 9);
x11 ^= rotate( x7+ x3,13);
x15 ^= rotate(x11+ x7,18);
x1 ^= rotate( x0+ x3, 7);
x2 ^= rotate( x1+ x0, 9);
x3 ^= rotate( x2+ x1,13);
x0 ^= rotate( x3+ x2,18);
x6 ^= rotate( x5+ x4, 7);
x7 ^= rotate( x6+ x5, 9);
x4 ^= rotate( x7+ x6,13);
x5 ^= rotate( x4+ x7,18);
x11 ^= rotate(x10+ x9, 7);
x8 ^= rotate(x11+x10, 9);
x9 ^= rotate( x8+x11,13);
x10 ^= rotate( x9+ x8,18);
x12 ^= rotate(x15+x14, 7);
x13 ^= rotate(x12+x15, 9);
x14 ^= rotate(x13+x12,13);
x15 ^= rotate(x14+x13,18);
}
x0 += j0;
x1 += j1;
x2 += j2;
x3 += j3;
x4 += j4;
x5 += j5;
x6 += j6;
x7 += j7;
x8 += j8;
x9 += j9;
x10 += j10;
x11 += j11;
x12 += j12;
x13 += j13;
x14 += j14;
x15 += j15;
mem = (unsigned int *)&A[blockno*XSALSA20_BLOCKSZ];
*mem ^= x0; mem++;
*mem ^= x1; mem++;
*mem ^= x2; mem++;
*mem ^= x3; mem++;
*mem ^= x4; mem++;
*mem ^= x5; mem++;
*mem ^= x6; mem++;
*mem ^= x7; mem++;
*mem ^= x8; mem++;
*mem ^= x9; mem++;
*mem ^= x10; mem++;
*mem ^= x11; mem++;
*mem ^= x12; mem++;
*mem ^= x13; mem++;
*mem ^= x14; mem++;
*mem ^= x15;
blockno++;
}
}
}
__host__ int
crypto_stream_salsa20_ref_xor(
unsigned char *m,unsigned long long mlen,
unsigned char *n,
unsigned char *k
)
{
unsigned char in[16];
unsigned char block[64];
int i;
unsigned int u;
unsigned int blk;
if (!mlen) return 0;
blk = 0;
for (i = 0;i < 8;++i) in[i] = n[i];
for (i = 8;i < 16;++i) in[i] = 0;
while (mlen >= XSALSA20_BLOCKSZ) {
crypto_core(block,in,k,hsigma);
for (i = 0;i < XSALSA20_BLOCKSZ;++i) m[i] ^= block[i];
u = 1;
for (i = 8;i < 16;++i) {
u += (unsigned int) in[i];
in[i] = u;
u >>= 8;
}
mlen -= XSALSA20_BLOCKSZ;
m += XSALSA20_BLOCKSZ;
blk++;
}
if (mlen) {
crypto_core(block,in,k,hsigma);
for (i = 0;i < mlen;++i) m[i] ^= block[i];
}
return 0;
}
__host__ double
get_wtime_millis(void)
{
struct timespec ts;
int rv;
rv = clock_gettime(CLOCK_MONOTONIC, &ts);
if (rv == 0)
return (ts.tv_sec * 1000 + ((double)ts.tv_nsec) / 1000000L);
return (1);
}
#define BYTES_TO_MB(x) ((x) / (1024 * 1024))
__host__ double
get_mb_s(uint64_t bytes, double diff)
{
double bytes_sec;
bytes_sec = ((double)bytes / diff) * 1000;
return (BYTES_TO_MB(bytes_sec));
}
// Host code
int main(int argc, char** argv)
{
printf("Salsa20 Vector Encryption\n");
unsigned int NBLKS = 4000000, N;
int rv, blks_per_chunk;
size_t size, i;
unsigned char k[32];
double gpuTime1, gpuTime2, cpuTime1, cpuTime2, strt, en;
uint64_t v_nonce;
cudaDeviceProp deviceProp;
ParseArguments(argc, argv);
cudaGetDeviceProperties(&deviceProp, 0);
if (deviceProp.major >= 2)
blks_per_chunk = BLOCKS_PER_CHUNK_2X;
else
blks_per_chunk = BLOCKS_PER_CHUNK_1X;
N = NBLKS / blks_per_chunk;
if (NBLKS % blks_per_chunk) N++;
size = NBLKS * XSALSA20_BLOCKSZ;
// Allocate input vectors h_A and h_B in host memory
pinned = 1;
if (cudaMallocHost(&h_A, size) != cudaSuccess) {
pinned = 0;
h_A = (unsigned char *)malloc(size);
}
if (h_A == 0) CleanupResources();
h_B = (unsigned char *)malloc(size);
if (h_B == 0) CleanupResources();
memset(k, 1, XSALSA20_CRYPTO_KEYBYTES);
memset(h_nonce, 0, XSALSA20_CRYPTO_NONCEBYTES);
// Initialize input vectors
printf("Initializing input data\n");
Init(h_A, size);
memcpy(h_B, h_A, size);
// Allocate vectors in device memory
printf("Allocating device buffer\n");
checkCudaErrors( cudaMalloc((void**)&d_A, size) );
// Copy vectors from host memory to device memory
printf("Copying buffer to device\n");
strt = get_wtime_millis();
checkCudaErrors( cudaMemcpy(d_A, h_A, size, cudaMemcpyHostToDevice) );
checkCudaErrors( cudaMemcpyToSymbol(key, k, XSALSA20_CRYPTO_KEYBYTES, 0, cudaMemcpyHostToDevice) );
checkCudaErrors( cudaMemcpyToSymbol(sigma, hsigma, 16, 0, cudaMemcpyHostToDevice) );
v_nonce = load_littleendian64(h_nonce);
checkCudaErrors( cudaDeviceSynchronize() );
en = get_wtime_millis();
gpuTime1 = en - strt;
printf("Invoking kernel\n");
strt = get_wtime_millis();
// Invoke kernel
int threadsPerBlock = THREADS_PER_BLOCK;
int blocksPerGrid = (N + threadsPerBlock - 1) / threadsPerBlock;
VecCrypt<<<blocksPerGrid, threadsPerBlock>>>(d_A, N, NBLKS, v_nonce, blks_per_chunk);
getLastCudaError("kernel launch failure");
checkCudaErrors( cudaDeviceSynchronize() );
en = get_wtime_millis();
gpuTime2 = en - strt;
printf("Copying buffer back to host memory\n");
// Copy result from device memory to host memory
strt = get_wtime_millis();
checkCudaErrors( cudaMemcpy(h_A, d_A, size, cudaMemcpyDeviceToHost) );
checkCudaErrors( cudaDeviceSynchronize() );
en = get_wtime_millis();
gpuTime1 += (en - strt);
// Verify result
printf("Computing reference code on CPU\n");
strt = get_wtime_millis();
crypto_stream_salsa20_ref_xor(h_B, size, h_nonce + 16, k);
en = get_wtime_millis();
cpuTime1 = en - strt;
rv = 0;
printf("Verifying result\n");
for (i = 0; i < size; i++) {
if (h_B[i] != h_A[i]) {
printf("Byte #%llu differ, %d, %d\n", i, h_B[i], h_A[i]);
rv = 1;
break;
}
}
printf("Computing optimized code on CPU\n");
strt = get_wtime_millis();
crypto_stream_salsa20_amd64_xmm6_xor(h_B, h_B, size, h_nonce + 16, k);
en = get_wtime_millis();
cpuTime2 = en - strt;
/*
* Clean out keying material on the GPU.
*/
memset(k, 0, XSALSA20_CRYPTO_KEYBYTES);
checkCudaErrors( cudaMemcpyToSymbol(key, k, XSALSA20_CRYPTO_KEYBYTES, 0, cudaMemcpyHostToDevice) );
CleanupResources();
if (pinned)
printf("Data transfer time (pinned mem) : %f msec\n", gpuTime1);
else
printf("Data transfer time (non-pinned mem) : %f msec\n", gpuTime1);
printf("GPU computation time : %f msec\n", gpuTime2);
printf("GPU throughput : %f MB/s\n", get_mb_s(size, gpuTime2));
printf("GPU throughput including naive transfer : %f MB/s\n", get_mb_s(size, gpuTime2 + gpuTime1));
printf("CPU computation time (reference code) : %f msec\n", cpuTime1);
printf("CPU throughput (reference code) : %f MB/s\n", get_mb_s(size, cpuTime1));
printf("CPU computation time (optimized code) : %f msec\n", cpuTime2);
printf("CPU throughput (optimized code) : %f MB/s\n", get_mb_s(size, cpuTime2));
if (rv == 0)
printf("PASSED\n");
else
printf("FAILED\n");
}
void CleanupResources(void)
{
// Free device memory
if (d_A)
cudaFree(d_A);
// Free host memory
if (h_A) {
if (pinned)
cudaFreeHost(h_A);
else
free(h_A);
}
if (h_B)
free(h_B);
cudaDeviceReset();
}
// Allocates an array with random float entries.
void Init(unsigned char *data, size_t n)
{
for (size_t i = 0; i < n; ++i)
data[i] = i;
}
// Parse program arguments
void ParseArguments(int argc, char** argv)
{
for (int i = 0; i < argc; ++i) {
if (strcmp(argv[i], "--noprompt") == 0 ||
strcmp(argv[i], "-noprompt") == 0)
{
noprompt = true;
break;
}
}
}