1 /* crypto/engine/hw_cswift.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
60 #include <openssl/crypto.h>
62 #include <openssl/dso.h>
63 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_HW_CSWIFT
68 /* Attribution notice: Rainbow have generously allowed me to reproduce
69 * the necessary definitions here from their API. This means the support
70 * can build independently of whether application builders have the
71 * API or hardware. This will allow developers to easily produce software
72 * that has latent hardware support for any users that have accelerators
73 * installed, without the developers themselves needing anything extra.
75 * I have only clipped the parts from the CryptoSwift header files that
76 * are (or seem) relevant to the CryptoSwift support code. This is
77 * simply to keep the file sizes reasonable.
83 #include "vendor_defns/cswift.h"
86 static int cswift_init(ENGINE *e);
87 static int cswift_finish(ENGINE *e);
88 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
91 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
92 const BIGNUM *m, BN_CTX *ctx);
93 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
94 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
95 const BIGNUM *iqmp, BN_CTX *ctx);
97 #ifndef OPENSSL_NO_RSA
99 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
101 /* This function is aliased to mod_exp (with the mont stuff dropped). */
102 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
103 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
105 #ifndef OPENSSL_NO_DSA
107 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
108 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
109 DSA_SIG *sig, DSA *dsa);
112 #ifndef OPENSSL_NO_DH
114 /* This function is alised to mod_exp (with the DH and mont dropped). */
115 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
116 const BIGNUM *a, const BIGNUM *p,
117 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
120 /* The definitions for control commands specific to this engine */
121 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
122 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
125 "Specifies the path to the 'cswift' shared library",
126 ENGINE_CMD_FLAG_STRING},
130 #ifndef OPENSSL_NO_RSA
131 /* Our internal RSA_METHOD that we provide pointers to */
132 static RSA_METHOD cswift_rsa =
134 "CryptoSwift RSA method",
150 #ifndef OPENSSL_NO_DSA
151 /* Our internal DSA_METHOD that we provide pointers to */
152 static DSA_METHOD cswift_dsa =
154 "CryptoSwift DSA method",
156 NULL, /* dsa_sign_setup */
158 NULL, /* dsa_mod_exp */
159 NULL, /* bn_mod_exp */
167 #ifndef OPENSSL_NO_DH
168 /* Our internal DH_METHOD that we provide pointers to */
169 static DH_METHOD cswift_dh =
171 "CryptoSwift DH method",
182 /* Constants used when creating the ENGINE */
183 static const char *engine_cswift_id = "cswift";
184 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
186 /* As this is only ever called once, there's no need for locking
187 * (indeed - the lock will already be held by our caller!!!) */
188 ENGINE *ENGINE_cswift()
190 #ifndef OPENSSL_NO_RSA
191 const RSA_METHOD *meth1;
193 #ifndef OPENSSL_NO_DH
194 const DH_METHOD *meth2;
196 ENGINE *ret = ENGINE_new();
199 if(!ENGINE_set_id(ret, engine_cswift_id) ||
200 !ENGINE_set_name(ret, engine_cswift_name) ||
201 #ifndef OPENSSL_NO_RSA
202 !ENGINE_set_RSA(ret, &cswift_rsa) ||
204 #ifndef OPENSSL_NO_DSA
205 !ENGINE_set_DSA(ret, &cswift_dsa) ||
207 #ifndef OPENSSL_NO_DH
208 !ENGINE_set_DH(ret, &cswift_dh) ||
210 !ENGINE_set_BN_mod_exp(ret, &cswift_mod_exp) ||
211 !ENGINE_set_BN_mod_exp_crt(ret, &cswift_mod_exp_crt) ||
212 !ENGINE_set_init_function(ret, cswift_init) ||
213 !ENGINE_set_finish_function(ret, cswift_finish) ||
214 !ENGINE_set_ctrl_function(ret, cswift_ctrl) ||
215 !ENGINE_set_cmd_defns(ret, cswift_cmd_defns))
221 #ifndef OPENSSL_NO_RSA
222 /* We know that the "PKCS1_SSLeay()" functions hook properly
223 * to the cswift-specific mod_exp and mod_exp_crt so we use
224 * those functions. NB: We don't use ENGINE_openssl() or
225 * anything "more generic" because something like the RSAref
226 * code may not hook properly, and if you own one of these
227 * cards then you have the right to do RSA operations on it
229 meth1 = RSA_PKCS1_SSLeay();
230 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
231 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
232 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
233 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
236 #ifndef OPENSSL_NO_DH
237 /* Much the same for Diffie-Hellman */
238 meth2 = DH_OpenSSL();
239 cswift_dh.generate_key = meth2->generate_key;
240 cswift_dh.compute_key = meth2->compute_key;
245 /* This is a process-global DSO handle used for loading and unloading
246 * the CryptoSwift library. NB: This is only set (or unset) during an
247 * init() or finish() call (reference counts permitting) and they're
248 * operating with global locks, so this should be thread-safe
250 static DSO *cswift_dso = NULL;
252 /* These are the function pointers that are (un)set when the library has
253 * successfully (un)loaded. */
254 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
255 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
256 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
257 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
259 /* Used in the DSO operations. */
260 static const char def_CSWIFT_LIBNAME[] = "swift";
261 static const char *CSWIFT_LIBNAME = def_CSWIFT_LIBNAME;
262 static const char *CSWIFT_F1 = "swAcquireAccContext";
263 static const char *CSWIFT_F2 = "swAttachKeyParam";
264 static const char *CSWIFT_F3 = "swSimpleRequest";
265 static const char *CSWIFT_F4 = "swReleaseAccContext";
268 /* CryptoSwift library functions and mechanics - these are used by the
269 * higher-level functions further down. NB: As and where there's no
270 * error checking, take a look lower down where these functions are
271 * called, the checking and error handling is probably down there. */
273 /* utility function to obtain a context */
274 static int get_context(SW_CONTEXT_HANDLE *hac)
278 status = p_CSwift_AcquireAccContext(hac);
284 /* similarly to release one. */
285 static void release_context(SW_CONTEXT_HANDLE hac)
287 p_CSwift_ReleaseAccContext(hac);
290 /* (de)initialisation functions. */
291 static int cswift_init(ENGINE *e)
293 SW_CONTEXT_HANDLE hac;
294 t_swAcquireAccContext *p1;
295 t_swAttachKeyParam *p2;
296 t_swSimpleRequest *p3;
297 t_swReleaseAccContext *p4;
299 if(cswift_dso != NULL)
301 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_ALREADY_LOADED);
304 /* Attempt to load libswift.so/swift.dll/whatever. */
305 cswift_dso = DSO_load(NULL, CSWIFT_LIBNAME, NULL, 0);
306 if(cswift_dso == NULL)
308 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
311 if(!(p1 = (t_swAcquireAccContext *)
312 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
313 !(p2 = (t_swAttachKeyParam *)
314 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
315 !(p3 = (t_swSimpleRequest *)
316 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
317 !(p4 = (t_swReleaseAccContext *)
318 DSO_bind_func(cswift_dso, CSWIFT_F4)))
320 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
323 /* Copy the pointers */
324 p_CSwift_AcquireAccContext = p1;
325 p_CSwift_AttachKeyParam = p2;
326 p_CSwift_SimpleRequest = p3;
327 p_CSwift_ReleaseAccContext = p4;
328 /* Try and get a context - if not, we may have a DSO but no
330 if(!get_context(&hac))
332 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_UNIT_FAILURE);
335 release_context(hac);
336 /* Everything's fine. */
340 DSO_free(cswift_dso);
341 p_CSwift_AcquireAccContext = NULL;
342 p_CSwift_AttachKeyParam = NULL;
343 p_CSwift_SimpleRequest = NULL;
344 p_CSwift_ReleaseAccContext = NULL;
348 static int cswift_finish(ENGINE *e)
350 if(cswift_dso == NULL)
352 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_NOT_LOADED);
355 if(!DSO_free(cswift_dso))
357 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_DSO_FAILURE);
361 p_CSwift_AcquireAccContext = NULL;
362 p_CSwift_AttachKeyParam = NULL;
363 p_CSwift_SimpleRequest = NULL;
364 p_CSwift_ReleaseAccContext = NULL;
368 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
370 int initialised = ((cswift_dso == NULL) ? 0 : 1);
373 case CSWIFT_CMD_SO_PATH:
376 ENGINEerr(ENGINE_F_CSWIFT_CTRL,
377 ERR_R_PASSED_NULL_PARAMETER);
382 ENGINEerr(ENGINE_F_CSWIFT_CTRL,
383 ENGINE_R_ALREADY_LOADED);
386 CSWIFT_LIBNAME = (const char *)p;
391 ENGINEerr(ENGINE_F_CSWIFT_CTRL,ENGINE_R_CTRL_COMMAND_NOT_IMPLEMENTED);
395 /* Un petit mod_exp */
396 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
397 const BIGNUM *m, BN_CTX *ctx)
399 /* I need somewhere to store temporary serialised values for
400 * use with the CryptoSwift API calls. A neat cheat - I'll use
401 * BIGNUMs from the BN_CTX but access their arrays directly as
402 * byte arrays <grin>. This way I don't have to clean anything
409 SW_LARGENUMBER arg, res;
411 SW_CONTEXT_HANDLE hac;
412 int to_return, acquired;
414 modulus = exponent = argument = result = NULL;
415 to_return = 0; /* expect failure */
418 if(!get_context(&hac))
420 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_GET_HANDLE_FAILED);
424 /* Prepare the params */
426 modulus = BN_CTX_get(ctx);
427 exponent = BN_CTX_get(ctx);
428 argument = BN_CTX_get(ctx);
429 result = BN_CTX_get(ctx);
432 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_CTX_FULL);
435 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
436 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
438 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
441 sw_param.type = SW_ALG_EXP;
442 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
443 (unsigned char *)modulus->d);
444 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
445 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
446 (unsigned char *)exponent->d);
447 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
448 /* Attach the key params */
449 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
454 case SW_ERR_INPUT_SIZE:
455 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,
456 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
461 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_REQUEST_FAILED);
462 sprintf(tmpbuf, "%ld", sw_status);
463 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
467 /* Prepare the argument and response */
468 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
469 arg.value = (unsigned char *)argument->d;
470 res.nbytes = BN_num_bytes(m);
471 memset(result->d, 0, res.nbytes);
472 res.value = (unsigned char *)result->d;
473 /* Perform the operation */
474 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
478 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_REQUEST_FAILED);
479 sprintf(tmpbuf, "%ld", sw_status);
480 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
483 /* Convert the response */
484 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
488 release_context(hac);
493 /* Un petit mod_exp chinois */
494 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
495 const BIGNUM *q, const BIGNUM *dmp1,
496 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
499 SW_LARGENUMBER arg, res;
501 SW_CONTEXT_HANDLE hac;
502 BIGNUM *rsa_p = NULL;
503 BIGNUM *rsa_q = NULL;
504 BIGNUM *rsa_dmp1 = NULL;
505 BIGNUM *rsa_dmq1 = NULL;
506 BIGNUM *rsa_iqmp = NULL;
507 BIGNUM *argument = NULL;
508 BIGNUM *result = NULL;
509 int to_return = 0; /* expect failure */
512 if(!get_context(&hac))
514 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_GET_HANDLE_FAILED);
518 /* Prepare the params */
520 rsa_p = BN_CTX_get(ctx);
521 rsa_q = BN_CTX_get(ctx);
522 rsa_dmp1 = BN_CTX_get(ctx);
523 rsa_dmq1 = BN_CTX_get(ctx);
524 rsa_iqmp = BN_CTX_get(ctx);
525 argument = BN_CTX_get(ctx);
526 result = BN_CTX_get(ctx);
529 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_CTX_FULL);
532 if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
533 !bn_wexpand(rsa_dmp1, dmp1->top) ||
534 !bn_wexpand(rsa_dmq1, dmq1->top) ||
535 !bn_wexpand(rsa_iqmp, iqmp->top) ||
536 !bn_wexpand(argument, a->top) ||
537 !bn_wexpand(result, p->top + q->top))
539 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_EXPAND_FAIL);
542 sw_param.type = SW_ALG_CRT;
543 sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
544 sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
545 sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
546 sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
547 sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
548 (unsigned char *)rsa_dmp1->d);
549 sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
550 sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
551 (unsigned char *)rsa_dmq1->d);
552 sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
553 sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
554 (unsigned char *)rsa_iqmp->d);
555 sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
556 /* Attach the key params */
557 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
562 case SW_ERR_INPUT_SIZE:
563 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,
564 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
569 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
570 sprintf(tmpbuf, "%ld", sw_status);
571 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
575 /* Prepare the argument and response */
576 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
577 arg.value = (unsigned char *)argument->d;
578 res.nbytes = 2 * BN_num_bytes(p);
579 memset(result->d, 0, res.nbytes);
580 res.value = (unsigned char *)result->d;
581 /* Perform the operation */
582 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
586 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
587 sprintf(tmpbuf, "%ld", sw_status);
588 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
591 /* Convert the response */
592 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
596 release_context(hac);
601 #ifndef OPENSSL_NO_RSA
602 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
607 if((ctx = BN_CTX_new()) == NULL)
609 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
611 ENGINEerr(ENGINE_F_CSWIFT_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
614 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
615 rsa->dmq1, rsa->iqmp, ctx);
623 /* This function is aliased to mod_exp (with the mont stuff dropped). */
624 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
625 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
627 return cswift_mod_exp(r, a, p, m, ctx);
630 #ifndef OPENSSL_NO_DSA
631 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
633 SW_CONTEXT_HANDLE hac;
636 SW_LARGENUMBER arg, res;
639 BIGNUM *dsa_p = NULL;
640 BIGNUM *dsa_q = NULL;
641 BIGNUM *dsa_g = NULL;
642 BIGNUM *dsa_key = NULL;
643 BIGNUM *result = NULL;
644 DSA_SIG *to_return = NULL;
647 if((ctx = BN_CTX_new()) == NULL)
649 if(!get_context(&hac))
651 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_GET_HANDLE_FAILED);
655 /* Prepare the params */
657 dsa_p = BN_CTX_get(ctx);
658 dsa_q = BN_CTX_get(ctx);
659 dsa_g = BN_CTX_get(ctx);
660 dsa_key = BN_CTX_get(ctx);
661 result = BN_CTX_get(ctx);
664 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_CTX_FULL);
667 if(!bn_wexpand(dsa_p, dsa->p->top) ||
668 !bn_wexpand(dsa_q, dsa->q->top) ||
669 !bn_wexpand(dsa_g, dsa->g->top) ||
670 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
671 !bn_wexpand(result, dsa->p->top))
673 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_EXPAND_FAIL);
676 sw_param.type = SW_ALG_DSA;
677 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
678 (unsigned char *)dsa_p->d);
679 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
680 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
681 (unsigned char *)dsa_q->d);
682 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
683 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
684 (unsigned char *)dsa_g->d);
685 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
686 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
687 (unsigned char *)dsa_key->d);
688 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
689 /* Attach the key params */
690 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
695 case SW_ERR_INPUT_SIZE:
696 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,
697 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
702 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
703 sprintf(tmpbuf, "%ld", sw_status);
704 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
708 /* Prepare the argument and response */
710 arg.value = (unsigned char *)dgst;
711 res.nbytes = BN_num_bytes(dsa->p);
712 memset(result->d, 0, res.nbytes);
713 res.value = (unsigned char *)result->d;
714 /* Perform the operation */
715 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
717 if(sw_status != SW_OK)
720 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
721 sprintf(tmpbuf, "%ld", sw_status);
722 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
725 /* Convert the response */
726 ptr = (unsigned char *)result->d;
727 if((to_return = DSA_SIG_new()) == NULL)
729 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
730 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
734 release_context(hac);
743 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
744 DSA_SIG *sig, DSA *dsa)
746 SW_CONTEXT_HANDLE hac;
749 SW_LARGENUMBER arg[2], res;
750 unsigned long sig_result;
752 BIGNUM *dsa_p = NULL;
753 BIGNUM *dsa_q = NULL;
754 BIGNUM *dsa_g = NULL;
755 BIGNUM *dsa_key = NULL;
756 BIGNUM *argument = NULL;
760 if((ctx = BN_CTX_new()) == NULL)
762 if(!get_context(&hac))
764 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_GET_HANDLE_FAILED);
768 /* Prepare the params */
770 dsa_p = BN_CTX_get(ctx);
771 dsa_q = BN_CTX_get(ctx);
772 dsa_g = BN_CTX_get(ctx);
773 dsa_key = BN_CTX_get(ctx);
774 argument = BN_CTX_get(ctx);
777 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_CTX_FULL);
780 if(!bn_wexpand(dsa_p, dsa->p->top) ||
781 !bn_wexpand(dsa_q, dsa->q->top) ||
782 !bn_wexpand(dsa_g, dsa->g->top) ||
783 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
784 !bn_wexpand(argument, 40))
786 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_EXPAND_FAIL);
789 sw_param.type = SW_ALG_DSA;
790 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
791 (unsigned char *)dsa_p->d);
792 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
793 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
794 (unsigned char *)dsa_q->d);
795 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
796 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
797 (unsigned char *)dsa_g->d);
798 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
799 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
800 (unsigned char *)dsa_key->d);
801 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
802 /* Attach the key params */
803 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
808 case SW_ERR_INPUT_SIZE:
809 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,
810 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
815 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
816 sprintf(tmpbuf, "%ld", sw_status);
817 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
821 /* Prepare the argument and response */
822 arg[0].nbytes = dgst_len;
823 arg[0].value = (unsigned char *)dgst;
825 arg[1].value = (unsigned char *)argument->d;
826 memset(arg[1].value, 0, 40);
827 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
828 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
829 res.nbytes = 4; /* unsigned long */
830 res.value = (unsigned char *)(&sig_result);
831 /* Perform the operation */
832 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
834 if(sw_status != SW_OK)
837 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
838 sprintf(tmpbuf, "%ld", sw_status);
839 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
842 /* Convert the response */
843 to_return = ((sig_result == 0) ? 0 : 1);
847 release_context(hac);
857 #ifndef OPENSSL_NO_DH
858 /* This function is aliased to mod_exp (with the dh and mont dropped). */
859 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
860 const BIGNUM *a, const BIGNUM *p,
861 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
863 return cswift_mod_exp(r, a, p, m, ctx);
867 #endif /* !OPENSSL_NO_HW_CSWIFT */
868 #endif /* !OPENSSL_NO_HW */