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