-
Notifications
You must be signed in to change notification settings - Fork 0
/
deadcafe.c
393 lines (338 loc) · 7.86 KB
/
deadcafe.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
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
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* Copyright (C) 2010, deadcafe.beef@gmail.com
* All rights reserved.
*/
#include <sys/types.h>
#include <stdio.h>
#include <string.h>
#include <gmp.h>
#include <openssl/crypto.h>
#include <openssl/buffer.h>
#include <openssl/engine.h>
#include <openssl/bn.h>
#define E_DEADCAFE_LIB_NAME "deadcafe engine"
#include "deadcafe_err.c"
static int deadcafe_destroy(ENGINE *e);
static int deadcafe_init(ENGINE *e);
static int deadcafe_finish(ENGINE *e);
static int deadcafe_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
static inline int
bn2gmp(const BIGNUM *bn,
mpz_t g)
{
int ret = 0;
bn_check_top(bn);
if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
(BN_BITS2 == GMP_NUMB_BITS)) {
if (_mpz_realloc(g, bn->top)) {
memcpy(&g->_mp_d[0], &bn->d[0],
bn->top * sizeof(bn->d[0]));
g->_mp_size = bn->top;
if (bn->neg)
g->_mp_size = -g->_mp_size;
ret = 1;
}
} else {
char *tmp = BN_bn2hex(bn);
if (tmp) {
ret = (mpz_set_str(g, tmp, 16) == 0 ? 1 : 0);
OPENSSL_free(tmp);
}
}
return ret;
}
static inline int
gmp2bn(const mpz_t g,
BIGNUM *bn)
{
int ret = 0;
if (((sizeof(bn->d[0]) * 8) == GMP_NUMB_BITS) &&
(BN_BITS2 == GMP_NUMB_BITS)) {
int s;
if (g->_mp_size >= 0)
s = g->_mp_size;
else
s = -(g->_mp_size);
BN_zero(bn);
if (bn_expand2(bn, s)) {
bn->top = s;
memcpy(&bn->d[0], &g->_mp_d[0], s * sizeof(bn->d[0]));
bn_correct_top(bn);
if (g->_mp_size >= 0)
bn->neg = 0;
else
bn->neg = 1;
ret = 1;
}
} else {
char *tmp = OPENSSL_malloc(mpz_sizeinbase(g, 16) + 10);
if (tmp) {
mpz_get_str(tmp, 16, g);
ret = BN_hex2bn(&bn, tmp);
OPENSSL_free(tmp);
}
}
return ret;
}
static int
deadcafe_bn_mod_exp(BIGNUM *r,
const BIGNUM *a,
const BIGNUM *p,
const BIGNUM *m)
{
mpz_t rop;
mpz_t base;
mpz_t exp;
mpz_t mod;
int ret = 0;
mpz_init(rop);
mpz_init(base);
mpz_init(exp);
mpz_init(mod);
if (!bn2gmp(a, base))
goto end;
if (!bn2gmp(p, exp))
goto end;
if (!bn2gmp(m, mod))
goto end;
mpz_powm(rop, base, exp, mod);
if (gmp2bn(rop, r))
ret = 1;
end:
mpz_clear(rop);
mpz_clear(base);
mpz_clear(exp);
mpz_clear(mod);
return ret;
}
/*****************************************************************************
* RSA stuff
*****************************************************************************/
#ifndef OPENSSL_NO_RSA
#include <openssl/rsa.h>
#define BIND_RSA(e_) bind_rsa((e_))
#define INIT_RSA() init_rsa()
static int
deadcafe_rsa_bn_mod_exp(BIGNUM *r,
const BIGNUM *a,
const BIGNUM *p,
const BIGNUM *m,
BN_CTX *ctx __attribute__((unused)),
BN_MONT_CTX *m_ctx __attribute__((unused)))
{
return deadcafe_bn_mod_exp(r, a, p, m);
}
static RSA_METHOD deadcafe_rsa;
static inline int
bind_rsa(ENGINE *e)
{
memcpy(&deadcafe_rsa, RSA_PKCS1_SSLeay(), sizeof(deadcafe_rsa));
deadcafe_rsa.name = "DC DEADCAFE RSA method";
deadcafe_rsa.bn_mod_exp = deadcafe_rsa_bn_mod_exp;
return ENGINE_set_RSA(e, &deadcafe_rsa);
}
static int hndidx_rsa = -1;
static inline int
init_rsa(void)
{
if (hndidx_rsa == -1)
hndidx_rsa = RSA_get_ex_new_index(0,
"DC GMP-based RSA key handle",
NULL, NULL, NULL);
if (hndidx_rsa == -1)
return 0;
return 1;
}
#else /* !OPENSSL_NO_RSA */
#define BIND_RSA(e_) true
#define INIT_RSA() true
#endif /* OPENSSL_NO_RSA */
/*****************************************************************************
* DSA stuff
*****************************************************************************/
#ifndef OPENSSL_NO_DSA
#include <openssl/dsa.h>
#define BIND_DSA(e_) bind_dsa((e_))
#define INIT_DSA(e_) init_dsa()
static int
deadcafe_dsa_bn_mod_exp(DSA *dsa __attribute__((unused)),
BIGNUM *r,
BIGNUM *a,
const BIGNUM *p,
const BIGNUM *m,
BN_CTX *ctx __attribute__((unused)),
BN_MONT_CTX *m_ctx __attribute__((unused)))
{
return deadcafe_bn_mod_exp(r, a, p, m);
}
static DSA_METHOD deadcafe_dsa;
static inline int
bind_dsa(ENGINE *e)
{
memcpy(&deadcafe_dsa, DSA_OpenSSL(), sizeof(deadcafe_dsa));
deadcafe_dsa.name = "DC DEADCAFE DSA method";
deadcafe_dsa.bn_mod_exp = deadcafe_dsa_bn_mod_exp;
return ENGINE_set_DSA(e, &deadcafe_dsa);
}
static int hndidx_dsa = -1;
static inline int
init_dsa(void)
{
if (hndidx_dsa == -1)
hndidx_dsa = DSA_get_ex_new_index(0,
"DC GMP-based DSA key handle",
NULL, NULL, NULL);
if (hndidx_dsa == -1)
return 0;
return 1;
}
#else /* !OPENSSL_NO_DSA */
#define BIND_DSA(e_) true
#define INIT_DSA(e_) true
#endif /* OPENSSL_NO_DSA */
/*****************************************************************************
* DH stuff
*****************************************************************************/
#ifndef OPENSSL_NO_DH
#include <openssl/dh.h>
#define BIND_DH(e_) bind_dh((e_))
#define INIT_DH() init_dh()
static int
deadcafe_dh_bn_mod_exp(const DH *dh __attribute__((unused)),
BIGNUM *r,
const BIGNUM *a,
const BIGNUM *p,
const BIGNUM *m,
BN_CTX *ctx __attribute__((unused)),
BN_MONT_CTX *m_ctx __attribute__((unused)))
{
return deadcafe_bn_mod_exp(r, a, p, m);
}
static DH_METHOD deadcafe_dh;
static inline int
bind_dh(ENGINE *e)
{
memcpy(&deadcafe_dh, DH_OpenSSL(), sizeof(deadcafe_dh));
deadcafe_dh.name = "DC DEADCAFE DH method";
deadcafe_dh.bn_mod_exp = deadcafe_dh_bn_mod_exp;
return ENGINE_set_DH(e, &deadcafe_dh);
}
static int hndidx_dh = -1;
static inline int
init_dh(void)
{
if (hndidx_dh == -1)
hndidx_dh = DH_get_ex_new_index(0,
"DC GMP-based DH key handle",
NULL, NULL, NULL);
if (hndidx_dh == -1)
return 0;
return 1;
}
#else /* !OPENSSL_NO_DH */
#define BIND_DH(e_) true
#define INIT_DH() true
#endif /* OPENSSL_NO_DH */
/*****************************************************************************
* DEADCAFE framwork
*****************************************************************************/
static const ENGINE_CMD_DEFN deadcafe_cmd_defns[] = {
{
0,
NULL,
NULL,
0
}
};
static const char *engine_deadcafe_id = "deadcafe";
static const char *engine_deadcafe_name = "DC-GMP engine support";
static int
bind_helper(ENGINE *e)
{
if (!ENGINE_set_id(e, engine_deadcafe_id) ||
!ENGINE_set_name(e, engine_deadcafe_name) ||
!ENGINE_set_destroy_function(e, deadcafe_destroy) ||
!ENGINE_set_init_function(e, deadcafe_init) ||
!ENGINE_set_finish_function(e, deadcafe_finish) ||
!ENGINE_set_ctrl_function(e, deadcafe_ctrl) ||
!ENGINE_set_cmd_defns(e, deadcafe_cmd_defns))
return 0;
if (!BIND_RSA(e) || !BIND_DSA(e) || !BIND_DH(e))
return 0;
ERR_load_DEADCAFE_strings();
return 1;
}
static inline ENGINE *
engine_deadcafe(void)
{
ENGINE *ret = ENGINE_new();
if (ret) {
if (!bind_helper(ret)) {
ENGINE_free(ret);
ret = NULL;
}
}
return ret;
}
#ifdef ENGINE_DYNAMIC_SUPPORT
static inline
#endif
void
ENGINE_load_deadcafe(void)
{
ENGINE *e = engine_deadcafe();
if (e) {
ENGINE_add(e);
ENGINE_free(e);
ERR_clear_error();
}
}
static int
deadcafe_destroy(ENGINE *e __attribute__((unused)))
{
ERR_unload_DEADCAFE_strings();
return 1;
}
static int
deadcafe_init(ENGINE *e __attribute__((unused)))
{
if (!INIT_RSA() || !INIT_DSA() || !INIT_DH())
return 0;
return 1;
}
static int
deadcafe_finish(ENGINE *e __attribute__((unused)))
{
return 1;
}
static int
deadcafe_ctrl(ENGINE *e __attribute__((unused)),
int cmd,
long i __attribute__((unused)),
void *p __attribute__((unused)),
void (*f)(void) __attribute__((unused)))
{
int ret = 1;
switch (cmd) {
default:
DEADCAFEerr(DEADCAFE_F_E_DEADCAFE_CTRL,
DEADCAFE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
ret = 0;
break;
}
return ret;
}
#ifdef ENGINE_DYNAMIC_SUPPORT
static int
bind_fn(ENGINE *e,
const char *id)
{
if (id && (strcmp(id, engine_deadcafe_id) != 0))
return 0;
if (!bind_helper(e))
return 0;
return 1;
}
IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
#endif /* ENGINE_DYNAMIC_SUPPORT */