forked from lwakefield/libgpucrypto
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrsa_context_mp.cc
369 lines (307 loc) · 10.2 KB
/
rsa_context_mp.cc
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
#include <cassert>
#include <sys/time.h>
#include <openssl/bn.h>
#include "rsa_context_mp.hh"
rsa_context_mp::rsa_context_mp(int keylen)
: rsa_context(keylen)
{
gpu_setup();
}
rsa_context_mp::rsa_context_mp(const std::string &filename,
const std::string &passwd)
: rsa_context(filename, passwd)
{
gpu_setup();
}
rsa_context_mp::rsa_context_mp(const char *filename, const char *passwd)
: rsa_context(filename, passwd)
{
gpu_setup();
}
rsa_context_mp::~rsa_context_mp()
{
BN_free(r);
BN_free(r_inv[0]);
BN_free(r_inv[1]);
BN_free(in_bn_p);
BN_free(in_bn_q);
BN_free(out_bn_p);
BN_free(out_bn_q);
checkCudaErrors(cudaFree(sw_d));
checkCudaErrors(cudaFree(n_d));
checkCudaErrors(cudaFree(np_d));
checkCudaErrors(cudaFree(r_sqr_d));
checkCudaErrors(cudaFree(iqmp_d));
for (unsigned int i = 0; i <= max_stream; i++) {
checkCudaErrors(cudaFree(streams[i].a_d));
checkCudaErrors(cudaFree(streams[i].ret_d));
checkCudaErrors(cudaFree(streams[i].dbg_d));
checkCudaErrors(cudaFreeHost(streams[i].a));
checkCudaErrors(cudaFreeHost(streams[i].ret));
checkCudaErrors(cudaFreeHost(streams[i].dbg));
}
}
void rsa_context_mp::dump()
{
if (is_crt_available()) {
mp_print("p'", mp_np[0], (get_key_bits() / 2) / BITS_PER_WORD);
mp_print("q'", mp_np[1], (get_key_bits() / 2) / BITS_PER_WORD);
} else {
mp_print("n'", mp_np[0], get_key_bits() / BITS_PER_WORD);
}
rsa_context::dump();
}
void rsa_context_mp::priv_decrypt(unsigned char *out, int *out_len,
const unsigned char *in, int in_len)
{
priv_decrypt_batch(&out, out_len, &in, &in_len, 1);
}
void rsa_context_mp::priv_decrypt_batch(unsigned char **out, int *out_len,
const unsigned char **in, const int *in_len,
int n)
{
// by default, stream is not used
priv_decrypt_stream(out, out_len, in, in_len, n, 0);
sync(0);
}
void rsa_context_mp::priv_decrypt_stream(unsigned char **out, int *out_len,
const unsigned char **in, const int *in_len,
int n, unsigned int stream_id)
{
assert(is_crt_available());
assert(0 < n && n <= max_batch);
assert(n <= MP_MAX_NUM_PAIRS);
assert(stream_id <= max_stream);
assert(dev_ctx_ != NULL);
assert(dev_ctx_->get_state(stream_id) == READY);
dev_ctx_->set_state(stream_id, WAIT_KERNEL);
int word_len = (get_key_bits() / 2) / BITS_PER_WORD;
int S = word_len;
int num_blks = ((n + MP_MSGS_PER_BLOCK - 1) / MP_MSGS_PER_BLOCK) * 2;
dev_ctx_->clear_checkbits(stream_id, num_blks);
streams[stream_id].post_launched = false;
for (int i = 0; i < n; i++) {
BN_bin2bn(in[i], in_len[i], in_bn_p);
BN_bin2bn(in[i], in_len[i], in_bn_q);
assert(in_bn_p != NULL);
assert(in_bn_q != NULL);
//assert(BN_cmp(in_bn_p, rsa->n) < 0);
BN_nnmod(in_bn_p, in_bn_p, rsa->p, bn_ctx); // TODO: test BN_nnmod
BN_nnmod(in_bn_q, in_bn_q, rsa->q, bn_ctx);
mp_bn2mp(streams[stream_id].a + (i * 2 * MAX_S), in_bn_p, word_len);
mp_bn2mp(streams[stream_id].a + (i * 2 * MAX_S) + MAX_S, in_bn_q, word_len);
}
//copy in put and execute kernel
mp_modexp_crt(streams[stream_id].a,
n, word_len,
streams[stream_id].ret_d, streams[stream_id].a_d,
sw_d,
n_d,
np_d,
r_sqr_d,
dev_ctx_->get_stream(stream_id),
stream_id,
dev_ctx_->get_dev_checkbits(stream_id));
streams[stream_id].n = n;
streams[stream_id].out = out;
streams[stream_id].out_len = out_len;
}
bool rsa_context_mp::sync(unsigned int stream_id, bool block, bool copy_result)
{
assert(stream_id <= max_stream);
int word_len = (get_key_bits() / 2) / BITS_PER_WORD;
if (dev_ctx_->get_state(stream_id) == READY)
return true;
//blocing case
if (block) {
//wait for previous operation to finish
dev_ctx_->sync(stream_id, true);
if (dev_ctx_->get_state(stream_id) == WAIT_KERNEL &&
streams[stream_id].post_launched == false) {
//post kernel launch
int S = word_len;
int num_blks = ((streams[stream_id].n + MP_MSGS_PER_BLOCK - 1) / MP_MSGS_PER_BLOCK);
dev_ctx_->clear_checkbits(stream_id, num_blks);
mp_modexp_crt_post_kernel(streams[stream_id].ret,
streams[stream_id].ret_d,
n_d,
np_d,
r_sqr_d,
iqmp_d,
streams[stream_id].n,
word_len,
block,
dev_ctx_->get_stream(stream_id),
dev_ctx_->get_dev_checkbits(stream_id));
streams[stream_id].post_launched = true;
dev_ctx_->sync(stream_id, true);
}
if (dev_ctx_->get_state(stream_id) == WAIT_KERNEL &&
streams[stream_id].post_launched == true) {
//copy result
dev_ctx_->set_state(stream_id, WAIT_COPY);
checkCudaErrors(cudaMemcpyAsync(streams[stream_id].ret,
streams[stream_id].ret_d,
sizeof(WORD[2][MAX_S]) * streams[stream_id].n,
cudaMemcpyDeviceToHost,
dev_ctx_->get_stream(stream_id)));
dev_ctx_->sync(stream_id, true);
}
if (dev_ctx_->get_state(stream_id) == WAIT_COPY) {
dev_ctx_->set_state(stream_id, READY);
}
//move result to out from gathred buffer
for (int i = 0; i < streams[stream_id].n; i++) {
int rsa_bytes = get_key_bits() / 8;
int ret = RSA_padding_check_PKCS1_type_2(streams[stream_id].out[i],
streams[stream_id].out_len[i],
(unsigned char *)(streams[stream_id].ret + (i * 2 * MAX_S)) + 1,
rsa_bytes - 1,
rsa_bytes);
if (ret == -1) {
for (int j = 0; j < 2 * word_len * (int)sizeof(WORD); j++)
printf("%02x ", *(((unsigned char *)(streams[stream_id].ret + (i * 2 * MAX_S)) + j)));
printf("\n");
assert(false);
}
streams[stream_id].out_len[i] = ret;
}
return true;
}
//nonblocking case
if (dev_ctx_->get_state(stream_id) == WAIT_KERNEL) {
if (!dev_ctx_->sync(stream_id, false))
return false;
if (!streams[stream_id].post_launched) {
//start post kernel execution
int S = word_len;
int num_blks = ((streams[stream_id].n + MP_MSGS_PER_BLOCK - 1) / MP_MSGS_PER_BLOCK);
streams[stream_id].post_launched = true;
dev_ctx_->clear_checkbits(stream_id, num_blks);
mp_modexp_crt_post_kernel(streams[stream_id].ret,
streams[stream_id].ret_d,
n_d,
np_d,
r_sqr_d,
iqmp_d,
streams[stream_id].n,
word_len,
block,
dev_ctx_->get_stream(stream_id),
dev_ctx_->get_dev_checkbits(stream_id));
streams[stream_id].post_launched = true;
return false;
} else {
//start copying result
dev_ctx_->set_state(stream_id, WAIT_COPY);
checkCudaErrors(cudaMemcpyAsync(streams[stream_id].ret,
streams[stream_id].ret_d,
sizeof(WORD[2][MAX_S]) * streams[stream_id].n,
cudaMemcpyDeviceToHost,
dev_ctx_->get_stream(stream_id)));
return false;
}
} else if (dev_ctx_->get_state(stream_id) == WAIT_COPY) {
if (!dev_ctx_->sync(stream_id, false))
return false;
//move result to out from gathred buffer
for (int i = 0; i < streams[stream_id].n; i++) {
int rsa_bytes = get_key_bits() / 8;
int ret = RSA_padding_check_PKCS1_type_2(streams[stream_id].out[i],
streams[stream_id].out_len[i],
(unsigned char *)(streams[stream_id].ret + (i * 2 * MAX_S)) + 1,
rsa_bytes - 1,
rsa_bytes);
if (ret == -1) {
for (int j = 0; j < 2 * word_len * (int)sizeof(WORD); j++)
printf("%02x ", *(((unsigned char *)(streams[stream_id].ret + (i * 2 * MAX_S)) + j)));
printf("\n");
assert(false);
}
streams[stream_id].out_len[i] = ret;
}
dev_ctx_->set_state(stream_id, READY);
return true;
}
return false;
}
void rsa_context_mp::gpu_setup()
{
assert(is_crt_available());
assert(get_key_bits() == 512 ||
get_key_bits() == 1024 ||
get_key_bits() == 2048 ||
get_key_bits() == 4096);
int word_len = (get_key_bits() / 2) / BITS_PER_WORD;
dev_ctx_ = NULL;
in_bn_p = BN_new();
in_bn_q = BN_new();
out_bn_p = BN_new();
out_bn_q = BN_new();
{
struct mp_sw sw[2];
mp_bn2mp(mp_e[0], rsa->dmp1, word_len);
mp_bn2mp(mp_e[1], rsa->dmq1, word_len);
mp_get_sw(&sw[0], mp_e[0], word_len);
mp_get_sw(&sw[1], mp_e[1], word_len);
checkCudaErrors(cudaMalloc(&sw_d, sizeof(struct mp_sw) * 2));
checkCudaErrors(cudaMemcpy(sw_d, sw, sizeof(struct mp_sw) * 2, cudaMemcpyHostToDevice));
}
{
mp_bn2mp(mp_n[0], rsa->p, word_len);
mp_bn2mp(mp_n[1], rsa->q, word_len);
checkCudaErrors(cudaMalloc(&n_d, sizeof(mp_n[0]) * 2));
checkCudaErrors(cudaMemcpy(n_d, mp_n, sizeof(mp_n[0]) * 2, cudaMemcpyHostToDevice));
}
{
mp_bn2mp(mp_iqmp, rsa->iqmp, word_len);
checkCudaErrors(cudaMalloc(&iqmp_d, sizeof(mp_iqmp)));
checkCudaErrors(cudaMemcpy(iqmp_d, mp_iqmp, sizeof(mp_iqmp), cudaMemcpyHostToDevice));
}
r = BN_new();
BN_set_bit(r, get_key_bits() / 2);
{
BIGNUM *NP = BN_new();
r_inv[0] = BN_new();
BN_mod_inverse(r_inv[0], r, rsa->p, bn_ctx);
BN_mul(NP, r, r_inv[0], bn_ctx);
BN_sub_word(NP, 1);
BN_div(NP, NULL, NP, rsa->p, bn_ctx);
mp_bn2mp(mp_np[0], NP, word_len);
r_inv[1] = BN_new();
BN_mod_inverse(r_inv[1], r, rsa->q, bn_ctx);
BN_mul(NP, r, r_inv[1], bn_ctx);
BN_sub_word(NP, 1);
BN_div(NP, NULL, NP, rsa->q, bn_ctx);
mp_bn2mp(mp_np[1], NP, word_len);
BN_free(NP);
}
{
BIGNUM *R_SQR = BN_new();
BN_mod_mul(R_SQR, r, r, rsa->p, bn_ctx);
mp_bn2mp(mp_r_sqr[0], R_SQR, word_len);
BN_mod_mul(R_SQR, r, r, rsa->q, bn_ctx);
mp_bn2mp(mp_r_sqr[1], R_SQR, word_len);
checkCudaErrors(cudaMalloc(&r_sqr_d, sizeof(mp_r_sqr[0]) * 2));
checkCudaErrors(cudaMemcpy(r_sqr_d, mp_r_sqr, sizeof(mp_r_sqr[0]) * 2, cudaMemcpyHostToDevice));
BN_free(R_SQR);
}
checkCudaErrors(cudaMalloc(&np_d, sizeof(mp_np[0]) * 2));
checkCudaErrors(cudaMemcpy(np_d, mp_np, sizeof(mp_np[0]) * 2, cudaMemcpyHostToDevice));
checkCudaErrors(cudaEventCreate(&begin_evt));
checkCudaErrors(cudaEventCreate(&end_evt));
for (unsigned int i = 0; i <= max_stream; i++) {
checkCudaErrors(cudaMalloc(&streams[i].dbg_d, sizeof(WORD[max_batch][2][MAX_S])));
checkCudaErrors(cudaMalloc(&streams[i].ret_d, sizeof(WORD[max_batch][2][MAX_S])));
checkCudaErrors(cudaMalloc(&streams[i].a_d, sizeof(WORD[max_batch][2][MAX_S])));
checkCudaErrors(cudaHostAlloc(&streams[i].a,
sizeof(WORD[max_batch][2][MAX_S]),
cudaHostAllocPortable));
checkCudaErrors(cudaHostAlloc(&streams[i].ret,
sizeof(WORD[max_batch][2][MAX_S]),
cudaHostAllocPortable));
checkCudaErrors(cudaHostAlloc(&streams[i].dbg,
sizeof(WORD[max_batch][2][MAX_S]),
cudaHostAllocPortable));
}
}