030d5a908c167e612da62b115189207ea6b36b59
[openssl.git] / crypto / engine / hw_atalla.c
1 /* crypto/engine/hw_atalla.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 <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_ATALLA
67
68 #ifdef FLAT_INC
69 #include "atalla.h"
70 #else
71 #include "vendor_defns/atalla.h"
72 #endif
73
74 static int atalla_destroy(ENGINE *e);
75 static int atalla_init(ENGINE *e);
76 static int atalla_finish(ENGINE *e);
77 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
78
79 /* BIGNUM stuff */
80 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
81                 const BIGNUM *m, BN_CTX *ctx);
82
83 #ifndef OPENSSL_NO_RSA
84 /* RSA stuff */
85 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
86 #endif
87 /* This function is aliased to mod_exp (with the mont stuff dropped). */
88 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
89                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
90
91 #ifndef OPENSSL_NO_DSA
92 /* DSA stuff */
93 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
94                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
95                 BN_CTX *ctx, BN_MONT_CTX *in_mont);
96 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
97                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
98                 BN_MONT_CTX *m_ctx);
99 #endif
100
101 #ifndef OPENSSL_NO_DH
102 /* DH stuff */
103 /* This function is alised to mod_exp (with the DH and mont dropped). */
104 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
105                 const BIGNUM *a, const BIGNUM *p,
106                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
107 #endif
108
109 /* The definitions for control commands specific to this engine */
110 #define ATALLA_CMD_SO_PATH              ENGINE_CMD_BASE
111 static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
112         {ATALLA_CMD_SO_PATH,
113                 "SO_PATH",
114                 "Specifies the path to the 'atasi' shared library",
115                 ENGINE_CMD_FLAG_STRING},
116         {0, NULL, NULL, 0}
117         };
118
119 #ifndef OPENSSL_NO_RSA
120 /* Our internal RSA_METHOD that we provide pointers to */
121 static RSA_METHOD atalla_rsa =
122         {
123         "Atalla RSA method",
124         NULL,
125         NULL,
126         NULL,
127         NULL,
128         atalla_rsa_mod_exp,
129         atalla_mod_exp_mont,
130         NULL,
131         NULL,
132         0,
133         NULL,
134         NULL,
135         NULL
136         };
137 #endif
138
139 #ifndef OPENSSL_NO_DSA
140 /* Our internal DSA_METHOD that we provide pointers to */
141 static DSA_METHOD atalla_dsa =
142         {
143         "Atalla DSA method",
144         NULL, /* dsa_do_sign */
145         NULL, /* dsa_sign_setup */
146         NULL, /* dsa_do_verify */
147         atalla_dsa_mod_exp, /* dsa_mod_exp */
148         atalla_mod_exp_dsa, /* bn_mod_exp */
149         NULL, /* init */
150         NULL, /* finish */
151         0, /* flags */
152         NULL /* app_data */
153         };
154 #endif
155
156 #ifndef OPENSSL_NO_DH
157 /* Our internal DH_METHOD that we provide pointers to */
158 static DH_METHOD atalla_dh =
159         {
160         "Atalla DH method",
161         NULL,
162         NULL,
163         atalla_mod_exp_dh,
164         NULL,
165         NULL,
166         0,
167         NULL
168         };
169 #endif
170
171 #ifndef OPENSSL_NO_ERR
172 /* Error function codes for use in atalla operation */
173 #define ATALLA_F_ATALLA_INIT                    100
174 #define ATALLA_F_ATALLA_FINISH                  101
175 #define ATALLA_F_ATALLA_CTRL                    102
176 #define ATALLA_F_ATALLA_MOD_EXP                 103
177 #define ATALLA_F_ATALLA_RSA_MOD_EXP             104
178 /* Error reason codes */
179 #define ATALLA_R_ALREADY_LOADED                 105
180 #define ATALLA_R_NOT_LOADED                     106
181 #define ATALLA_R_UNIT_FAILURE                   107
182 #define ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED   108
183 #define ATALLA_R_BN_CTX_FULL                    109
184 #define ATALLA_R_BN_EXPAND_FAIL                 110
185 #define ATALLA_R_REQUEST_FAILED                 111
186 #define ATALLA_R_MISSING_KEY_COMPONENTS         112
187 static ERR_STRING_DATA atalla_str_functs[] =
188         {
189         /* This first element is changed to match the dynamic 'lib' number */
190 {ERR_PACK(0,0,0),                               "atalla engine code"},
191 {ERR_PACK(0,ATALLA_F_ATALLA_INIT,0),            "atalla_init"},
192 {ERR_PACK(0,ATALLA_F_ATALLA_FINISH,0),          "atalla_finish"},
193 {ERR_PACK(0,ATALLA_F_ATALLA_CTRL,0),            "atalla_ctrl"},
194 {ERR_PACK(0,ATALLA_F_ATALLA_MOD_EXP,0),         "atalla_mod_exp"},
195 {ERR_PACK(0,ATALLA_F_ATALLA_RSA_MOD_EXP,0),"atalla_rsa_mod_exp"},
196 {ATALLA_R_ALREADY_LOADED                ,"already loaded"},
197 {ATALLA_R_UNIT_FAILURE                  ,"unit failure"},
198 {ATALLA_R_NOT_LOADED,                   "not loaded"},
199 {ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED  ,"control command not implemented"},
200 {ATALLA_R_BN_CTX_FULL                   ,"BN_CTX full"},
201 {ATALLA_R_BN_EXPAND_FAIL                ,"BN_expand failed"},
202 {ATALLA_R_REQUEST_FAILED                ,"request failed"},
203 {ATALLA_R_MISSING_KEY_COMPONENTS        ,"missing key components"},
204 {0,NULL}
205         };
206 /* The library number we obtain dynamically from the ERR code */
207 static int atalla_err_lib = -1;
208 #define ATALLAerr(f,r) ERR_PUT_error(atalla_err_lib,(f),(r),__FILE__,__LINE__)
209 static void atalla_load_error_strings(void)
210         {
211         if (atalla_err_lib < 0)
212                 {
213                 if((atalla_err_lib = ERR_get_next_error_library()) <= 0)
214                         return;
215                 atalla_str_functs[0].error = ERR_PACK(atalla_err_lib, 0, 0);
216                 ERR_load_strings(atalla_err_lib,atalla_str_functs);
217                 }
218         }
219 static void atalla_unload_error_strings(void)
220         {
221         if (atalla_err_lib >= 0)
222                 {
223                 ERR_unload_strings(atalla_err_lib,atalla_str_functs);
224                 atalla_err_lib = -1;
225                 }
226         }
227 #else
228 #define ATALLAerr(f,r)                                  /* NOP */
229 static void atalla_load_error_strings(void) { }         /* NOP */
230 static void atalla_unload_error_strings(void) { }       /* NOP */
231 #endif
232
233 /* Constants used when creating the ENGINE */
234 static const char *engine_atalla_id = "atalla";
235 static const char *engine_atalla_name = "Atalla hardware engine support";
236
237 /* This internal function is used by ENGINE_atalla() and possibly by the
238  * "dynamic" ENGINE support too */
239 static int bind_helper(ENGINE *e)
240         {
241 #ifndef OPENSSL_NO_RSA
242         const RSA_METHOD *meth1;
243 #endif
244 #ifndef OPENSSL_NO_DSA
245         const DSA_METHOD *meth2;
246 #endif
247 #ifndef OPENSSL_NO_DH
248         const DH_METHOD *meth3;
249 #endif
250         if(!ENGINE_set_id(e, engine_atalla_id) ||
251                         !ENGINE_set_name(e, engine_atalla_name) ||
252 #ifndef OPENSSL_NO_RSA
253                         !ENGINE_set_RSA(e, &atalla_rsa) ||
254 #endif
255 #ifndef OPENSSL_NO_DSA
256                         !ENGINE_set_DSA(e, &atalla_dsa) ||
257 #endif
258 #ifndef OPENSSL_NO_DH
259                         !ENGINE_set_DH(e, &atalla_dh) ||
260 #endif
261                         !ENGINE_set_destroy_function(e, atalla_destroy) ||
262                         !ENGINE_set_init_function(e, atalla_init) ||
263                         !ENGINE_set_finish_function(e, atalla_finish) ||
264                         !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
265                         !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
266                 return 0;
267
268 #ifndef OPENSSL_NO_RSA
269         /* We know that the "PKCS1_SSLeay()" functions hook properly
270          * to the atalla-specific mod_exp and mod_exp_crt so we use
271          * those functions. NB: We don't use ENGINE_openssl() or
272          * anything "more generic" because something like the RSAref
273          * code may not hook properly, and if you own one of these
274          * cards then you have the right to do RSA operations on it
275          * anyway! */ 
276         meth1 = RSA_PKCS1_SSLeay();
277         atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
278         atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
279         atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
280         atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
281 #endif
282
283 #ifndef OPENSSL_NO_DSA
284         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
285          * bits. */
286         meth2 = DSA_OpenSSL();
287         atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
288         atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
289         atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
290 #endif
291
292 #ifndef OPENSSL_NO_DH
293         /* Much the same for Diffie-Hellman */
294         meth3 = DH_OpenSSL();
295         atalla_dh.generate_key = meth3->generate_key;
296         atalla_dh.compute_key = meth3->compute_key;
297 #endif
298
299         /* Ensure the atalla error handling is set up */
300         atalla_load_error_strings();
301         return 1;
302         }
303
304 static ENGINE *engine_atalla(void)
305         {
306         ENGINE *ret = ENGINE_new();
307         if(!ret)
308                 return NULL;
309         if(!bind_helper(ret))
310                 {
311                 ENGINE_free(ret);
312                 return NULL;
313                 }
314         return ret;
315         }
316
317 void ENGINE_load_atalla(void)
318         {
319         /* Copied from eng_[openssl|dyn].c */
320         ENGINE *toadd = engine_atalla();
321         if(!toadd) return;
322         ENGINE_add(toadd);
323         ENGINE_free(toadd);
324         ERR_clear_error();
325         }
326
327 /* This is a process-global DSO handle used for loading and unloading
328  * the Atalla library. NB: This is only set (or unset) during an
329  * init() or finish() call (reference counts permitting) and they're
330  * operating with global locks, so this should be thread-safe
331  * implicitly. */
332 static DSO *atalla_dso = NULL;
333
334 /* These are the function pointers that are (un)set when the library has
335  * successfully (un)loaded. */
336 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
337 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
338 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
339
340 /* These are the static string constants for the DSO file name and the function
341  * symbol names to bind to. Regrettably, the DSO name on *nix appears to be
342  * "atasi.so" rather than something more consistent like "libatasi.so". At the
343  * time of writing, I'm not sure what the file name on win32 is but clearly
344  * native name translation is not possible (eg libatasi.so on *nix, and
345  * atasi.dll on win32). For the purposes of testing, I have created a symbollic
346  * link called "libatasi.so" so that we can use native name-translation - a
347  * better solution will be needed. */
348 static const char def_ATALLA_LIBNAME[] = "atasi";
349 static const char *ATALLA_LIBNAME = def_ATALLA_LIBNAME;
350 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
351 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
352 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
353
354 /* Destructor (complements the "ENGINE_atalla()" constructor) */
355 static int atalla_destroy(ENGINE *e)
356         {
357         /* Unload the atalla error strings so any error state including our
358          * functs or reasons won't lead to a segfault (they simply get displayed
359          * without corresponding string data because none will be found). */
360         atalla_unload_error_strings();
361         return 1;
362         }
363
364 /* (de)initialisation functions. */
365 static int atalla_init(ENGINE *e)
366         {
367         tfnASI_GetHardwareConfig *p1;
368         tfnASI_RSAPrivateKeyOpFn *p2;
369         tfnASI_GetPerformanceStatistics *p3;
370         /* Not sure of the origin of this magic value, but Ben's code had it
371          * and it seemed to have been working for a few people. :-) */
372         unsigned int config_buf[1024];
373
374         if(atalla_dso != NULL)
375                 {
376                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED);
377                 goto err;
378                 }
379         /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
380          * changed unfortunately because the Atalla drivers don't have
381          * standard library names that can be platform-translated well. */
382         /* TODO: Work out how to actually map to the names the Atalla
383          * drivers really use - for now a symbollic link needs to be
384          * created on the host system from libatasi.so to atasi.so on
385          * unix variants. */
386         atalla_dso = DSO_load(NULL, ATALLA_LIBNAME, NULL, 0);
387         if(atalla_dso == NULL)
388                 {
389                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
390                 goto err;
391                 }
392         if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
393                                 atalla_dso, ATALLA_F1)) ||
394                         !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
395                                 atalla_dso, ATALLA_F2)) ||
396                         !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
397                                 atalla_dso, ATALLA_F3)))
398                 {
399                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
400                 goto err;
401                 }
402         /* Copy the pointers */
403         p_Atalla_GetHardwareConfig = p1;
404         p_Atalla_RSAPrivateKeyOpFn = p2;
405         p_Atalla_GetPerformanceStatistics = p3;
406         /* Perform a basic test to see if there's actually any unit
407          * running. */
408         if(p1(0L, config_buf) != 0)
409                 {
410                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE);
411                 goto err;
412                 }
413         /* Everything's fine. */
414         return 1;
415 err:
416         if(atalla_dso)
417                 DSO_free(atalla_dso);
418         p_Atalla_GetHardwareConfig = NULL;
419         p_Atalla_RSAPrivateKeyOpFn = NULL;
420         p_Atalla_GetPerformanceStatistics = NULL;
421         return 0;
422         }
423
424 static int atalla_finish(ENGINE *e)
425         {
426         if(atalla_dso == NULL)
427                 {
428                 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED);
429                 return 0;
430                 }
431         if(!DSO_free(atalla_dso))
432                 {
433                 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE);
434                 return 0;
435                 }
436         atalla_dso = NULL;
437         p_Atalla_GetHardwareConfig = NULL;
438         p_Atalla_RSAPrivateKeyOpFn = NULL;
439         p_Atalla_GetPerformanceStatistics = NULL;
440         return 1;
441         }
442
443 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
444         {
445         int initialised = ((atalla_dso == NULL) ? 0 : 1);
446         switch(cmd)
447                 {
448         case ATALLA_CMD_SO_PATH:
449                 if(p == NULL)
450                         {
451                         ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER);
452                         return 0;
453                         }
454                 if(initialised)
455                         {
456                         ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED);
457                         return 0;
458                         }
459                 ATALLA_LIBNAME = (const char *)p;
460                 return 1;
461         default:
462                 break;
463                 }
464         ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
465         return 0;
466         }
467
468 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
469                         const BIGNUM *m, BN_CTX *ctx)
470         {
471         /* I need somewhere to store temporary serialised values for
472          * use with the Atalla API calls. A neat cheat - I'll use
473          * BIGNUMs from the BN_CTX but access their arrays directly as
474          * byte arrays <grin>. This way I don't have to clean anything
475          * up. */
476         BIGNUM *modulus;
477         BIGNUM *exponent;
478         BIGNUM *argument;
479         BIGNUM *result;
480         RSAPrivateKey keydata;
481         int to_return, numbytes;
482
483         modulus = exponent = argument = result = NULL;
484         to_return = 0; /* expect failure */
485
486         if(!atalla_dso)
487                 {
488                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);
489                 goto err;
490                 }
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                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);
500                 goto err;
501                 }
502         if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
503            !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
504                 {
505                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);
506                 goto err;
507                 }
508         /* Prepare the key-data */
509         memset(&keydata, 0,sizeof keydata);
510         numbytes = BN_num_bytes(m);
511         memset(exponent->d, 0, numbytes);
512         memset(modulus->d, 0, numbytes);
513         BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
514         BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
515         keydata.privateExponent.data = (unsigned char *)exponent->d;
516         keydata.privateExponent.len = numbytes;
517         keydata.modulus.data = (unsigned char *)modulus->d;
518         keydata.modulus.len = numbytes;
519         /* Prepare the argument */
520         memset(argument->d, 0, numbytes);
521         memset(result->d, 0, numbytes);
522         BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
523         /* Perform the operation */
524         if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
525                         (unsigned char *)argument->d,
526                         keydata.modulus.len) != 0)
527                 {
528                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);
529                 goto err;
530                 }
531         /* Convert the response */
532         BN_bin2bn((unsigned char *)result->d, numbytes, r);
533         to_return = 1;
534 err:
535         BN_CTX_end(ctx);
536         return to_return;
537         }
538
539 #ifndef OPENSSL_NO_RSA
540 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
541         {
542         BN_CTX *ctx = NULL;
543         int to_return = 0;
544
545         if(!atalla_dso)
546                 {
547                 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED);
548                 goto err;
549                 }
550         if((ctx = BN_CTX_new()) == NULL)
551                 goto err;
552         if(!rsa->d || !rsa->n)
553                 {
554                 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS);
555                 goto err;
556                 }
557         to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
558 err:
559         if(ctx)
560                 BN_CTX_free(ctx);
561         return to_return;
562         }
563 #endif
564
565 #ifndef OPENSSL_NO_DSA
566 /* This code was liberated and adapted from the commented-out code in
567  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
568  * (it doesn't have a CRT form for RSA), this function means that an
569  * Atalla system running with a DSA server certificate can handshake
570  * around 5 or 6 times faster/more than an equivalent system running with
571  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
572  * of "openssl speed -engine atalla dsa1024 rsa1024". */
573 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
574                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
575                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
576         {
577         BIGNUM t;
578         int to_return = 0;
579  
580         BN_init(&t);
581         /* let rr = a1 ^ p1 mod m */
582         if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
583         /* let t = a2 ^ p2 mod m */
584         if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
585         /* let rr = rr * t mod m */
586         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
587         to_return = 1;
588 end:
589         BN_free(&t);
590         return to_return;
591         }
592
593 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
594                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
595                 BN_MONT_CTX *m_ctx)
596         {
597         return atalla_mod_exp(r, a, p, m, ctx);
598         }
599 #endif
600
601 /* This function is aliased to mod_exp (with the mont stuff dropped). */
602 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
603                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
604         {
605         return atalla_mod_exp(r, a, p, m, ctx);
606         }
607
608 #ifndef OPENSSL_NO_DH
609 /* This function is aliased to mod_exp (with the dh and mont dropped). */
610 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
611                 const BIGNUM *a, const BIGNUM *p,
612                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
613         {
614         return atalla_mod_exp(r, a, p, m, ctx);
615         }
616 #endif
617
618 /* This stuff is needed if this ENGINE is being compiled into a self-contained
619  * shared-library. */
620 #ifdef ENGINE_DYNAMIC_SUPPORT
621 static int bind_fn(ENGINE *e, const char *id)
622         {
623         if(id && (strcmp(id, engine_atalla_id) != 0))
624                 return 0;
625         if(!bind_helper(e))
626                 return 0;
627         return 1;
628         }
629 IMPLEMENT_DYNAMIC_CHECK_FN()
630 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
631 #endif /* ENGINE_DYNAMIC_SUPPORT */
632
633 #endif /* !OPENSSL_NO_HW_ATALLA */
634 #endif /* !OPENSSL_NO_HW */