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: #include <stdio.h>
60: #include <string.h>
61: #include <openssl/crypto.h>
62: #include <openssl/buffer.h>
63: #include <openssl/dso.h>
64: #include <openssl/engine.h>
65: #ifndef OPENSSL_NO_RSA
66: #include <openssl/rsa.h>
67: #endif
68: #ifndef OPENSSL_NO_DSA
69: #include <openssl/dsa.h>
70: #endif
71: #ifndef OPENSSL_NO_DH
72: #include <openssl/dh.h>
73: #endif
74: #include <openssl/bn.h>
75:
76: #ifndef OPENSSL_NO_HW
77: #ifndef OPENSSL_NO_HW_ATALLA
78:
79: #ifdef FLAT_INC
80: #include "atalla.h"
81: #else
82: #include "vendor_defns/atalla.h"
83: #endif
84:
85: #define ATALLA_LIB_NAME "atalla engine"
86: #include "e_atalla_err.c"
87:
88: static int atalla_destroy(ENGINE *e);
89: static int atalla_init(ENGINE *e);
90: static int atalla_finish(ENGINE *e);
91: static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
92:
93:
94: static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
95: const BIGNUM *m, BN_CTX *ctx);
96:
97: #ifndef OPENSSL_NO_RSA
98:
99: static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
100:
101: static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
102: const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
103: #endif
104:
105: #ifndef OPENSSL_NO_DSA
106:
107: static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
108: BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
109: BN_CTX *ctx, BN_MONT_CTX *in_mont);
110: static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
111: const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
112: BN_MONT_CTX *m_ctx);
113: #endif
114:
115: #ifndef OPENSSL_NO_DH
116:
117:
118: static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
119: const BIGNUM *a, const BIGNUM *p,
120: const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
121: #endif
122:
123:
124: #define ATALLA_CMD_SO_PATH ENGINE_CMD_BASE
125: static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
126: {ATALLA_CMD_SO_PATH,
127: "SO_PATH",
128: "Specifies the path to the 'atasi' shared library",
129: ENGINE_CMD_FLAG_STRING},
130: {0, NULL, NULL, 0}
131: };
132:
133: #ifndef OPENSSL_NO_RSA
134:
135: static RSA_METHOD atalla_rsa =
136: {
137: "Atalla RSA method",
138: NULL,
139: NULL,
140: NULL,
141: NULL,
142: atalla_rsa_mod_exp,
143: atalla_mod_exp_mont,
144: NULL,
145: NULL,
146: 0,
147: NULL,
148: NULL,
149: NULL,
150: NULL
151: };
152: #endif
153:
154: #ifndef OPENSSL_NO_DSA
155:
156: static DSA_METHOD atalla_dsa =
157: {
158: "Atalla DSA method",
159: NULL,
160: NULL,
161: NULL,
162: atalla_dsa_mod_exp,
163: atalla_mod_exp_dsa,
164: NULL,
165: NULL,
166: 0,
167: NULL,
168: NULL,
169: NULL
170: };
171: #endif
172:
173: #ifndef OPENSSL_NO_DH
174:
175: static DH_METHOD atalla_dh =
176: {
177: "Atalla DH method",
178: NULL,
179: NULL,
180: atalla_mod_exp_dh,
181: NULL,
182: NULL,
183: 0,
184: NULL,
185: NULL
186: };
187: #endif
188:
189:
190: static const char *engine_atalla_id = "atalla";
191: static const char *engine_atalla_name = "Atalla hardware engine support";
192:
193:
194:
195: static int bind_helper(ENGINE *e)
196: {
197: #ifndef OPENSSL_NO_RSA
198: const RSA_METHOD *meth1;
199: #endif
200: #ifndef OPENSSL_NO_DSA
201: const DSA_METHOD *meth2;
202: #endif
203: #ifndef OPENSSL_NO_DH
204: const DH_METHOD *meth3;
205: #endif
206: if(!ENGINE_set_id(e, engine_atalla_id) ||
207: !ENGINE_set_name(e, engine_atalla_name) ||
208: #ifndef OPENSSL_NO_RSA
209: !ENGINE_set_RSA(e, &atalla_rsa) ||
210: #endif
211: #ifndef OPENSSL_NO_DSA
212: !ENGINE_set_DSA(e, &atalla_dsa) ||
213: #endif
214: #ifndef OPENSSL_NO_DH
215: !ENGINE_set_DH(e, &atalla_dh) ||
216: #endif
217: !ENGINE_set_destroy_function(e, atalla_destroy) ||
218: !ENGINE_set_init_function(e, atalla_init) ||
219: !ENGINE_set_finish_function(e, atalla_finish) ||
220: !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
221: !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
222: return 0;
223:
224: #ifndef OPENSSL_NO_RSA
225:
226:
227:
228:
229:
230:
231:
232: meth1 = RSA_PKCS1_SSLeay();
233: atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
234: atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
235: atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
236: atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
237: #endif
238:
239: #ifndef OPENSSL_NO_DSA
240:
241:
242: meth2 = DSA_OpenSSL();
243: atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
244: atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
245: atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
246: #endif
247:
248: #ifndef OPENSSL_NO_DH
249:
250: meth3 = DH_OpenSSL();
251: atalla_dh.generate_key = meth3->generate_key;
252: atalla_dh.compute_key = meth3->compute_key;
253: #endif
254:
255:
256: ERR_load_ATALLA_strings();
257: return 1;
258: }
259:
260: #ifdef OPENSSL_NO_DYNAMIC_ENGINE
261: static ENGINE *engine_atalla(void)
262: {
263: ENGINE *ret = ENGINE_new();
264: if(!ret)
265: return NULL;
266: if(!bind_helper(ret))
267: {
268: ENGINE_free(ret);
269: return NULL;
270: }
271: return ret;
272: }
273:
274: void ENGINE_load_atalla(void)
275: {
276:
277: ENGINE *toadd = engine_atalla();
278: if(!toadd) return;
279: ENGINE_add(toadd);
280: ENGINE_free(toadd);
281: ERR_clear_error();
282: }
283: #endif
284:
285:
286:
287:
288:
289:
290: static DSO *atalla_dso = NULL;
291:
292:
293:
294: static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
295: static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
296: static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
297:
298:
299:
300:
301:
302:
303:
304:
305:
306: static const char *ATALLA_LIBNAME = NULL;
307: static const char *get_ATALLA_LIBNAME(void)
308: {
309: if(ATALLA_LIBNAME)
310: return ATALLA_LIBNAME;
311: return "atasi";
312: }
313: static void free_ATALLA_LIBNAME(void)
314: {
315: if(ATALLA_LIBNAME)
316: OPENSSL_free((void*)ATALLA_LIBNAME);
317: ATALLA_LIBNAME = NULL;
318: }
319: static long set_ATALLA_LIBNAME(const char *name)
320: {
321: free_ATALLA_LIBNAME();
322: return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
323: }
324: static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
325: static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
326: static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
327:
328:
329: static int atalla_destroy(ENGINE *e)
330: {
331: free_ATALLA_LIBNAME();
332:
333:
334:
335: ERR_unload_ATALLA_strings();
336: return 1;
337: }
338:
339:
340: static int atalla_init(ENGINE *e)
341: {
342: tfnASI_GetHardwareConfig *p1;
343: tfnASI_RSAPrivateKeyOpFn *p2;
344: tfnASI_GetPerformanceStatistics *p3;
345:
346:
347: unsigned int config_buf[1024];
348:
349: if(atalla_dso != NULL)
350: {
351: ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED);
352: goto err;
353: }
354:
355:
356:
357:
358:
359:
360:
361: atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0);
362: if(atalla_dso == NULL)
363: {
364: ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
365: goto err;
366: }
367: if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
368: atalla_dso, ATALLA_F1)) ||
369: !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
370: atalla_dso, ATALLA_F2)) ||
371: !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
372: atalla_dso, ATALLA_F3)))
373: {
374: ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
375: goto err;
376: }
377:
378: p_Atalla_GetHardwareConfig = p1;
379: p_Atalla_RSAPrivateKeyOpFn = p2;
380: p_Atalla_GetPerformanceStatistics = p3;
381:
382:
383: if(p1(0L, config_buf) != 0)
384: {
385: ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE);
386: goto err;
387: }
388:
389: return 1;
390: err:
391: if(atalla_dso)
392: DSO_free(atalla_dso);
393: atalla_dso = NULL;
394: p_Atalla_GetHardwareConfig = NULL;
395: p_Atalla_RSAPrivateKeyOpFn = NULL;
396: p_Atalla_GetPerformanceStatistics = NULL;
397: return 0;
398: }
399:
400: static int atalla_finish(ENGINE *e)
401: {
402: free_ATALLA_LIBNAME();
403: if(atalla_dso == NULL)
404: {
405: ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED);
406: return 0;
407: }
408: if(!DSO_free(atalla_dso))
409: {
410: ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE);
411: return 0;
412: }
413: atalla_dso = NULL;
414: p_Atalla_GetHardwareConfig = NULL;
415: p_Atalla_RSAPrivateKeyOpFn = NULL;
416: p_Atalla_GetPerformanceStatistics = NULL;
417: return 1;
418: }
419:
420: static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
421: {
422: int initialised = ((atalla_dso == NULL) ? 0 : 1);
423: switch(cmd)
424: {
425: case ATALLA_CMD_SO_PATH:
426: if(p == NULL)
427: {
428: ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER);
429: return 0;
430: }
431: if(initialised)
432: {
433: ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED);
434: return 0;
435: }
436: return set_ATALLA_LIBNAME((const char *)p);
437: default:
438: break;
439: }
440: ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
441: return 0;
442: }
443:
444: static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
445: const BIGNUM *m, BN_CTX *ctx)
446: {
447:
448:
449:
450:
451:
452: BIGNUM *modulus;
453: BIGNUM *exponent;
454: BIGNUM *argument;
455: BIGNUM *result;
456: RSAPrivateKey keydata;
457: int to_return, numbytes;
458:
459: modulus = exponent = argument = result = NULL;
460: to_return = 0;
461:
462: if(!atalla_dso)
463: {
464: ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);
465: goto err;
466: }
467:
468: BN_CTX_start(ctx);
469: modulus = BN_CTX_get(ctx);
470: exponent = BN_CTX_get(ctx);
471: argument = BN_CTX_get(ctx);
472: result = BN_CTX_get(ctx);
473: if (!result)
474: {
475: ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);
476: goto err;
477: }
478: if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
479: !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
480: {
481: ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);
482: goto err;
483: }
484:
485: memset(&keydata, 0,sizeof keydata);
486: numbytes = BN_num_bytes(m);
487: memset(exponent->d, 0, numbytes);
488: memset(modulus->d, 0, numbytes);
489: BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
490: BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
491: keydata.privateExponent.data = (unsigned char *)exponent->d;
492: keydata.privateExponent.len = numbytes;
493: keydata.modulus.data = (unsigned char *)modulus->d;
494: keydata.modulus.len = numbytes;
495:
496: memset(argument->d, 0, numbytes);
497: memset(result->d, 0, numbytes);
498: BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
499:
500: if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
501: (unsigned char *)argument->d,
502: keydata.modulus.len) != 0)
503: {
504: ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);
505: goto err;
506: }
507:
508: BN_bin2bn((unsigned char *)result->d, numbytes, r);
509: to_return = 1;
510: err:
511: BN_CTX_end(ctx);
512: return to_return;
513: }
514:
515: #ifndef OPENSSL_NO_RSA
516: static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
517: {
518: int to_return = 0;
519:
520: if(!atalla_dso)
521: {
522: ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED);
523: goto err;
524: }
525: if(!rsa->d || !rsa->n)
526: {
527: ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS);
528: goto err;
529: }
530: to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
531: err:
532: return to_return;
533: }
534: #endif
535:
536: #ifndef OPENSSL_NO_DSA
537:
538:
539:
540:
541:
542:
543:
544: static int atalla_dsa_mod_exp