-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathbenchmark.cu
90 lines (70 loc) · 2.01 KB
/
benchmark.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include "AES.h"
#include "main.h"
using namespace std;
int main(int argc, char **argv) {
if(argc < 2) {
printf("USAGE: benchmark FILE\n");
return 1;
}
uchar key[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 };
//uchar pt_debug[] = { 0x03, 0x02, 0x01, 0x00, 0x07, 0x06, 0x05, 0x04, 0x0b, 0x0a, 0x09, 0x08, 0x0f, 0x0e, 0x0d, 0x0c };
uint keySize = 16;
uint *ct, *pt;
FILE *f = fopen(argv[1], "rb");
if(f == NULL) {
printf("File not found.\n");
return 1;
}
fseek(f, 0, SEEK_END);
uint f_size = ftell(f);
rewind(f);
if(f_size % 4*sizeof(uint) != 0) {
printf("Plaintext size must be a multiple of AES block size.\n");
return 1;
}
uint ptSize = f_size / sizeof(uint);
#ifdef ASYNC
cudaMallocHost((void**)&pt, f_size);
cudaMallocHost((void**)&ct, f_size);
#else
pt = (uint*)malloc(f_size);
ct = (uint *)malloc(f_size);
#endif
fread(pt, sizeof(uint), ptSize, f);
fclose(f);
AES *aes = new AES();
aes->makeKey(key, keySize << 3, DIR_ENCRYPT);
/*
uchar pt_debug[] = { 0x0, 0x1, 0x2, 0x3, 0x4, 0x5, 0x6, 0x7, 0x8, 0x9, 0xa, 0xb, 0xc, 0xd, 0xe, 0xf };
pt = (uint*)pt_debug;
aes->encrypt_ecb(pt, ct, 4);
printHexArray(ct, 4);
*/
aes->encrypt_ecb(pt, ct, ptSize);
//printHexArray(ct, ptSize);
return 0;
}
uint stringToUcharArray(char *str, uchar **array) {
uint i, len = strlen(str) >> 1;
*array = (uchar *)malloc(len * sizeof(uchar));
for(i=0; i<len; i++)
sscanf(str + i*2, "%02X", *array+i);
return len;
}
uint stringToUcharArray(char *str, uint **array) {
uint i, len = strlen(str) >> 3;
*array = (uint *)malloc(len * sizeof(uint));
for(i=0; i<len; i++)
sscanf(str + i*8, "%08X", *array+i);
return len;
}
void printHexArray(uint *array, uint size) {
uint i;
for(i=0; i<size; i++)
printf("%08X", array[i]);
printf("\n");
}