1 /* crypto/engine/hw_cswift.c */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
6 /* ====================================================================
7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/crypto.h>
63 #include <openssl/buffer.h>
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_RSA
67 # include <openssl/rsa.h>
69 #ifndef OPENSSL_NO_DSA
70 # include <openssl/dsa.h>
73 # include <openssl/dh.h>
75 #include <openssl/rand.h>
76 #include <openssl/bn.h>
79 # ifndef OPENSSL_NO_HW_CSWIFT
82 * Attribution notice: Rainbow have generously allowed me to reproduce the
83 * necessary definitions here from their API. This means the support can
84 * build independently of whether application builders have the API or
85 * hardware. This will allow developers to easily produce software that has
86 * latent hardware support for any users that have accelerators installed,
87 * without the developers themselves needing anything extra. I have only
88 * clipped the parts from the CryptoSwift header files that are (or seem)
89 * relevant to the CryptoSwift support code. This is simply to keep the file
90 * sizes reasonable. [Geoff]
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,
117 const BIGNUM *dmq1, const BIGNUM *iqmp,
121 # ifndef OPENSSL_NO_RSA
123 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
125 /* This function is aliased to mod_exp (with the mont stuff dropped). */
126 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
127 const BIGNUM *m, BN_CTX *ctx,
131 # ifndef OPENSSL_NO_DSA
133 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen,
135 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
136 DSA_SIG *sig, DSA *dsa);
139 # ifndef OPENSSL_NO_DH
141 /* This function is alised to mod_exp (with the DH and mont dropped). */
142 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
143 const BIGNUM *a, const BIGNUM *p,
144 const BIGNUM *m, BN_CTX *ctx,
149 static int cswift_rand_bytes(unsigned char *buf, int num);
150 static int cswift_rand_status(void);
152 /* The definitions for control commands specific to this engine */
153 # define CSWIFT_CMD_SO_PATH ENGINE_CMD_BASE
154 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
157 "Specifies the path to the 'cswift' shared library",
158 ENGINE_CMD_FLAG_STRING},
162 # ifndef OPENSSL_NO_RSA
163 /* Our internal RSA_METHOD that we provide pointers to */
164 static RSA_METHOD cswift_rsa = {
165 "CryptoSwift RSA method",
182 # ifndef OPENSSL_NO_DSA
183 /* Our internal DSA_METHOD that we provide pointers to */
184 static DSA_METHOD cswift_dsa = {
185 "CryptoSwift DSA method",
187 NULL, /* dsa_sign_setup */
189 NULL, /* dsa_mod_exp */
190 NULL, /* bn_mod_exp */
195 NULL, /* dsa_paramgen */
196 NULL /* dsa_keygen */
200 # ifndef OPENSSL_NO_DH
201 /* Our internal DH_METHOD that we provide pointers to */
202 static DH_METHOD cswift_dh = {
203 "CryptoSwift DH method",
215 static RAND_METHOD cswift_random = {
216 /* "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";
230 * This internal function is used by ENGINE_cswift() and possibly by the
231 * "dynamic" ENGINE support too
233 static int bind_helper(ENGINE *e)
235 # ifndef OPENSSL_NO_RSA
236 const RSA_METHOD *meth1;
238 # ifndef OPENSSL_NO_DH
239 const DH_METHOD *meth2;
241 if (!ENGINE_set_id(e, engine_cswift_id) ||
242 !ENGINE_set_name(e, engine_cswift_name) ||
243 # ifndef OPENSSL_NO_RSA
244 !ENGINE_set_RSA(e, &cswift_rsa) ||
246 # ifndef OPENSSL_NO_DSA
247 !ENGINE_set_DSA(e, &cswift_dsa) ||
249 # ifndef OPENSSL_NO_DH
250 !ENGINE_set_DH(e, &cswift_dh) ||
252 !ENGINE_set_RAND(e, &cswift_random) ||
253 !ENGINE_set_destroy_function(e, cswift_destroy) ||
254 !ENGINE_set_init_function(e, cswift_init) ||
255 !ENGINE_set_finish_function(e, cswift_finish) ||
256 !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
257 !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
260 # ifndef OPENSSL_NO_RSA
262 * We know that the "PKCS1_SSLeay()" functions hook properly to the
263 * cswift-specific mod_exp and mod_exp_crt so we use those functions. NB:
264 * We don't use ENGINE_openssl() or anything "more generic" because
265 * something like the RSAref code may not hook properly, and if you own
266 * one of these cards then you have the right to do RSA operations on it
269 meth1 = RSA_PKCS1_SSLeay();
270 cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
271 cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
272 cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
273 cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
276 # ifndef OPENSSL_NO_DH
277 /* Much the same for Diffie-Hellman */
278 meth2 = DH_OpenSSL();
279 cswift_dh.generate_key = meth2->generate_key;
280 cswift_dh.compute_key = meth2->compute_key;
283 /* Ensure the cswift error handling is set up */
284 ERR_load_CSWIFT_strings();
288 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
289 static ENGINE *engine_cswift(void)
291 ENGINE *ret = ENGINE_new();
294 if (!bind_helper(ret)) {
301 void ENGINE_load_cswift(void)
303 /* Copied from eng_[openssl|dyn].c */
304 ENGINE *toadd = engine_cswift();
314 * This is a process-global DSO handle used for loading and unloading the
315 * CryptoSwift library. NB: This is only set (or unset) during an init() or
316 * finish() call (reference counts permitting) and they're operating with
317 * global locks, so this should be thread-safe implicitly.
319 static DSO *cswift_dso = NULL;
322 * These are the function pointers that are (un)set when the library has
323 * successfully (un)loaded.
325 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
326 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
327 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
328 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
330 /* Used in the DSO operations. */
331 static const char *CSWIFT_LIBNAME = NULL;
332 static const char *get_CSWIFT_LIBNAME(void)
335 return CSWIFT_LIBNAME;
339 static void free_CSWIFT_LIBNAME(void)
341 OPENSSL_free(CSWIFT_LIBNAME);
342 CSWIFT_LIBNAME = NULL;
345 static long set_CSWIFT_LIBNAME(const char *name)
347 free_CSWIFT_LIBNAME();
348 return (((CSWIFT_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
351 static const char *CSWIFT_F1 = "swAcquireAccContext";
352 static const char *CSWIFT_F2 = "swAttachKeyParam";
353 static const char *CSWIFT_F3 = "swSimpleRequest";
354 static const char *CSWIFT_F4 = "swReleaseAccContext";
357 * CryptoSwift library functions and mechanics - these are used by the
358 * higher-level functions further down. NB: As and where there's no error
359 * checking, take a look lower down where these functions are called, the
360 * checking and error handling is probably down there.
363 /* utility function to obtain a context */
364 static int get_context(SW_CONTEXT_HANDLE *hac)
368 status = p_CSwift_AcquireAccContext(hac);
374 /* similarly to release one. */
375 static void release_context(SW_CONTEXT_HANDLE hac)
377 p_CSwift_ReleaseAccContext(hac);
380 /* Destructor (complements the "ENGINE_cswift()" constructor) */
381 static int cswift_destroy(ENGINE *e)
383 free_CSWIFT_LIBNAME();
384 ERR_unload_CSWIFT_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) {
398 CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_ALREADY_LOADED);
401 /* Attempt to load libswift.so/swift.dll/whatever. */
402 cswift_dso = DSO_load(NULL, get_CSWIFT_LIBNAME(), NULL, 0);
403 if (cswift_dso == NULL) {
404 CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_NOT_LOADED);
408 #define BINDIT(t, name) (t *)DSO_bind_func(cswift_dso, name)
409 if ((p1 = BINDIT(t_swAcquireAccContext, CSWIFT_F1)) == NULL
410 || (p2 = BINDIT(t_swAttachKeyParam, CSWIFT_F2)) == NULL
411 || (p3 = BINDIT(t_swSimpleRequest *) DSO_bind_func(cswift_dso, CSWIFT_F3)) == NULL
412 || (p4 = BINDIT(t_swReleaseAccContext *) DSO_bind_func(cswift_dso, CSWIFT_F4)) == NULL) {
413 CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_NOT_LOADED);
416 /* Copy the pointers */
417 p_CSwift_AcquireAccContext = p1;
418 p_CSwift_AttachKeyParam = p2;
419 p_CSwift_SimpleRequest = p3;
420 p_CSwift_ReleaseAccContext = p4;
422 * Try and get a context - if not, we may have a DSO but no accelerator!
424 if (!get_context(&hac)) {
425 CSWIFTerr(CSWIFT_F_CSWIFT_INIT, CSWIFT_R_UNIT_FAILURE);
428 release_context(hac);
429 /* Everything's fine. */
432 DSO_free(cswift_dso);
434 p_CSwift_AcquireAccContext = NULL;
435 p_CSwift_AttachKeyParam = NULL;
436 p_CSwift_SimpleRequest = NULL;
437 p_CSwift_ReleaseAccContext = NULL;
441 static int cswift_finish(ENGINE *e)
443 free_CSWIFT_LIBNAME();
444 if (cswift_dso == NULL) {
445 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH, CSWIFT_R_NOT_LOADED);
448 if (!DSO_free(cswift_dso)) {
449 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH, CSWIFT_R_UNIT_FAILURE);
453 p_CSwift_AcquireAccContext = NULL;
454 p_CSwift_AttachKeyParam = NULL;
455 p_CSwift_SimpleRequest = NULL;
456 p_CSwift_ReleaseAccContext = NULL;
460 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
462 int initialised = ((cswift_dso == NULL) ? 0 : 1);
464 case CSWIFT_CMD_SO_PATH:
466 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, ERR_R_PASSED_NULL_PARAMETER);
470 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_ALREADY_LOADED);
473 return set_CSWIFT_LIBNAME((const char *)p);
477 CSWIFTerr(CSWIFT_F_CSWIFT_CTRL, CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
481 /* Un petit mod_exp */
482 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
483 const BIGNUM *m, BN_CTX *ctx)
486 * I need somewhere to store temporary serialised values for use with the
487 * CryptoSwift API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX
488 * but access their arrays directly as byte arrays <grin>. This way I
489 * don't have to clean anything up.
496 SW_LARGENUMBER arg, res;
498 SW_CONTEXT_HANDLE hac;
499 int to_return, acquired;
501 modulus = exponent = argument = result = NULL;
502 to_return = 0; /* expect failure */
505 if (!get_context(&hac)) {
506 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_UNIT_FAILURE);
510 /* Prepare the params */
512 modulus = BN_CTX_get(ctx);
513 exponent = BN_CTX_get(ctx);
514 argument = BN_CTX_get(ctx);
515 result = BN_CTX_get(ctx);
517 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_BN_CTX_FULL);
520 if (!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
521 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top)) {
522 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_BN_EXPAND_FAIL);
525 sw_param.type = SW_ALG_EXP;
526 sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
527 (unsigned char *)modulus->d);
528 sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
529 sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
530 (unsigned char *)exponent->d);
531 sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
532 /* Attach the key params */
533 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
537 case SW_ERR_INPUT_SIZE:
538 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_BAD_KEY_SIZE);
542 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
543 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_REQUEST_FAILED);
544 sprintf(tmpbuf, "%ld", sw_status);
545 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
549 /* Prepare the argument and response */
550 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
551 arg.value = (unsigned char *)argument->d;
552 res.nbytes = BN_num_bytes(m);
553 memset(result->d, 0, res.nbytes);
554 res.value = (unsigned char *)result->d;
555 /* Perform the operation */
556 if ((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
557 &res, 1)) != SW_OK) {
558 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
559 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP, CSWIFT_R_REQUEST_FAILED);
560 sprintf(tmpbuf, "%ld", sw_status);
561 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
564 /* Convert the response */
565 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
569 release_context(hac);
574 # ifndef OPENSSL_NO_RSA
575 int cswift_bn_32copy(SW_LARGENUMBER *out, const BIGNUM *in)
578 int numbytes = BN_num_bytes(in);
581 while (((out->nbytes = (numbytes + mod)) % 32)) {
584 out->value = OPENSSL_malloc(out->nbytes);
588 BN_bn2bin(in, &out->value[mod]);
590 memset(out->value, 0, mod);
596 # ifndef OPENSSL_NO_RSA
597 /* Un petit mod_exp chinois */
598 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
599 const BIGNUM *q, const BIGNUM *dmp1,
600 const BIGNUM *dmq1, const BIGNUM *iqmp,
604 SW_LARGENUMBER arg, res;
606 SW_CONTEXT_HANDLE hac;
607 BIGNUM *result = NULL;
608 BIGNUM *argument = NULL;
609 int to_return = 0; /* expect failure */
612 sw_param.up.crt.p.value = NULL;
613 sw_param.up.crt.q.value = NULL;
614 sw_param.up.crt.dmp1.value = NULL;
615 sw_param.up.crt.dmq1.value = NULL;
616 sw_param.up.crt.iqmp.value = NULL;
618 if (!get_context(&hac)) {
619 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_UNIT_FAILURE);
624 /* Prepare the params */
627 if (!result || !argument) {
628 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_CTX_FULL);
632 sw_param.type = SW_ALG_CRT;
633 /************************************************************************/
638 * Modified by Frederic Giudicelli (deny-all.com) to overcome the
641 * limitation of cswift with values not a multiple of 32
643 /************************************************************************/
644 if (!cswift_bn_32copy(&sw_param.up.crt.p, p)) {
645 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL);
648 if (!cswift_bn_32copy(&sw_param.up.crt.q, q)) {
649 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL);
652 if (!cswift_bn_32copy(&sw_param.up.crt.dmp1, dmp1)) {
653 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL);
656 if (!cswift_bn_32copy(&sw_param.up.crt.dmq1, dmq1)) {
657 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL);
660 if (!cswift_bn_32copy(&sw_param.up.crt.iqmp, iqmp)) {
661 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL);
664 if (!bn_wexpand(argument, a->top) || !bn_wexpand(result, p->top + q->top)) {
665 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BN_EXPAND_FAIL);
669 /* Attach the key params */
670 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
674 case SW_ERR_INPUT_SIZE:
675 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_BAD_KEY_SIZE);
679 char tmpbuf[DECIMAL_SIZE(sw_status) + 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);
686 /* Prepare the argument and response */
687 arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
688 arg.value = (unsigned char *)argument->d;
689 res.nbytes = 2 * BN_num_bytes(p);
690 memset(result->d, 0, res.nbytes);
691 res.value = (unsigned char *)result->d;
692 /* Perform the operation */
693 if ((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
694 &res, 1)) != SW_OK) {
695 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
696 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT, CSWIFT_R_REQUEST_FAILED);
697 sprintf(tmpbuf, "%ld", sw_status);
698 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
701 /* Convert the response */
702 BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
705 OPENSSL_free(sw_param.up.crt.p.value);
706 OPENSSL_free(sw_param.up.crt.q.value);
707 OPENSSL_free(sw_param.up.crt.dmp1.value);
708 OPENSSL_free(sw_param.up.crt.dmq1.value);
709 OPENSSL_free(sw_param.up.crt.iqmp.value);
713 release_context(hac);
718 # ifndef OPENSSL_NO_RSA
719 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
723 const RSA_METHOD *def_rsa_method;
725 if (!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp) {
726 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,
727 CSWIFT_R_MISSING_KEY_COMPONENTS);
731 /* Try the limits of RSA (2048 bits) */
732 if (BN_num_bytes(rsa->p) > 128 ||
733 BN_num_bytes(rsa->q) > 128 ||
734 BN_num_bytes(rsa->dmp1) > 128 ||
735 BN_num_bytes(rsa->dmq1) > 128 || BN_num_bytes(rsa->iqmp) > 128) {
737 def_rsa_method = RSA_null_method();
739 def_rsa_method = RSA_PKCS1_SSLeay();
742 return def_rsa_method->rsa_mod_exp(r0, I, rsa, ctx);
745 to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
746 rsa->dmq1, rsa->iqmp, ctx);
751 /* This function is aliased to mod_exp (with the mont stuff dropped). */
752 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
753 const BIGNUM *m, BN_CTX *ctx,
756 const RSA_METHOD *def_rsa_method;
758 /* Try the limits of RSA (2048 bits) */
759 if (BN_num_bytes(r) > 256 ||
760 BN_num_bytes(a) > 256 || BN_num_bytes(m) > 256) {
762 def_rsa_method = RSA_null_method();
764 def_rsa_method = RSA_PKCS1_SSLeay();
767 return def_rsa_method->bn_mod_exp(r, a, p, m, ctx, m_ctx);
770 return cswift_mod_exp(r, a, p, m, ctx);
772 # endif /* OPENSSL_NO_RSA */
774 # ifndef OPENSSL_NO_DSA
775 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
777 SW_CONTEXT_HANDLE hac;
780 SW_LARGENUMBER arg, res;
782 BIGNUM *dsa_p = NULL;
783 BIGNUM *dsa_q = NULL;
784 BIGNUM *dsa_g = NULL;
785 BIGNUM *dsa_key = NULL;
786 BIGNUM *result = NULL;
787 DSA_SIG *to_return = NULL;
790 if ((ctx = BN_CTX_new()) == NULL)
792 if (!get_context(&hac)) {
793 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_UNIT_FAILURE);
797 /* Prepare the params */
799 dsa_p = BN_CTX_get(ctx);
800 dsa_q = BN_CTX_get(ctx);
801 dsa_g = BN_CTX_get(ctx);
802 dsa_key = BN_CTX_get(ctx);
803 result = BN_CTX_get(ctx);
805 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_BN_CTX_FULL);
808 if (!bn_wexpand(dsa_p, dsa->p->top) ||
809 !bn_wexpand(dsa_q, dsa->q->top) ||
810 !bn_wexpand(dsa_g, dsa->g->top) ||
811 !bn_wexpand(dsa_key, dsa->priv_key->top) ||
812 !bn_wexpand(result, dsa->p->top)) {
813 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_BN_EXPAND_FAIL);
816 sw_param.type = SW_ALG_DSA;
817 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p, (unsigned char *)dsa_p->d);
818 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
819 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q, (unsigned char *)dsa_q->d);
820 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
821 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g, (unsigned char *)dsa_g->d);
822 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
823 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
824 (unsigned char *)dsa_key->d);
825 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
826 /* Attach the key params */
827 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
831 case SW_ERR_INPUT_SIZE:
832 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_BAD_KEY_SIZE);
836 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
837 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_REQUEST_FAILED);
838 sprintf(tmpbuf, "%ld", sw_status);
839 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
843 /* Prepare the argument and response */
845 arg.value = (unsigned char *)dgst;
846 res.nbytes = BN_num_bytes(dsa->p);
847 memset(result->d, 0, res.nbytes);
848 res.value = (unsigned char *)result->d;
849 /* Perform the operation */
850 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
852 if (sw_status != SW_OK) {
853 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
854 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN, CSWIFT_R_REQUEST_FAILED);
855 sprintf(tmpbuf, "%ld", sw_status);
856 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
859 /* Convert the response */
860 if ((to_return = DSA_SIG_new()) == NULL)
862 to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
863 to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
867 release_context(hac);
874 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
875 DSA_SIG *sig, DSA *dsa)
877 SW_CONTEXT_HANDLE hac;
880 SW_LARGENUMBER arg[2], res;
881 unsigned long sig_result;
883 BIGNUM *dsa_p = NULL;
884 BIGNUM *dsa_q = NULL;
885 BIGNUM *dsa_g = NULL;
886 BIGNUM *dsa_key = NULL;
887 BIGNUM *argument = NULL;
891 if ((ctx = BN_CTX_new()) == NULL)
893 if (!get_context(&hac)) {
894 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_UNIT_FAILURE);
898 /* Prepare the params */
900 dsa_p = BN_CTX_get(ctx);
901 dsa_q = BN_CTX_get(ctx);
902 dsa_g = BN_CTX_get(ctx);
903 dsa_key = BN_CTX_get(ctx);
904 argument = BN_CTX_get(ctx);
906 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_BN_CTX_FULL);
909 if (!bn_wexpand(dsa_p, dsa->p->top) ||
910 !bn_wexpand(dsa_q, dsa->q->top) ||
911 !bn_wexpand(dsa_g, dsa->g->top) ||
912 !bn_wexpand(dsa_key, dsa->pub_key->top) ||
913 !bn_wexpand(argument, 40)) {
914 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_BN_EXPAND_FAIL);
917 sw_param.type = SW_ALG_DSA;
918 sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p, (unsigned char *)dsa_p->d);
919 sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
920 sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q, (unsigned char *)dsa_q->d);
921 sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
922 sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g, (unsigned char *)dsa_g->d);
923 sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
924 sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
925 (unsigned char *)dsa_key->d);
926 sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
927 /* Attach the key params */
928 sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
932 case SW_ERR_INPUT_SIZE:
933 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_BAD_KEY_SIZE);
937 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
938 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_REQUEST_FAILED);
939 sprintf(tmpbuf, "%ld", sw_status);
940 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
944 /* Prepare the argument and response */
945 arg[0].nbytes = dgst_len;
946 arg[0].value = (unsigned char *)dgst;
948 arg[1].value = (unsigned char *)argument->d;
949 memset(arg[1].value, 0, 40);
950 BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
951 BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
952 res.nbytes = 4; /* unsigned long */
953 res.value = (unsigned char *)(&sig_result);
954 /* Perform the operation */
955 sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
957 if (sw_status != SW_OK) {
958 char tmpbuf[DECIMAL_SIZE(sw_status) + 1];
959 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY, CSWIFT_R_REQUEST_FAILED);
960 sprintf(tmpbuf, "%ld", sw_status);
961 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
964 /* Convert the response */
965 to_return = ((sig_result == 0) ? 0 : 1);
969 release_context(hac);
977 # ifndef OPENSSL_NO_DH
978 /* This function is aliased to mod_exp (with the dh and mont dropped). */
979 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
980 const BIGNUM *a, const BIGNUM *p,
981 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
983 return cswift_mod_exp(r, a, p, m, ctx);
987 /* Random bytes are good */
988 static int cswift_rand_bytes(unsigned char *buf, int num)
990 SW_CONTEXT_HANDLE hac;
992 SW_LARGENUMBER largenum;
994 int to_return = 0; /* assume failure */
995 unsigned char buf32[1024];
997 if (!get_context(&hac)) {
998 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_UNIT_FAILURE);
1003 /************************************************************************/
1008 * Modified by Frederic Giudicelli (deny-all.com) to overcome the
1011 * limitation of cswift with values not a multiple of 32
1013 /************************************************************************/
1015 while (num >= (int)sizeof(buf32)) {
1016 largenum.value = buf;
1017 largenum.nbytes = sizeof(buf32);
1019 * tell CryptoSwift how many bytes we want and where we want it.
1020 * Note: - CryptoSwift cannot do more than 4096 bytes at a time.
1021 * - CryptoSwift can only do multiple of 32-bits.
1024 p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
1025 if (swrc != SW_OK) {
1027 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
1028 sprintf(tmpbuf, "%ld", swrc);
1029 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
1032 buf += sizeof(buf32);
1033 num -= sizeof(buf32);
1036 largenum.nbytes = sizeof(buf32);
1037 largenum.value = buf32;
1039 p_CSwift_SimpleRequest(hac, SW_CMD_RAND, NULL, 0, &largenum, 1);
1040 if (swrc != SW_OK) {
1042 CSWIFTerr(CSWIFT_F_CSWIFT_RAND_BYTES, CSWIFT_R_REQUEST_FAILED);
1043 sprintf(tmpbuf, "%ld", swrc);
1044 ERR_add_error_data(2, "CryptoSwift error number is ", tmpbuf);
1047 memcpy(buf, largenum.value, num);
1050 to_return = 1; /* success */
1053 release_context(hac);
1058 static int cswift_rand_status(void)
1064 * This stuff is needed if this ENGINE is being compiled into a
1065 * self-contained shared-library.
1067 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1068 static int bind_fn(ENGINE *e, const char *id)
1070 if (id && (strcmp(id, engine_cswift_id) != 0))
1072 if (!bind_helper(e))
1077 IMPLEMENT_DYNAMIC_CHECK_FN()
1078 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1079 # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
1080 # endif /* !OPENSSL_NO_HW_CSWIFT */
1081 #endif /* !OPENSSL_NO_HW */