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