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