Some more tweaks to ENGINE code.
[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 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 static int cswift_init(ENGINE *);
87 static int cswift_finish(ENGINE *);
88
89 /* BIGNUM stuff */
90 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
91                 const BIGNUM *m, BN_CTX *ctx);
92 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
93                 const BIGNUM *q, const BIGNUM *dmp1, const BIGNUM *dmq1,
94                 const BIGNUM *iqmp, BN_CTX *ctx);
95
96 /* RSA stuff */
97 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
98 /* This function is aliased to mod_exp (with the mont stuff dropped). */
99 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
100                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
101
102 /* DSA stuff */
103 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa);
104 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
105                                 DSA_SIG *sig, DSA *dsa);
106
107 /* DH stuff */
108 /* This function is alised to mod_exp (with the DH and mont dropped). */
109 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
110                 const BIGNUM *a, const BIGNUM *p,
111                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
112
113
114 /* Our internal RSA_METHOD that we provide pointers to */
115 static RSA_METHOD cswift_rsa =
116         {
117         "CryptoSwift RSA method",
118         NULL,
119         NULL,
120         NULL,
121         NULL,
122         cswift_rsa_mod_exp,
123         cswift_mod_exp_mont,
124         NULL,
125         NULL,
126         0,
127         NULL,
128         NULL,
129         NULL
130         };
131
132 /* Our internal DSA_METHOD that we provide pointers to */
133 static DSA_METHOD cswift_dsa =
134         {
135         "CryptoSwift DSA method",
136         cswift_dsa_sign,
137         NULL, /* dsa_sign_setup */
138         cswift_dsa_verify,
139         NULL, /* dsa_mod_exp */
140         NULL, /* bn_mod_exp */
141         NULL, /* init */
142         NULL, /* finish */
143         0, /* flags */
144         NULL /* app_data */
145         };
146
147 /* Our internal DH_METHOD that we provide pointers to */
148 static DH_METHOD cswift_dh =
149         {
150         "CryptoSwift DH method",
151         NULL,
152         NULL,
153         cswift_mod_exp_dh,
154         NULL,
155         NULL,
156         0,
157         NULL
158         };
159
160 /* Constants used when creating the ENGINE */
161 static const char *engine_cswift_id = "cswift";
162 static const char *engine_cswift_name = "CryptoSwift hardware engine support";
163
164 /* As this is only ever called once, there's no need for locking
165  * (indeed - the lock will already be held by our caller!!!) */
166 ENGINE *ENGINE_cswift()
167         {
168         const RSA_METHOD *meth1;
169         const DH_METHOD *meth2;
170         ENGINE *ret = ENGINE_new();
171         if(!ret)
172                 return NULL;
173         if(!ENGINE_set_id(ret, engine_cswift_id) ||
174                         !ENGINE_set_name(ret, engine_cswift_name) ||
175                         !ENGINE_set_RSA(ret, &cswift_rsa) ||
176                         !ENGINE_set_DSA(ret, &cswift_dsa) ||
177                         !ENGINE_set_DH(ret, &cswift_dh) ||
178                         !ENGINE_set_BN_mod_exp(ret, &cswift_mod_exp) ||
179                         !ENGINE_set_BN_mod_exp_crt(ret, &cswift_mod_exp_crt) ||
180                         !ENGINE_set_init_function(ret, cswift_init) ||
181                         !ENGINE_set_finish_function(ret, cswift_finish))
182                 {
183                 ENGINE_free(ret);
184                 return NULL;
185                 }
186
187         /* We know that the "PKCS1_SSLeay()" functions hook properly
188          * to the cswift-specific mod_exp and mod_exp_crt so we use
189          * those functions. NB: We don't use ENGINE_openssl() or
190          * anything "more generic" because something like the RSAref
191          * code may not hook properly, and if you own one of these
192          * cards then you have the right to do RSA operations on it
193          * anyway! */ 
194         meth1 = RSA_PKCS1_SSLeay();
195         cswift_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
196         cswift_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
197         cswift_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
198         cswift_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
199
200         /* Much the same for Diffie-Hellman */
201         meth2 = DH_OpenSSL();
202         cswift_dh.generate_key = meth2->generate_key;
203         cswift_dh.compute_key = meth2->compute_key;
204         return ret;
205         }
206
207 /* This is a process-global DSO handle used for loading and unloading
208  * the CryptoSwift library. NB: This is only set (or unset) during an
209  * init() or finish() call (reference counts permitting) and they're
210  * operating with global locks, so this should be thread-safe
211  * implicitly. */
212 static DSO *cswift_dso = NULL;
213
214 /* These are the function pointers that are (un)set when the library has
215  * successfully (un)loaded. */
216 t_swAcquireAccContext *p_CSwift_AcquireAccContext = NULL;
217 t_swAttachKeyParam *p_CSwift_AttachKeyParam = NULL;
218 t_swSimpleRequest *p_CSwift_SimpleRequest = NULL;
219 t_swReleaseAccContext *p_CSwift_ReleaseAccContext = NULL;
220
221 /* Used in the DSO operations. */
222 static const char *CSWIFT_LIBNAME = "swift";
223 static const char *CSWIFT_F1 = "swAcquireAccContext";
224 static const char *CSWIFT_F2 = "swAttachKeyParam";
225 static const char *CSWIFT_F3 = "swSimpleRequest";
226 static const char *CSWIFT_F4 = "swReleaseAccContext";
227
228
229 /* CryptoSwift library functions and mechanics - these are used by the
230  * higher-level functions further down. NB: As and where there's no
231  * error checking, take a look lower down where these functions are
232  * called, the checking and error handling is probably down there. */
233
234 /* utility function to obtain a context */
235 static int get_context(SW_CONTEXT_HANDLE *hac)
236         {
237         SW_STATUS status;
238  
239         status = p_CSwift_AcquireAccContext(hac);
240         if(status != SW_OK)
241                 return 0;
242         return 1;
243         }
244  
245 /* similarly to release one. */
246 static void release_context(SW_CONTEXT_HANDLE hac)
247         {
248         p_CSwift_ReleaseAccContext(hac);
249         }
250
251 /* (de)initialisation functions. */
252 static int cswift_init(ENGINE *e)
253         {
254         SW_CONTEXT_HANDLE hac;
255         t_swAcquireAccContext *p1;
256         t_swAttachKeyParam *p2;
257         t_swSimpleRequest *p3;
258         t_swReleaseAccContext *p4;
259
260         if(cswift_dso != NULL)
261                 {
262                 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_ALREADY_LOADED);
263                 goto err;
264                 }
265         /* Attempt to load libswift.so/swift.dll/whatever. */
266         cswift_dso = DSO_load(NULL, CSWIFT_LIBNAME, NULL, 0);
267         if(cswift_dso == NULL)
268                 {
269                 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
270                 goto err;
271                 }
272         if(!(p1 = (t_swAcquireAccContext *)
273                                 DSO_bind_func(cswift_dso, CSWIFT_F1)) ||
274                         !(p2 = (t_swAttachKeyParam *)
275                                 DSO_bind_func(cswift_dso, CSWIFT_F2)) ||
276                         !(p3 = (t_swSimpleRequest *)
277                                 DSO_bind_func(cswift_dso, CSWIFT_F3)) ||
278                         !(p4 = (t_swReleaseAccContext *)
279                                 DSO_bind_func(cswift_dso, CSWIFT_F4)))
280                 {
281                 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_DSO_FAILURE);
282                 goto err;
283                 }
284         /* Copy the pointers */
285         p_CSwift_AcquireAccContext = p1;
286         p_CSwift_AttachKeyParam = p2;
287         p_CSwift_SimpleRequest = p3;
288         p_CSwift_ReleaseAccContext = p4;
289         /* Try and get a context - if not, we may have a DSO but no
290          * accelerator! */
291         if(!get_context(&hac))
292                 {
293                 ENGINEerr(ENGINE_F_CSWIFT_INIT,ENGINE_R_UNIT_FAILURE);
294                 goto err;
295                 }
296         release_context(hac);
297         /* Everything's fine. */
298         return 1;
299 err:
300         if(cswift_dso)
301                 DSO_free(cswift_dso);
302         p_CSwift_AcquireAccContext = NULL;
303         p_CSwift_AttachKeyParam = NULL;
304         p_CSwift_SimpleRequest = NULL;
305         p_CSwift_ReleaseAccContext = NULL;
306         return 0;
307         }
308
309 static int cswift_finish(ENGINE *e)
310         {
311         if(cswift_dso == NULL)
312                 {
313                 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_NOT_LOADED);
314                 return 0;
315                 }
316         if(!DSO_free(cswift_dso))
317                 {
318                 ENGINEerr(ENGINE_F_CSWIFT_FINISH,ENGINE_R_DSO_FAILURE);
319                 return 0;
320                 }
321         cswift_dso = NULL;
322         p_CSwift_AcquireAccContext = NULL;
323         p_CSwift_AttachKeyParam = NULL;
324         p_CSwift_SimpleRequest = NULL;
325         p_CSwift_ReleaseAccContext = NULL;
326         return 1;
327         }
328
329 /* Un petit mod_exp */
330 static int cswift_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
331                         const BIGNUM *m, BN_CTX *ctx)
332         {
333         /* I need somewhere to store temporary serialised values for
334          * use with the CryptoSwift API calls. A neat cheat - I'll use
335          * BIGNUMs from the BN_CTX but access their arrays directly as
336          * byte arrays <grin>. This way I don't have to clean anything
337          * up. */
338         BIGNUM *modulus;
339         BIGNUM *exponent;
340         BIGNUM *argument;
341         BIGNUM *result;
342         SW_STATUS sw_status;
343         SW_LARGENUMBER arg, res;
344         SW_PARAM sw_param;
345         SW_CONTEXT_HANDLE hac;
346         int to_return, acquired;
347  
348         modulus = exponent = argument = result = NULL;
349         to_return = 0; /* expect failure */
350         acquired = 0;
351  
352         if(!get_context(&hac))
353                 {
354                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_GET_HANDLE_FAILED);
355                 goto err;
356                 }
357         acquired = 1;
358         /* Prepare the params */
359         BN_CTX_start(ctx);
360         modulus = BN_CTX_get(ctx);
361         exponent = BN_CTX_get(ctx);
362         argument = BN_CTX_get(ctx);
363         result = BN_CTX_get(ctx);
364         if(!result)
365                 {
366                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_CTX_FULL);
367                 goto err;
368                 }
369         if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, p->top) ||
370                 !bn_wexpand(argument, a->top) || !bn_wexpand(result, m->top))
371                 {
372                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_BN_EXPAND_FAIL);
373                 goto err;
374                 }
375         sw_param.type = SW_ALG_EXP;
376         sw_param.up.exp.modulus.nbytes = BN_bn2bin(m,
377                 (unsigned char *)modulus->d);
378         sw_param.up.exp.modulus.value = (unsigned char *)modulus->d;
379         sw_param.up.exp.exponent.nbytes = BN_bn2bin(p,
380                 (unsigned char *)exponent->d);
381         sw_param.up.exp.exponent.value = (unsigned char *)exponent->d;
382         /* Attach the key params */
383         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
384         switch(sw_status)
385                 {
386         case SW_OK:
387                 break;
388         case SW_ERR_INPUT_SIZE:
389                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,
390                         ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
391                 goto err;
392         default:
393                 {
394                 char tmpbuf[20];
395                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_REQUEST_FAILED);
396                 sprintf(tmpbuf, "%ld", sw_status);
397                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
398                 }
399                 goto err;
400                 }
401         /* Prepare the argument and response */
402         arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
403         arg.value = (unsigned char *)argument->d;
404         res.nbytes = BN_num_bytes(m);
405         memset(result->d, 0, res.nbytes);
406         res.value = (unsigned char *)result->d;
407         /* Perform the operation */
408         if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP, &arg, 1,
409                 &res, 1)) != SW_OK)
410                 {
411                 char tmpbuf[20];
412                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP,ENGINE_R_REQUEST_FAILED);
413                 sprintf(tmpbuf, "%ld", sw_status);
414                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
415                 goto err;
416                 }
417         /* Convert the response */
418         BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
419         to_return = 1;
420 err:
421         if(acquired)
422                 release_context(hac);
423         BN_CTX_end(ctx);
424         return to_return;
425         }
426
427 /* Un petit mod_exp chinois */
428 static int cswift_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
429                         const BIGNUM *q, const BIGNUM *dmp1,
430                         const BIGNUM *dmq1, const BIGNUM *iqmp, BN_CTX *ctx)
431         {
432         SW_STATUS sw_status;
433         SW_LARGENUMBER arg, res;
434         SW_PARAM sw_param;
435         SW_CONTEXT_HANDLE hac;
436         BIGNUM *rsa_p = NULL;
437         BIGNUM *rsa_q = NULL;
438         BIGNUM *rsa_dmp1 = NULL;
439         BIGNUM *rsa_dmq1 = NULL;
440         BIGNUM *rsa_iqmp = NULL;
441         BIGNUM *argument = NULL;
442         BIGNUM *result = NULL;
443         int to_return = 0; /* expect failure */
444         int acquired = 0;
445  
446         if(!get_context(&hac))
447                 {
448                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_GET_HANDLE_FAILED);
449                 goto err;
450                 }
451         acquired = 1;
452         /* Prepare the params */
453         BN_CTX_start(ctx);
454         rsa_p = BN_CTX_get(ctx);
455         rsa_q = BN_CTX_get(ctx);
456         rsa_dmp1 = BN_CTX_get(ctx);
457         rsa_dmq1 = BN_CTX_get(ctx);
458         rsa_iqmp = BN_CTX_get(ctx);
459         argument = BN_CTX_get(ctx);
460         result = BN_CTX_get(ctx);
461         if(!result)
462                 {
463                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_CTX_FULL);
464                 goto err;
465                 }
466         if(!bn_wexpand(rsa_p, p->top) || !bn_wexpand(rsa_q, q->top) ||
467                         !bn_wexpand(rsa_dmp1, dmp1->top) ||
468                         !bn_wexpand(rsa_dmq1, dmq1->top) ||
469                         !bn_wexpand(rsa_iqmp, iqmp->top) ||
470                         !bn_wexpand(argument, a->top) ||
471                         !bn_wexpand(result, p->top + q->top))
472                 {
473                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_BN_EXPAND_FAIL);
474                 goto err;
475                 }
476         sw_param.type = SW_ALG_CRT;
477         sw_param.up.crt.p.nbytes = BN_bn2bin(p, (unsigned char *)rsa_p->d);
478         sw_param.up.crt.p.value = (unsigned char *)rsa_p->d;
479         sw_param.up.crt.q.nbytes = BN_bn2bin(q, (unsigned char *)rsa_q->d);
480         sw_param.up.crt.q.value = (unsigned char *)rsa_q->d;
481         sw_param.up.crt.dmp1.nbytes = BN_bn2bin(dmp1,
482                 (unsigned char *)rsa_dmp1->d);
483         sw_param.up.crt.dmp1.value = (unsigned char *)rsa_dmp1->d;
484         sw_param.up.crt.dmq1.nbytes = BN_bn2bin(dmq1,
485                 (unsigned char *)rsa_dmq1->d);
486         sw_param.up.crt.dmq1.value = (unsigned char *)rsa_dmq1->d;
487         sw_param.up.crt.iqmp.nbytes = BN_bn2bin(iqmp,
488                 (unsigned char *)rsa_iqmp->d);
489         sw_param.up.crt.iqmp.value = (unsigned char *)rsa_iqmp->d;
490         /* Attach the key params */
491         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
492         switch(sw_status)
493                 {
494         case SW_OK:
495                 break;
496         case SW_ERR_INPUT_SIZE:
497                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,
498                         ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
499                 goto err;
500         default:
501                 {
502                 char tmpbuf[20];
503                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
504                 sprintf(tmpbuf, "%ld", sw_status);
505                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
506                 }
507                 goto err;
508                 }
509         /* Prepare the argument and response */
510         arg.nbytes = BN_bn2bin(a, (unsigned char *)argument->d);
511         arg.value = (unsigned char *)argument->d;
512         res.nbytes = 2 * BN_num_bytes(p);
513         memset(result->d, 0, res.nbytes);
514         res.value = (unsigned char *)result->d;
515         /* Perform the operation */
516         if((sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_MODEXP_CRT, &arg, 1,
517                 &res, 1)) != SW_OK)
518                 {
519                 char tmpbuf[20];
520                 ENGINEerr(ENGINE_F_CSWIFT_MOD_EXP_CRT,ENGINE_R_REQUEST_FAILED);
521                 sprintf(tmpbuf, "%ld", sw_status);
522                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
523                 goto err;
524                 }
525         /* Convert the response */
526         BN_bin2bn((unsigned char *)result->d, res.nbytes, r);
527         to_return = 1;
528 err:
529         if(acquired)
530                 release_context(hac);
531         BN_CTX_end(ctx);
532         return to_return;
533         }
534  
535 static int cswift_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
536         {
537         BN_CTX *ctx;
538         int to_return = 0;
539
540         if((ctx = BN_CTX_new()) == NULL)
541                 goto err;
542         if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
543                 {
544                 ENGINEerr(ENGINE_F_CSWIFT_RSA_MOD_EXP,ENGINE_R_MISSING_KEY_COMPONENTS);
545                 goto err;
546                 }
547         to_return = cswift_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
548                 rsa->dmq1, rsa->iqmp, ctx);
549 err:
550         if(ctx)
551                 BN_CTX_free(ctx);
552         return to_return;
553         }
554
555 /* This function is aliased to mod_exp (with the mont stuff dropped). */
556 static int cswift_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
557                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
558         {
559         return cswift_mod_exp(r, a, p, m, ctx);
560         }
561
562 static DSA_SIG *cswift_dsa_sign(const unsigned char *dgst, int dlen, DSA *dsa)
563         {
564         SW_CONTEXT_HANDLE hac;
565         SW_PARAM sw_param;
566         SW_STATUS sw_status;
567         SW_LARGENUMBER arg, res;
568         unsigned char *ptr;
569         BN_CTX *ctx;
570         BIGNUM *dsa_p = NULL;
571         BIGNUM *dsa_q = NULL;
572         BIGNUM *dsa_g = NULL;
573         BIGNUM *dsa_key = NULL;
574         BIGNUM *result = NULL;
575         DSA_SIG *to_return = NULL;
576         int acquired = 0;
577
578         if((ctx = BN_CTX_new()) == NULL)
579                 goto err;
580         if(!get_context(&hac))
581                 {
582                 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_GET_HANDLE_FAILED);
583                 goto err;
584                 }
585         acquired = 1;
586         /* Prepare the params */
587         BN_CTX_start(ctx);
588         dsa_p = BN_CTX_get(ctx);
589         dsa_q = BN_CTX_get(ctx);
590         dsa_g = BN_CTX_get(ctx);
591         dsa_key = BN_CTX_get(ctx);
592         result = BN_CTX_get(ctx);
593         if(!result)
594                 {
595                 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_CTX_FULL);
596                 goto err;
597                 }
598         if(!bn_wexpand(dsa_p, dsa->p->top) ||
599                         !bn_wexpand(dsa_q, dsa->q->top) ||
600                         !bn_wexpand(dsa_g, dsa->g->top) ||
601                         !bn_wexpand(dsa_key, dsa->priv_key->top) ||
602                         !bn_wexpand(result, dsa->p->top))
603                 {
604                 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_BN_EXPAND_FAIL);
605                 goto err;
606                 }
607         sw_param.type = SW_ALG_DSA;
608         sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
609                                 (unsigned char *)dsa_p->d);
610         sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
611         sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
612                                 (unsigned char *)dsa_q->d);
613         sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
614         sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
615                                 (unsigned char *)dsa_g->d);
616         sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
617         sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->priv_key,
618                                 (unsigned char *)dsa_key->d);
619         sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
620         /* Attach the key params */
621         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
622         switch(sw_status)
623                 {
624         case SW_OK:
625                 break;
626         case SW_ERR_INPUT_SIZE:
627                 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,
628                         ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
629                 goto err;
630         default:
631                 {
632                 char tmpbuf[20];
633                 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
634                 sprintf(tmpbuf, "%ld", sw_status);
635                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
636                 }
637                 goto err;
638                 }
639         /* Prepare the argument and response */
640         arg.nbytes = dlen;
641         arg.value = (unsigned char *)dgst;
642         res.nbytes = BN_num_bytes(dsa->p);
643         memset(result->d, 0, res.nbytes);
644         res.value = (unsigned char *)result->d;
645         /* Perform the operation */
646         sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_SIGN, &arg, 1,
647                 &res, 1);
648         if(sw_status != SW_OK)
649                 {
650                 char tmpbuf[20];
651                 ENGINEerr(ENGINE_F_CSWIFT_DSA_SIGN,ENGINE_R_REQUEST_FAILED);
652                 sprintf(tmpbuf, "%ld", sw_status);
653                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
654                 goto err;
655                 }
656         /* Convert the response */
657         ptr = (unsigned char *)result->d;
658         if((to_return = DSA_SIG_new()) == NULL)
659                 goto err;
660         to_return->r = BN_bin2bn((unsigned char *)result->d, 20, NULL);
661         to_return->s = BN_bin2bn((unsigned char *)result->d + 20, 20, NULL);
662
663 err:
664         if(acquired)
665                 release_context(hac);
666         if(ctx)
667                 {
668                 BN_CTX_end(ctx);
669                 BN_CTX_free(ctx);
670                 }
671         return to_return;
672         }
673
674 static int cswift_dsa_verify(const unsigned char *dgst, int dgst_len,
675                                 DSA_SIG *sig, DSA *dsa)
676         {
677         SW_CONTEXT_HANDLE hac;
678         SW_PARAM sw_param;
679         SW_STATUS sw_status;
680         SW_LARGENUMBER arg[2], res;
681         unsigned long sig_result;
682         BN_CTX *ctx;
683         BIGNUM *dsa_p = NULL;
684         BIGNUM *dsa_q = NULL;
685         BIGNUM *dsa_g = NULL;
686         BIGNUM *dsa_key = NULL;
687         BIGNUM *argument = NULL;
688         int to_return = -1;
689         int acquired = 0;
690
691         if((ctx = BN_CTX_new()) == NULL)
692                 goto err;
693         if(!get_context(&hac))
694                 {
695                 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_GET_HANDLE_FAILED);
696                 goto err;
697                 }
698         acquired = 1;
699         /* Prepare the params */
700         BN_CTX_start(ctx);
701         dsa_p = BN_CTX_get(ctx);
702         dsa_q = BN_CTX_get(ctx);
703         dsa_g = BN_CTX_get(ctx);
704         dsa_key = BN_CTX_get(ctx);
705         argument = BN_CTX_get(ctx);
706         if(!argument)
707                 {
708                 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_CTX_FULL);
709                 goto err;
710                 }
711         if(!bn_wexpand(dsa_p, dsa->p->top) ||
712                         !bn_wexpand(dsa_q, dsa->q->top) ||
713                         !bn_wexpand(dsa_g, dsa->g->top) ||
714                         !bn_wexpand(dsa_key, dsa->pub_key->top) ||
715                         !bn_wexpand(argument, 40))
716                 {
717                 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_BN_EXPAND_FAIL);
718                 goto err;
719                 }
720         sw_param.type = SW_ALG_DSA;
721         sw_param.up.dsa.p.nbytes = BN_bn2bin(dsa->p,
722                                 (unsigned char *)dsa_p->d);
723         sw_param.up.dsa.p.value = (unsigned char *)dsa_p->d;
724         sw_param.up.dsa.q.nbytes = BN_bn2bin(dsa->q,
725                                 (unsigned char *)dsa_q->d);
726         sw_param.up.dsa.q.value = (unsigned char *)dsa_q->d;
727         sw_param.up.dsa.g.nbytes = BN_bn2bin(dsa->g,
728                                 (unsigned char *)dsa_g->d);
729         sw_param.up.dsa.g.value = (unsigned char *)dsa_g->d;
730         sw_param.up.dsa.key.nbytes = BN_bn2bin(dsa->pub_key,
731                                 (unsigned char *)dsa_key->d);
732         sw_param.up.dsa.key.value = (unsigned char *)dsa_key->d;
733         /* Attach the key params */
734         sw_status = p_CSwift_AttachKeyParam(hac, &sw_param);
735         switch(sw_status)
736                 {
737         case SW_OK:
738                 break;
739         case SW_ERR_INPUT_SIZE:
740                 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,
741                         ENGINE_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
742                 goto err;
743         default:
744                 {
745                 char tmpbuf[20];
746                 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
747                 sprintf(tmpbuf, "%ld", sw_status);
748                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
749                 }
750                 goto err;
751                 }
752         /* Prepare the argument and response */
753         arg[0].nbytes = dgst_len;
754         arg[0].value = (unsigned char *)dgst;
755         arg[1].nbytes = 40;
756         arg[1].value = (unsigned char *)argument->d;
757         memset(arg[1].value, 0, 40);
758         BN_bn2bin(sig->r, arg[1].value + 20 - BN_num_bytes(sig->r));
759         BN_bn2bin(sig->s, arg[1].value + 40 - BN_num_bytes(sig->s));
760         res.nbytes = 4; /* unsigned long */
761         res.value = (unsigned char *)(&sig_result);
762         /* Perform the operation */
763         sw_status = p_CSwift_SimpleRequest(hac, SW_CMD_DSS_VERIFY, arg, 2,
764                 &res, 1);
765         if(sw_status != SW_OK)
766                 {
767                 char tmpbuf[20];
768                 ENGINEerr(ENGINE_F_CSWIFT_DSA_VERIFY,ENGINE_R_REQUEST_FAILED);
769                 sprintf(tmpbuf, "%ld", sw_status);
770                 ERR_add_error_data(2, "CryptoSwift error number is ",tmpbuf);
771                 goto err;
772                 }
773         /* Convert the response */
774         to_return = ((sig_result == 0) ? 0 : 1);
775
776 err:
777         if(acquired)
778                 release_context(hac);
779         if(ctx)
780                 {
781                 BN_CTX_end(ctx);
782                 BN_CTX_free(ctx);
783                 }
784         return to_return;
785         }
786
787 /* This function is aliased to mod_exp (with the dh and mont dropped). */
788 static int cswift_mod_exp_dh(const DH *dh, BIGNUM *r,
789                 const BIGNUM *a, const BIGNUM *p,
790                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
791         {
792         return cswift_mod_exp(r, a, p, m, ctx);
793         }
794
795 #endif /* !OPENSSL_NO_HW_CSWIFT */
796 #endif /* !OPENSSL_NO_HW */