-
Notifications
You must be signed in to change notification settings - Fork 1
/
rsa-meet-in-middle-parallelized.c
293 lines (265 loc) · 7.49 KB
/
rsa-meet-in-middle-parallelized.c
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
/**
* Meet in the middle attack on Textbook RSA
* Loris Reiff <loris.reiff@liblor.ch>
* 2021-11-04
*
* Reference:
* Boneh D., Joux A., Nguyen P.Q. (2000) Why Textbook ElGamal and RSA
* Encryption Are Insecure.
*
* Note: For larger messages (i.e. L) additional swap might be required
*
*/
#define NUM_THREADS 10
#define MIN(x,y) ((x) < (y) ? (x) : (y))
#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdbool.h>
#include <pthread.h>
#include <errno.h>
#include <gmp.h>
static const char *CIPHERTEXT = // base 16
("382ed82bad6afb2f789f5078b058c939a1e94a5bc7d4fc0e9c346418fda0cc95886ba4a"
"20efe921482829839e2163f945e7cc04cc319b3ca0a8a78ca6ed1b2feeb97ea40a7aa1d"
"c705a722b9a2605877809915b");
static const char *N = // base 10
("38268664315844449075926253758060306191986182502270296650968512313561046"
"51628384629975111689738391393872672317305934874128000449244952040977576"
"80232772228733125618598449291877751388758432792252179470197");
/* Private key:
* d = ("11022744042147117890752702934081578344845250272294374015904490699041"
* "63869398797879976809792440783287819189252777386882105127045286886091"
* "25115421115850811914142401785895152103499686385340140798997839229")
*/
static uint32_t L = 12;
static uint32_t E = 65537;
struct precalc {
// val == pow(i,e) mod N
size_t i;
mpz_t val;
};
struct build_table_thread_args {
struct precalc *table;
size_t start;
size_t end;
const mpz_t *e;
const mpz_t *n;
};
struct meet_in_the_middle_thread_args {
const mpz_t *c;
const struct precalc *arr;
size_t size;
size_t start;
size_t end;
const mpz_t *e;
const mpz_t *n;
volatile bool *stop;
mpz_t *res;
};
static struct precalc *alloc_table(
size_t size
) {
if (size * sizeof(struct precalc) <= size) { return NULL; }
struct precalc *table = malloc(size * sizeof(struct precalc));
if (!table) { return NULL; }
for (size_t i = 0; i < size; i++) {
mpz_init(table[i].val);
}
return table;
}
static void free_table(
struct precalc *table,
size_t size
) {
for (size_t i = 0; i < size; i++) {
mpz_clear(table[i].val);
}
free(table);
}
static ssize_t binary_search(
const struct precalc *arr,
const mpz_t x,
size_t size
) {
size_t low = 0;
size_t high = size - 1;
size_t mid = 0;
while (low <= high) {
mid = low + (high - low) / 2;
if (mpz_cmp(arr[mid].val, x) < 0) {
low = mid + 1;
} else if (mpz_cmp(arr[mid].val, x) > 0) {
high = mid - 1;
} else {
return (ssize_t) mid;
}
}
return (ssize_t)(-1);
}
static inline void build_table(
struct precalc *arr,
size_t start,
size_t end,
const mpz_t e,
const mpz_t n
) {
mpz_t ii; mpz_init(ii);
for (size_t i = start; i < end; i++) {
mpz_set_ui(ii, i+1);
arr[i].i = i+1;
mpz_powm(arr[i].val, ii, e, n);
}
mpz_clear(ii);
}
static inline int compare_precalc(
const void *p1,
const void *p2
) {
struct precalc *ptr1 = (struct precalc *) p1;
struct precalc *ptr2 = (struct precalc *) p2;
return mpz_cmp(ptr1->val, ptr2->val);
}
/**
* Textbook RSA meet in the middle attack according to Boneh, Joux & Nguyen.
*
* Returns 0 if successful
*/
static int rsa_meet_in_the_middle(
const mpz_t c,
const struct precalc *arr,
size_t size,
size_t start,
size_t end,
const mpz_t e,
const mpz_t n,
volatile bool *stop,
mpz_t res
) {
mpz_t ii; mpz_init(ii);
mpz_t s; mpz_init(s);
mpz_t tmp; mpz_init(tmp);
ssize_t idx = -1;
for (size_t i = start; i < end; i++) {
mpz_set_ui(ii, i+1);
// s = c * ii^(-e)
mpz_powm(tmp, ii, e, n);
mpz_invert(tmp, tmp, n);
mpz_mul(s, c, tmp);
mpz_mod(s, s, n);
idx = binary_search(arr, s, size);
if (idx != -1 || *stop) { break; }
}
if (idx != -1) {
mpz_set_ui(tmp, arr[idx].i);
mpz_mul(res, ii, tmp);
*stop = true;
}
mpz_clear(tmp);
mpz_clear(s);
mpz_clear(ii);
return idx == -1;
}
static void *build_table_thread(void *args) {
struct build_table_thread_args *targs = args;
build_table(
targs->table,
targs->start,
targs->end,
*(targs->e),
*(targs->n));
return NULL;
}
static void *rsa_meet_in_the_middle_thread(void *args) {
struct meet_in_the_middle_thread_args *targs = args;
targs->res = malloc(sizeof(mpz_t));
if (!targs->res) {
*(targs->stop) = true;
perror("Allocation failed\n");
return NULL;
}
mpz_init(*targs->res);
int r = rsa_meet_in_the_middle(
*(targs->c),
targs->arr,
targs->size,
targs->start,
targs->end,
*(targs->e),
*(targs->n),
targs->stop,
*(targs->res));
if (r) {
free(targs->res);
targs->res = NULL;
}
return NULL;
}
int main(int argc, char* argv[]) {
printf("[+] Program started\n");
mpz_t e; mpz_init(e); mpz_set_ui(e, E);
mpz_t n; mpz_init(n); mpz_set_str(n, N, 10);
mpz_t ciphertext; mpz_init(ciphertext);
mpz_set_str(ciphertext, CIPHERTEXT, 16);
mpz_t *m = NULL;
printf("[+] Allocating memory\n");
size_t table_size = 1UL << (uint64_t)L/2;
struct precalc *table = alloc_table(table_size);
if (table == NULL) {
perror("Allocation failed\n");
return 1;
}
printf("[+] Calculating table entries\n");
pthread_t tids[NUM_THREADS];
struct build_table_thread_args args[NUM_THREADS];
size_t range = (table_size + NUM_THREADS - 1) / NUM_THREADS;
for (size_t i = 0; i < NUM_THREADS; i++) {
args[i].table = table;
args[i].start = i * range;
args[i].end = MIN((i + 1) * range, table_size);
args[i].e = (const mpz_t *)&e;
args[i].n = (const mpz_t *)&n;
pthread_create(&tids[i], NULL, build_table_thread, &args[i]);
}
for (size_t i = 0; i < NUM_THREADS; i++) {
pthread_join(tids[i], NULL);
}
printf("[+] Sorting the table\n");
qsort(table, table_size, sizeof(struct precalc), &compare_precalc);
printf("[+] Performing meet-in-the-middle lookup\n");
volatile bool stop = false;
struct meet_in_the_middle_thread_args mitm_args[NUM_THREADS];
for (size_t i = 0; i < NUM_THREADS; i++) {
mitm_args[i].c = (const mpz_t *)&ciphertext;
mitm_args[i].arr = table;
mitm_args[i].size = table_size;
mitm_args[i].start = i * range;
mitm_args[i].end = MIN((i + 1) * range, table_size);
mitm_args[i].e = (const mpz_t *)&e;
mitm_args[i].n = (const mpz_t *)&n;
mitm_args[i].stop = &stop;
mitm_args[i].res = NULL;
pthread_create(&tids[i], NULL, rsa_meet_in_the_middle_thread, &mitm_args[i]);
}
for (size_t i = 0; i < NUM_THREADS; i++) {
pthread_join(tids[i], NULL);
if (mitm_args[i].res) {
m = mitm_args[i].res;
}
}
if (!m) {
printf("[-] Meet-in-the-middle attack failed\n");
free_table(table, table_size);
return 1;
}
printf("[+] Recovered message:\n");
mpz_out_str(stdout, 10, *m);
printf("\n");
free_table(table, table_size);
mpz_clear(*m);
free(m);
mpz_clear(ciphertext);
mpz_clear(n);
mpz_clear(e);
return 0;
}