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/rand.h>
75: #include <openssl/bn.h>
76:
77: #ifndef OPENSSL_NO_HW
78: #ifndef OPENSSL_NO_HW_CSWIFT
79:
80:
81:
82:
83:
84:
85:
86:
87:
88:
89:
90:
91:
92: #ifdef FLAT_INC
93: #include "cswift.h"
94: #else
95: #include "vendor_defns/cswift.h"
96: #endif
97:
98: #define CSWIFT_LIB_NAME "cswift engine"
99: #include "e_cswift_err.c"
100:
101: #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
102:
103: static int cswift_destroy(ENGINE *e);
104: static int cswift_init(ENGINE *e);
105: static int cswift_finish(ENGINE *e);
106: static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
107: #ifndef OPENSSL_NO_RSA
108: static int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in);
109: #endif
110:
111:
112: static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
113: const BIGNUM *m, BN_CTX *ctx);
114: #ifndef OPENSSL_NO_RSA
115: static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
116: const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
117: const BIGNUM *iqmp, BN_CTX *ctx);
118: #endif
119:
120: #ifndef OPENSSL_NO_RSA
121:
122: static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
123:
124: static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
125: const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
126: #endif
127:
128: #ifndef OPENSSL_NO_DSA
129:
130: static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
131: static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
132: DSA_SIG *sig, DSA *dsa);
133: #endif
134:
135: #ifndef OPENSSL_NO_DH
136:
137:
138: static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
139: const BIGNUM *a, const BIGNUM *p,
140: const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
141: #endif
142:
143:
144: static int cswift_rand_bytes(unsigned char *buf, int num);
145: static int cswift_rand_status(void);
146:
147:
148: #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
149: static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
150: {CSWIFT_CMD_SO_PATH,
151: "SO_PATH",
152: "Specifies the path to the 'cswift' shared library",
153: ENGINE_CMD_FLAG_STRING},
154: {0, NULL, NULL, 0}
155: };
156:
157: #ifndef OPENSSL_NO_RSA
158:
159: static RSA_METHOD cswift_rsa =
160: {
161: "CryptoSwift RSA method",
162: NULL,
163: NULL,
164: NULL,
165: NULL,
166: cswift_rsa_mod_exp,
167: cswift_mod_exp_mont,
168: NULL,
169: NULL,
170: 0,
171: NULL,
172: NULL,
173: NULL,
174: NULL
175: };
176: #endif
177:
178: #ifndef OPENSSL_NO_DSA
179:
180: static DSA_METHOD cswift_dsa =
181: {
182: "CryptoSwift DSA method",
183: cswift_dsa_sign,
184: NULL,
185: cswift_dsa_verify,
186: NULL,
187: NULL,
188: NULL,
189: NULL,
190: 0,
191: NULL,
192: NULL,
193: NULL
194: };
195: #endif
196:
197: #ifndef OPENSSL_NO_DH
198:
199: static DH_METHOD cswift_dh =
200: {
201: "CryptoSwift DH method",
202: NULL,
203: NULL,
204: cswift_mod_exp_dh,
205: NULL,
206: NULL,
207: 0,
208: NULL,
209: NULL
210: };
211: #endif
212:
213: static RAND_METHOD cswift_random =
214: {
215:
216: NULL,
217: cswift_rand_bytes,
218: NULL,
219: NULL,
220: cswift_rand_bytes,
221: cswift_rand_status,
222: };
223:
224:
225:
226: static const char *engine_cswift_id = "cswift";
227: static const char *engine_cswift_name = "CryptoSwift hardware engine support";
228:
229:
230:
231: static int bind_helper(ENGINE *e)
232: {
233: #ifndef OPENSSL_NO_RSA
234: const RSA_METHOD *meth1;
235: #endif
236: #ifndef OPENSSL_NO_DH
237: const DH_METHOD *meth2;
238: #endif
239: if(!ENGINE_set_id(e, engine_cswift_id) ||
240: !ENGINE_set_name(e, engine_cswift_name) ||
241: #ifndef OPENSSL_NO_RSA
242: !ENGINE_set_RSA(e, &cswift_rsa) ||
243: #endif
244: #ifndef OPENSSL_NO_DSA
245: !ENGINE_set_DSA(e, &cswift_dsa) ||
246: #endif
247: #ifndef OPENSSL_NO_DH
248: !ENGINE_set_DH(e, &cswift_dh) ||
249: #endif
250: !ENGINE_set_RAND(e, &cswift_random) ||
251: !ENGINE_set_destroy_function(e, cswift_destroy) ||
252: !ENGINE_set_init_function(e, cswift_init) ||
253: !ENGINE_set_finish_function(e, cswift_finish) ||
254: !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
255: !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
256: return 0;
257:
258: #ifndef OPENSSL_NO_RSA
259:
260:
261:
262:
263:
264:
265:
266: meth1 = RSA_PKCS1_SSLeay();
267: cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
268: cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
269: cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
270: cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
271: #endif
272:
273: #ifndef OPENSSL_NO_DH
274:
275: meth2 = DH_OpenSSL();
276: cswift_dh.generate_key = meth2->generate_key;
277: cswift_dh.compute_key = meth2->compute_key;
278: #endif
279:
280:
281: ERR_load_CSWIFT_strings();
282: return 1;
283: }
284:
285: #ifdef OPENSSL_NO_DYNAMIC_ENGINE
286: static ENGINE *engine_cswift(void)
287: {
288: ENGINE *ret = ENGINE_new();
289: if(!ret)
290: return NULL;
291: if(!bind_helper(ret))
292: {
293: ENGINE_free(ret);
294: return NULL;
295: }
296: return ret;
297: }
298:
299: void ENGINE_load_cswift(void)
300: {
301:
302: ENGINE *toadd = engine_cswift();
303: if(!toadd) return;
304: ENGINE_add(toadd);
305: ENGINE_free(toadd);
306: ERR_clear_error();
307: }
308: #endif
309:
310:
311:
312:
313:
314:
315: static DSO *cswift_dso = NULL;
316:
317:
318:
319: t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
320: t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
321: t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
322: t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
323:
324:
325: static const char *CSWIFT_LIBNAME = NULL;
326: static const char *get_CSWIFT_LIBNAME(void)
327: {
328: if(CSWIFT_LIBNAME)
329: return CSWIFT_LIBNAME;
330: return "swift";
331: }
332: static void free_CSWIFT_LIBNAME(void)
333: {
334: if(CSWIFT_LIBNAME)
335: OPENSSL_free((void*)CSWIFT_LIBNAME);
336: CSWIFT_LIBNAME = NULL;
337: }
338: static long set_CSWIFT_LIBNAME(const char *name)
339: {
340: free_CSWIFT_LIBNAME();
341: return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
342: }
343: static const char *CSWIFT_F1 = "swAcquireAccContext";
344: static const char *CSWIFT_F2 = "swAttachKeyParam";
345: static const char *CSWIFT_F3 = "swSimpleRequest";
346: static const char *CSWIFT_F4 = "swReleaseAccContext";
347:
348:
349:
350:
351:
352:
353:
354:
355: static int get_context(SW_CONTEXT_HANDLE *hac)
356: {
357: SW_STATUS status;
358:
359: status = p_CSwift_AcquireAccContext(hac);
360: if(status != SW_OK)
361: return 0;
362: return 1;
363: }
364:
365:
366: static void release_context(SW_CONTEXT_HANDLE hac)
367: {
368: p_CSwift_ReleaseAccContext(hac);
369: }
370:
371:
372: static int cswift_destroy(ENGINE *e)
373: {
374: free_CSWIFT_LIBNAME();
375: ERR_unload_CSWIFT_strings();
376: return 1;
377: }
378:
379:
380: static int cswift_init(ENGINE *e)
381: {
382: SW_CONTEXT_HANDLE hac;
383: t_swAcquireAccContext *p1;
384: t_swAttachKeyParam *p2;
385: t_swSimpleRequest *p3;
386: t_swReleaseAccContext *p4;
387:
388: if(cswift_dso != NULL)
389: {
390: CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
391: goto err;
392: }
393:
394: cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
395: if(cswift_dso == NULL)
396: {
397: CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
398: goto err;
399: }
400: if(!(p1 = (t_swAcquireAccContext *)
401: DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
402: !(p2 = (t_swAttachKeyParam *)
403: DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
404: !(p3 = (t_swSimpleRequest *)
405: DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
406: !(p4 = (t_swReleaseAccContext *)
407: DSO_bind_func(cswift_dso, CSWIFT_F4)))
408: {
409: CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
410: goto err;
411: }
412:
413: p_CSwift_AcquireAccContext = p1;
414: p_CSwift_AttachKeyParam = p2;
415: p_CSwift_SimpleRequest = p3;
416: p_CSwift_ReleaseAccContext = p4;
417:
418:
419: if(!get_context(&hac))
420: {
421: CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
422: goto err;
423: }
424: release_context(hac);
425:
426: return 1;
427: err:
428: if(cswift_dso)
429: {
430: DSO_free(cswift_dso);
431: cswift_dso = NULL;
432: }
433: p_CSwift_AcquireAccContext = NULL;
434: p_CSwift_AttachKeyParam = NULL;
435: p_CSwift_SimpleRequest = NULL;
436: p_CSwift_ReleaseAccContext = NULL;
437: return 0;
438: }
439:
440: static int cswift_finish(ENGINE *e)
441: {
442: free_CSWIFT_LIBNAME();
443: if(cswift_dso == NULL)
444: {
445: CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
446: return 0;
447: }
448: if(!DSO_free(cswift_dso))
449: {
450: CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
451: return 0;
452: }
453: cswift_dso = NULL;
454: p_CSwift_AcquireAccContext = NULL;
455: p_CSwift_AttachKeyParam = NULL;
456: p_CSwift_SimpleRequest = NULL;
457: p_CSwift_ReleaseAccContext = NULL;
458: return 1;
459: }
460:
461: static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
462: {
463: int initialised = ((cswift_dso == NULL) ? 0 : 1);
464: switch(cmd)
465: {
466: case CSWIFT_CMD_SO_PATH:
467: if(p == NULL)
468: {
469: CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
470: return 0;
471: }
472: if(initialised)
473: {
474: CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
475: return 0;
476: }
477: return set_CSWIFT_LIBNAME((const char *)p);
478: default:
479: break;
480: }
481: CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
482: return 0;
483: }
484:
485:
486: static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
487: const BIGNUM *m, BN_CTX *ctx)
488: {
489:
490:
491:
492:
493:
494: BIGNUM *modulus;
495: BIGNUM *exponent;
496: BIGNUM *argument;
497: BIGNUM *result;
498: SW_STATUS sw_status;
499: SW_LARGENUMBER arg, res;
500: SW_PARAM sw_param;
501: SW_CONTEXT_HANDLE hac;
502: int to_return, acquired;
503:
504: modulus = exponent = argument = result = NULL;
505: to_return = 0;
506: acquired = 0;
507:
508: if(!get_context(&hac))
509: {
510: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
511: goto err;
512: }
513: acquired = 1;
514:
515: BN_CTX_start(ctx);
516: modulus = BN_CTX_get(ctx);
517: exponent = BN_CTX_get(ctx);
518: argument = BN_CTX_get(ctx);
519: result = BN_CTX_get(ctx);
520: if(!result)
521: {
522: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
523: goto err;
524: }
525: if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
526: !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
527: {
528: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
529: goto err;
530: }
531: sw_param.type = SW_ALG_EXP;
532: sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
533: (unsigned char *)modulus->d);
534: sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
535: sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
536: (unsigned char *)exponent->d);
537: sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
538:
539: sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
540: switch(sw_status)
541: {
542: case SW_OK:
543: break;
544: case SW_ERR_INPUT_SIZE:
545: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
546: goto err;
547: default:
548: {
549: char tmpbuf[DECIMAL_SIZE(sw_status)+1];
550: CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
551: sprintf(tmpbuf, "%ld", sw_status);
552: ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);