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 "engine_int.h"
64 #include <openssl/engine.h>
69 /* Attribution notice: Rainbow have generously allowed me to reproduce
70 * the necessary definitions here from their API. This means the support
71 * can build independently of whether application builders have the
72 * API or hardware. This will allow developers to easily produce software
73 * that has latent hardware support for any users that have accelerators
74 * installed, without the developers themselves needing anything extra.
76 * I have only clipped the parts from the CryptoSwift header files that
77 * are (or seem) relevant to the CryptoSwift support code. This is
78 * simply to keep the file sizes reasonable.
84 #include "vendor_defns/cswift.h"
87 static int cswift_init(void);
88 static int cswift_finish(void);
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);
98 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
99 /* This function is aliased to mod_exp (with the mont stuff dropped). */
100 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
101 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
104 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
105 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
106 DSA_SIG *sig, DSA *dsa);
109 /* This function is alised to mod_exp (with the DH and mont dropped). */
110 static int cswift_mod_exp_dh(DH *dh, BIGNUM *r, 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 /* Our ENGINE structure. */
161 static ENGINE engine_cswift =
164 "CryptoSwift hardware engine support",
173 NULL, /* no ctrl() */
174 NULL, /* no load_privkey() */
175 NULL, /* no load_pubkey() */
177 0, 0, /* no references */
178 NULL, NULL /* unlinked */
181 /* As this is only ever called once, there's no need for locking
182 * (indeed - the lock will already be held by our caller!!!) */
183 ENGINE *ENGINE_cswift()
185 const RSA_METHOD *meth1;
188 /* We know that the "PKCS1_SSLeay()" functions hook properly
189 * to the cswift-specific mod_exp and mod_exp_crt so we use
190 * those functions. NB: We don't use ENGINE_openssl() or
191 * anything "more generic" because something like the RSAref
192 * code may not hook properly, and if you own one of these
193 * cards then you have the right to do RSA operations on it
195 meth1 = RSA_PKCS1_SSLeay();
196 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
197 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
198 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
199 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
201 /* Much the same for Diffie-Hellman */
202 meth2 = DH_OpenSSL();
203 cswift_dh.generate_key = meth2->generate_key;
204 cswift_dh.compute_key = meth2->compute_key;
205 return &engine_cswift;
208 /* This is a process-global DSO handle used for loading and unloading
209 * the CryptoSwift library. NB: This is only set (or unset) during an
210 * init() or finish() call (reference counts permitting) and they're
211 * operating with global locks, so this should be thread-safe
213 static DSO *cswift_dso = NULL;
215 /* These are the function pointers that are (un)set when the library has
216 * successfully (un)loaded. */
217 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
218 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
219 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
220 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
222 /* Used in the DSO operations. */
223 static const char *CSWIFT_LIBNAME = "swift";
224 static const char *CSWIFT_F1 = "swAcquireAccContext";
225 static const char *CSWIFT_F2 = "swAttachKeyParam";
226 static const char *CSWIFT_F3 = "swSimpleRequest";
227 static const char *CSWIFT_F4 = "swReleaseAccContext";
230 /* CryptoSwift library functions and mechanics - these are used by the
231 * higher-level functions further down. NB: As and where there's no
232 * error checking, take a look lower down where these functions are
233 * called, the checking and error handling is probably down there. */
235 /* utility function to obtain a context */
236 static int get_context(SW_CONTEXT_HANDLE *hac)
240 status = p_CSwift_AcquireAccContext(hac);
246 /* similarly to release one. */
247 static void release_context(SW_CONTEXT_HANDLE hac)
249 p_CSwift_ReleaseAccContext(hac);
252 /* (de)initialisation functions. */
253 static int cswift_init()
255 SW_CONTEXT_HANDLE hac;
256 t_swAcquireAccContext *p1;
257 t_swAttachKeyParam *p2;
258 t_swSimpleRequest *p3;
259 t_swReleaseAccContext *p4;
261 if(cswift_dso != NULL)
263 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_ALREADY_LOADED);
266 /* Attempt to load libswift.so/swift.dll/whatever. */
267 cswift_dso = DSO_load(NULL, CSWIFT_LIBNAME, NULL, 0);
268 if(cswift_dso == NULL)
270 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
273 if(!(p1 = (t_swAcquireAccContext *)
274 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
275 !(p2 = (t_swAttachKeyParam *)
276 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
277 !(p3 = (t_swSimpleRequest *)
278 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
279 !(p4 = (t_swReleaseAccContext *)
280 DSO_bind_func(cswift_dso, CSWIFT_F4)))
282 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
285 /* Copy the pointers */
286 p_CSwift_AcquireAccContext = p1;
287 p_CSwift_AttachKeyParam = p2;
288 p_CSwift_SimpleRequest = p3;
289 p_CSwift_ReleaseAccContext = p4;
290 /* Try and get a context - if not, we may have a DSO but no
292 if(!get_context(&hac))
294 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_UNIT_FAILURE);
297 release_context(hac);
298 /* Everything's fine. */
302 DSO_free(cswift_dso);
303 p_CSwift_AcquireAccContext = NULL;
304 p_CSwift_AttachKeyParam = NULL;
305 p_CSwift_SimpleRequest = NULL;
306 p_CSwift_ReleaseAccContext = NULL;
310 static int cswift_finish()
312 if(cswift_dso == NULL)
314 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_NOT_LOADED);
317 if(!DSO_free(cswift_dso))
319 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_DSO_FAILURE);
323 p_CSwift_AcquireAccContext = NULL;
324 p_CSwift_AttachKeyParam = NULL;
325 p_CSwift_SimpleRequest = NULL;
326 p_CSwift_ReleaseAccContext = NULL;
330 /* Un petit mod_exp */
331 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
332 const BIGNUM *m, BN_CTX *ctx)
334 /* I need somewhere to store temporary serialised values for
335 * use with the CryptoSwift API calls. A neat cheat - I'll use
336 * BIGNUMs from the BN_CTX but access their arrays directly as
337 * byte arrays <grin>. This way I don't have to clean anything
344 SW_LARGENUMBER arg, res;
346 SW_CONTEXT_HANDLE hac;
347 int to_return, acquired;
349 modulus = exponent = argument = result = NULL;
350 to_return = 0; /* expect failure */
353 if(!get_context(&hac))
355 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_GET_HANDLE_FAILED);
359 /* 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);
364 if(!modulus || !exponent || !argument || !result)
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);
423 if(modulus) ctx->tos--;
424 if(exponent) ctx->tos--;
425 if(argument) ctx->tos--;
426 if(result) ctx->tos--;
430 /* Un petit mod_exp chinois */
431 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
432 const BIGNUM *q, const BIGNUM *dmp1,
433 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
436 SW_LARGENUMBER arg, res;
438 SW_CONTEXT_HANDLE hac;
439 BIGNUM *rsa_p = NULL;
440 BIGNUM *rsa_q = NULL;
441 BIGNUM *rsa_dmp1 = NULL;
442 BIGNUM *rsa_dmq1 = NULL;
443 BIGNUM *rsa_iqmp = NULL;
444 BIGNUM *argument = NULL;
445 BIGNUM *result = NULL;
446 int to_return = 0; /* expect failure */
449 if(!get_context(&hac))
451 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_GET_HANDLE_FAILED);
455 /* Prepare the params */
456 rsa_p = BN_CTX_get(ctx);
457 rsa_q = BN_CTX_get(ctx);
458 rsa_dmp1 = BN_CTX_get(ctx);
459 rsa_dmq1 = BN_CTX_get(ctx);
460 rsa_iqmp = BN_CTX_get(ctx);
461 argument = BN_CTX_get(ctx);
462 result = BN_CTX_get(ctx);
463 if(!rsa_p || !rsa_q || !rsa_dmp1 || !rsa_dmq1 || !rsa_iqmp ||
464 !argument || !result)
466 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_CTX_FULL);
469 if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
470 !bn_wexpand(rsa_dmp1, dmp1->top) ||
471 !bn_wexpand(rsa_dmq1, dmq1->top) ||
472 !bn_wexpand(rsa_iqmp, iqmp->top) ||
473 !bn_wexpand(argument, a->top) ||
474 !bn_wexpand(result, p->top + q->top))
476 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_EXPAND_FAIL);
479 sw_param.type = SW_ALG_CRT;
480 sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
481 sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
482 sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
483 sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
484 sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
485 (unsigned char *)rsa_dmp1->d);
486 sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
487 sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
488 (unsigned char *)rsa_dmq1->d);
489 sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
490 sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
491 (unsigned char *)rsa_iqmp->d);
492 sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
493 /* Attach the key params */
494 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
499 case SW_ERR_INPUT_SIZE:
500 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,
501 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
506 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
507 sprintf(tmpbuf, "%ld", sw_status);
508 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
512 /* Prepare the argument and response */
513 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
514 arg.value = (unsigned char *)argument->d;
515 res.nbytes = 2 * BN_num_bytes(p);
516 memset(result->d, 0, res.nbytes);
517 res.value = (unsigned char *)result->d;
518 /* Perform the operation */
519 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
523 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
524 sprintf(tmpbuf, "%ld", sw_status);
525 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
528 /* Convert the response */
529 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
533 release_context(hac);
534 if(rsa_p) ctx->tos--;
535 if(rsa_q) ctx->tos--;
536 if(rsa_dmp1) ctx->tos--;
537 if(rsa_dmq1) ctx->tos--;
538 if(rsa_iqmp) ctx->tos--;
539 if(argument) ctx->tos--;
540 if(result) ctx->tos--;
544 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
549 if((ctx = BN_CTX_new()) == NULL)
551 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
553 ENGINEerr(ENGINE_F_CSWIFT_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
556 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
557 rsa->dmq1, rsa->iqmp, ctx);
564 /* This function is aliased to mod_exp (with the mont stuff dropped). */
565 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
566 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
568 return cswift_mod_exp(r, a, p, m, ctx);
571 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
573 SW_CONTEXT_HANDLE hac;
576 SW_LARGENUMBER arg, res;
579 BIGNUM *dsa_p = NULL;
580 BIGNUM *dsa_q = NULL;
581 BIGNUM *dsa_g = NULL;
582 BIGNUM *dsa_key = NULL;
583 BIGNUM *result = NULL;
584 DSA_SIG *to_return = NULL;
587 if((ctx = BN_CTX_new()) == NULL)
589 if(!get_context(&hac))
591 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_GET_HANDLE_FAILED);
595 /* Prepare the params */
596 dsa_p = BN_CTX_get(ctx);
597 dsa_q = BN_CTX_get(ctx);
598 dsa_g = BN_CTX_get(ctx);
599 dsa_key = BN_CTX_get(ctx);
600 result = BN_CTX_get(ctx);
601 if(!dsa_p || !dsa_q || !dsa_g || !dsa_key || !result)
603 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_CTX_FULL);
606 if(!bn_wexpand(dsa_p, dsa->p->top) ||
607 !bn_wexpand(dsa_q, dsa->q->top) ||
608 !bn_wexpand(dsa_g, dsa->g->top) ||
609 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
610 !bn_wexpand(result, dsa->p->top))
612 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_EXPAND_FAIL);
615 sw_param.type = SW_ALG_DSA;
616 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
617 (unsigned char *)dsa_p->d);
618 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
619 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
620 (unsigned char *)dsa_q->d);
621 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
622 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
623 (unsigned char *)dsa_g->d);
624 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
625 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
626 (unsigned char *)dsa_key->d);
627 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
628 /* Attach the key params */
629 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
634 case SW_ERR_INPUT_SIZE:
635 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,
636 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
641 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
642 sprintf(tmpbuf, "%ld", sw_status);
643 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
647 /* Prepare the argument and response */
649 arg.value = (unsigned char *)dgst;
650 res.nbytes = BN_num_bytes(dsa->p);
651 memset(result->d, 0, res.nbytes);
652 res.value = (unsigned char *)result->d;
653 /* Perform the operation */
654 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
656 if(sw_status != SW_OK)
659 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
660 sprintf(tmpbuf, "%ld", sw_status);
661 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
664 /* Convert the response */
665 ptr = (unsigned char *)result->d;
666 if((to_return = DSA_SIG_new()) == NULL)
668 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
669 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
673 release_context(hac);
674 if(dsa_p) ctx->tos--;
675 if(dsa_q) ctx->tos--;
676 if(dsa_g) ctx->tos--;
677 if(dsa_key) ctx->tos--;
678 if(result) ctx->tos--;
684 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
685 DSA_SIG *sig, DSA *dsa)
687 SW_CONTEXT_HANDLE hac;
690 SW_LARGENUMBER arg[2], res;
691 unsigned long sig_result;
693 BIGNUM *dsa_p = NULL;
694 BIGNUM *dsa_q = NULL;
695 BIGNUM *dsa_g = NULL;
696 BIGNUM *dsa_key = NULL;
697 BIGNUM *argument = NULL;
701 if((ctx = BN_CTX_new()) == NULL)
703 if(!get_context(&hac))
705 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_GET_HANDLE_FAILED);
709 /* Prepare the params */
710 dsa_p = BN_CTX_get(ctx);
711 dsa_q = BN_CTX_get(ctx);
712 dsa_g = BN_CTX_get(ctx);
713 dsa_key = BN_CTX_get(ctx);
714 argument = BN_CTX_get(ctx);
715 if(!dsa_p || !dsa_q || !dsa_g || !dsa_key || !argument)
717 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_CTX_FULL);
720 if(!bn_wexpand(dsa_p, dsa->p->top) ||
721 !bn_wexpand(dsa_q, dsa->q->top) ||
722 !bn_wexpand(dsa_g, dsa->g->top) ||
723 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
724 !bn_wexpand(argument, 40))
726 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_EXPAND_FAIL);
729 sw_param.type = SW_ALG_DSA;
730 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
731 (unsigned char *)dsa_p->d);
732 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
733 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
734 (unsigned char *)dsa_q->d);
735 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
736 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
737 (unsigned char *)dsa_g->d);
738 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
739 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
740 (unsigned char *)dsa_key->d);
741 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
742 /* Attach the key params */
743 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
748 case SW_ERR_INPUT_SIZE:
749 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,
750 ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
755 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
756 sprintf(tmpbuf, "%ld", sw_status);
757 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
761 /* Prepare the argument and response */
762 arg[0].nbytes = dgst_len;
763 arg[0].value = (unsigned char *)dgst;
765 arg[1].value = (unsigned char *)argument->d;
766 memset(arg[1].value, 0, 40);
767 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
768 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
769 res.nbytes = 4; /* unsigned long */
770 res.value = (unsigned char *)(&sig_result);
771 /* Perform the operation */
772 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
774 if(sw_status != SW_OK)
777 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
778 sprintf(tmpbuf, "%ld", sw_status);
779 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
782 /* Convert the response */
783 to_return = ((sig_result == 0) ? 0 : 1);
787 release_context(hac);
788 if(dsa_p) ctx->tos--;
789 if(dsa_q) ctx->tos--;
790 if(dsa_g) ctx->tos--;
791 if(dsa_key) ctx->tos--;
792 if(argument) ctx->tos--;
798 /* This function is aliased to mod_exp (with the dh and mont dropped). */
799 static int cswift_mod_exp_dh(DH *dh, BIGNUM *r, BIGNUM *a, const BIGNUM *p,
800 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
802 return cswift_mod_exp(r, a, p, m, ctx);
805 #endif /* !NO_HW_CSWIFT */