1 /* crypto/engine/hw_cswift.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 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 #define CSWIFT_LIB_NAME "cswift engine"
87 #include "hw_cswift_err.c"
89 static int cswift_destroy(ENGINE *e);
90 static int cswift_init(ENGINE *e);
91 static int cswift_finish(ENGINE *e);
92 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
95 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
96 const BIGNUM *m, BN_CTX *ctx);
97 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
98 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
99 const BIGNUM *iqmp, BN_CTX *ctx);
101 #ifndef OPENSSL_NO_RSA
103 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
105 /* This function is aliased to mod_exp (with the mont stuff dropped). */
106 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
107 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
109 #ifndef OPENSSL_NO_DSA
111 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
112 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
113 DSA_SIG *sig, DSA *dsa);
116 #ifndef OPENSSL_NO_DH
118 /* This function is alised to mod_exp (with the DH and mont dropped). */
119 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
120 const BIGNUM *a, const BIGNUM *p,
121 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
124 /* The definitions for control commands specific to this engine */
125 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
126 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
129 "Specifies the path to the 'cswift' shared library",
130 ENGINE_CMD_FLAG_STRING},
134 #ifndef OPENSSL_NO_RSA
135 /* Our internal RSA_METHOD that we provide pointers to */
136 static RSA_METHOD cswift_rsa =
138 "CryptoSwift RSA method",
154 #ifndef OPENSSL_NO_DSA
155 /* Our internal DSA_METHOD that we provide pointers to */
156 static DSA_METHOD cswift_dsa =
158 "CryptoSwift DSA method",
160 NULL, /* dsa_sign_setup */
162 NULL, /* dsa_mod_exp */
163 NULL, /* bn_mod_exp */
171 #ifndef OPENSSL_NO_DH
172 /* Our internal DH_METHOD that we provide pointers to */
173 static DH_METHOD cswift_dh =
175 "CryptoSwift DH method",
186 /* Constants used when creating the ENGINE */
187 static const char *engine_cswift_id = "cswift";
188 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
190 /* This internal function is used by ENGINE_cswift() and possibly by the
191 * "dynamic" ENGINE support too */
192 static int bind_helper(ENGINE *e)
194 #ifndef OPENSSL_NO_RSA
195 const RSA_METHOD *meth1;
197 #ifndef OPENSSL_NO_DH
198 const DH_METHOD *meth2;
200 if(!ENGINE_set_id(e, engine_cswift_id) ||
201 !ENGINE_set_name(e, engine_cswift_name) ||
202 #ifndef OPENSSL_NO_RSA
203 !ENGINE_set_RSA(e, &cswift_rsa) ||
205 #ifndef OPENSSL_NO_DSA
206 !ENGINE_set_DSA(e, &cswift_dsa) ||
208 #ifndef OPENSSL_NO_DH
209 !ENGINE_set_DH(e, &cswift_dh) ||
211 !ENGINE_set_destroy_function(e, cswift_destroy) ||
212 !ENGINE_set_init_function(e, cswift_init) ||
213 !ENGINE_set_finish_function(e, cswift_finish) ||
214 !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
215 !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
218 #ifndef OPENSSL_NO_RSA
219 /* We know that the "PKCS1_SSLeay()" functions hook properly
220 * to the cswift-specific mod_exp and mod_exp_crt so we use
221 * those functions. NB: We don't use ENGINE_openssl() or
222 * anything "more generic" because something like the RSAref
223 * code may not hook properly, and if you own one of these
224 * cards then you have the right to do RSA operations on it
226 meth1 = RSA_PKCS1_SSLeay();
227 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
228 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
229 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
230 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
233 #ifndef OPENSSL_NO_DH
234 /* Much the same for Diffie-Hellman */
235 meth2 = DH_OpenSSL();
236 cswift_dh.generate_key = meth2->generate_key;
237 cswift_dh.compute_key = meth2->compute_key;
240 /* Ensure the cswift error handling is set up */
241 ERR_load_CSWIFT_strings();
245 static ENGINE *engine_cswift(void)
247 ENGINE *ret = ENGINE_new();
250 if(!bind_helper(ret))
258 void ENGINE_load_cswift(void)
260 /* Copied from eng_[openssl|dyn].c */
261 ENGINE *toadd = engine_cswift();
268 /* This is a process-global DSO handle used for loading and unloading
269 * the CryptoSwift library. NB: This is only set (or unset) during an
270 * init() or finish() call (reference counts permitting) and they're
271 * operating with global locks, so this should be thread-safe
273 static DSO *cswift_dso = NULL;
275 /* These are the function pointers that are (un)set when the library has
276 * successfully (un)loaded. */
277 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
278 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
279 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
280 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
282 /* Used in the DSO operations. */
283 static const char *CSWIFT_LIBNAME = NULL;
284 static const char *get_CSWIFT_LIBNAME(void)
287 return CSWIFT_LIBNAME;
290 static void free_CSWIFT_LIBNAME(void)
293 OPENSSL_free((void*)CSWIFT_LIBNAME);
294 CSWIFT_LIBNAME = NULL;
296 static long set_CSWIFT_LIBNAME(const char *name)
298 free_CSWIFT_LIBNAME();
299 return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
301 static const char *CSWIFT_F1 = "swAcquireAccContext";
302 static const char *CSWIFT_F2 = "swAttachKeyParam";
303 static const char *CSWIFT_F3 = "swSimpleRequest";
304 static const char *CSWIFT_F4 = "swReleaseAccContext";
307 /* CryptoSwift library functions and mechanics - these are used by the
308 * higher-level functions further down. NB: As and where there's no
309 * error checking, take a look lower down where these functions are
310 * called, the checking and error handling is probably down there. */
312 /* utility function to obtain a context */
313 static int get_context(SW_CONTEXT_HANDLE *hac)
317 status = p_CSwift_AcquireAccContext(hac);
323 /* similarly to release one. */
324 static void release_context(SW_CONTEXT_HANDLE hac)
326 p_CSwift_ReleaseAccContext(hac);
329 /* Destructor (complements the "ENGINE_cswift()" constructor) */
330 static int cswift_destroy(ENGINE *e)
332 free_CSWIFT_LIBNAME();
333 ERR_unload_CSWIFT_strings();
337 /* (de)initialisation functions. */
338 static int cswift_init(ENGINE *e)
340 SW_CONTEXT_HANDLE hac;
341 t_swAcquireAccContext *p1;
342 t_swAttachKeyParam *p2;
343 t_swSimpleRequest *p3;
344 t_swReleaseAccContext *p4;
346 if(cswift_dso != NULL)
348 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
351 /* Attempt to load libswift.so/swift.dll/whatever. */
352 cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
353 if(cswift_dso == NULL)
355 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
358 if(!(p1 = (t_swAcquireAccContext *)
359 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
360 !(p2 = (t_swAttachKeyParam *)
361 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
362 !(p3 = (t_swSimpleRequest *)
363 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
364 !(p4 = (t_swReleaseAccContext *)
365 DSO_bind_func(cswift_dso, CSWIFT_F4)))
367 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
370 /* Copy the pointers */
371 p_CSwift_AcquireAccContext = p1;
372 p_CSwift_AttachKeyParam = p2;
373 p_CSwift_SimpleRequest = p3;
374 p_CSwift_ReleaseAccContext = p4;
375 /* Try and get a context - if not, we may have a DSO but no
377 if(!get_context(&hac))
379 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
382 release_context(hac);
383 /* Everything's fine. */
387 DSO_free(cswift_dso);
388 p_CSwift_AcquireAccContext = NULL;
389 p_CSwift_AttachKeyParam = NULL;
390 p_CSwift_SimpleRequest = NULL;
391 p_CSwift_ReleaseAccContext = NULL;
395 static int cswift_finish(ENGINE *e)
397 free_CSWIFT_LIBNAME();
398 if(cswift_dso == NULL)
400 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
403 if(!DSO_free(cswift_dso))
405 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
409 p_CSwift_AcquireAccContext = NULL;
410 p_CSwift_AttachKeyParam = NULL;
411 p_CSwift_SimpleRequest = NULL;
412 p_CSwift_ReleaseAccContext = NULL;
416 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
418 int initialised = ((cswift_dso == NULL) ? 0 : 1);
421 case CSWIFT_CMD_SO_PATH:
424 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
429 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
432 return set_CSWIFT_LIBNAME((const char *)p);
436 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
440 /* Un petit mod_exp */
441 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
442 const BIGNUM *m, BN_CTX *ctx)
444 /* I need somewhere to store temporary serialised values for
445 * use with the CryptoSwift API calls. A neat cheat - I'll use
446 * BIGNUMs from the BN_CTX but access their arrays directly as
447 * byte arrays <grin>. This way I don't have to clean anything
454 SW_LARGENUMBER arg, res;
456 SW_CONTEXT_HANDLE hac;
457 int to_return, acquired;
459 modulus = exponent = argument = result = NULL;
460 to_return = 0; /* expect failure */
463 if(!get_context(&hac))
465 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
469 /* Prepare the params */
471 modulus = BN_CTX_get(ctx);
472 exponent = BN_CTX_get(ctx);
473 argument = BN_CTX_get(ctx);
474 result = BN_CTX_get(ctx);
477 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
480 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
481 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
483 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
486 sw_param.type = SW_ALG_EXP;
487 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
488 (unsigned char *)modulus->d);
489 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
490 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
491 (unsigned char *)exponent->d);
492 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
493 /* Attach the key params */
494 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
499 case SW_ERR_INPUT_SIZE:
500 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
505 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
506 sprintf(tmpbuf, "%ld", sw_status);
507 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
511 /* Prepare the argument and response */
512 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
513 arg.value = (unsigned char *)argument->d;
514 res.nbytes = BN_num_bytes(m);
515 memset(result->d, 0, res.nbytes);
516 res.value = (unsigned char *)result->d;
517 /* Perform the operation */
518 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
522 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
523 sprintf(tmpbuf, "%ld", sw_status);
524 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
527 /* Convert the response */
528 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
532 release_context(hac);
537 /* Un petit mod_exp chinois */
538 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
539 const BIGNUM *q, const BIGNUM *dmp1,
540 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
543 SW_LARGENUMBER arg, res;
545 SW_CONTEXT_HANDLE hac;
546 BIGNUM *rsa_p = NULL;
547 BIGNUM *rsa_q = NULL;
548 BIGNUM *rsa_dmp1 = NULL;
549 BIGNUM *rsa_dmq1 = NULL;
550 BIGNUM *rsa_iqmp = NULL;
551 BIGNUM *argument = NULL;
552 BIGNUM *result = NULL;
553 int to_return = 0; /* expect failure */
556 if(!get_context(&hac))
558 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
562 /* Prepare the params */
564 rsa_p = BN_CTX_get(ctx);
565 rsa_q = BN_CTX_get(ctx);
566 rsa_dmp1 = BN_CTX_get(ctx);
567 rsa_dmq1 = BN_CTX_get(ctx);
568 rsa_iqmp = BN_CTX_get(ctx);
569 argument = BN_CTX_get(ctx);
570 result = BN_CTX_get(ctx);
573 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
576 if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
577 !bn_wexpand(rsa_dmp1, dmp1->top) ||
578 !bn_wexpand(rsa_dmq1, dmq1->top) ||
579 !bn_wexpand(rsa_iqmp, iqmp->top) ||
580 !bn_wexpand(argument, a->top) ||
581 !bn_wexpand(result, p->top + q->top))
583 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
586 sw_param.type = SW_ALG_CRT;
587 sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
588 sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
589 sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
590 sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
591 sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
592 (unsigned char *)rsa_dmp1->d);
593 sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
594 sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
595 (unsigned char *)rsa_dmq1->d);
596 sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
597 sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
598 (unsigned char *)rsa_iqmp->d);
599 sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
600 /* Attach the key params */
601 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
606 case SW_ERR_INPUT_SIZE:
607 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
612 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
613 sprintf(tmpbuf, "%ld", sw_status);
614 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
618 /* Prepare the argument and response */
619 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
620 arg.value = (unsigned char *)argument->d;
621 res.nbytes = 2 * BN_num_bytes(p);
622 memset(result->d, 0, res.nbytes);
623 res.value = (unsigned char *)result->d;
624 /* Perform the operation */
625 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
629 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
630 sprintf(tmpbuf, "%ld", sw_status);
631 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
634 /* Convert the response */
635 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
639 release_context(hac);
644 #ifndef OPENSSL_NO_RSA
645 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
650 if((ctx = BN_CTX_new()) == NULL)
652 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
654 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
657 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
658 rsa->dmq1, rsa->iqmp, ctx);
666 /* This function is aliased to mod_exp (with the mont stuff dropped). */
667 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
668 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
670 return cswift_mod_exp(r, a, p, m, ctx);
673 #ifndef OPENSSL_NO_DSA
674 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
676 SW_CONTEXT_HANDLE hac;
679 SW_LARGENUMBER arg, res;
682 BIGNUM *dsa_p = NULL;
683 BIGNUM *dsa_q = NULL;
684 BIGNUM *dsa_g = NULL;
685 BIGNUM *dsa_key = NULL;
686 BIGNUM *result = NULL;
687 DSA_SIG *to_return = NULL;
690 if((ctx = BN_CTX_new()) == NULL)
692 if(!get_context(&hac))
694 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
698 /* Prepare the params */
700 dsa_p = BN_CTX_get(ctx);
701 dsa_q = BN_CTX_get(ctx);
702 dsa_g = BN_CTX_get(ctx);
703 dsa_key = BN_CTX_get(ctx);
704 result = BN_CTX_get(ctx);
707 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
710 if(!bn_wexpand(dsa_p, dsa->p->top) ||
711 !bn_wexpand(dsa_q, dsa->q->top) ||
712 !bn_wexpand(dsa_g, dsa->g->top) ||
713 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
714 !bn_wexpand(result, dsa->p->top))
716 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
719 sw_param.type = SW_ALG_DSA;
720 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
721 (unsigned char *)dsa_p->d);
722 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
723 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
724 (unsigned char *)dsa_q->d);
725 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
726 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
727 (unsigned char *)dsa_g->d);
728 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
729 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
730 (unsigned char *)dsa_key->d);
731 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
732 /* Attach the key params */
733 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
738 case SW_ERR_INPUT_SIZE:
739 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
744 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
745 sprintf(tmpbuf, "%ld", sw_status);
746 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
750 /* Prepare the argument and response */
752 arg.value = (unsigned char *)dgst;
753 res.nbytes = BN_num_bytes(dsa->p);
754 memset(result->d, 0, res.nbytes);
755 res.value = (unsigned char *)result->d;
756 /* Perform the operation */
757 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
759 if(sw_status != SW_OK)
762 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
763 sprintf(tmpbuf, "%ld", sw_status);
764 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
767 /* Convert the response */
768 ptr = (unsigned char *)result->d;
769 if((to_return = DSA_SIG_new()) == NULL)
771 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
772 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
776 release_context(hac);
785 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
786 DSA_SIG *sig, DSA *dsa)
788 SW_CONTEXT_HANDLE hac;
791 SW_LARGENUMBER arg[2], res;
792 unsigned long sig_result;
794 BIGNUM *dsa_p = NULL;
795 BIGNUM *dsa_q = NULL;
796 BIGNUM *dsa_g = NULL;
797 BIGNUM *dsa_key = NULL;
798 BIGNUM *argument = NULL;
802 if((ctx = BN_CTX_new()) == NULL)
804 if(!get_context(&hac))
806 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
810 /* Prepare the params */
812 dsa_p = BN_CTX_get(ctx);
813 dsa_q = BN_CTX_get(ctx);
814 dsa_g = BN_CTX_get(ctx);
815 dsa_key = BN_CTX_get(ctx);
816 argument = BN_CTX_get(ctx);
819 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
822 if(!bn_wexpand(dsa_p, dsa->p->top) ||
823 !bn_wexpand(dsa_q, dsa->q->top) ||
824 !bn_wexpand(dsa_g, dsa->g->top) ||
825 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
826 !bn_wexpand(argument, 40))
828 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
831 sw_param.type = SW_ALG_DSA;
832 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
833 (unsigned char *)dsa_p->d);
834 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
835 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
836 (unsigned char *)dsa_q->d);
837 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
838 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
839 (unsigned char *)dsa_g->d);
840 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
841 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
842 (unsigned char *)dsa_key->d);
843 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
844 /* Attach the key params */
845 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
850 case SW_ERR_INPUT_SIZE:
851 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
856 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
857 sprintf(tmpbuf, "%ld", sw_status);
858 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
862 /* Prepare the argument and response */
863 arg[0].nbytes = dgst_len;
864 arg[0].value = (unsigned char *)dgst;
866 arg[1].value = (unsigned char *)argument->d;
867 memset(arg[1].value, 0, 40);
868 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
869 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
870 res.nbytes = 4; /* unsigned long */
871 res.value = (unsigned char *)(&sig_result);
872 /* Perform the operation */
873 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
875 if(sw_status != SW_OK)
878 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
879 sprintf(tmpbuf, "%ld", sw_status);
880 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
883 /* Convert the response */
884 to_return = ((sig_result == 0) ? 0 : 1);
888 release_context(hac);
898 #ifndef OPENSSL_NO_DH
899 /* This function is aliased to mod_exp (with the dh and mont dropped). */
900 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
901 const BIGNUM *a, const BIGNUM *p,
902 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
904 return cswift_mod_exp(r, a, p, m, ctx);
908 /* This stuff is needed if this ENGINE is being compiled into a self-contained
910 #ifdef ENGINE_DYNAMIC_SUPPORT
911 static int bind_fn(ENGINE *e, const char *id)
913 if(id && (strcmp(id, engine_cswift_id) != 0))
919 IMPLEMENT_DYNAMIC_CHECK_FN()
920 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
921 #endif /* ENGINE_DYNAMIC_SUPPORT */
923 #endif /* !OPENSSL_NO_HW_CSWIFT */
924 #endif /* !OPENSSL_NO_HW */