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).
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 #include <openssl/bn.h>
72 #ifndef OPENSSL_NO_HW_CSWIFT
74 /* Attribution notice: Rainbow have generously allowed me to reproduce
75 * the necessary definitions here from their API. This means the support
76 * can build independently of whether application builders have the
77 * API or hardware. This will allow developers to easily produce software
78 * that has latent hardware support for any users that have accelerators
79 * installed, without the developers themselves needing anything extra.
81 * I have only clipped the parts from the CryptoSwift header files that
82 * are (or seem) relevant to the CryptoSwift support code. This is
83 * simply to keep the file sizes reasonable.
89 #include "vendor_defns/cswift.h"
92 #define CSWIFT_LIB_NAME "cswift engine"
93 #include "e_cswift_err.c"
95 #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
97 static int cswift_destroy(ENGINE *e);
98 static int cswift_init(ENGINE *e);
99 static int cswift_finish(ENGINE *e);
100 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
101 static int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in);
104 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
105 const BIGNUM *m, BN_CTX *ctx);
106 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
107 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
108 const BIGNUM *iqmp, BN_CTX *ctx);
110 #ifndef OPENSSL_NO_RSA
112 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
114 /* This function is aliased to mod_exp (with the mont stuff dropped). */
115 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
116 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
118 #ifndef OPENSSL_NO_DSA
120 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
121 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
122 DSA_SIG *sig, DSA *dsa);
125 #ifndef OPENSSL_NO_DH
127 /* This function is alised to mod_exp (with the DH and mont dropped). */
128 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
129 const BIGNUM *a, const BIGNUM *p,
130 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
134 static int cswift_rand_bytes(unsigned char *buf, int num);
135 static int cswift_rand_status(void);
137 /* The definitions for control commands specific to this engine */
138 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
139 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
142 "Specifies the path to the 'cswift' shared library",
143 ENGINE_CMD_FLAG_STRING},
147 #ifndef OPENSSL_NO_RSA
148 /* Our internal RSA_METHOD that we provide pointers to */
149 static RSA_METHOD cswift_rsa =
151 "CryptoSwift RSA method",
168 #ifndef OPENSSL_NO_DSA
169 /* Our internal DSA_METHOD that we provide pointers to */
170 static DSA_METHOD cswift_dsa =
172 "CryptoSwift DSA method",
174 NULL, /* dsa_sign_setup */
176 NULL, /* dsa_mod_exp */
177 NULL, /* bn_mod_exp */
182 NULL, /* dsa_paramgen */
183 NULL /* dsa_keygen */
187 #ifndef OPENSSL_NO_DH
188 /* Our internal DH_METHOD that we provide pointers to */
189 static DH_METHOD cswift_dh =
191 "CryptoSwift DH method",
203 static RAND_METHOD cswift_random =
205 /* "CryptoSwift RAND method", */
215 /* Constants used when creating the ENGINE */
216 static const char *engine_cswift_id = "cswift";
217 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
219 /* This internal function is used by ENGINE_cswift() and possibly by the
220 * "dynamic" ENGINE support too */
221 static int bind_helper(ENGINE *e)
223 #ifndef OPENSSL_NO_RSA
224 const RSA_METHOD *meth1;
226 #ifndef OPENSSL_NO_DH
227 const DH_METHOD *meth2;
229 if(!ENGINE_set_id(e, engine_cswift_id) ||
230 !ENGINE_set_name(e, engine_cswift_name) ||
231 #ifndef OPENSSL_NO_RSA
232 !ENGINE_set_RSA(e, &cswift_rsa) ||
234 #ifndef OPENSSL_NO_DSA
235 !ENGINE_set_DSA(e, &cswift_dsa) ||
237 #ifndef OPENSSL_NO_DH
238 !ENGINE_set_DH(e, &cswift_dh) ||
240 !ENGINE_set_RAND(e, &cswift_random) ||
241 !ENGINE_set_destroy_function(e, cswift_destroy) ||
242 !ENGINE_set_init_function(e, cswift_init) ||
243 !ENGINE_set_finish_function(e, cswift_finish) ||
244 !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
245 !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
248 #ifndef OPENSSL_NO_RSA
249 /* We know that the "PKCS1_SSLeay()" functions hook properly
250 * to the cswift-specific mod_exp and mod_exp_crt so we use
251 * those functions. NB: We don't use ENGINE_openssl() or
252 * anything "more generic" because something like the RSAref
253 * code may not hook properly, and if you own one of these
254 * cards then you have the right to do RSA operations on it
256 meth1 = RSA_PKCS1_SSLeay();
257 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
258 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
259 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
260 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
263 #ifndef OPENSSL_NO_DH
264 /* Much the same for Diffie-Hellman */
265 meth2 = DH_OpenSSL();
266 cswift_dh.generate_key = meth2->generate_key;
267 cswift_dh.compute_key = meth2->compute_key;
270 /* Ensure the cswift error handling is set up */
271 ERR_load_CSWIFT_strings();
275 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
276 static ENGINE *engine_cswift(void)
278 ENGINE *ret = ENGINE_new();
281 if(!bind_helper(ret))
289 void ENGINE_load_cswift(void)
291 /* Copied from eng_[openssl|dyn].c */
292 ENGINE *toadd = engine_cswift();
300 /* This is a process-global DSO handle used for loading and unloading
301 * the CryptoSwift library. NB: This is only set (or unset) during an
302 * init() or finish() call (reference counts permitting) and they're
303 * operating with global locks, so this should be thread-safe
305 static DSO *cswift_dso = NULL;
307 /* These are the function pointers that are (un)set when the library has
308 * successfully (un)loaded. */
309 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
310 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
311 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
312 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
314 /* Used in the DSO operations. */
315 static const char *CSWIFT_LIBNAME = NULL;
316 static const char *get_CSWIFT_LIBNAME(void)
319 return CSWIFT_LIBNAME;
322 static void free_CSWIFT_LIBNAME(void)
325 OPENSSL_free((void*)CSWIFT_LIBNAME);
326 CSWIFT_LIBNAME = NULL;
328 static long set_CSWIFT_LIBNAME(const char *name)
330 free_CSWIFT_LIBNAME();
331 return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
333 static const char *CSWIFT_F1 = "swAcquireAccContext";
334 static const char *CSWIFT_F2 = "swAttachKeyParam";
335 static const char *CSWIFT_F3 = "swSimpleRequest";
336 static const char *CSWIFT_F4 = "swReleaseAccContext";
339 /* CryptoSwift library functions and mechanics - these are used by the
340 * higher-level functions further down. NB: As and where there's no
341 * error checking, take a look lower down where these functions are
342 * called, the checking and error handling is probably down there. */
344 /* utility function to obtain a context */
345 static int get_context(SW_CONTEXT_HANDLE *hac)
349 status = p_CSwift_AcquireAccContext(hac);
355 /* similarly to release one. */
356 static void release_context(SW_CONTEXT_HANDLE hac)
358 p_CSwift_ReleaseAccContext(hac);
361 /* Destructor (complements the "ENGINE_cswift()" constructor) */
362 static int cswift_destroy(ENGINE *e)
364 free_CSWIFT_LIBNAME();
365 ERR_unload_CSWIFT_strings();
369 /* (de)initialisation functions. */
370 static int cswift_init(ENGINE *e)
372 SW_CONTEXT_HANDLE hac;
373 t_swAcquireAccContext *p1;
374 t_swAttachKeyParam *p2;
375 t_swSimpleRequest *p3;
376 t_swReleaseAccContext *p4;
378 if(cswift_dso != NULL)
380 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
383 /* Attempt to load libswift.so/swift.dll/whatever. */
384 cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
385 if(cswift_dso == NULL)
387 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
390 if(!(p1 = (t_swAcquireAccContext *)
391 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
392 !(p2 = (t_swAttachKeyParam *)
393 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
394 !(p3 = (t_swSimpleRequest *)
395 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
396 !(p4 = (t_swReleaseAccContext *)
397 DSO_bind_func(cswift_dso, CSWIFT_F4)))
399 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
402 /* Copy the pointers */
403 p_CSwift_AcquireAccContext = p1;
404 p_CSwift_AttachKeyParam = p2;
405 p_CSwift_SimpleRequest = p3;
406 p_CSwift_ReleaseAccContext = p4;
407 /* Try and get a context - if not, we may have a DSO but no
409 if(!get_context(&hac))
411 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
414 release_context(hac);
415 /* Everything's fine. */
420 DSO_free(cswift_dso);
423 p_CSwift_AcquireAccContext = NULL;
424 p_CSwift_AttachKeyParam = NULL;
425 p_CSwift_SimpleRequest = NULL;
426 p_CSwift_ReleaseAccContext = NULL;
430 static int cswift_finish(ENGINE *e)
432 free_CSWIFT_LIBNAME();
433 if(cswift_dso == NULL)
435 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
438 if(!DSO_free(cswift_dso))
440 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
444 p_CSwift_AcquireAccContext = NULL;
445 p_CSwift_AttachKeyParam = NULL;
446 p_CSwift_SimpleRequest = NULL;
447 p_CSwift_ReleaseAccContext = NULL;
451 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
453 int initialised = ((cswift_dso == NULL) ? 0 : 1);
456 case CSWIFT_CMD_SO_PATH:
459 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
464 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
467 return set_CSWIFT_LIBNAME((const char *)p);
471 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
475 /* Un petit mod_exp */
476 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
477 const BIGNUM *m, BN_CTX *ctx)
479 /* I need somewhere to store temporary serialised values for
480 * use with the CryptoSwift API calls. A neat cheat - I'll use
481 * BIGNUMs from the BN_CTX but access their arrays directly as
482 * byte arrays <grin>. This way I don't have to clean anything
489 SW_LARGENUMBER arg, res;
491 SW_CONTEXT_HANDLE hac;
492 int to_return, acquired;
494 modulus = exponent = argument = result = NULL;
495 to_return = 0; /* expect failure */
498 if(!get_context(&hac))
500 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
504 /* Prepare the params */
506 modulus = BN_CTX_get(ctx);
507 exponent = BN_CTX_get(ctx);
508 argument = BN_CTX_get(ctx);
509 result = BN_CTX_get(ctx);
512 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
515 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
516 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
518 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
521 sw_param.type = SW_ALG_EXP;
522 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
523 (unsigned char *)modulus->d);
524 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
525 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
526 (unsigned char *)exponent->d);
527 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
528 /* Attach the key params */
529 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
534 case SW_ERR_INPUT_SIZE:
535 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
539 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
540 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
541 sprintf(tmpbuf, "%ld", sw_status);
542 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
546 /* Prepare the argument and response */
547 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
548 arg.value = (unsigned char *)argument->d;
549 res.nbytes = BN_num_bytes(m);
550 memset(result->d, 0, res.nbytes);
551 res.value = (unsigned char *)result->d;
552 /* Perform the operation */
553 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
556 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
557 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
558 sprintf(tmpbuf, "%ld", sw_status);
559 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
562 /* Convert the response */
563 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
567 release_context(hac);
573 int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in)
576 int numbytes = BN_num_bytes(in);
579 while( ((out->nbytes = (numbytes+mod)) % 32) )
583 out->value = (unsigned char*)OPENSSL_malloc(out->nbytes);
588 BN_bn2bin(in, &out->value[mod]);
590 memset(out->value, 0, mod);
595 /* Un petit mod_exp chinois */
596 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
597 const BIGNUM *q, const BIGNUM *dmp1,
598 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
601 SW_LARGENUMBER arg, res;
603 SW_CONTEXT_HANDLE hac;
604 BIGNUM *result = NULL;
605 BIGNUM *argument = NULL;
606 int to_return = 0; /* expect failure */
609 sw_param.up.crt.p.value = NULL;
610 sw_param.up.crt.q.value = NULL;
611 sw_param.up.crt.dmp1.value = NULL;
612 sw_param.up.crt.dmq1.value = NULL;
613 sw_param.up.crt.iqmp.value = NULL;
615 if(!get_context(&hac))
617 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
622 /* Prepare the params */
625 if(!result || !argument)
627 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
632 sw_param.type = SW_ALG_CRT;
633 /************************************************************************/
635 /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */
636 /* limitation of cswift with values not a multiple of 32 */
637 /************************************************************************/
638 if(!cswift_bn_32copy(&sw_param.up.crt.p, p))
640 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
643 if(!cswift_bn_32copy(&sw_param.up.crt.q, q))
645 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
648 if(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1))
650 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
653 if(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1))
655 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
658 if(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp))
660 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
663 if( !bn_wexpand(argument, a->top) ||
664 !bn_wexpand(result, p->top + q->top))
666 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
670 /* Attach the key params */
671 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
676 case SW_ERR_INPUT_SIZE:
677 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
681 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
682 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
683 sprintf(tmpbuf, "%ld", sw_status);
684 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
688 /* Prepare the argument and response */
689 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
690 arg.value = (unsigned char *)argument->d;
691 res.nbytes = 2 * BN_num_bytes(p);
692 memset(result->d, 0, res.nbytes);
693 res.value = (unsigned char *)result->d;
694 /* Perform the operation */
695 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
698 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
699 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
700 sprintf(tmpbuf, "%ld", sw_status);
701 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
704 /* Convert the response */
705 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
708 if(sw_param.up.crt.p.value)
709 OPENSSL_free(sw_param.up.crt.p.value);
710 if(sw_param.up.crt.q.value)
711 OPENSSL_free(sw_param.up.crt.q.value);
712 if(sw_param.up.crt.dmp1.value)
713 OPENSSL_free(sw_param.up.crt.dmp1.value);
714 if(sw_param.up.crt.dmq1.value)
715 OPENSSL_free(sw_param.up.crt.dmq1.value);
716 if(sw_param.up.crt.iqmp.value)
717 OPENSSL_free(sw_param.up.crt.iqmp.value);
723 release_context(hac);
727 #ifndef OPENSSL_NO_RSA
728 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
731 const RSA_METHOD * def_rsa_method;
733 /* Try the limits of RSA (2048 bits) */
734 if(BN_num_bytes(rsa->p) > 128 ||
735 BN_num_bytes(rsa->q) > 128 ||
736 BN_num_bytes(rsa->dmp1) > 128 ||
737 BN_num_bytes(rsa->dmq1) > 128 ||
738 BN_num_bytes(rsa->iqmp) > 128)
741 def_rsa_method=RSA_null_method();
744 def_rsa_method=RSA_PKCS1_RSAref();
746 def_rsa_method=RSA_PKCS1_SSLeay();
750 return def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx);
753 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
755 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
758 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
759 rsa->dmq1, rsa->iqmp, ctx);
765 /* This function is aliased to mod_exp (with the mont stuff dropped). */
766 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
767 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
769 const RSA_METHOD * def_rsa_method;
771 /* Try the limits of RSA (2048 bits) */
772 if(BN_num_bytes(r) > 256 ||
773 BN_num_bytes(a) > 256 ||
774 BN_num_bytes(m) > 256)
777 def_rsa_method=RSA_null_method();
780 def_rsa_method=RSA_PKCS1_RSAref();
782 def_rsa_method=RSA_PKCS1_SSLeay();
786 return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx);
789 return cswift_mod_exp(r, a, p, m, ctx);
792 #ifndef OPENSSL_NO_DSA
793 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
795 SW_CONTEXT_HANDLE hac;
798 SW_LARGENUMBER arg, res;
801 BIGNUM *dsa_p = NULL;
802 BIGNUM *dsa_q = NULL;
803 BIGNUM *dsa_g = NULL;
804 BIGNUM *dsa_key = NULL;
805 BIGNUM *result = NULL;
806 DSA_SIG *to_return = NULL;
809 if((ctx = BN_CTX_new()) == NULL)
811 if(!get_context(&hac))
813 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
817 /* Prepare the params */
819 dsa_p = BN_CTX_get(ctx);
820 dsa_q = BN_CTX_get(ctx);
821 dsa_g = BN_CTX_get(ctx);
822 dsa_key = BN_CTX_get(ctx);
823 result = BN_CTX_get(ctx);
826 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
829 if(!bn_wexpand(dsa_p, dsa->p->top) ||
830 !bn_wexpand(dsa_q, dsa->q->top) ||
831 !bn_wexpand(dsa_g, dsa->g->top) ||
832 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
833 !bn_wexpand(result, dsa->p->top))
835 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
838 sw_param.type = SW_ALG_DSA;
839 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
840 (unsigned char *)dsa_p->d);
841 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
842 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
843 (unsigned char *)dsa_q->d);
844 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
845 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
846 (unsigned char *)dsa_g->d);
847 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
848 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
849 (unsigned char *)dsa_key->d);
850 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
851 /* Attach the key params */
852 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
857 case SW_ERR_INPUT_SIZE:
858 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
862 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
863 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
864 sprintf(tmpbuf, "%ld", sw_status);
865 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
869 /* Prepare the argument and response */
871 arg.value = (unsigned char *)dgst;
872 res.nbytes = BN_num_bytes(dsa->p);
873 memset(result->d, 0, res.nbytes);
874 res.value = (unsigned char *)result->d;
875 /* Perform the operation */
876 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
878 if(sw_status != SW_OK)
880 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
881 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
882 sprintf(tmpbuf, "%ld", sw_status);
883 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
886 /* Convert the response */
887 ptr = (unsigned char *)result->d;
888 if((to_return = DSA_SIG_new()) == NULL)
890 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
891 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
895 release_context(hac);
904 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
905 DSA_SIG *sig, DSA *dsa)
907 SW_CONTEXT_HANDLE hac;
910 SW_LARGENUMBER arg[2], res;
911 unsigned long sig_result;
913 BIGNUM *dsa_p = NULL;
914 BIGNUM *dsa_q = NULL;
915 BIGNUM *dsa_g = NULL;
916 BIGNUM *dsa_key = NULL;
917 BIGNUM *argument = NULL;
921 if((ctx = BN_CTX_new()) == NULL)
923 if(!get_context(&hac))
925 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
929 /* Prepare the params */
931 dsa_p = BN_CTX_get(ctx);
932 dsa_q = BN_CTX_get(ctx);
933 dsa_g = BN_CTX_get(ctx);
934 dsa_key = BN_CTX_get(ctx);
935 argument = BN_CTX_get(ctx);
938 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
941 if(!bn_wexpand(dsa_p, dsa->p->top) ||
942 !bn_wexpand(dsa_q, dsa->q->top) ||
943 !bn_wexpand(dsa_g, dsa->g->top) ||
944 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
945 !bn_wexpand(argument, 40))
947 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
950 sw_param.type = SW_ALG_DSA;
951 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
952 (unsigned char *)dsa_p->d);
953 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
954 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
955 (unsigned char *)dsa_q->d);
956 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
957 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
958 (unsigned char *)dsa_g->d);
959 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
960 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
961 (unsigned char *)dsa_key->d);
962 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
963 /* Attach the key params */
964 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
969 case SW_ERR_INPUT_SIZE:
970 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
974 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
975 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
976 sprintf(tmpbuf, "%ld", sw_status);
977 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
981 /* Prepare the argument and response */
982 arg[0].nbytes = dgst_len;
983 arg[0].value = (unsigned char *)dgst;
985 arg[1].value = (unsigned char *)argument->d;
986 memset(arg[1].value, 0, 40);
987 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
988 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
989 res.nbytes = 4; /* unsigned long */
990 res.value = (unsigned char *)(&sig_result);
991 /* Perform the operation */
992 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
994 if(sw_status != SW_OK)
996 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
997 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
998 sprintf(tmpbuf, "%ld", sw_status);
999 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
1002 /* Convert the response */
1003 to_return = ((sig_result == 0) ? 0 : 1);
1007 release_context(hac);
1017 #ifndef OPENSSL_NO_DH
1018 /* This function is aliased to mod_exp (with the dh and mont dropped). */
1019 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
1020 const BIGNUM *a, const BIGNUM *p,
1021 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1023 return cswift_mod_exp(r, a, p, m, ctx);
1027 /* Random bytes are good */
1028 static int cswift_rand_bytes(unsigned char *buf, int num)
1030 SW_CONTEXT_HANDLE hac;
1032 SW_LARGENUMBER largenum;
1034 int to_return = 0; /* assume failure */
1035 unsigned char buf32[1024];
1038 if (!get_context(&hac))
1040 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE);
1045 /************************************************************************/
1047 /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */
1048 /* limitation of cswift with values not a multiple of 32 */
1049 /************************************************************************/
1051 while(num >= sizeof(buf32))
1053 largenum.value = buf;
1054 largenum.nbytes = sizeof(buf32);
1055 /* tell CryptoSwift how many bytes we want and where we want it.
1056 * Note: - CryptoSwift cannot do more than 4096 bytes at a time.
1057 * - CryptoSwift can only do multiple of 32-bits. */
1058 swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
1062 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
1063 sprintf(tmpbuf, "%ld", swrc);
1064 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
1067 buf += sizeof(buf32);
1068 num -= sizeof(buf32);
1072 largenum.nbytes = sizeof(buf32);
1073 largenum.value = buf32;
1074 swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
1078 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_REQUEST_FAILED);
1079 sprintf(tmpbuf, "%ld", swrc);
1080 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
1083 memcpy(buf, largenum.value, num);
1086 to_return = 1; /* success */
1089 release_context(hac);
1094 static int cswift_rand_status(void)
1100 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1101 * shared-library. */
1102 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
1103 static int bind_fn(ENGINE *e, const char *id)
1105 if(id && (strcmp(id, engine_cswift_id) != 0))
1111 IMPLEMENT_DYNAMIC_CHECK_FN()
1112 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1113 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
1115 #endif /* !OPENSSL_NO_HW_CSWIFT */
1116 #endif /* !OPENSSL_NO_HW */