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