1 /* crypto/engine/hw_cswift.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 /* ====================================================================
6 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
12 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * licensing@OpenSSL.org.
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
34 * 6. Redistributions of any form whatsoever must retain the following
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
60 #include <openssl/crypto.h>
62 #include <openssl/dso.h>
63 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_HW_CSWIFT
68 /* Attribution notice: Rainbow have generously allowed me to reproduce
69 * the necessary definitions here from their API. This means the support
70 * can build independently of whether application builders have the
71 * API or hardware. This will allow developers to easily produce software
72 * that has latent hardware support for any users that have accelerators
73 * installed, without the developers themselves needing anything extra.
75 * I have only clipped the parts from the CryptoSwift header files that
76 * are (or seem) relevant to the CryptoSwift support code. This is
77 * simply to keep the file sizes reasonable.
83 #include "vendor_defns/cswift.h"
86 static int cswift_destroy(ENGINE *e);
87 static int cswift_init(ENGINE *e);
88 static int cswift_finish(ENGINE *e);
89 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
92 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
93 const BIGNUM *m, BN_CTX *ctx);
94 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
95 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
96 const BIGNUM *iqmp, BN_CTX *ctx);
98 #ifndef OPENSSL_NO_RSA
100 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
102 /* This function is aliased to mod_exp (with the mont stuff dropped). */
103 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
104 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
106 #ifndef OPENSSL_NO_DSA
108 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
109 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
110 DSA_SIG *sig, DSA *dsa);
113 #ifndef OPENSSL_NO_DH
115 /* This function is alised to mod_exp (with the DH and mont dropped). */
116 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
117 const BIGNUM *a, const BIGNUM *p,
118 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
121 /* The definitions for control commands specific to this engine */
122 #define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
123 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
126 "Specifies the path to the 'cswift' shared library",
127 ENGINE_CMD_FLAG_STRING},
131 #ifndef OPENSSL_NO_RSA
132 /* Our internal RSA_METHOD that we provide pointers to */
133 static RSA_METHOD cswift_rsa =
135 "CryptoSwift RSA method",
151 #ifndef OPENSSL_NO_DSA
152 /* Our internal DSA_METHOD that we provide pointers to */
153 static DSA_METHOD cswift_dsa =
155 "CryptoSwift DSA method",
157 NULL, /* dsa_sign_setup */
159 NULL, /* dsa_mod_exp */
160 NULL, /* bn_mod_exp */
168 #ifndef OPENSSL_NO_DH
169 /* Our internal DH_METHOD that we provide pointers to */
170 static DH_METHOD cswift_dh =
172 "CryptoSwift DH method",
183 #ifndef OPENSSL_NO_ERR
184 /* Error function codes for use in cswift operation */
185 #define CSWIFT_F_CSWIFT_INIT 100
186 #define CSWIFT_F_CSWIFT_FINISH 101
187 #define CSWIFT_F_CSWIFT_CTRL 102
188 #define CSWIFT_F_CSWIFT_MOD_EXP 103
189 #define CSWIFT_F_CSWIFT_MOD_EXP_CRT 104
190 #define CSWIFT_F_CSWIFT_RSA_MOD_EXP 105
191 #define CSWIFT_F_CSWIFT_DSA_SIGN 106
192 #define CSWIFT_F_CSWIFT_DSA_VERIFY 107
193 /* Error reason codes */
194 #define CSWIFT_R_ALREADY_LOADED 108
195 #define CSWIFT_R_NOT_LOADED 109
196 #define CSWIFT_R_UNIT_FAILURE 110
197 #define CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED 113
198 #define CSWIFT_R_BN_CTX_FULL 115
199 #define CSWIFT_R_BN_EXPAND_FAIL 116
200 #define CSWIFT_R_BAD_KEY_SIZE 117
201 #define CSWIFT_R_REQUEST_FAILED 118
202 #define CSWIFT_R_MISSING_KEY_COMPONENTS 120
203 static ERR_STRING_DATA cswift_str_functs[] =
205 /* This first element is changed to match the dynamic 'lib' number */
206 {ERR_PACK(0,0,0), "cswift engine code"},
207 {ERR_PACK(0,CSWIFT_F_CSWIFT_INIT,0), "cswift_init"},
208 {ERR_PACK(0,CSWIFT_F_CSWIFT_FINISH,0), "cswift_finish"},
209 {ERR_PACK(0,CSWIFT_F_CSWIFT_CTRL,0), "cswift_ctrl"},
210 {ERR_PACK(0,CSWIFT_F_CSWIFT_MOD_EXP,0), "cswift_mod_exp"},
211 {ERR_PACK(0,CSWIFT_F_CSWIFT_MOD_EXP_CRT,0), "cswift_mod_exp_crt"},
212 {ERR_PACK(0,CSWIFT_F_CSWIFT_RSA_MOD_EXP,0), "cswift_rsa_mod_exp"},
213 {ERR_PACK(0,CSWIFT_F_CSWIFT_DSA_SIGN,0), "cswift_dsa_sign"},
214 {ERR_PACK(0,CSWIFT_F_CSWIFT_DSA_VERIFY,0), "cswift_dsa_verify"},
215 /* Error reason codes */
216 {CSWIFT_R_ALREADY_LOADED ,"already loaded"},
217 {CSWIFT_R_NOT_LOADED ,"not loaded"},
218 {CSWIFT_R_UNIT_FAILURE ,"unit failure"},
219 {CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED ,"ctrl command not implemented"},
220 {CSWIFT_R_BN_CTX_FULL ,"BN_CTX full"},
221 {CSWIFT_R_BN_EXPAND_FAIL ,"bn_expand fail"},
222 {CSWIFT_R_BAD_KEY_SIZE ,"bad key size"},
223 {CSWIFT_R_REQUEST_FAILED ,"request failed"},
224 {CSWIFT_R_MISSING_KEY_COMPONENTS ,"missing key components"},
227 /* The library number we obtain dynamically from the ERR code */
228 static int cswift_err_lib = -1;
229 #define CSWIFTerr(f,r) ERR_PUT_error(cswift_err_lib,(f),(r),__FILE__,__LINE__)
230 static void cswift_load_error_strings(void)
232 if(cswift_err_lib < 0)
234 if((cswift_err_lib = ERR_get_next_error_library()) <= 0)
236 cswift_str_functs[0].error = ERR_PACK(cswift_err_lib,0,0);
237 ERR_load_strings(cswift_err_lib, cswift_str_functs);
240 static void cswift_unload_error_strings(void)
242 if(cswift_err_lib >= 0)
244 ERR_unload_strings(cswift_err_lib, cswift_str_functs);
249 #define CSWIFTerr(f,r) /* NOP */
250 static void cswift_load_error_strings(void) { } /* NOP */
251 static void cswift_unload_error_strings(void) { } /* NOP */
254 /* Constants used when creating the ENGINE */
255 static const char *engine_cswift_id = "cswift";
256 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
258 /* This internal function is used by ENGINE_cswift() and possibly by the
259 * "dynamic" ENGINE support too */
260 static int bind_helper(ENGINE *e)
262 #ifndef OPENSSL_NO_RSA
263 const RSA_METHOD *meth1;
265 #ifndef OPENSSL_NO_DH
266 const DH_METHOD *meth2;
268 if(!ENGINE_set_id(e, engine_cswift_id) ||
269 !ENGINE_set_name(e, engine_cswift_name) ||
270 #ifndef OPENSSL_NO_RSA
271 !ENGINE_set_RSA(e, &cswift_rsa) ||
273 #ifndef OPENSSL_NO_DSA
274 !ENGINE_set_DSA(e, &cswift_dsa) ||
276 #ifndef OPENSSL_NO_DH
277 !ENGINE_set_DH(e, &cswift_dh) ||
279 !ENGINE_set_destroy_function(e, cswift_destroy) ||
280 !ENGINE_set_init_function(e, cswift_init) ||
281 !ENGINE_set_finish_function(e, cswift_finish) ||
282 !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
283 !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
286 #ifndef OPENSSL_NO_RSA
287 /* We know that the "PKCS1_SSLeay()" functions hook properly
288 * to the cswift-specific mod_exp and mod_exp_crt so we use
289 * those functions. NB: We don't use ENGINE_openssl() or
290 * anything "more generic" because something like the RSAref
291 * code may not hook properly, and if you own one of these
292 * cards then you have the right to do RSA operations on it
294 meth1 = RSA_PKCS1_SSLeay();
295 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
296 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
297 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
298 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
301 #ifndef OPENSSL_NO_DH
302 /* Much the same for Diffie-Hellman */
303 meth2 = DH_OpenSSL();
304 cswift_dh.generate_key = meth2->generate_key;
305 cswift_dh.compute_key = meth2->compute_key;
308 /* Ensure the cswift error handling is set up */
309 cswift_load_error_strings();
313 static ENGINE *engine_cswift(void)
315 ENGINE *ret = ENGINE_new();
318 if(!bind_helper(ret))
326 void ENGINE_load_cswift(void)
328 /* Copied from eng_[openssl|dyn].c */
329 ENGINE *toadd = engine_cswift();
336 /* This is a process-global DSO handle used for loading and unloading
337 * the CryptoSwift library. NB: This is only set (or unset) during an
338 * init() or finish() call (reference counts permitting) and they're
339 * operating with global locks, so this should be thread-safe
341 static DSO *cswift_dso = NULL;
343 /* These are the function pointers that are (un)set when the library has
344 * successfully (un)loaded. */
345 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
346 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
347 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
348 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
350 /* Used in the DSO operations. */
351 static const char def_CSWIFT_LIBNAME[] = "swift";
352 static const char *CSWIFT_LIBNAME = def_CSWIFT_LIBNAME;
353 static const char *CSWIFT_F1 = "swAcquireAccContext";
354 static const char *CSWIFT_F2 = "swAttachKeyParam";
355 static const char *CSWIFT_F3 = "swSimpleRequest";
356 static const char *CSWIFT_F4 = "swReleaseAccContext";
359 /* CryptoSwift library functions and mechanics - these are used by the
360 * higher-level functions further down. NB: As and where there's no
361 * error checking, take a look lower down where these functions are
362 * called, the checking and error handling is probably down there. */
364 /* utility function to obtain a context */
365 static int get_context(SW_CONTEXT_HANDLE *hac)
369 status = p_CSwift_AcquireAccContext(hac);
375 /* similarly to release one. */
376 static void release_context(SW_CONTEXT_HANDLE hac)
378 p_CSwift_ReleaseAccContext(hac);
381 /* Destructor (complements the "ENGINE_cswift()" constructor) */
382 static int cswift_destroy(ENGINE *e)
384 cswift_unload_error_strings();
388 /* (de)initialisation functions. */
389 static int cswift_init(ENGINE *e)
391 SW_CONTEXT_HANDLE hac;
392 t_swAcquireAccContext *p1;
393 t_swAttachKeyParam *p2;
394 t_swSimpleRequest *p3;
395 t_swReleaseAccContext *p4;
397 if(cswift_dso != NULL)
399 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
402 /* Attempt to load libswift.so/swift.dll/whatever. */
403 cswift_dso = DSO_load(NULL, CSWIFT_LIBNAME, NULL, 0);
404 if(cswift_dso == NULL)
406 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
409 if(!(p1 = (t_swAcquireAccContext *)
410 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
411 !(p2 = (t_swAttachKeyParam *)
412 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
413 !(p3 = (t_swSimpleRequest *)
414 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
415 !(p4 = (t_swReleaseAccContext *)
416 DSO_bind_func(cswift_dso, CSWIFT_F4)))
418 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
421 /* Copy the pointers */
422 p_CSwift_AcquireAccContext = p1;
423 p_CSwift_AttachKeyParam = p2;
424 p_CSwift_SimpleRequest = p3;
425 p_CSwift_ReleaseAccContext = p4;
426 /* Try and get a context - if not, we may have a DSO but no
428 if(!get_context(&hac))
430 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
433 release_context(hac);
434 /* Everything's fine. */
438 DSO_free(cswift_dso);
439 p_CSwift_AcquireAccContext = NULL;
440 p_CSwift_AttachKeyParam = NULL;
441 p_CSwift_SimpleRequest = NULL;
442 p_CSwift_ReleaseAccContext = NULL;
446 static int cswift_finish(ENGINE *e)
448 if(cswift_dso == NULL)
450 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
453 if(!DSO_free(cswift_dso))
455 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
459 p_CSwift_AcquireAccContext = NULL;
460 p_CSwift_AttachKeyParam = NULL;
461 p_CSwift_SimpleRequest = NULL;
462 p_CSwift_ReleaseAccContext = NULL;
466 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
468 int initialised = ((cswift_dso == NULL) ? 0 : 1);
471 case CSWIFT_CMD_SO_PATH:
474 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
479 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
482 CSWIFT_LIBNAME = (const char *)p;
487 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
491 /* Un petit mod_exp */
492 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
493 const BIGNUM *m, BN_CTX *ctx)
495 /* I need somewhere to store temporary serialised values for
496 * use with the CryptoSwift API calls. A neat cheat - I'll use
497 * BIGNUMs from the BN_CTX but access their arrays directly as
498 * byte arrays <grin>. This way I don't have to clean anything
505 SW_LARGENUMBER arg, res;
507 SW_CONTEXT_HANDLE hac;
508 int to_return, acquired;
510 modulus = exponent = argument = result = NULL;
511 to_return = 0; /* expect failure */
514 if(!get_context(&hac))
516 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
520 /* Prepare the params */
522 modulus = BN_CTX_get(ctx);
523 exponent = BN_CTX_get(ctx);
524 argument = BN_CTX_get(ctx);
525 result = BN_CTX_get(ctx);
528 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
531 if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
532 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
534 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
537 sw_param.type = SW_ALG_EXP;
538 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
539 (unsigned char *)modulus->d);
540 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
541 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
542 (unsigned char *)exponent->d);
543 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
544 /* Attach the key params */
545 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
550 case SW_ERR_INPUT_SIZE:
551 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
556 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
557 sprintf(tmpbuf, "%ld", sw_status);
558 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
562 /* Prepare the argument and response */
563 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
564 arg.value = (unsigned char *)argument->d;
565 res.nbytes = BN_num_bytes(m);
566 memset(result->d, 0, res.nbytes);
567 res.value = (unsigned char *)result->d;
568 /* Perform the operation */
569 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
573 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
574 sprintf(tmpbuf, "%ld", sw_status);
575 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
578 /* Convert the response */
579 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
583 release_context(hac);
588 /* Un petit mod_exp chinois */
589 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
590 const BIGNUM *q, const BIGNUM *dmp1,
591 const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
594 SW_LARGENUMBER arg, res;
596 SW_CONTEXT_HANDLE hac;
597 BIGNUM *rsa_p = NULL;
598 BIGNUM *rsa_q = NULL;
599 BIGNUM *rsa_dmp1 = NULL;
600 BIGNUM *rsa_dmq1 = NULL;
601 BIGNUM *rsa_iqmp = NULL;
602 BIGNUM *argument = NULL;
603 BIGNUM *result = NULL;
604 int to_return = 0; /* expect failure */
607 if(!get_context(&hac))
609 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
613 /* Prepare the params */
615 rsa_p = BN_CTX_get(ctx);
616 rsa_q = BN_CTX_get(ctx);
617 rsa_dmp1 = BN_CTX_get(ctx);
618 rsa_dmq1 = BN_CTX_get(ctx);
619 rsa_iqmp = BN_CTX_get(ctx);
620 argument = BN_CTX_get(ctx);
621 result = BN_CTX_get(ctx);
624 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
627 if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
628 !bn_wexpand(rsa_dmp1, dmp1->top) ||
629 !bn_wexpand(rsa_dmq1, dmq1->top) ||
630 !bn_wexpand(rsa_iqmp, iqmp->top) ||
631 !bn_wexpand(argument, a->top) ||
632 !bn_wexpand(result, p->top + q->top))
634 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
637 sw_param.type = SW_ALG_CRT;
638 sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
639 sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
640 sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
641 sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
642 sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
643 (unsigned char *)rsa_dmp1->d);
644 sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
645 sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
646 (unsigned char *)rsa_dmq1->d);
647 sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
648 sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
649 (unsigned char *)rsa_iqmp->d);
650 sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
651 /* Attach the key params */
652 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
657 case SW_ERR_INPUT_SIZE:
658 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
663 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
664 sprintf(tmpbuf, "%ld", sw_status);
665 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
669 /* Prepare the argument and response */
670 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
671 arg.value = (unsigned char *)argument->d;
672 res.nbytes = 2 * BN_num_bytes(p);
673 memset(result->d, 0, res.nbytes);
674 res.value = (unsigned char *)result->d;
675 /* Perform the operation */
676 if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
680 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
681 sprintf(tmpbuf, "%ld", sw_status);
682 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
685 /* Convert the response */
686 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
690 release_context(hac);
695 #ifndef OPENSSL_NO_RSA
696 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
701 if((ctx = BN_CTX_new()) == NULL)
703 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
705 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
708 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
709 rsa->dmq1, rsa->iqmp, ctx);
717 /* This function is aliased to mod_exp (with the mont stuff dropped). */
718 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
719 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
721 return cswift_mod_exp(r, a, p, m, ctx);
724 #ifndef OPENSSL_NO_DSA
725 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
727 SW_CONTEXT_HANDLE hac;
730 SW_LARGENUMBER arg, res;
733 BIGNUM *dsa_p = NULL;
734 BIGNUM *dsa_q = NULL;
735 BIGNUM *dsa_g = NULL;
736 BIGNUM *dsa_key = NULL;
737 BIGNUM *result = NULL;
738 DSA_SIG *to_return = NULL;
741 if((ctx = BN_CTX_new()) == NULL)
743 if(!get_context(&hac))
745 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
749 /* Prepare the params */
751 dsa_p = BN_CTX_get(ctx);
752 dsa_q = BN_CTX_get(ctx);
753 dsa_g = BN_CTX_get(ctx);
754 dsa_key = BN_CTX_get(ctx);
755 result = BN_CTX_get(ctx);
758 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
761 if(!bn_wexpand(dsa_p, dsa->p->top) ||
762 !bn_wexpand(dsa_q, dsa->q->top) ||
763 !bn_wexpand(dsa_g, dsa->g->top) ||
764 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
765 !bn_wexpand(result, dsa->p->top))
767 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
770 sw_param.type = SW_ALG_DSA;
771 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
772 (unsigned char *)dsa_p->d);
773 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
774 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
775 (unsigned char *)dsa_q->d);
776 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
777 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
778 (unsigned char *)dsa_g->d);
779 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
780 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
781 (unsigned char *)dsa_key->d);
782 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
783 /* Attach the key params */
784 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
789 case SW_ERR_INPUT_SIZE:
790 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
795 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
796 sprintf(tmpbuf, "%ld", sw_status);
797 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
801 /* Prepare the argument and response */
803 arg.value = (unsigned char *)dgst;
804 res.nbytes = BN_num_bytes(dsa->p);
805 memset(result->d, 0, res.nbytes);
806 res.value = (unsigned char *)result->d;
807 /* Perform the operation */
808 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
810 if(sw_status != SW_OK)
813 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
814 sprintf(tmpbuf, "%ld", sw_status);
815 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
818 /* Convert the response */
819 ptr = (unsigned char *)result->d;
820 if((to_return = DSA_SIG_new()) == NULL)
822 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
823 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
827 release_context(hac);
836 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
837 DSA_SIG *sig, DSA *dsa)
839 SW_CONTEXT_HANDLE hac;
842 SW_LARGENUMBER arg[2], res;
843 unsigned long sig_result;
845 BIGNUM *dsa_p = NULL;
846 BIGNUM *dsa_q = NULL;
847 BIGNUM *dsa_g = NULL;
848 BIGNUM *dsa_key = NULL;
849 BIGNUM *argument = NULL;
853 if((ctx = BN_CTX_new()) == NULL)
855 if(!get_context(&hac))
857 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
861 /* Prepare the params */
863 dsa_p = BN_CTX_get(ctx);
864 dsa_q = BN_CTX_get(ctx);
865 dsa_g = BN_CTX_get(ctx);
866 dsa_key = BN_CTX_get(ctx);
867 argument = BN_CTX_get(ctx);
870 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
873 if(!bn_wexpand(dsa_p, dsa->p->top) ||
874 !bn_wexpand(dsa_q, dsa->q->top) ||
875 !bn_wexpand(dsa_g, dsa->g->top) ||
876 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
877 !bn_wexpand(argument, 40))
879 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
882 sw_param.type = SW_ALG_DSA;
883 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
884 (unsigned char *)dsa_p->d);
885 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
886 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
887 (unsigned char *)dsa_q->d);
888 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
889 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
890 (unsigned char *)dsa_g->d);
891 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
892 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
893 (unsigned char *)dsa_key->d);
894 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
895 /* Attach the key params */
896 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
901 case SW_ERR_INPUT_SIZE:
902 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
907 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
908 sprintf(tmpbuf, "%ld", sw_status);
909 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
913 /* Prepare the argument and response */
914 arg[0].nbytes = dgst_len;
915 arg[0].value = (unsigned char *)dgst;
917 arg[1].value = (unsigned char *)argument->d;
918 memset(arg[1].value, 0, 40);
919 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
920 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
921 res.nbytes = 4; /* unsigned long */
922 res.value = (unsigned char *)(&sig_result);
923 /* Perform the operation */
924 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
926 if(sw_status != SW_OK)
929 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
930 sprintf(tmpbuf, "%ld", sw_status);
931 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
934 /* Convert the response */
935 to_return = ((sig_result == 0) ? 0 : 1);
939 release_context(hac);
949 #ifndef OPENSSL_NO_DH
950 /* This function is aliased to mod_exp (with the dh and mont dropped). */
951 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
952 const BIGNUM *a, const BIGNUM *p,
953 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
955 return cswift_mod_exp(r, a, p, m, ctx);
959 /* This stuff is needed if this ENGINE is being compiled into a self-contained
961 #ifdef ENGINE_DYNAMIC_SUPPORT
962 static int bind_fn(ENGINE *e, const char *id)
964 if(id && (strcmp(id, engine_cswift_id) != 0))
970 IMPLEMENT_DYNAMIC_CHECK_FN()
971 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
972 #endif /* ENGINE_DYNAMIC_SUPPORT */
974 #endif /* !OPENSSL_NO_HW_CSWIFT */
975 #endif /* !OPENSSL_NO_HW */