-
Notifications
You must be signed in to change notification settings - Fork 0
/
main_kernel_sel.cu
331 lines (279 loc) · 10.8 KB
/
main_kernel_sel.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
#include "device_cuda_bignum.h"
/*#include "cuPrintf.cu"
#if __CUDA_ARCH__ < 200 //Compute capability 1.x architectures
#define CUPRINTF cuPrintf
#else //Compute capability 2.x architectures
#define CUPRINTF(fmt, ...) printf("[%d, %d]:\t" fmt, \
blockIdx.y*gridDim.x+blockIdx.x,\
threadIdx.z*blockDim.x*blockDim.y+threadIdx.y*blockDim.x+threadIdx.x,\
__VA_ARGS__)
#endif*/
typedef enum {
EUCLIDEAN=0,
BINARY_EUCLIDEAN,
FAST_BINARY_EUCLIDEAN,
UNKNOWN
} algorithms;
algorithms set_enum_algorithm(char * algorithm){
if(!strcmp( "euclid", algorithm)){
return EUCLIDEAN;
} else if(!strcmp( "binary", algorithm)) {
return BINARY_EUCLIDEAN;
} else if(!strcmp( "fast", algorithm)) {
return FAST_BINARY_EUCLIDEAN;
} else {
return UNKNOWN;
}
}
int main(int argc, char* argv[]){
unsigned number_of_keys;
unsigned key_size, combinations;
unsigned thread_per_block;
unsigned number_of_comutations;
char *keys_directory;
int counter;
algorithms gcd_kind;
if(argc==6) {
for(counter=0;counter<argc;counter++){
switch(counter){
case 1:
printf("\nnumber_of_keys argv[%d]: %s\n",counter,argv[counter]);
number_of_keys=atoi(argv[counter]);
break;
case 2:
printf("\nkey_size argv[%d]: %s\n",counter,argv[counter]);
key_size=atoi(argv[counter]);
break;
case 3:
printf("\nthreads_per_block argv[%d]: %s\n",counter,argv[counter]);
thread_per_block=atoi(argv[counter]);
break;
case 4:
printf("\nname of keys directory argv[%d]: %s\n",counter,argv[counter]);
keys_directory=argv[counter];
break;
case 5:
printf("\nkind of algorithm argv[%d]: %s\n",counter,argv[counter]);
gcd_kind = set_enum_algorithm(argv[counter]);
break;
default:
break;
}
}
} else {
printf("\nFind weak keys\n\rUsage:\n\r ./GCD_RSA number_of_keys key_size threads_per_block directory_name kind_of_algorithm\n\rAlgorithms:\n\r\t-\"euclid\"\n\r\t-\"binary\"\n\r\t-\"fast\"\n\r");
return 0;
}
// simplified binomial coefficient
number_of_comutations=((number_of_keys/2)*(number_of_keys-1));
combinations=(number_of_keys*number_of_keys);
U_BN tmp;
int L = ((key_size+31) / (8*sizeof(unsigned)));
unsigned i, j, l;
unsigned k = 0;
U_BN *device_U_BN_A, *device_U_BN_B, *device_U_BN_R;
U_BN *KEYS, *R;
char *tmp_path;
U_BN *A, *B;
cudaError_t cudaStatus;
//unit_test();
OpenSSL_GCD(number_of_keys, key_size, keys_directory);
R = (U_BN*)malloc(number_of_comutations*sizeof(U_BN));
KEYS = (U_BN*)malloc(number_of_keys*sizeof(U_BN));
A = (U_BN*)malloc(number_of_keys*sizeof(U_BN));
B = (U_BN*)malloc(number_of_keys*sizeof(U_BN));
for(i=0; i<number_of_keys; i++){
U_BN a;
U_BN b;
U_BN keys;
a.d = (unsigned*)malloc(L*sizeof(unsigned));
b.d = (unsigned*)malloc(L*sizeof(unsigned));
keys.d = (unsigned*)malloc(L*sizeof(unsigned));
a.top = L;
b.top = L;
keys.top = L;
for(j=0; j<L; j++)
a.d[j]=0;
for(j=0; j<L; j++)
b.d[j]=0;
for(j=0; j<L; j++)
keys.d[j]=0;
A[i] = a;
B[i] = b;
KEYS[i] = keys;
}
for(i=0; i<number_of_comutations; i++){
U_BN r;
r.d = (unsigned*)malloc(L*sizeof(unsigned));
r.top = L;
for(j=0; j<L; j++)
r.d[j]=0;
R[i] = r;
}
for(i=0; i<number_of_keys; i++){
//KEYS[i] = tmp;
asprintf(&tmp_path, "%s/%d.pem", keys_directory, (i+1));
get_u_bn_from_mod_PEM(tmp_path, &A[i]);
get_u_bn_from_mod_PEM(tmp_path, &B[i]);
}
/*int l;
for(k=0; k<number_of_keys; k++){
A[k].top = KEYS[k].top;
B[k].top = KEYS[k].top;
for(l=0;l<L;l++){
A[k].d[l] = KEYS[k].d[l];
}
for(l=0;l<L;l++){
B[k].d[l] = KEYS[k].d[l];
}
printf("[CPU] A: %s\n", cu_bn_bn2hex(&A[k]));
printf("[CPU] B: %s\n", cu_bn_bn2hex(&B[k]));
}*/
U_BN *tmp_A = NULL;
U_BN *tmp_B = NULL;
tmp_A = (U_BN*)malloc(sizeof(U_BN));
tmp_B = (U_BN*)malloc(sizeof(U_BN));
tmp_A->d = (unsigned*)malloc(32*sizeof(unsigned));
tmp_B->d = (unsigned*)malloc(32*sizeof(unsigned));
int sum=0;
clock_t start = clock();
switch(gcd_kind){
case EUCLIDEAN:
for(i=0; i<number_of_keys; i++){
for(j=(i+1); j<number_of_keys; j++){
tmp_A->top = A[i].top;
tmp_B->top = B[j].top;
for(l=0; l<L; l++){
tmp_A->d[l] = A[i].d[l];
tmp_B->d[l] = B[j].d[l];
}
if( strcmp( "1", cu_bn_bn2hex(cu_dev_classic_euclid(tmp_A, tmp_B)))){
printf("[CPU] Euclidean Weak key with selection: %s\n", cu_bn_bn2hex(cu_dev_classic_euclid(tmp_A, tmp_B)));
sum+=1;
}
}
}
break;
case BINARY_EUCLIDEAN:
for(i=0; i<number_of_keys; i++){
for(j=(i+1); j<number_of_keys; j++){
tmp_A->top = A[i].top;
tmp_B->top = B[j].top;
for(l=0; l<L; l++){
tmp_A->d[l] = A[i].d[l];
tmp_B->d[l] = B[j].d[l];
}
cu_dev_binary_gcd(tmp_A, tmp_B);
if( strcmp( "1", cu_bn_bn2hex(cu_dev_binary_gcd(tmp_A, tmp_B)))){
//printf("[CPU] Binary Weak key with selection: %s\n", cu_bn_bn2hex(cu_dev_binary_gcd(tmp_A, tmp_B)));
sum+=1;
}
}
}
break;
case FAST_BINARY_EUCLIDEAN:
for(i=0, k=0; i<number_of_keys; i++){
for(j=(i+1); j<number_of_keys; j++, k++){
tmp_A->top = A[i].top;
tmp_B->top = B[j].top;
for(l=0; l<L; l++){
tmp_A->d[l] = A[i].d[l];
tmp_B->d[l] = B[j].d[l];
}
if( strcmp( "1", cu_bn_bn2hex(cu_dev_fast_binary_euclid(tmp_A, tmp_B)))){
//printf("[CPU] Fast Weak key: %s\n", cu_bn_bn2hex(cu_dev_fast_binary_euclid(tmp_A, tmp_B)));
sum+=1;
}
}
}
break;
default:
printf("[CPU] Unknown GCD algorithm");
break;
}
clock_t stop = clock();
double elapsed = (double)(stop - start) * 1000.0 / CLOCKS_PER_SEC;
printf("[CPU] Time elapsed in ms: %f\n", elapsed);
printf("[CPU] Weak keys: %d\n", sum);
cudaDeviceReset();
cudaStatus = cudaMalloc((void**)&device_U_BN_A, number_of_keys*sizeof(U_BN));
cudaStatus = cudaMalloc((void**)&device_U_BN_B, number_of_keys*sizeof(U_BN));
cudaStatus = cudaMalloc((void**)&device_U_BN_R, number_of_comutations*sizeof(U_BN));
cudaStatus = cudaMemcpy(device_U_BN_A, A, number_of_keys*sizeof(U_BN), cudaMemcpyHostToDevice);
cudaStatus = cudaMemcpy(device_U_BN_B, B, number_of_keys*sizeof(U_BN), cudaMemcpyHostToDevice);
cudaStatus = cudaMemcpy(device_U_BN_R, R, number_of_comutations*sizeof(U_BN), cudaMemcpyHostToDevice);
unsigned long *out;
for(i = 0; i < number_of_keys; i++) {
cudaMalloc(&out, L*sizeof(unsigned));
cudaMemcpy(out, A[i].d, L*sizeof(unsigned), cudaMemcpyHostToDevice);
cudaMemcpy(&device_U_BN_A[i].d, &out, sizeof(void*), cudaMemcpyHostToDevice);
cudaMalloc(&out, L*sizeof(unsigned));
cudaMemcpy(out, B[i].d, L*sizeof(unsigned), cudaMemcpyHostToDevice);
cudaMemcpy(&device_U_BN_B[i].d, &out, sizeof(void*), cudaMemcpyHostToDevice);
}
for(i = 0; i < number_of_comutations; i++) {
cudaMalloc(&out, L*sizeof(unsigned));
cudaMemcpy(out, R[i].d, L*sizeof(unsigned), cudaMemcpyHostToDevice);
cudaMemcpy(&device_U_BN_R[i].d, &out, sizeof(void*), cudaMemcpyHostToDevice);
}
float time;
cudaEvent_t start_cu, stop_cu;
cudaEventCreate(&start_cu);
cudaEventCreate(&stop_cu);
cudaEventRecord(start_cu, 0);
/*switch(gcd_kind){
case EUCLIDEAN:
printf("[GPU] Euclidean algorithm with selection\n");
orgEuclideanKernel_with_selection<<<((combinations + thread_per_block -1)/thread_per_block), thread_per_block>>>(device_U_BN_A, device_U_BN_B, device_U_BN_R, number_of_comutations, number_of_keys);
break;
case BINARY_EUCLIDEAN:
printf("[GPU] Binary algorithm with selection\n");
binEuclideanKernel_with_selection<<<((combinations + thread_per_block -1)/thread_per_block), thread_per_block>>>(device_U_BN_A, device_U_BN_B, device_U_BN_R, number_of_comutations, number_of_keys);
break;
case FAST_BINARY_EUCLIDEAN:
printf("[GPU] Fast Binary algorithm with selection\n");
fastBinaryKernel_with_selection<<<((combinations + thread_per_block -1)/thread_per_block), thread_per_block>>>(device_U_BN_A, device_U_BN_B, device_U_BN_R, number_of_comutations, number_of_keys);
break;
default:
printf("[GPU] Unknown GCD algorithm\n");
break;
}
cudaEventRecord(stop_cu, 0);
cudaEventSynchronize(stop_cu);
cudaEventElapsedTime(&time, start_cu, stop_cu);
printf("[GPU] Time elapsed in ms %fms\n", time);
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "\n testKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
return 1;
}
cudaStatus = cudaMemcpy(R, device_U_BN_R, number_of_comutations*sizeof(U_BN), cudaMemcpyDeviceToHost);
unsigned *array = (unsigned*)malloc(L*sizeof(unsigned));
for(i = 0; i < number_of_comutations; ++i) {
array = (unsigned*)malloc(L*sizeof(unsigned));
cudaMemcpy(array, R[i].d, L*sizeof(unsigned), cudaMemcpyDeviceToHost);
R[i].d = array;
}
cudaStatus = cudaGetLastError();
if (cudaStatus != cudaSuccess) {
fprintf(stderr, "\n testKernel launch failed: %s\n", cudaGetErrorString(cudaStatus));
return 1;
}
sum=0;
for (k = 0; k < number_of_comutations; k++){
if( strcmp( "1", cu_bn_bn2hex(&R[k]))){
sum += 1;
}
}
printf("[GPU] Weak keys: %d\n", sum);
cudaFree(out);
free(array);
cudaFree(device_U_BN_A);
cudaFree(device_U_BN_B);
cudaFree(device_U_BN_R);*/
free(A);
free(B);
free(KEYS);
free(R);
return (0);
}