Remove an unused variable.
[openssl.git] / crypto / engine / hw_cswift.c
1 /* crypto/engine/hw_cswift.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  */
5 /* ====================================================================
6  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer. 
14  *
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
18  *    distribution.
19  *
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/)"
24  *
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.
29  *
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.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
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  * ====================================================================
52  *
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).
56  *
57  */
58
59 #include <stdio.h>
60 #include <openssl/crypto.h>
61 #include "cryptlib.h"
62 #include <openssl/dso.h>
63 #include <openssl/engine.h>
64
65 #ifndef OPENSSL_NO_HW
66 #ifndef OPENSSL_NO_HW_CSWIFT
67
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.
74  *
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.
78  * [Geoff]
79  */
80 #ifdef FLAT_INC
81 #include "cswift.h"
82 #else
83 #include "vendor_defns/cswift.h"
84 #endif
85
86 #define CSWIFT_LIB_NAME "cswift engine"
87 #include "hw_cswift_err.c"
88
89 static int cswift_destroy(ENGINE *e);
90 static int cswift_init(ENGINE *e);
91 static int cswift_finish(ENGINE *e);
92 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
93
94 /* BIGNUM stuff */
95 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
96                 const BIGNUM *m, BN_CTX *ctx);
97 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
98                 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
99                 const BIGNUM *iqmp, BN_CTX *ctx);
100
101 #ifndef OPENSSL_NO_RSA
102 /* RSA stuff */
103 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
104 #endif
105 /* This function is aliased to mod_exp (with the mont stuff dropped). */
106 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
107                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
108
109 #ifndef OPENSSL_NO_DSA
110 /* DSA stuff */
111 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
112 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
113                                 DSA_SIG *sig, DSA *dsa);
114 #endif
115
116 #ifndef OPENSSL_NO_DH
117 /* DH stuff */
118 /* This function is alised to mod_exp (with the DH and mont dropped). */
119 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
120                 const BIGNUM *a, const BIGNUM *p,
121                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
122 #endif
123
124 /* The definitions for control commands specific to this engine */
125 #define CSWIFT_CMD_SO_PATH              ENGINE_CMD_BASE
126 static const ENGINE_CMD_DEFN cswift_cmd_defns[] = {
127         {CSWIFT_CMD_SO_PATH,
128                 "SO_PATH",
129                 "Specifies the path to the 'cswift' shared library",
130                 ENGINE_CMD_FLAG_STRING},
131         {0, NULL, NULL, 0}
132         };
133
134 #ifndef OPENSSL_NO_RSA
135 /* Our internal RSA_METHOD that we provide pointers to */
136 static RSA_METHOD cswift_rsa =
137         {
138         "CryptoSwift RSA method",
139         NULL,
140         NULL,
141         NULL,
142         NULL,
143         cswift_rsa_mod_exp,
144         cswift_mod_exp_mont,
145         NULL,
146         NULL,
147         0,
148         NULL,
149         NULL,
150         NULL
151         };
152 #endif
153
154 #ifndef OPENSSL_NO_DSA
155 /* Our internal DSA_METHOD that we provide pointers to */
156 static DSA_METHOD cswift_dsa =
157         {
158         "CryptoSwift DSA method",
159         cswift_dsa_sign,
160         NULL, /* dsa_sign_setup */
161         cswift_dsa_verify,
162         NULL, /* dsa_mod_exp */
163         NULL, /* bn_mod_exp */
164         NULL, /* init */
165         NULL, /* finish */
166         0, /* flags */
167         NULL /* app_data */
168         };
169 #endif
170
171 #ifndef OPENSSL_NO_DH
172 /* Our internal DH_METHOD that we provide pointers to */
173 static DH_METHOD cswift_dh =
174         {
175         "CryptoSwift DH method",
176         NULL,
177         NULL,
178         cswift_mod_exp_dh,
179         NULL,
180         NULL,
181         0,
182         NULL
183         };
184 #endif
185
186 /* Constants used when creating the ENGINE */
187 static const char *engine_cswift_id = "cswift";
188 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
189
190 /* This internal function is used by ENGINE_cswift() and possibly by the
191  * "dynamic" ENGINE support too */
192 static int bind_helper(ENGINE *e)
193         {
194 #ifndef OPENSSL_NO_RSA
195         const RSA_METHOD *meth1;
196 #endif
197 #ifndef OPENSSL_NO_DH
198         const DH_METHOD *meth2;
199 #endif
200         if(!ENGINE_set_id(e, engine_cswift_id) ||
201                         !ENGINE_set_name(e, engine_cswift_name) ||
202 #ifndef OPENSSL_NO_RSA
203                         !ENGINE_set_RSA(e, &cswift_rsa) ||
204 #endif
205 #ifndef OPENSSL_NO_DSA
206                         !ENGINE_set_DSA(e, &cswift_dsa) ||
207 #endif
208 #ifndef OPENSSL_NO_DH
209                         !ENGINE_set_DH(e, &cswift_dh) ||
210 #endif
211                         !ENGINE_set_destroy_function(e, cswift_destroy) ||
212                         !ENGINE_set_init_function(e, cswift_init) ||
213                         !ENGINE_set_finish_function(e, cswift_finish) ||
214                         !ENGINE_set_ctrl_function(e, cswift_ctrl) ||
215                         !ENGINE_set_cmd_defns(e, cswift_cmd_defns))
216                 return 0;
217
218 #ifndef OPENSSL_NO_RSA
219         /* We know that the "PKCS1_SSLeay()" functions hook properly
220          * to the cswift-specific mod_exp and mod_exp_crt so we use
221          * those functions. NB: We don't use ENGINE_openssl() or
222          * anything "more generic" because something like the RSAref
223          * code may not hook properly, and if you own one of these
224          * cards then you have the right to do RSA operations on it
225          * anyway! */ 
226         meth1 = RSA_PKCS1_SSLeay();
227         cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
228         cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
229         cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
230         cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
231 #endif
232
233 #ifndef OPENSSL_NO_DH
234         /* Much the same for Diffie-Hellman */
235         meth2 = DH_OpenSSL();
236         cswift_dh.generate_key = meth2->generate_key;
237         cswift_dh.compute_key = meth2->compute_key;
238 #endif
239
240         /* Ensure the cswift error handling is set up */
241         ERR_load_CSWIFT_strings();
242         return 1;
243         }
244
245 static ENGINE *engine_cswift(void)
246         {
247         ENGINE *ret = ENGINE_new();
248         if(!ret)
249                 return NULL;
250         if(!bind_helper(ret))
251                 {
252                 ENGINE_free(ret);
253                 return NULL;
254                 }
255         return ret;
256         }
257
258 void ENGINE_load_cswift(void)
259         {
260         /* Copied from eng_[openssl|dyn].c */
261         ENGINE *toadd = engine_cswift();
262         if(!toadd) return;
263         ENGINE_add(toadd);
264         ENGINE_free(toadd);
265         ERR_clear_error();
266         }
267
268 /* This is a process-global DSO handle used for loading and unloading
269  * the CryptoSwift library. NB: This is only set (or unset) during an
270  * init() or finish() call (reference counts permitting) and they're
271  * operating with global locks, so this should be thread-safe
272  * implicitly. */
273 static DSO *cswift_dso = NULL;
274
275 /* These are the function pointers that are (un)set when the library has
276  * successfully (un)loaded. */
277 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
278 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
279 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
280 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
281
282 /* Used in the DSO operations. */
283 static const char def_CSWIFT_LIBNAME[] = "swift";
284 static const char *CSWIFT_LIBNAME = def_CSWIFT_LIBNAME;
285 static const char *CSWIFT_F1 = "swAcquireAccContext";
286 static const char *CSWIFT_F2 = "swAttachKeyParam";
287 static const char *CSWIFT_F3 = "swSimpleRequest";
288 static const char *CSWIFT_F4 = "swReleaseAccContext";
289
290
291 /* CryptoSwift library functions and mechanics - these are used by the
292  * higher-level functions further down. NB: As and where there's no
293  * error checking, take a look lower down where these functions are
294  * called, the checking and error handling is probably down there. */
295
296 /* utility function to obtain a context */
297 static int get_context(SW_CONTEXT_HANDLE *hac)
298         {
299         SW_STATUS status;
300  
301         status = p_CSwift_AcquireAccContext(hac);
302         if(status != SW_OK)
303                 return 0;
304         return 1;
305         }
306  
307 /* similarly to release one. */
308 static void release_context(SW_CONTEXT_HANDLE hac)
309         {
310         p_CSwift_ReleaseAccContext(hac);
311         }
312
313 /* Destructor (complements the "ENGINE_cswift()" constructor) */
314 static int cswift_destroy(ENGINE *e)
315         {
316         ERR_unload_CSWIFT_strings();
317         return 1;
318         }
319
320 /* (de)initialisation functions. */
321 static int cswift_init(ENGINE *e)
322         {
323         SW_CONTEXT_HANDLE hac;
324         t_swAcquireAccContext *p1;
325         t_swAttachKeyParam *p2;
326         t_swSimpleRequest *p3;
327         t_swReleaseAccContext *p4;
328
329         if(cswift_dso != NULL)
330                 {
331                 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_ALREADY_LOADED);
332                 goto err;
333                 }
334         /* Attempt to load libswift.so/swift.dll/whatever. */
335         cswift_dso = DSO_load(NULL, CSWIFT_LIBNAME, NULL, 0);
336         if(cswift_dso == NULL)
337                 {
338                 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
339                 goto err;
340                 }
341         if(!(p1 = (t_swAcquireAccContext *)
342                                 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
343                         !(p2 = (t_swAttachKeyParam *)
344                                 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
345                         !(p3 = (t_swSimpleRequest *)
346                                 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
347                         !(p4 = (t_swReleaseAccContext *)
348                                 DSO_bind_func(cswift_dso, CSWIFT_F4)))
349                 {
350                 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_NOT_LOADED);
351                 goto err;
352                 }
353         /* Copy the pointers */
354         p_CSwift_AcquireAccContext = p1;
355         p_CSwift_AttachKeyParam = p2;
356         p_CSwift_SimpleRequest = p3;
357         p_CSwift_ReleaseAccContext = p4;
358         /* Try and get a context - if not, we may have a DSO but no
359          * accelerator! */
360         if(!get_context(&hac))
361                 {
362                 CSWIFTerr(CSWIFT_F_CSWIFT_INIT,CSWIFT_R_UNIT_FAILURE);
363                 goto err;
364                 }
365         release_context(hac);
366         /* Everything's fine. */
367         return 1;
368 err:
369         if(cswift_dso)
370                 DSO_free(cswift_dso);
371         p_CSwift_AcquireAccContext = NULL;
372         p_CSwift_AttachKeyParam = NULL;
373         p_CSwift_SimpleRequest = NULL;
374         p_CSwift_ReleaseAccContext = NULL;
375         return 0;
376         }
377
378 static int cswift_finish(ENGINE *e)
379         {
380         if(cswift_dso == NULL)
381                 {
382                 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_NOT_LOADED);
383                 return 0;
384                 }
385         if(!DSO_free(cswift_dso))
386                 {
387                 CSWIFTerr(CSWIFT_F_CSWIFT_FINISH,CSWIFT_R_UNIT_FAILURE);
388                 return 0;
389                 }
390         cswift_dso = NULL;
391         p_CSwift_AcquireAccContext = NULL;
392         p_CSwift_AttachKeyParam = NULL;
393         p_CSwift_SimpleRequest = NULL;
394         p_CSwift_ReleaseAccContext = NULL;
395         return 1;
396         }
397
398 static int cswift_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
399         {
400         int initialised = ((cswift_dso == NULL) ? 0 : 1);
401         switch(cmd)
402                 {
403         case CSWIFT_CMD_SO_PATH:
404                 if(p == NULL)
405                         {
406                         CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,ERR_R_PASSED_NULL_PARAMETER);
407                         return 0;
408                         }
409                 if(initialised)
410                         {
411                         CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_ALREADY_LOADED);
412                         return 0;
413                         }
414                 CSWIFT_LIBNAME = (const char *)p;
415                 return 1;
416         default:
417                 break;
418                 }
419         CSWIFTerr(CSWIFT_F_CSWIFT_CTRL,CSWIFT_R_CTRL_COMMAND_NOT_IMPLEMENTED);
420         return 0;
421         }
422
423 /* Un petit mod_exp */
424 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
425                         const BIGNUM *m, BN_CTX *ctx)
426         {
427         /* I need somewhere to store temporary serialised values for
428          * use with the CryptoSwift API calls. A neat cheat - I'll use
429          * BIGNUMs from the BN_CTX but access their arrays directly as
430          * byte arrays <grin>. This way I don't have to clean anything
431          * up. */
432         BIGNUM *modulus;
433         BIGNUM *exponent;
434         BIGNUM *argument;
435         BIGNUM *result;
436         SW_STATUS sw_status;
437         SW_LARGENUMBER arg, res;
438         SW_PARAM sw_param;
439         SW_CONTEXT_HANDLE hac;
440         int to_return, acquired;
441  
442         modulus = exponent = argument = result = NULL;
443         to_return = 0; /* expect failure */
444         acquired = 0;
445  
446         if(!get_context(&hac))
447                 {
448                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_UNIT_FAILURE);
449                 goto err;
450                 }
451         acquired = 1;
452         /* Prepare the params */
453         BN_CTX_start(ctx);
454         modulus = BN_CTX_get(ctx);
455         exponent = BN_CTX_get(ctx);
456         argument = BN_CTX_get(ctx);
457         result = BN_CTX_get(ctx);
458         if(!result)
459                 {
460                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_CTX_FULL);
461                 goto err;
462                 }
463         if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
464                 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
465                 {
466                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BN_EXPAND_FAIL);
467                 goto err;
468                 }
469         sw_param.type = SW_ALG_EXP;
470         sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
471                 (unsigned char *)modulus->d);
472         sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
473         sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
474                 (unsigned char *)exponent->d);
475         sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
476         /* Attach the key params */
477         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
478         switch(sw_status)
479                 {
480         case SW_OK:
481                 break;
482         case SW_ERR_INPUT_SIZE:
483                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_BAD_KEY_SIZE);
484                 goto err;
485         default:
486                 {
487                 char tmpbuf[20];
488                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
489                 sprintf(tmpbuf, "%ld", sw_status);
490                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
491                 }
492                 goto err;
493                 }
494         /* Prepare the argument and response */
495         arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
496         arg.value = (unsigned char *)argument->d;
497         res.nbytes = BN_num_bytes(m);
498         memset(result->d, 0, res.nbytes);
499         res.value = (unsigned char *)result->d;
500         /* Perform the operation */
501         if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
502                 &res, 1)) != SW_OK)
503                 {
504                 char tmpbuf[20];
505                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP,CSWIFT_R_REQUEST_FAILED);
506                 sprintf(tmpbuf, "%ld", sw_status);
507                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
508                 goto err;
509                 }
510         /* Convert the response */
511         BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
512         to_return = 1;
513 err:
514         if(acquired)
515                 release_context(hac);
516         BN_CTX_end(ctx);
517         return to_return;
518         }
519
520 /* Un petit mod_exp chinois */
521 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
522                         const BIGNUM *q, const BIGNUM *dmp1,
523                         const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
524         {
525         SW_STATUS sw_status;
526         SW_LARGENUMBER arg, res;
527         SW_PARAM sw_param;
528         SW_CONTEXT_HANDLE hac;
529         BIGNUM *rsa_p = NULL;
530         BIGNUM *rsa_q = NULL;
531         BIGNUM *rsa_dmp1 = NULL;
532         BIGNUM *rsa_dmq1 = NULL;
533         BIGNUM *rsa_iqmp = NULL;
534         BIGNUM *argument = NULL;
535         BIGNUM *result = NULL;
536         int to_return = 0; /* expect failure */
537         int acquired = 0;
538  
539         if(!get_context(&hac))
540                 {
541                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_UNIT_FAILURE);
542                 goto err;
543                 }
544         acquired = 1;
545         /* Prepare the params */
546         BN_CTX_start(ctx);
547         rsa_p = BN_CTX_get(ctx);
548         rsa_q = BN_CTX_get(ctx);
549         rsa_dmp1 = BN_CTX_get(ctx);
550         rsa_dmq1 = BN_CTX_get(ctx);
551         rsa_iqmp = BN_CTX_get(ctx);
552         argument = BN_CTX_get(ctx);
553         result = BN_CTX_get(ctx);
554         if(!result)
555                 {
556                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_CTX_FULL);
557                 goto err;
558                 }
559         if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
560                         !bn_wexpand(rsa_dmp1, dmp1->top) ||
561                         !bn_wexpand(rsa_dmq1, dmq1->top) ||
562                         !bn_wexpand(rsa_iqmp, iqmp->top) ||
563                         !bn_wexpand(argument, a->top) ||
564                         !bn_wexpand(result, p->top + q->top))
565                 {
566                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BN_EXPAND_FAIL);
567                 goto err;
568                 }
569         sw_param.type = SW_ALG_CRT;
570         sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
571         sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
572         sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
573         sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
574         sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
575                 (unsigned char *)rsa_dmp1->d);
576         sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
577         sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
578                 (unsigned char *)rsa_dmq1->d);
579         sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
580         sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
581                 (unsigned char *)rsa_iqmp->d);
582         sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
583         /* Attach the key params */
584         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
585         switch(sw_status)
586                 {
587         case SW_OK:
588                 break;
589         case SW_ERR_INPUT_SIZE:
590                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_BAD_KEY_SIZE);
591                 goto err;
592         default:
593                 {
594                 char tmpbuf[20];
595                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
596                 sprintf(tmpbuf, "%ld", sw_status);
597                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
598                 }
599                 goto err;
600                 }
601         /* Prepare the argument and response */
602         arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
603         arg.value = (unsigned char *)argument->d;
604         res.nbytes = 2 * BN_num_bytes(p);
605         memset(result->d, 0, res.nbytes);
606         res.value = (unsigned char *)result->d;
607         /* Perform the operation */
608         if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
609                 &res, 1)) != SW_OK)
610                 {
611                 char tmpbuf[20];
612                 CSWIFTerr(CSWIFT_F_CSWIFT_MOD_EXP_CRT,CSWIFT_R_REQUEST_FAILED);
613                 sprintf(tmpbuf, "%ld", sw_status);
614                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
615                 goto err;
616                 }
617         /* Convert the response */
618         BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
619         to_return = 1;
620 err:
621         if(acquired)
622                 release_context(hac);
623         BN_CTX_end(ctx);
624         return to_return;
625         }
626  
627 #ifndef OPENSSL_NO_RSA
628 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
629         {
630         BN_CTX *ctx;
631         int to_return = 0;
632
633         if((ctx = BN_CTX_new()) == NULL)
634                 goto err;
635         if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
636                 {
637                 CSWIFTerr(CSWIFT_F_CSWIFT_RSA_MOD_EXP,CSWIFT_R_MISSING_KEY_COMPONENTS);
638                 goto err;
639                 }
640         to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
641                 rsa->dmq1, rsa->iqmp, ctx);
642 err:
643         if(ctx)
644                 BN_CTX_free(ctx);
645         return to_return;
646         }
647 #endif
648
649 /* This function is aliased to mod_exp (with the mont stuff dropped). */
650 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
651                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
652         {
653         return cswift_mod_exp(r, a, p, m, ctx);
654         }
655
656 #ifndef OPENSSL_NO_DSA
657 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
658         {
659         SW_CONTEXT_HANDLE hac;
660         SW_PARAM sw_param;
661         SW_STATUS sw_status;
662         SW_LARGENUMBER arg, res;
663         unsigned char *ptr;
664         BN_CTX *ctx;
665         BIGNUM *dsa_p = NULL;
666         BIGNUM *dsa_q = NULL;
667         BIGNUM *dsa_g = NULL;
668         BIGNUM *dsa_key = NULL;
669         BIGNUM *result = NULL;
670         DSA_SIG *to_return = NULL;
671         int acquired = 0;
672
673         if((ctx = BN_CTX_new()) == NULL)
674                 goto err;
675         if(!get_context(&hac))
676                 {
677                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_UNIT_FAILURE);
678                 goto err;
679                 }
680         acquired = 1;
681         /* Prepare the params */
682         BN_CTX_start(ctx);
683         dsa_p = BN_CTX_get(ctx);
684         dsa_q = BN_CTX_get(ctx);
685         dsa_g = BN_CTX_get(ctx);
686         dsa_key = BN_CTX_get(ctx);
687         result = BN_CTX_get(ctx);
688         if(!result)
689                 {
690                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_CTX_FULL);
691                 goto err;
692                 }
693         if(!bn_wexpand(dsa_p, dsa->p->top) ||
694                         !bn_wexpand(dsa_q, dsa->q->top) ||
695                         !bn_wexpand(dsa_g, dsa->g->top) ||
696                         !bn_wexpand(dsa_key, dsa->priv_key->top) ||
697                         !bn_wexpand(result, dsa->p->top))
698                 {
699                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BN_EXPAND_FAIL);
700                 goto err;
701                 }
702         sw_param.type = SW_ALG_DSA;
703         sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
704                                 (unsigned char *)dsa_p->d);
705         sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
706         sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
707                                 (unsigned char *)dsa_q->d);
708         sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
709         sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
710                                 (unsigned char *)dsa_g->d);
711         sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
712         sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
713                                 (unsigned char *)dsa_key->d);
714         sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
715         /* Attach the key params */
716         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
717         switch(sw_status)
718                 {
719         case SW_OK:
720                 break;
721         case SW_ERR_INPUT_SIZE:
722                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_BAD_KEY_SIZE);
723                 goto err;
724         default:
725                 {
726                 char tmpbuf[20];
727                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
728                 sprintf(tmpbuf, "%ld", sw_status);
729                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
730                 }
731                 goto err;
732                 }
733         /* Prepare the argument and response */
734         arg.nbytes = dlen;
735         arg.value = (unsigned char *)dgst;
736         res.nbytes = BN_num_bytes(dsa->p);
737         memset(result->d, 0, res.nbytes);
738         res.value = (unsigned char *)result->d;
739         /* Perform the operation */
740         sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
741                 &res, 1);
742         if(sw_status != SW_OK)
743                 {
744                 char tmpbuf[20];
745                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_SIGN,CSWIFT_R_REQUEST_FAILED);
746                 sprintf(tmpbuf, "%ld", sw_status);
747                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
748                 goto err;
749                 }
750         /* Convert the response */
751         ptr = (unsigned char *)result->d;
752         if((to_return = DSA_SIG_new()) == NULL)
753                 goto err;
754         to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
755         to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
756
757 err:
758         if(acquired)
759                 release_context(hac);
760         if(ctx)
761                 {
762                 BN_CTX_end(ctx);
763                 BN_CTX_free(ctx);
764                 }
765         return to_return;
766         }
767
768 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
769                                 DSA_SIG *sig, DSA *dsa)
770         {
771         SW_CONTEXT_HANDLE hac;
772         SW_PARAM sw_param;
773         SW_STATUS sw_status;
774         SW_LARGENUMBER arg[2], res;
775         unsigned long sig_result;
776         BN_CTX *ctx;
777         BIGNUM *dsa_p = NULL;
778         BIGNUM *dsa_q = NULL;
779         BIGNUM *dsa_g = NULL;
780         BIGNUM *dsa_key = NULL;
781         BIGNUM *argument = NULL;
782         int to_return = -1;
783         int acquired = 0;
784
785         if((ctx = BN_CTX_new()) == NULL)
786                 goto err;
787         if(!get_context(&hac))
788                 {
789                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_UNIT_FAILURE);
790                 goto err;
791                 }
792         acquired = 1;
793         /* Prepare the params */
794         BN_CTX_start(ctx);
795         dsa_p = BN_CTX_get(ctx);
796         dsa_q = BN_CTX_get(ctx);
797         dsa_g = BN_CTX_get(ctx);
798         dsa_key = BN_CTX_get(ctx);
799         argument = BN_CTX_get(ctx);
800         if(!argument)
801                 {
802                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_CTX_FULL);
803                 goto err;
804                 }
805         if(!bn_wexpand(dsa_p, dsa->p->top) ||
806                         !bn_wexpand(dsa_q, dsa->q->top) ||
807                         !bn_wexpand(dsa_g, dsa->g->top) ||
808                         !bn_wexpand(dsa_key, dsa->pub_key->top) ||
809                         !bn_wexpand(argument, 40))
810                 {
811                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BN_EXPAND_FAIL);
812                 goto err;
813                 }
814         sw_param.type = SW_ALG_DSA;
815         sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
816                                 (unsigned char *)dsa_p->d);
817         sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
818         sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
819                                 (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,
822                                 (unsigned char *)dsa_g->d);
823         sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
824         sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
825                                 (unsigned char *)dsa_key->d);
826         sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
827         /* Attach the key params */
828         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
829         switch(sw_status)
830                 {
831         case SW_OK:
832                 break;
833         case SW_ERR_INPUT_SIZE:
834                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_BAD_KEY_SIZE);
835                 goto err;
836         default:
837                 {
838                 char tmpbuf[20];
839                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
840                 sprintf(tmpbuf, "%ld", sw_status);
841                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
842                 }
843                 goto err;
844                 }
845         /* Prepare the argument and response */
846         arg[0].nbytes = dgst_len;
847         arg[0].value = (unsigned char *)dgst;
848         arg[1].nbytes = 40;
849         arg[1].value = (unsigned char *)argument->d;
850         memset(arg[1].value, 0, 40);
851         BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
852         BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
853         res.nbytes = 4; /* unsigned long */
854         res.value = (unsigned char *)(&sig_result);
855         /* Perform the operation */
856         sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
857                 &res, 1);
858         if(sw_status != SW_OK)
859                 {
860                 char tmpbuf[20];
861                 CSWIFTerr(CSWIFT_F_CSWIFT_DSA_VERIFY,CSWIFT_R_REQUEST_FAILED);
862                 sprintf(tmpbuf, "%ld", sw_status);
863                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
864                 goto err;
865                 }
866         /* Convert the response */
867         to_return = ((sig_result == 0) ? 0 : 1);
868
869 err:
870         if(acquired)
871                 release_context(hac);
872         if(ctx)
873                 {
874                 BN_CTX_end(ctx);
875                 BN_CTX_free(ctx);
876                 }
877         return to_return;
878         }
879 #endif
880
881 #ifndef OPENSSL_NO_DH
882 /* This function is aliased to mod_exp (with the dh and mont dropped). */
883 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
884                 const BIGNUM *a, const BIGNUM *p,
885                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
886         {
887         return cswift_mod_exp(r, a, p, m, ctx);
888         }
889 #endif
890
891 /* This stuff is needed if this ENGINE is being compiled into a self-contained
892  * shared-library. */
893 #ifdef ENGINE_DYNAMIC_SUPPORT
894 static int bind_fn(ENGINE *e, const char *id)
895         {
896         if(id && (strcmp(id, engine_cswift_id) != 0))
897                 return 0;
898         if(!bind_helper(e))
899                 return 0;
900         return 1;
901         }       
902 IMPLEMENT_DYNAMIC_CHECK_FN()
903 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
904 #endif /* ENGINE_DYNAMIC_SUPPORT */
905
906 #endif /* !OPENSSL_NO_HW_CSWIFT */
907 #endif /* !OPENSSL_NO_HW */