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