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 *);
87 static int cswift_finish(ENGINE *);
90 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
91 const BIGNUM *m, BN_CTX *ctx);
92 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
93 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
94 const BIGNUM *iqmp, BN_CTX *ctx);
97 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
98 /* This function is aliased to mod_exp (with the mont stuff dropped). */
99 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
100 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
103 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
104 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
105 DSA_SIG *sig, DSA *dsa);
108 /* This function is alised to mod_exp (with the DH and mont dropped). */
109 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
110 const BIGNUM *a, const BIGNUM *p,
111 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
114 /* Our internal RSA_METHOD that we provide pointers to */
115 static RSA_METHOD cswift_rsa =
117 "CryptoSwift RSA method",
132 /* Our internal DSA_METHOD that we provide pointers to */
133 static DSA_METHOD cswift_dsa =
135 "CryptoSwift DSA method",
137 NULL, /* dsa_sign_setup */
139 NULL, /* dsa_mod_exp */
140 NULL, /* bn_mod_exp */
147 /* Our internal DH_METHOD that we provide pointers to */
148 static DH_METHOD cswift_dh =
150 "CryptoSwift DH method",
160 /* Constants used when creating the ENGINE */
161 static const char *engine_cswift_id = "cswift";
162 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
164 /* As this is only ever called once, there's no need for locking
165 * (indeed - the lock will already be held by our caller!!!) */
166 ENGINE *ENGINE_cswift()
168 const RSA_METHOD *meth1;
169 const DH_METHOD *meth2;
170 ENGINE *ret = ENGINE_new();
173 if(!ENGINE_set_id(ret, engine_cswift_id) ||
174 !ENGINE_set_name(ret, engine_cswift_name) ||
175 !ENGINE_set_RSA(ret, &cswift_rsa) ||
176 !ENGINE_set_DSA(ret, &cswift_dsa) ||
177 !ENGINE_set_DH(ret, &cswift_dh) ||
178 !ENGINE_set_BN_mod_exp(ret, &cswift_mod_exp) ||
179 !ENGINE_set_BN_mod_exp_crt(ret, &cswift_mod_exp_crt) ||
180 !ENGINE_set_init_function(ret, cswift_init) ||
181 !ENGINE_set_finish_function(ret, cswift_finish))
187 /* We know that the "PKCS1_SSLeay()" functions hook properly
188 * to the cswift-specific mod_exp and mod_exp_crt so we use
189 * those functions. NB: We don't use ENGINE_openssl() or
190 * anything "more generic" because something like the RSAref
191 * code may not hook properly, and if you own one of these
192 * cards then you have the right to do RSA operations on it
194 meth1 = RSA_PKCS1_SSLeay();
195 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
196 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
197 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
198 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
200 /* Much the same for Diffie-Hellman */
201 meth2 = DH_OpenSSL();
202 cswift_dh.generate_key = meth2->generate_key;
203 cswift_dh.compute_key = meth2->compute_key;
207 /* This is a process-global DSO handle used for loading and unloading
208 * the CryptoSwift library. NB: This is only set (or unset) during an
209 * init() or finish() call (reference counts permitting) and they're
210 * operating with global locks, so this should be thread-safe
212 static DSO *cswift_dso = NULL;
214 /* These are the function pointers that are (un)set when the library has
215 * successfully (un)loaded. */
216 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
217 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
218 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
219 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
221 /* Used in the DSO operations. */
222 static const char *CSWIFT_LIBNAME = "swift";
223 static const char *CSWIFT_F1 = "swAcquireAccContext";
224 static const char *CSWIFT_F2 = "swAttachKeyParam";
225 static const char *CSWIFT_F3 = "swSimpleRequest";
226 static const char *CSWIFT_F4 = "swReleaseAccContext";
229 /* CryptoSwift library functions and mechanics - these are used by the
230 * higher-level functions further down. NB: As and where there's no
231 * error checking, take a look lower down where these functions are
232 * called, the checking and error handling is probably down there. */
234 /* utility function to obtain a context */
235 static int get_context(SW_CONTEXT_HANDLE *hac)
239 status = p_CSwift_AcquireAccContext(hac);
245 /* similarly to release one. */
246 static void release_context(SW_CONTEXT_HANDLE hac)
248 p_CSwift_ReleaseAccContext(hac);
251 /* (de)initialisation functions. */
252 static int cswift_init(ENGINE *e)
254 SW_CONTEXT_HANDLE hac;
255 t_swAcquireAccContext *p1;
256 t_swAttachKeyParam *p2;
257 t_swSimpleRequest *p3;
258 t_swReleaseAccContext *p4;
260 if(cswift_dso != NULL)
262 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_ALREADY_LOADED);
265 /* Attempt to load libswift.so/swift.dll/whatever. */
266 cswift_dso = DSO_load(NULL, CSWIFT_LIBNAME, NULL, 0);
267 if(cswift_dso == NULL)
269 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
272 if(!(p1 = (t_swAcquireAccContext *)
273 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
274 !(p2 = (t_swAttachKeyParam *)
275 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
276 !(p3 = (t_swSimpleRequest *)
277 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
278 !(p4 = (t_swReleaseAccContext *)
279 DSO_bind_func(cswift_dso, CSWIFT_F4)))
281 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
284 /* Copy the pointers */
285 p_CSwift_AcquireAccContext = p1;
286 p_CSwift_AttachKeyParam = p2;
287 p_CSwift_SimpleRequest = p3;
288 p_CSwift_ReleaseAccContext = p4;
289 /* Try and get a context - if not, we may have a DSO but no
291 if(!get_context(&hac))
293 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_UNIT_FAILURE);
296 release_context(hac);
297 /* Everything's fine. */
301 DSO_free(cswift_dso);
302 p_CSwift_AcquireAccContext = NULL;
303 p_CSwift_AttachKeyParam = NULL;
304 p_CSwift_SimpleRequest = NULL;
305 p_CSwift_ReleaseAccContext = NULL;
309 static int cswift_finish(ENGINE *e)
311 if(cswift_dso == NULL)
313 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_NOT_LOADED);
316 if(!DSO_free(cswift_dso))
318 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_DSO_FAILURE);
322 p_CSwift_AcquireAccContext = NULL;
323 p_CSwift_AttachKeyParam = NULL;
324 p_CSwift_SimpleRequest = NULL;
325 p_CSwift_ReleaseAccContext = NULL;
329 /* Un petit mod_exp */
330 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
331 const BIGNUM *m, BN_CTX *ctx)
333 /* I need somewhere to store temporary serialised values for
334 * use with the CryptoSwift API calls. A neat cheat - I'll use
335 * BIGNUMs from the BN_CTX but access their arrays directly as
336 * byte arrays <grin>. This way I don't have to clean anything
343 SW_LARGENUMBER arg, res;
345 SW_CONTEXT_HANDLE hac;
346 int to_return, acquired;
348 modulus = exponent = argument = result = NULL;
349 to_return = 0; /* expect failure */
352 if(!get_context(&hac))
354 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_GET_HANDLE_FAILED);
358 /* Prepare the params */
360 modulus = BN_CTX_get(ctx);
361 exponent = BN_CTX_get(ctx);
362 argument = BN_CTX_get(ctx);
363 result = BN_CTX_get(ctx);
366 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_CTX_FULL);
369 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
370 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
372 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
375 sw_param.type = SW_ALG_EXP;
376 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
377 (unsigned char *)modulus->d);
378 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
379 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
380 (unsigned char *)exponent->d);
381 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
382 /* Attach the key params */
383 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
388 case SW_ERR_INPUT_SIZE:
389 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,
390 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
395 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_REQUEST_FAILED);
396 sprintf(tmpbuf, "%ld", sw_status);
397 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
401 /* Prepare the argument and response */
402 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
403 arg.value = (unsigned char *)argument->d;
404 res.nbytes = BN_num_bytes(m);
405 memset(result->d, 0, res.nbytes);
406 res.value = (unsigned char *)result->d;
407 /* Perform the operation */
408 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
412 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_REQUEST_FAILED);
413 sprintf(tmpbuf, "%ld", sw_status);
414 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
417 /* Convert the response */
418 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
422 release_context(hac);
427 /* Un petit mod_exp chinois */
428 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
429 const BIGNUM *q, const BIGNUM *dmp1,
430 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
433 SW_LARGENUMBER arg, res;
435 SW_CONTEXT_HANDLE hac;
436 BIGNUM *rsa_p = NULL;
437 BIGNUM *rsa_q = NULL;
438 BIGNUM *rsa_dmp1 = NULL;
439 BIGNUM *rsa_dmq1 = NULL;
440 BIGNUM *rsa_iqmp = NULL;
441 BIGNUM *argument = NULL;
442 BIGNUM *result = NULL;
443 int to_return = 0; /* expect failure */
446 if(!get_context(&hac))
448 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_GET_HANDLE_FAILED);
452 /* Prepare the params */
454 rsa_p = BN_CTX_get(ctx);
455 rsa_q = BN_CTX_get(ctx);
456 rsa_dmp1 = BN_CTX_get(ctx);
457 rsa_dmq1 = BN_CTX_get(ctx);
458 rsa_iqmp = BN_CTX_get(ctx);
459 argument = BN_CTX_get(ctx);
460 result = BN_CTX_get(ctx);
463 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_CTX_FULL);
466 if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
467 !bn_wexpand(rsa_dmp1, dmp1->top) ||
468 !bn_wexpand(rsa_dmq1, dmq1->top) ||
469 !bn_wexpand(rsa_iqmp, iqmp->top) ||
470 !bn_wexpand(argument, a->top) ||
471 !bn_wexpand(result, p->top + q->top))
473 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_EXPAND_FAIL);
476 sw_param.type = SW_ALG_CRT;
477 sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
478 sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
479 sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
480 sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
481 sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
482 (unsigned char *)rsa_dmp1->d);
483 sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
484 sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
485 (unsigned char *)rsa_dmq1->d);
486 sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
487 sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
488 (unsigned char *)rsa_iqmp->d);
489 sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
490 /* Attach the key params */
491 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
496 case SW_ERR_INPUT_SIZE:
497 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,
498 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
503 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
504 sprintf(tmpbuf, "%ld", sw_status);
505 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
509 /* Prepare the argument and response */
510 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
511 arg.value = (unsigned char *)argument->d;
512 res.nbytes = 2 * BN_num_bytes(p);
513 memset(result->d, 0, res.nbytes);
514 res.value = (unsigned char *)result->d;
515 /* Perform the operation */
516 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
520 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
521 sprintf(tmpbuf, "%ld", sw_status);
522 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
525 /* Convert the response */
526 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
530 release_context(hac);
535 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
540 if((ctx = BN_CTX_new()) == NULL)
542 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
544 ENGINEerr(ENGINE_F_CSWIFT_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
547 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
548 rsa->dmq1, rsa->iqmp, ctx);
555 /* This function is aliased to mod_exp (with the mont stuff dropped). */
556 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
557 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
559 return cswift_mod_exp(r, a, p, m, ctx);
562 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
564 SW_CONTEXT_HANDLE hac;
567 SW_LARGENUMBER arg, res;
570 BIGNUM *dsa_p = NULL;
571 BIGNUM *dsa_q = NULL;
572 BIGNUM *dsa_g = NULL;
573 BIGNUM *dsa_key = NULL;
574 BIGNUM *result = NULL;
575 DSA_SIG *to_return = NULL;
578 if((ctx = BN_CTX_new()) == NULL)
580 if(!get_context(&hac))
582 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_GET_HANDLE_FAILED);
586 /* Prepare the params */
588 dsa_p = BN_CTX_get(ctx);
589 dsa_q = BN_CTX_get(ctx);
590 dsa_g = BN_CTX_get(ctx);
591 dsa_key = BN_CTX_get(ctx);
592 result = BN_CTX_get(ctx);
595 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_CTX_FULL);
598 if(!bn_wexpand(dsa_p, dsa->p->top) ||
599 !bn_wexpand(dsa_q, dsa->q->top) ||
600 !bn_wexpand(dsa_g, dsa->g->top) ||
601 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
602 !bn_wexpand(result, dsa->p->top))
604 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_EXPAND_FAIL);
607 sw_param.type = SW_ALG_DSA;
608 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
609 (unsigned char *)dsa_p->d);
610 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
611 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
612 (unsigned char *)dsa_q->d);
613 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
614 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
615 (unsigned char *)dsa_g->d);
616 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
617 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
618 (unsigned char *)dsa_key->d);
619 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
620 /* Attach the key params */
621 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
626 case SW_ERR_INPUT_SIZE:
627 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,
628 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
633 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
634 sprintf(tmpbuf, "%ld", sw_status);
635 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
639 /* Prepare the argument and response */
641 arg.value = (unsigned char *)dgst;
642 res.nbytes = BN_num_bytes(dsa->p);
643 memset(result->d, 0, res.nbytes);
644 res.value = (unsigned char *)result->d;
645 /* Perform the operation */
646 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
648 if(sw_status != SW_OK)
651 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
652 sprintf(tmpbuf, "%ld", sw_status);
653 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
656 /* Convert the response */
657 ptr = (unsigned char *)result->d;
658 if((to_return = DSA_SIG_new()) == NULL)
660 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
661 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
665 release_context(hac);
674 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
675 DSA_SIG *sig, DSA *dsa)
677 SW_CONTEXT_HANDLE hac;
680 SW_LARGENUMBER arg[2], res;
681 unsigned long sig_result;
683 BIGNUM *dsa_p = NULL;
684 BIGNUM *dsa_q = NULL;
685 BIGNUM *dsa_g = NULL;
686 BIGNUM *dsa_key = NULL;
687 BIGNUM *argument = NULL;
691 if((ctx = BN_CTX_new()) == NULL)
693 if(!get_context(&hac))
695 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_GET_HANDLE_FAILED);
699 /* Prepare the params */
701 dsa_p = BN_CTX_get(ctx);
702 dsa_q = BN_CTX_get(ctx);
703 dsa_g = BN_CTX_get(ctx);
704 dsa_key = BN_CTX_get(ctx);
705 argument = BN_CTX_get(ctx);
708 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_CTX_FULL);
711 if(!bn_wexpand(dsa_p, dsa->p->top) ||
712 !bn_wexpand(dsa_q, dsa->q->top) ||
713 !bn_wexpand(dsa_g, dsa->g->top) ||
714 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
715 !bn_wexpand(argument, 40))
717 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_EXPAND_FAIL);
720 sw_param.type = SW_ALG_DSA;
721 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
722 (unsigned char *)dsa_p->d);
723 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
724 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
725 (unsigned char *)dsa_q->d);
726 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
727 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
728 (unsigned char *)dsa_g->d);
729 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
730 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
731 (unsigned char *)dsa_key->d);
732 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
733 /* Attach the key params */
734 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
739 case SW_ERR_INPUT_SIZE:
740 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,
741 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
746 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
747 sprintf(tmpbuf, "%ld", sw_status);
748 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
752 /* Prepare the argument and response */
753 arg[0].nbytes = dgst_len;
754 arg[0].value = (unsigned char *)dgst;
756 arg[1].value = (unsigned char *)argument->d;
757 memset(arg[1].value, 0, 40);
758 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
759 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
760 res.nbytes = 4; /* unsigned long */
761 res.value = (unsigned char *)(&sig_result);
762 /* Perform the operation */
763 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
765 if(sw_status != SW_OK)
768 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
769 sprintf(tmpbuf, "%ld", sw_status);
770 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
773 /* Convert the response */
774 to_return = ((sig_result == 0) ? 0 : 1);
778 release_context(hac);
787 /* This function is aliased to mod_exp (with the dh and mont dropped). */
788 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
789 const BIGNUM *a, const BIGNUM *p,
790 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
792 return cswift_mod_exp(r, a, p, m, ctx);
795 #endif /* !OPENSSL_NO_HW_CSWIFT */
796 #endif /* !OPENSSL_NO_HW */