-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa_gmp.cu
173 lines (123 loc) · 4 KB
/
rsa_gmp.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
#include <stdlib.h>
#include <stdio.h>
//GMP Bibliothek
#include <gmp.h>
__device__ mpz_t biginteger;
//#define DEBUG
//Variablen
#define p 3
#define q 5
#define n 15
#define e 3
#define v 3
#define z 8
#define anzahl_Texte 80
#define count_cores 8
/*
Klartext: K
Geheimtext: G
Verschluesselung: G = K^v mod n
Entschluesselung: K = G^e mod n
Index des CUDA Kerns: blockIdx.x blockIdx.y
*/
static void HandleError( cudaError_t err, const char *file, int line ) {
if (err != cudaSuccess) {
printf( "%s in %s at line %d\n", cudaGetErrorString( err ), file, line );
exit( EXIT_FAILURE );
}
}
#define HANDLE_ERROR( err ) (HandleError( err, __FILE__, __LINE__ ))
void biAdd(mpz_t a, unsigned long int b)
{
mpz_add_ui(a, a, b);
}
__global__ void verschluessselung(long int klartexte[], long int geheimtexte[])
{
long int i, j, multi, x;
long int block_length = anzahl_Texte/count_cores;
for (i = 0 ; i < block_length; i++)
{
multi = x = klartexte[i+blockIdx.x*block_length];
for (j = 1; j < v; j++)
x *= multi;
geheimtexte[i+blockIdx.x*block_length] = x % n;
}
//GMP
biAdd(biginteger, (unsigned long int)25);
}
__global__ void entschluessselung(long int geheimtexte[], long int klartexte_pruefung[])
{
long int i, j, multi, x;
long int block_length = anzahl_Texte/count_cores;
for (i = 0 ; i < block_length; i++)
{
multi = x = geheimtexte[i+blockIdx.x*block_length];
for (j = 1; j < e; j++)
x *= multi;
klartexte_pruefung[i+blockIdx.x*block_length] = x % n;
}
}
int main(void) {
int i;
cudaEvent_t start, stop;
float elapsedTime;
long int klartexte[anzahl_Texte];
long int klartexte_pruefung[anzahl_Texte];
long int geheimtexte[anzahl_Texte];
// GMP Integer
// mpz_t biginteger;
mpz_init(biginteger);
unsigned long int number = 1;
number = number<<31;
mpz_set_ui(biginteger, number);
// gmp_printf("\nGMP Integer: %Zd\n\n", biginteger);
// printf("\nlong int: %d\n", sizeof(long int));
// printf("\nunsigned long int: %d\n", sizeof(unsigned long int));
// printf("\nunsigned long long: %d\n", sizeof(unsigned long long));
//Klartetexte Array belegen
//rand initialisieren
srand((unsigned)time(NULL));
for (i = 0; i < anzahl_Texte; i ++)
{
klartexte[i] = rand() % 15; //Zahlen nicht zu gross waehlen
}
printf("Die Klartexte:\n");
for (i = 0; i < anzahl_Texte; i++)
{
printf("%ld, ", klartexte[i]);
}
printf("\n\n");
long int *dev_klartexte, *dev_geheimtexte, *dev_klartexte_pruefung;
HANDLE_ERROR(cudaEventCreate(&start));
HANDLE_ERROR(cudaEventCreate(&stop));
HANDLE_ERROR(cudaEventRecord(start, 0));
HANDLE_ERROR(cudaMalloc((void **)&dev_klartexte, sizeof(klartexte)));
HANDLE_ERROR(cudaMalloc((void **)&dev_geheimtexte, sizeof(geheimtexte)));
HANDLE_ERROR(cudaMalloc((void **)&dev_klartexte_pruefung, sizeof(klartexte_pruefung)));
HANDLE_ERROR(cudaMemcpy(dev_klartexte, klartexte, sizeof(klartexte), cudaMemcpyHostToDevice));
dim3 blocks(count_cores, 1);
verschluessselung<<<blocks, 1>>>(dev_klartexte, dev_geheimtexte);
HANDLE_ERROR(cudaMemcpy(geheimtexte, dev_geheimtexte, sizeof(geheimtexte), cudaMemcpyDeviceToHost));
printf("Die Klartexte wurden verschluesselt.\n\nGeheimtexte:\n");
for (i = 0; i < anzahl_Texte; i++)
{
printf("%ld, ", geheimtexte[i]);
}
printf("\n\n");
entschluessselung<<<blocks, 1>>>(dev_geheimtexte, dev_klartexte_pruefung);
HANDLE_ERROR(cudaMemcpy(klartexte_pruefung, dev_klartexte_pruefung, sizeof(klartexte_pruefung), cudaMemcpyDeviceToHost));
HANDLE_ERROR(cudaEventRecord(stop, 0));
HANDLE_ERROR(cudaEventSynchronize(stop));
printf("Die Geheimtexte wurden entschluesselt.\n\nKlartexte:\n");
for (i = 0; i < anzahl_Texte; i++)
{
printf("%ld, ", klartexte_pruefung[i]);
}
printf("\n\n");
HANDLE_ERROR(cudaEventElapsedTime(&elapsedTime, start, stop));
printf("Elapsed time: %3.1f ms\n", elapsedTime);
HANDLE_ERROR(cudaEventDestroy(start));
HANDLE_ERROR(cudaEventDestroy(stop));
gmp_printf("\nGMP Integer: %Zd\n\n", biginteger);
return EXIT_SUCCESS;
}