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