]> git.ipfire.org Git - thirdparty/openssl.git/blob - engines/e_cswift.c
Reduce header interdependencies, initially in engine.h (the rest of the
[thirdparty/openssl.git] / engines / e_cswift.c
1 /* crypto/engine/hw_cswift.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3 * project 2000.
4 */
5 /* ====================================================================
6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
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 #include <openssl/rsa.h>
66 #include <openssl/dsa.h>
67 #include <openssl/dh.h>
68 #include <openssl/rand.h>
69
70 #ifndef OPENSSL_NO_HW
71 #ifndef OPENSSL_NO_HW_CSWIFT
72
73 /* Attribution notice: Rainbow have generously allowed me to reproduce
74 * the necessary definitions here from their API. This means the support
75 * can build independently of whether application builders have the
76 * API or hardware. This will allow developers to easily produce software
77 * that has latent hardware support for any users that have accelerators
78 * installed, without the developers themselves needing anything extra.
79 *
80 * I have only clipped the parts from the CryptoSwift header files that
81 * are (or seem) relevant to the CryptoSwift support code. This is
82 * simply to keep the file sizes reasonable.
83 * [Geoff]
84 */
85 #ifdef FLAT_INC
86 #include "cswift.h"
87 #else
88 #include "vendor_defns/cswift.h"
89 #endif
90
91 #define CSWIFT_LIB_NAME "cswift engine"
92 #include "e_cswift_err.c"
93
94 #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
95
96 static int cswift_destroy(ENGINE *e);
97 static int cswift_init(ENGINE *e);
98 static int cswift_finish(ENGINE *e);
99 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
100
101 /* BIGNUM stuff */
102 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
103 const BIGNUM *m, BN_CTX *ctx);
104 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
105 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
106 const BIGNUM *iqmp, BN_CTX *ctx);
107
108 #ifndef OPENSSL_NO_RSA
109 /* RSA stuff */
110 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
111 #endif
112 /* This function is aliased to mod_exp (with the mont stuff dropped). */
113 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
114 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
115
116 #ifndef OPENSSL_NO_DSA
117 /* DSA stuff */
118 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
119 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
120 DSA_SIG *sig, DSA *dsa);
121 #endif
122
123 #ifndef OPENSSL_NO_DH
124 /* DH stuff */
125 /* This function is alised to mod_exp (with the DH and mont dropped). */
126 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
127 const BIGNUM *a, const BIGNUM *p,
128 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
129 #endif
130
131 /* RAND stuff */
132 static int cswift_rand_bytes(unsigned char *buf, int num);
133 static int cswift_rand_status(void);
134
135 /* The definitions for control commands specific to this engine */
136 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
137 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
138 {CSWIFT_CMD_SO_PATH,
139 "SO_PATH",
140 "Specifies the path to the 'cswift' shared library",
141 ENGINE_CMD_FLAG_STRING},
142 {0, NULL, NULL, 0}
143 };
144
145 #ifndef OPENSSL_NO_RSA
146 /* Our internal RSA_METHOD that we provide pointers to */
147 static RSA_METHOD cswift_rsa =
148 {
149 "CryptoSwift RSA method",
150 NULL,
151 NULL,
152 NULL,
153 NULL,
154 cswift_rsa_mod_exp,
155 cswift_mod_exp_mont,
156 NULL,
157 NULL,
158 0,
159 NULL,
160 NULL,
161 NULL,
162 NULL
163 };
164 #endif
165
166 #ifndef OPENSSL_NO_DSA
167 /* Our internal DSA_METHOD that we provide pointers to */
168 static DSA_METHOD cswift_dsa =
169 {
170 "CryptoSwift DSA method",
171 cswift_dsa_sign,
172 NULL, /* dsa_sign_setup */
173 cswift_dsa_verify,
174 NULL, /* dsa_mod_exp */
175 NULL, /* bn_mod_exp */
176 NULL, /* init */
177 NULL, /* finish */
178 0, /* flags */
179 NULL, /* app_data */
180 NULL, /* dsa_paramgen */
181 NULL /* dsa_keygen */
182 };
183 #endif
184
185 #ifndef OPENSSL_NO_DH
186 /* Our internal DH_METHOD that we provide pointers to */
187 static DH_METHOD cswift_dh =
188 {
189 "CryptoSwift DH method",
190 NULL,
191 NULL,
192 cswift_mod_exp_dh,
193 NULL,
194 NULL,
195 0,
196 NULL,
197 NULL
198 };
199 #endif
200
201 static RAND_METHOD cswift_random =
202 {
203 /* "CryptoSwift RAND method", */
204 NULL,
205 cswift_rand_bytes,
206 NULL,
207 NULL,
208 cswift_rand_bytes,
209 cswift_rand_status,
210 };
211
212
213 /* Constants used when creating the ENGINE */
214 static const char *engine_cswift_id = "cswift";
215 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
216
217 /* This internal function is used by ENGINE_cswift() and possibly by the
218 * "dynamic" ENGINE support too */
219 static int bind_helper(ENGINE *e)
220 {
221 #ifndef OPENSSL_NO_RSA
222 const RSA_METHOD *meth1;
223 #endif
224 #ifndef OPENSSL_NO_DH
225 const DH_METHOD *meth2;
226 #endif
227 if(!ENGINE_set_id(e, engine_cswift_id) ||
228 !ENGINE_set_name(e, engine_cswift_name) ||
229 #ifndef OPENSSL_NO_RSA
230 !ENGINE_set_RSA(e, &cswift_rsa) ||
231 #endif
232 #ifndef OPENSSL_NO_DSA
233 !ENGINE_set_DSA(e, &cswift_dsa) ||
234 #endif
235 #ifndef OPENSSL_NO_DH
236 !ENGINE_set_DH(e, &cswift_dh) ||
237 #endif
238 !ENGINE_set_RAND(e, &cswift_random) ||
239 !ENGINE_set_destroy_function(e, cswift_destroy) ||
240 !ENGINE_set_init_function(e, cswift_init) ||
241 !ENGINE_set_finish_function(e, cswift_finish) ||
242 !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
243 !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
244 return 0;
245
246 #ifndef OPENSSL_NO_RSA
247 /* We know that the "PKCS1_SSLeay()" functions hook properly
248 * to the cswift-specific mod_exp and mod_exp_crt so we use
249 * those functions. NB: We don't use ENGINE_openssl() or
250 * anything "more generic" because something like the RSAref
251 * code may not hook properly, and if you own one of these
252 * cards then you have the right to do RSA operations on it
253 * anyway! */
254 meth1 = RSA_PKCS1_SSLeay();
255 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
256 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
257 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
258 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
259 #endif
260
261 #ifndef OPENSSL_NO_DH
262 /* Much the same for Diffie-Hellman */
263 meth2 = DH_OpenSSL();
264 cswift_dh.generate_key = meth2->generate_key;
265 cswift_dh.compute_key = meth2->compute_key;
266 #endif
267
268 /* Ensure the cswift error handling is set up */
269 ERR_load_CSWIFT_strings();
270 return 1;
271 }
272
273 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
274 static ENGINE *engine_cswift(void)
275 {
276 ENGINE *ret = ENGINE_new();
277 if(!ret)
278 return NULL;
279 if(!bind_helper(ret))
280 {
281 ENGINE_free(ret);
282 return NULL;
283 }
284 return ret;
285 }
286
287 void ENGINE_load_cswift(void)
288 {
289 /* Copied from eng_[openssl|dyn].c */
290 ENGINE *toadd = engine_cswift();
291 if(!toadd) return;
292 ENGINE_add(toadd);
293 ENGINE_free(toadd);
294 ERR_clear_error();
295 }
296 #endif
297
298 /* This is a process-global DSO handle used for loading and unloading
299 * the CryptoSwift library. NB: This is only set (or unset) during an
300 * init() or finish() call (reference counts permitting) and they're
301 * operating with global locks, so this should be thread-safe
302 * implicitly. */
303 static DSO *cswift_dso = NULL;
304
305 /* These are the function pointers that are (un)set when the library has
306 * successfully (un)loaded. */
307 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
308 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
309 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
310 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
311
312 /* Used in the DSO operations. */
313 static const char *CSWIFT_LIBNAME = NULL;
314 static const char *get_CSWIFT_LIBNAME(void)
315 {
316 if(CSWIFT_LIBNAME)
317 return CSWIFT_LIBNAME;
318 return "swift";
319 }
320 static void free_CSWIFT_LIBNAME(void)
321 {
322 if(CSWIFT_LIBNAME)
323 OPENSSL_free((void*)CSWIFT_LIBNAME);
324 CSWIFT_LIBNAME = NULL;
325 }
326 static long set_CSWIFT_LIBNAME(const char *name)
327 {
328 free_CSWIFT_LIBNAME();
329 return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
330 }
331 static const char *CSWIFT_F1 = "swAcquireAccContext";
332 static const char *CSWIFT_F2 = "swAttachKeyParam";
333 static const char *CSWIFT_F3 = "swSimpleRequest";
334 static const char *CSWIFT_F4 = "swReleaseAccContext";
335
336
337 /* CryptoSwift library functions and mechanics - these are used by the
338 * higher-level functions further down. NB: As and where there's no
339 * error checking, take a look lower down where these functions are
340 * called, the checking and error handling is probably down there. */
341
342 /* utility function to obtain a context */
343 static int get_context(SW_CONTEXT_HANDLE *hac)
344 {
345 SW_STATUS status;
346
347 status = p_CSwift_AcquireAccContext(hac);
348 if(status != SW_OK)
349 return 0;
350 return 1;
351 }
352
353 /* similarly to release one. */
354 static void release_context(SW_CONTEXT_HANDLE hac)
355 {
356 p_CSwift_ReleaseAccContext(hac);
357 }
358
359 /* Destructor (complements the "ENGINE_cswift()" constructor) */
360 static int cswift_destroy(ENGINE *e)
361 {
362 free_CSWIFT_LIBNAME();
363 ERR_unload_CSWIFT_strings();
364 return 1;
365 }
366
367 /* (de)initialisation functions. */
368 static int cswift_init(ENGINE *e)
369 {
370 SW_CONTEXT_HANDLE hac;
371 t_swAcquireAccContext *p1;
372 t_swAttachKeyParam *p2;
373 t_swSimpleRequest *p3;
374 t_swReleaseAccContext *p4;
375
376 if(cswift_dso != NULL)
377 {
378 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
379 goto err;
380 }
381 /* Attempt to load libswift.so/swift.dll/whatever. */
382 cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
383 if(cswift_dso == NULL)
384 {
385 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
386 goto err;
387 }
388 if(!(p1 = (t_swAcquireAccContext *)
389 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
390 !(p2 = (t_swAttachKeyParam *)
391 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
392 !(p3 = (t_swSimpleRequest *)
393 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
394 !(p4 = (t_swReleaseAccContext *)
395 DSO_bind_func(cswift_dso, CSWIFT_F4)))
396 {
397 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
398 goto err;
399 }
400 /* Copy the pointers */
401 p_CSwift_AcquireAccContext = p1;
402 p_CSwift_AttachKeyParam = p2;
403 p_CSwift_SimpleRequest = p3;
404 p_CSwift_ReleaseAccContext = p4;
405 /* Try and get a context - if not, we may have a DSO but no
406 * accelerator! */
407 if(!get_context(&hac))
408 {
409 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
410 goto err;
411 }
412 release_context(hac);
413 /* Everything's fine. */
414 return 1;
415 err:
416 if(cswift_dso)
417 DSO_free(cswift_dso);
418 p_CSwift_AcquireAccContext = NULL;
419 p_CSwift_AttachKeyParam = NULL;
420 p_CSwift_SimpleRequest = NULL;
421 p_CSwift_ReleaseAccContext = NULL;
422 return 0;
423 }
424
425 static int cswift_finish(ENGINE *e)
426 {
427 free_CSWIFT_LIBNAME();
428 if(cswift_dso == NULL)
429 {
430 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
431 return 0;
432 }
433 if(!DSO_free(cswift_dso))
434 {
435 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
436 return 0;
437 }
438 cswift_dso = NULL;
439 p_CSwift_AcquireAccContext = NULL;
440 p_CSwift_AttachKeyParam = NULL;
441 p_CSwift_SimpleRequest = NULL;
442 p_CSwift_ReleaseAccContext = NULL;
443 return 1;
444 }
445
446 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
447 {
448 int initialised = ((cswift_dso == NULL) ? 0 : 1);
449 switch(cmd)
450 {
451 case CSWIFT_CMD_SO_PATH:
452 if(p == NULL)
453 {
454 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
455 return 0;
456 }
457 if(initialised)
458 {
459 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
460 return 0;
461 }
462 return set_CSWIFT_LIBNAME((const char *)p);
463 default:
464 break;
465 }
466 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
467 return 0;
468 }
469
470 /* Un petit mod_exp */
471 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
472 const BIGNUM *m, BN_CTX *ctx)
473 {
474 /* I need somewhere to store temporary serialised values for
475 * use with the CryptoSwift API calls. A neat cheat - I'll use
476 * BIGNUMs from the BN_CTX but access their arrays directly as
477 * byte arrays <grin>. This way I don't have to clean anything
478 * up. */
479 BIGNUM *modulus;
480 BIGNUM *exponent;
481 BIGNUM *argument;
482 BIGNUM *result;
483 SW_STATUS sw_status;
484 SW_LARGENUMBER arg, res;
485 SW_PARAM sw_param;
486 SW_CONTEXT_HANDLE hac;
487 int to_return, acquired;
488
489 modulus = exponent = argument = result = NULL;
490 to_return = 0; /* expect failure */
491 acquired = 0;
492
493 if(!get_context(&hac))
494 {
495 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
496 goto err;
497 }
498 acquired = 1;
499 /* Prepare the params */
500 BN_CTX_start(ctx);
501 modulus = BN_CTX_get(ctx);
502 exponent = BN_CTX_get(ctx);
503 argument = BN_CTX_get(ctx);
504 result = BN_CTX_get(ctx);
505 if(!result)
506 {
507 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
508 goto err;
509 }
510 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
511 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
512 {
513 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
514 goto err;
515 }
516 sw_param.type = SW_ALG_EXP;
517 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
518 (unsigned char *)modulus->d);
519 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
520 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
521 (unsigned char *)exponent->d);
522 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
523 /* Attach the key params */
524 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
525 switch(sw_status)
526 {
527 case SW_OK:
528 break;
529 case SW_ERR_INPUT_SIZE:
530 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
531 goto err;
532 default:
533 {
534 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
535 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
536 sprintf(tmpbuf, "%ld", sw_status);
537 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
538 }
539 goto err;
540 }
541 /* Prepare the argument and response */
542 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
543 arg.value = (unsigned char *)argument->d;
544 res.nbytes = BN_num_bytes(m);
545 memset(result->d, 0, res.nbytes);
546 res.value = (unsigned char *)result->d;
547 /* Perform the operation */
548 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
549 &res, 1)) != SW_OK)
550 {
551 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
552 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
553 sprintf(tmpbuf, "%ld", sw_status);
554 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
555 goto err;
556 }
557 /* Convert the response */
558 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
559 to_return = 1;
560 err:
561 if(acquired)
562 release_context(hac);
563 BN_CTX_end(ctx);
564 return to_return;
565 }
566
567 /* Un petit mod_exp chinois */
568 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
569 const BIGNUM *q, const BIGNUM *dmp1,
570 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
571 {
572 SW_STATUS sw_status;
573 SW_LARGENUMBER arg, res;
574 SW_PARAM sw_param;
575 SW_CONTEXT_HANDLE hac;
576 BIGNUM *rsa_p = NULL;
577 BIGNUM *rsa_q = NULL;
578 BIGNUM *rsa_dmp1 = NULL;
579 BIGNUM *rsa_dmq1 = NULL;
580 BIGNUM *rsa_iqmp = NULL;
581 BIGNUM *argument = NULL;
582 BIGNUM *result = NULL;
583 int to_return = 0; /* expect failure */
584 int acquired = 0;
585
586 if(!get_context(&hac))
587 {
588 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
589 goto err;
590 }
591 acquired = 1;
592 /* Prepare the params */
593 BN_CTX_start(ctx);
594 rsa_p = BN_CTX_get(ctx);
595 rsa_q = BN_CTX_get(ctx);
596 rsa_dmp1 = BN_CTX_get(ctx);
597 rsa_dmq1 = BN_CTX_get(ctx);
598 rsa_iqmp = BN_CTX_get(ctx);
599 argument = BN_CTX_get(ctx);
600 result = BN_CTX_get(ctx);
601 if(!result)
602 {
603 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
604 goto err;
605 }
606 if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
607 !bn_wexpand(rsa_dmp1, dmp1->top) ||
608 !bn_wexpand(rsa_dmq1, dmq1->top) ||
609 !bn_wexpand(rsa_iqmp, iqmp->top) ||
610 !bn_wexpand(argument, a->top) ||
611 !bn_wexpand(result, p->top + q->top))
612 {
613 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
614 goto err;
615 }
616 sw_param.type = SW_ALG_CRT;
617 sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
618 sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
619 sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
620 sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
621 sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
622 (unsigned char *)rsa_dmp1->d);
623 sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
624 sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
625 (unsigned char *)rsa_dmq1->d);
626 sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
627 sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
628 (unsigned char *)rsa_iqmp->d);
629 sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
630 /* Attach the key params */
631 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
632 switch(sw_status)
633 {
634 case SW_OK:
635 break;
636 case SW_ERR_INPUT_SIZE:
637 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
638 goto err;
639 default:
640 {
641 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
642 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
643 sprintf(tmpbuf, "%ld", sw_status);
644 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
645 }
646 goto err;
647 }
648 /* Prepare the argument and response */
649 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
650 arg.value = (unsigned char *)argument->d;
651 res.nbytes = 2 * BN_num_bytes(p);
652 memset(result->d, 0, res.nbytes);
653 res.value = (unsigned char *)result->d;
654 /* Perform the operation */
655 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
656 &res, 1)) != SW_OK)
657 {
658 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
659 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
660 sprintf(tmpbuf, "%ld", sw_status);
661 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
662 goto err;
663 }
664 /* Convert the response */
665 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
666 to_return = 1;
667 err:
668 if(acquired)
669 release_context(hac);
670 BN_CTX_end(ctx);
671 return to_return;
672 }
673
674 #ifndef OPENSSL_NO_RSA
675 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
676 {
677 int to_return = 0;
678
679 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
680 {
681 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
682 goto err;
683 }
684 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
685 rsa->dmq1, rsa->iqmp, ctx);
686 err:
687 return to_return;
688 }
689 #endif
690
691 /* This function is aliased to mod_exp (with the mont stuff dropped). */
692 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
693 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
694 {
695 return cswift_mod_exp(r, a, p, m, ctx);
696 }
697
698 #ifndef OPENSSL_NO_DSA
699 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
700 {
701 SW_CONTEXT_HANDLE hac;
702 SW_PARAM sw_param;
703 SW_STATUS sw_status;
704 SW_LARGENUMBER arg, res;
705 unsigned char *ptr;
706 BN_CTX *ctx;
707 BIGNUM *dsa_p = NULL;
708 BIGNUM *dsa_q = NULL;
709 BIGNUM *dsa_g = NULL;
710 BIGNUM *dsa_key = NULL;
711 BIGNUM *result = NULL;
712 DSA_SIG *to_return = NULL;
713 int acquired = 0;
714
715 if((ctx = BN_CTX_new()) == NULL)
716 goto err;
717 if(!get_context(&hac))
718 {
719 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
720 goto err;
721 }
722 acquired = 1;
723 /* Prepare the params */
724 BN_CTX_start(ctx);
725 dsa_p = BN_CTX_get(ctx);
726 dsa_q = BN_CTX_get(ctx);
727 dsa_g = BN_CTX_get(ctx);
728 dsa_key = BN_CTX_get(ctx);
729 result = BN_CTX_get(ctx);
730 if(!result)
731 {
732 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
733 goto err;
734 }
735 if(!bn_wexpand(dsa_p, dsa->p->top) ||
736 !bn_wexpand(dsa_q, dsa->q->top) ||
737 !bn_wexpand(dsa_g, dsa->g->top) ||
738 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
739 !bn_wexpand(result, dsa->p->top))
740 {
741 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
742 goto err;
743 }
744 sw_param.type = SW_ALG_DSA;
745 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
746 (unsigned char *)dsa_p->d);
747 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
748 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
749 (unsigned char *)dsa_q->d);
750 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
751 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
752 (unsigned char *)dsa_g->d);
753 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
754 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
755 (unsigned char *)dsa_key->d);
756 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
757 /* Attach the key params */
758 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
759 switch(sw_status)
760 {
761 case SW_OK:
762 break;
763 case SW_ERR_INPUT_SIZE:
764 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
765 goto err;
766 default:
767 {
768 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
769 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
770 sprintf(tmpbuf, "%ld", sw_status);
771 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
772 }
773 goto err;
774 }
775 /* Prepare the argument and response */
776 arg.nbytes = dlen;
777 arg.value = (unsigned char *)dgst;
778 res.nbytes = BN_num_bytes(dsa->p);
779 memset(result->d, 0, res.nbytes);
780 res.value = (unsigned char *)result->d;
781 /* Perform the operation */
782 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
783 &res, 1);
784 if(sw_status != SW_OK)
785 {
786 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
787 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
788 sprintf(tmpbuf, "%ld", sw_status);
789 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
790 goto err;
791 }
792 /* Convert the response */
793 ptr = (unsigned char *)result->d;
794 if((to_return = DSA_SIG_new()) == NULL)
795 goto err;
796 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
797 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
798
799 err:
800 if(acquired)
801 release_context(hac);
802 if(ctx)
803 {
804 BN_CTX_end(ctx);
805 BN_CTX_free(ctx);
806 }
807 return to_return;
808 }
809
810 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
811 DSA_SIG *sig, DSA *dsa)
812 {
813 SW_CONTEXT_HANDLE hac;
814 SW_PARAM sw_param;
815 SW_STATUS sw_status;
816 SW_LARGENUMBER arg[2], res;
817 unsigned long sig_result;
818 BN_CTX *ctx;
819 BIGNUM *dsa_p = NULL;
820 BIGNUM *dsa_q = NULL;
821 BIGNUM *dsa_g = NULL;
822 BIGNUM *dsa_key = NULL;
823 BIGNUM *argument = NULL;
824 int to_return = -1;
825 int acquired = 0;
826
827 if((ctx = BN_CTX_new()) == NULL)
828 goto err;
829 if(!get_context(&hac))
830 {
831 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
832 goto err;
833 }
834 acquired = 1;
835 /* Prepare the params */
836 BN_CTX_start(ctx);
837 dsa_p = BN_CTX_get(ctx);
838 dsa_q = BN_CTX_get(ctx);
839 dsa_g = BN_CTX_get(ctx);
840 dsa_key = BN_CTX_get(ctx);
841 argument = BN_CTX_get(ctx);
842 if(!argument)
843 {
844 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
845 goto err;
846 }
847 if(!bn_wexpand(dsa_p, dsa->p->top) ||
848 !bn_wexpand(dsa_q, dsa->q->top) ||
849 !bn_wexpand(dsa_g, dsa->g->top) ||
850 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
851 !bn_wexpand(argument, 40))
852 {
853 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
854 goto err;
855 }
856 sw_param.type = SW_ALG_DSA;
857 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
858 (unsigned char *)dsa_p->d);
859 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
860 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
861 (unsigned char *)dsa_q->d);
862 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
863 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
864 (unsigned char *)dsa_g->d);
865 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
866 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
867 (unsigned char *)dsa_key->d);
868 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
869 /* Attach the key params */
870 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
871 switch(sw_status)
872 {
873 case SW_OK:
874 break;
875 case SW_ERR_INPUT_SIZE:
876 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
877 goto err;
878 default:
879 {
880 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
881 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
882 sprintf(tmpbuf, "%ld", sw_status);
883 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
884 }
885 goto err;
886 }
887 /* Prepare the argument and response */
888 arg[0].nbytes = dgst_len;
889 arg[0].value = (unsigned char *)dgst;
890 arg[1].nbytes = 40;
891 arg[1].value = (unsigned char *)argument->d;
892 memset(arg[1].value, 0, 40);
893 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
894 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
895 res.nbytes = 4; /* unsigned long */
896 res.value = (unsigned char *)(&sig_result);
897 /* Perform the operation */
898 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
899 &res, 1);
900 if(sw_status != SW_OK)
901 {
902 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
903 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
904 sprintf(tmpbuf, "%ld", sw_status);
905 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
906 goto err;
907 }
908 /* Convert the response */
909 to_return = ((sig_result == 0) ? 0 : 1);
910
911 err:
912 if(acquired)
913 release_context(hac);
914 if(ctx)
915 {
916 BN_CTX_end(ctx);
917 BN_CTX_free(ctx);
918 }
919 return to_return;
920 }
921 #endif
922
923 #ifndef OPENSSL_NO_DH
924 /* This function is aliased to mod_exp (with the dh and mont dropped). */
925 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
926 const BIGNUM *a, const BIGNUM *p,
927 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
928 {
929 return cswift_mod_exp(r, a, p, m, ctx);
930 }
931 #endif
932
933 /* Random bytes are good */
934 static int cswift_rand_bytes(unsigned char *buf, int num)
935 {
936 SW_CONTEXT_HANDLE hac;
937 SW_STATUS swrc;
938 SW_LARGENUMBER largenum;
939 size_t nbytes = 0;
940 int acquired = 0;
941 int to_return = 0; /* assume failure */
942
943 if (!get_context(&hac))
944 {
945 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_UNIT_FAILURE);
946 goto err;
947 }
948 acquired = 1;
949
950 while (nbytes < (size_t)num)
951 {
952 /* tell CryptoSwift how many bytes we want and where we want it.
953 * Note: - CryptoSwift cannot do more than 4096 bytes at a time.
954 * - CryptoSwift can only do multiple of 32-bits. */
955 largenum.value = (SW_BYTE *) buf + nbytes;
956 if (4096 > num - nbytes)
957 largenum.nbytes = num - nbytes;
958 else
959 largenum.nbytes = 4096;
960
961 swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
962 if (swrc != SW_OK)
963 {
964 char tmpbuf[20];
965 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_REQUEST_FAILED);
966 sprintf(tmpbuf, "%ld", swrc);
967 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
968 goto err;
969 }
970
971 nbytes += largenum.nbytes;
972 }
973 to_return = 1; /* success */
974
975 err:
976 if (acquired)
977 release_context(hac);
978 return to_return;
979 }
980
981 static int cswift_rand_status(void)
982 {
983 return 1;
984 }
985
986
987 /* This stuff is needed if this ENGINE is being compiled into a self-contained
988 * shared-library. */
989 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
990 static int bind_fn(ENGINE *e, const char *id)
991 {
992 if(id && (strcmp(id, engine_cswift_id) != 0))
993 return 0;
994 if(!bind_helper(e))
995 return 0;
996 return 1;
997 }
998 IMPLEMENT_DYNAMIC_CHECK_FN()
999 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1000 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
1001
1002 #endif /* !OPENSSL_NO_HW_CSWIFT */
1003 #endif /* !OPENSSL_NO_HW */