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 #ifndef OPENSSL_NO_RSA
66 #include <openssl/rsa.h>
68 #ifndef OPENSSL_NO_DSA
69 #include <openssl/dsa.h>
72 #include <openssl/dh.h>
74 #include <openssl/rand.h>
75 #include <openssl/bn.h>
78 #ifndef OPENSSL_NO_HW_CSWIFT
80 /* Attribution notice: Rainbow have generously allowed me to reproduce
81 * the necessary definitions here from their API. This means the support
82 * can build independently of whether application builders have the
83 * API or hardware. This will allow developers to easily produce software
84 * that has latent hardware support for any users that have accelerators
85 * installed, without the developers themselves needing anything extra.
87 * I have only clipped the parts from the CryptoSwift header files that
88 * are (or seem) relevant to the CryptoSwift support code. This is
89 * simply to keep the file sizes reasonable.
95 #include "vendor_defns/cswift.h"
98 #define CSWIFT_LIB_NAME "cswift engine"
99 #include "e_cswift_err.c"
101 #define DECIMAL_SIZE(type) ((sizeof(type)*8+2)/3+1)
103 static int cswift_destroy(ENGINE *e);
104 static int cswift_init(ENGINE *e);
105 static int cswift_finish(ENGINE *e);
106 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void));
107 #ifndef OPENSSL_NO_RSA
108 static int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in);
112 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
113 const BIGNUM *m, BN_CTX *ctx);
114 #ifndef OPENSSL_NO_RSA
115 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
116 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
117 const BIGNUM *iqmp, BN_CTX *ctx);
120 #ifndef OPENSSL_NO_RSA
122 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx);
123 /* This function is aliased to mod_exp (with the mont stuff dropped). */
124 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
125 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
128 #ifndef OPENSSL_NO_DSA
130 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
131 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
132 DSA_SIG *sig, DSA *dsa);
135 #ifndef OPENSSL_NO_DH
137 /* This function is alised to mod_exp (with the DH and mont dropped). */
138 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
139 const BIGNUM *a, const BIGNUM *p,
140 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
144 static int cswift_rand_bytes(unsigned char *buf, int num);
145 static int cswift_rand_status(void);
147 /* The definitions for control commands specific to this engine */
148 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
149 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
152 "Specifies the path to the 'cswift' shared library",
153 ENGINE_CMD_FLAG_STRING},
157 #ifndef OPENSSL_NO_RSA
158 /* Our internal RSA_METHOD that we provide pointers to */
159 static RSA_METHOD cswift_rsa =
161 "CryptoSwift RSA method",
178 #ifndef OPENSSL_NO_DSA
179 /* Our internal DSA_METHOD that we provide pointers to */
180 static DSA_METHOD cswift_dsa =
182 "CryptoSwift DSA method",
184 NULL, /* dsa_sign_setup */
186 NULL, /* dsa_mod_exp */
187 NULL, /* bn_mod_exp */
192 NULL, /* dsa_paramgen */
193 NULL /* dsa_keygen */
197 #ifndef OPENSSL_NO_DH
198 /* Our internal DH_METHOD that we provide pointers to */
199 static DH_METHOD cswift_dh =
201 "CryptoSwift DH method",
213 static RAND_METHOD cswift_random =
215 /* "CryptoSwift RAND method", */
225 /* Constants used when creating the ENGINE */
226 static const char *engine_cswift_id = "cswift";
227 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
229 /* This internal function is used by ENGINE_cswift() and possibly by the
230 * "dynamic" ENGINE support too */
231 static int bind_helper(ENGINE *e)
233 #ifndef OPENSSL_NO_RSA
234 const RSA_METHOD *meth1;
236 #ifndef OPENSSL_NO_DH
237 const DH_METHOD *meth2;
239 if(!ENGINE_set_id(e, engine_cswift_id) ||
240 !ENGINE_set_name(e, engine_cswift_name) ||
241 #ifndef OPENSSL_NO_RSA
242 !ENGINE_set_RSA(e, &cswift_rsa) ||
244 #ifndef OPENSSL_NO_DSA
245 !ENGINE_set_DSA(e, &cswift_dsa) ||
247 #ifndef OPENSSL_NO_DH
248 !ENGINE_set_DH(e, &cswift_dh) ||
250 !ENGINE_set_RAND(e, &cswift_random) ||
251 !ENGINE_set_destroy_function(e, cswift_destroy) ||
252 !ENGINE_set_init_function(e, cswift_init) ||
253 !ENGINE_set_finish_function(e, cswift_finish) ||
254 !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
255 !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
258 #ifndef OPENSSL_NO_RSA
259 /* We know that the "PKCS1_SSLeay()" functions hook properly
260 * to the cswift-specific mod_exp and mod_exp_crt so we use
261 * those functions. NB: We don't use ENGINE_openssl() or
262 * anything "more generic" because something like the RSAref
263 * code may not hook properly, and if you own one of these
264 * cards then you have the right to do RSA operations on it
266 meth1 = RSA_PKCS1_SSLeay();
267 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
268 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
269 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
270 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
273 #ifndef OPENSSL_NO_DH
274 /* Much the same for Diffie-Hellman */
275 meth2 = DH_OpenSSL();
276 cswift_dh.generate_key = meth2->generate_key;
277 cswift_dh.compute_key = meth2->compute_key;
280 /* Ensure the cswift error handling is set up */
281 ERR_load_CSWIFT_strings();
285 #ifdef OPENSSL_NO_DYNAMIC_ENGINE
286 static ENGINE *engine_cswift(void)
288 ENGINE *ret = ENGINE_new();
291 if(!bind_helper(ret))
299 void ENGINE_load_cswift(void)
301 /* Copied from eng_[openssl|dyn].c */
302 ENGINE *toadd = engine_cswift();
310 /* This is a process-global DSO handle used for loading and unloading
311 * the CryptoSwift library. NB: This is only set (or unset) during an
312 * init() or finish() call (reference counts permitting) and they're
313 * operating with global locks, so this should be thread-safe
315 static DSO *cswift_dso = NULL;
317 /* These are the function pointers that are (un)set when the library has
318 * successfully (un)loaded. */
319 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
320 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
321 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
322 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
324 /* Used in the DSO operations. */
325 static const char *CSWIFT_LIBNAME = NULL;
326 static const char *get_CSWIFT_LIBNAME(void)
329 return CSWIFT_LIBNAME;
332 static void free_CSWIFT_LIBNAME(void)
335 OPENSSL_free((void*)CSWIFT_LIBNAME);
336 CSWIFT_LIBNAME = NULL;
338 static long set_CSWIFT_LIBNAME(const char *name)
340 free_CSWIFT_LIBNAME();
341 return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
343 static const char *CSWIFT_F1 = "swAcquireAccContext";
344 static const char *CSWIFT_F2 = "swAttachKeyParam";
345 static const char *CSWIFT_F3 = "swSimpleRequest";
346 static const char *CSWIFT_F4 = "swReleaseAccContext";
349 /* CryptoSwift library functions and mechanics - these are used by the
350 * higher-level functions further down. NB: As and where there's no
351 * error checking, take a look lower down where these functions are
352 * called, the checking and error handling is probably down there. */
354 /* utility function to obtain a context */
355 static int get_context(SW_CONTEXT_HANDLE *hac)
359 status = p_CSwift_AcquireAccContext(hac);
365 /* similarly to release one. */
366 static void release_context(SW_CONTEXT_HANDLE hac)
368 p_CSwift_ReleaseAccContext(hac);
371 /* Destructor (complements the "ENGINE_cswift()" constructor) */
372 static int cswift_destroy(ENGINE *e)
374 free_CSWIFT_LIBNAME();
375 ERR_unload_CSWIFT_strings();
379 /* (de)initialisation functions. */
380 static int cswift_init(ENGINE *e)
382 SW_CONTEXT_HANDLE hac;
383 t_swAcquireAccContext *p1;
384 t_swAttachKeyParam *p2;
385 t_swSimpleRequest *p3;
386 t_swReleaseAccContext *p4;
388 if(cswift_dso != NULL)
390 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
393 /* Attempt to load libswift.so/swift.dll/whatever. */
394 cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
395 if(cswift_dso == NULL)
397 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
400 if(!(p1 = (t_swAcquireAccContext *)
401 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
402 !(p2 = (t_swAttachKeyParam *)
403 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
404 !(p3 = (t_swSimpleRequest *)
405 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
406 !(p4 = (t_swReleaseAccContext *)
407 DSO_bind_func(cswift_dso, CSWIFT_F4)))
409 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
412 /* Copy the pointers */
413 p_CSwift_AcquireAccContext = p1;
414 p_CSwift_AttachKeyParam = p2;
415 p_CSwift_SimpleRequest = p3;
416 p_CSwift_ReleaseAccContext = p4;
417 /* Try and get a context - if not, we may have a DSO but no
419 if(!get_context(&hac))
421 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
424 release_context(hac);
425 /* Everything's fine. */
430 DSO_free(cswift_dso);
433 p_CSwift_AcquireAccContext = NULL;
434 p_CSwift_AttachKeyParam = NULL;
435 p_CSwift_SimpleRequest = NULL;
436 p_CSwift_ReleaseAccContext = NULL;
440 static int cswift_finish(ENGINE *e)
442 free_CSWIFT_LIBNAME();
443 if(cswift_dso == NULL)
445 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
448 if(!DSO_free(cswift_dso))
450 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
454 p_CSwift_AcquireAccContext = NULL;
455 p_CSwift_AttachKeyParam = NULL;
456 p_CSwift_SimpleRequest = NULL;
457 p_CSwift_ReleaseAccContext = NULL;
461 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)(void))
463 int initialised = ((cswift_dso == NULL) ? 0 : 1);
466 case CSWIFT_CMD_SO_PATH:
469 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
474 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
477 return set_CSWIFT_LIBNAME((const char *)p);
481 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
485 /* Un petit mod_exp */
486 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
487 const BIGNUM *m, BN_CTX *ctx)
489 /* I need somewhere to store temporary serialised values for
490 * use with the CryptoSwift API calls. A neat cheat - I'll use
491 * BIGNUMs from the BN_CTX but access their arrays directly as
492 * byte arrays <grin>. This way I don't have to clean anything
499 SW_LARGENUMBER arg, res;
501 SW_CONTEXT_HANDLE hac;
502 int to_return, acquired;
504 modulus = exponent = argument = result = NULL;
505 to_return = 0; /* expect failure */
508 if(!get_context(&hac))
510 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
514 /* Prepare the params */
516 modulus = BN_CTX_get(ctx);
517 exponent = BN_CTX_get(ctx);
518 argument = BN_CTX_get(ctx);
519 result = BN_CTX_get(ctx);
522 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
525 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
526 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
528 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
531 sw_param.type = SW_ALG_EXP;
532 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
533 (unsigned char *)modulus->d);
534 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
535 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
536 (unsigned char *)exponent->d);
537 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
538 /* Attach the key params */
539 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
544 case SW_ERR_INPUT_SIZE:
545 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
549 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
550 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
551 sprintf(tmpbuf, "%ld", sw_status);
552 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
556 /* Prepare the argument and response */
557 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
558 arg.value = (unsigned char *)argument->d;
559 res.nbytes = BN_num_bytes(m);
560 memset(result->d, 0, res.nbytes);
561 res.value = (unsigned char *)result->d;
562 /* Perform the operation */
563 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
566 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
567 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
568 sprintf(tmpbuf, "%ld", sw_status);
569 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
572 /* Convert the response */
573 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
577 release_context(hac);
583 #ifndef OPENSSL_NO_RSA
584 int cswift_bn_32copy(SW_LARGENUMBER * out, const BIGNUM * in)
587 int numbytes = BN_num_bytes(in);
590 while( ((out->nbytes = (numbytes+mod)) % 32) )
594 out->value = (unsigned char*)OPENSSL_malloc(out->nbytes);
599 BN_bn2bin(in, &out->value[mod]);
601 memset(out->value, 0, mod);
607 #ifndef OPENSSL_NO_RSA
608 /* Un petit mod_exp chinois */
609 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
610 const BIGNUM *q, const BIGNUM *dmp1,
611 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
614 SW_LARGENUMBER arg, res;
616 SW_CONTEXT_HANDLE hac;
617 BIGNUM *result = NULL;
618 BIGNUM *argument = NULL;
619 int to_return = 0; /* expect failure */
622 sw_param.up.crt.p.value = NULL;
623 sw_param.up.crt.q.value = NULL;
624 sw_param.up.crt.dmp1.value = NULL;
625 sw_param.up.crt.dmq1.value = NULL;
626 sw_param.up.crt.iqmp.value = NULL;
628 if(!get_context(&hac))
630 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
635 /* Prepare the params */
638 if(!result || !argument)
640 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
645 sw_param.type = SW_ALG_CRT;
646 /************************************************************************/
648 /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */
649 /* limitation of cswift with values not a multiple of 32 */
650 /************************************************************************/
651 if(!cswift_bn_32copy(&sw_param.up.crt.p, p))
653 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
656 if(!cswift_bn_32copy(&sw_param.up.crt.q, q))
658 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
661 if(!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1))
663 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
666 if(!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1))
668 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
671 if(!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp))
673 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
676 if( !bn_wexpand(argument, a->top) ||
677 !bn_wexpand(result, p->top + q->top))
679 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
683 /* Attach the key params */
684 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
689 case SW_ERR_INPUT_SIZE:
690 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
694 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
695 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
696 sprintf(tmpbuf, "%ld", sw_status);
697 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
701 /* Prepare the argument and response */
702 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
703 arg.value = (unsigned char *)argument->d;
704 res.nbytes = 2 * BN_num_bytes(p);
705 memset(result->d, 0, res.nbytes);
706 res.value = (unsigned char *)result->d;
707 /* Perform the operation */
708 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
711 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
712 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
713 sprintf(tmpbuf, "%ld", sw_status);
714 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
717 /* Convert the response */
718 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
721 if(sw_param.up.crt.p.value)
722 OPENSSL_free(sw_param.up.crt.p.value);
723 if(sw_param.up.crt.q.value)
724 OPENSSL_free(sw_param.up.crt.q.value);
725 if(sw_param.up.crt.dmp1.value)
726 OPENSSL_free(sw_param.up.crt.dmp1.value);
727 if(sw_param.up.crt.dmq1.value)
728 OPENSSL_free(sw_param.up.crt.dmq1.value);
729 if(sw_param.up.crt.iqmp.value)
730 OPENSSL_free(sw_param.up.crt.iqmp.value);
736 release_context(hac);
741 #ifndef OPENSSL_NO_RSA
742 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa, BN_CTX *ctx)
745 const RSA_METHOD * def_rsa_method;
747 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
749 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
753 /* Try the limits of RSA (2048 bits) */
754 if(BN_num_bytes(rsa->p) > 128 ||
755 BN_num_bytes(rsa->q) > 128 ||
756 BN_num_bytes(rsa->dmp1) > 128 ||
757 BN_num_bytes(rsa->dmq1) > 128 ||
758 BN_num_bytes(rsa->iqmp) > 128)
761 def_rsa_method=RSA_null_method();
764 def_rsa_method=RSA_PKCS1_RSAref();
766 def_rsa_method=RSA_PKCS1_SSLeay();
770 return def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx);
773 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
774 rsa->dmq1, rsa->iqmp, ctx);
779 /* This function is aliased to mod_exp (with the mont stuff dropped). */
780 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
781 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
783 const RSA_METHOD * def_rsa_method;
785 /* Try the limits of RSA (2048 bits) */
786 if(BN_num_bytes(r) > 256 ||
787 BN_num_bytes(a) > 256 ||
788 BN_num_bytes(m) > 256)
791 def_rsa_method=RSA_null_method();
794 def_rsa_method=RSA_PKCS1_RSAref();
796 def_rsa_method=RSA_PKCS1_SSLeay();
800 return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx);
803 return cswift_mod_exp(r, a, p, m, ctx);
805 #endif /* OPENSSL_NO_RSA */
807 #ifndef OPENSSL_NO_DSA
808 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
810 SW_CONTEXT_HANDLE hac;
813 SW_LARGENUMBER arg, res;
816 BIGNUM *dsa_p = NULL;
817 BIGNUM *dsa_q = NULL;
818 BIGNUM *dsa_g = NULL;
819 BIGNUM *dsa_key = NULL;
820 BIGNUM *result = NULL;
821 DSA_SIG *to_return = NULL;
824 if((ctx = BN_CTX_new()) == NULL)
826 if(!get_context(&hac))
828 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
832 /* Prepare the params */
834 dsa_p = BN_CTX_get(ctx);
835 dsa_q = BN_CTX_get(ctx);
836 dsa_g = BN_CTX_get(ctx);
837 dsa_key = BN_CTX_get(ctx);
838 result = BN_CTX_get(ctx);
841 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
844 if(!bn_wexpand(dsa_p, dsa->p->top) ||
845 !bn_wexpand(dsa_q, dsa->q->top) ||
846 !bn_wexpand(dsa_g, dsa->g->top) ||
847 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
848 !bn_wexpand(result, dsa->p->top))
850 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
853 sw_param.type = SW_ALG_DSA;
854 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
855 (unsigned char *)dsa_p->d);
856 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
857 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
858 (unsigned char *)dsa_q->d);
859 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
860 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
861 (unsigned char *)dsa_g->d);
862 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
863 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
864 (unsigned char *)dsa_key->d);
865 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
866 /* Attach the key params */
867 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
872 case SW_ERR_INPUT_SIZE:
873 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
877 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
878 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
879 sprintf(tmpbuf, "%ld", sw_status);
880 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
884 /* Prepare the argument and response */
886 arg.value = (unsigned char *)dgst;
887 res.nbytes = BN_num_bytes(dsa->p);
888 memset(result->d, 0, res.nbytes);
889 res.value = (unsigned char *)result->d;
890 /* Perform the operation */
891 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
893 if(sw_status != SW_OK)
895 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
896 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
897 sprintf(tmpbuf, "%ld", sw_status);
898 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
901 /* Convert the response */
902 ptr = (unsigned char *)result->d;
903 if((to_return = DSA_SIG_new()) == NULL)
905 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
906 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
910 release_context(hac);
919 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
920 DSA_SIG *sig, DSA *dsa)
922 SW_CONTEXT_HANDLE hac;
925 SW_LARGENUMBER arg[2], res;
926 unsigned long sig_result;
928 BIGNUM *dsa_p = NULL;
929 BIGNUM *dsa_q = NULL;
930 BIGNUM *dsa_g = NULL;
931 BIGNUM *dsa_key = NULL;
932 BIGNUM *argument = NULL;
936 if((ctx = BN_CTX_new()) == NULL)
938 if(!get_context(&hac))
940 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
944 /* Prepare the params */
946 dsa_p = BN_CTX_get(ctx);
947 dsa_q = BN_CTX_get(ctx);
948 dsa_g = BN_CTX_get(ctx);
949 dsa_key = BN_CTX_get(ctx);
950 argument = BN_CTX_get(ctx);
953 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
956 if(!bn_wexpand(dsa_p, dsa->p->top) ||
957 !bn_wexpand(dsa_q, dsa->q->top) ||
958 !bn_wexpand(dsa_g, dsa->g->top) ||
959 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
960 !bn_wexpand(argument, 40))
962 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
965 sw_param.type = SW_ALG_DSA;
966 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
967 (unsigned char *)dsa_p->d);
968 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
969 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
970 (unsigned char *)dsa_q->d);
971 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
972 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
973 (unsigned char *)dsa_g->d);
974 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
975 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
976 (unsigned char *)dsa_key->d);
977 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
978 /* Attach the key params */
979 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
984 case SW_ERR_INPUT_SIZE:
985 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
989 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
990 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
991 sprintf(tmpbuf, "%ld", sw_status);
992 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
996 /* Prepare the argument and response */
997 arg[0].nbytes = dgst_len;
998 arg[0].value = (unsigned char *)dgst;
1000 arg[1].value = (unsigned char *)argument->d;
1001 memset(arg[1].value, 0, 40);
1002 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
1003 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
1004 res.nbytes = 4; /* unsigned long */
1005 res.value = (unsigned char *)(&sig_result);
1006 /* Perform the operation */
1007 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
1009 if(sw_status != SW_OK)
1011 char tmpbuf[DECIMAL_SIZE(sw_status)+1];
1012 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
1013 sprintf(tmpbuf, "%ld", sw_status);
1014 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
1017 /* Convert the response */
1018 to_return = ((sig_result == 0) ? 0 : 1);
1022 release_context(hac);
1032 #ifndef OPENSSL_NO_DH
1033 /* This function is aliased to mod_exp (with the dh and mont dropped). */
1034 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
1035 const BIGNUM *a, const BIGNUM *p,
1036 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
1038 return cswift_mod_exp(r, a, p, m, ctx);
1042 /* Random bytes are good */
1043 static int cswift_rand_bytes(unsigned char *buf, int num)
1045 SW_CONTEXT_HANDLE hac;
1047 SW_LARGENUMBER largenum;
1049 int to_return = 0; /* assume failure */
1050 unsigned char buf32[1024];
1053 if (!get_context(&hac))
1055 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE);
1060 /************************************************************************/
1062 /* Modified by Frederic Giudicelli (deny-all.com) to overcome the */
1063 /* limitation of cswift with values not a multiple of 32 */
1064 /************************************************************************/
1066 while(num >= (int)sizeof(buf32))
1068 largenum.value = buf;
1069 largenum.nbytes = sizeof(buf32);
1070 /* tell CryptoSwift how many bytes we want and where we want it.
1071 * Note: - CryptoSwift cannot do more than 4096 bytes at a time.
1072 * - CryptoSwift can only do multiple of 32-bits. */
1073 swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
1077 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
1078 sprintf(tmpbuf, "%ld", swrc);
1079 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
1082 buf += sizeof(buf32);
1083 num -= sizeof(buf32);
1087 largenum.nbytes = sizeof(buf32);
1088 largenum.value = buf32;
1089 swrc = p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
1093 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
1094 sprintf(tmpbuf, "%ld", swrc);
1095 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
1098 memcpy(buf, largenum.value, num);
1101 to_return = 1; /* success */
1104 release_context(hac);
1109 static int cswift_rand_status(void)
1115 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1116 * shared-library. */
1117 #ifndef OPENSSL_NO_DYNAMIC_ENGINE
1118 static int bind_fn(ENGINE *e, const char *id)
1120 if(id && (strcmp(id, engine_cswift_id) != 0))
1126 IMPLEMENT_DYNAMIC_CHECK_FN()
1127 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1128 #endif /* OPENSSL_NO_DYNAMIC_ENGINE */
1130 #endif /* !OPENSSL_NO_HW_CSWIFT */
1131 #endif /* !OPENSSL_NO_HW */