Update documentation with Diffie-Hellman best practices.
[openssl.git] / engines / e_atalla.c
1 /* crypto/engine/hw_atalla.c */
2 /*
3  * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4  * 2000.
5  */
6 /* ====================================================================
7  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  *    notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  *    notice, this list of conditions and the following disclaimer in
18  *    the documentation and/or other materials provided with the
19  *    distribution.
20  *
21  * 3. All advertising materials mentioning features or use of this
22  *    software must display the following acknowledgment:
23  *    "This product includes software developed by the OpenSSL Project
24  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
25  *
26  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27  *    endorse or promote products derived from this software without
28  *    prior written permission. For written permission, please contact
29  *    licensing@OpenSSL.org.
30  *
31  * 5. Products derived from this software may not be called "OpenSSL"
32  *    nor may "OpenSSL" appear in their names without prior written
33  *    permission of the OpenSSL Project.
34  *
35  * 6. Redistributions of any form whatsoever must retain the following
36  *    acknowledgment:
37  *    "This product includes software developed by the OpenSSL Project
38  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
39  *
40  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
44  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51  * OF THE POSSIBILITY OF SUCH DAMAGE.
52  * ====================================================================
53  *
54  * This product includes cryptographic software written by Eric Young
55  * (eay@cryptsoft.com).  This product includes software written by Tim
56  * Hudson (tjh@cryptsoft.com).
57  *
58  */
59
60 #include <stdio.h>
61 #include <string.h>
62 #include <openssl/crypto.h>
63 #include <openssl/buffer.h>
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_RSA
67 # include <openssl/rsa.h>
68 #endif
69 #ifndef OPENSSL_NO_DSA
70 # include <openssl/dsa.h>
71 #endif
72 #ifndef OPENSSL_NO_DH
73 # include <openssl/dh.h>
74 #endif
75 #include <openssl/bn.h>
76
77 #ifndef OPENSSL_NO_HW
78 # ifndef OPENSSL_NO_HW_ATALLA
79
80 #  ifdef FLAT_INC
81 #   include "atalla.h"
82 #  else
83 #   include "vendor_defns/atalla.h"
84 #  endif
85
86 #  define ATALLA_LIB_NAME "atalla engine"
87 #  include "e_atalla_err.c"
88
89 static int atalla_destroy(ENGINE *e);
90 static int atalla_init(ENGINE *e);
91 static int atalla_finish(ENGINE *e);
92 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void));
93
94 /* BIGNUM stuff */
95 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
96                           const BIGNUM *m, BN_CTX *ctx);
97
98 #  ifndef OPENSSL_NO_RSA
99 /* RSA stuff */
100 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
101                               BN_CTX *ctx);
102 /* This function is aliased to mod_exp (with the mont stuff dropped). */
103 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
104                                const BIGNUM *m, BN_CTX *ctx,
105                                BN_MONT_CTX *m_ctx);
106 #  endif
107
108 #  ifndef OPENSSL_NO_DSA
109 /* DSA stuff */
110 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
111                               BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
112                               BN_CTX *ctx, BN_MONT_CTX *in_mont);
113 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
114                               const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
115                               BN_MONT_CTX *m_ctx);
116 #  endif
117
118 #  ifndef OPENSSL_NO_DH
119 /* DH stuff */
120 /* This function is alised to mod_exp (with the DH and mont dropped). */
121 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
122                              const BIGNUM *a, const BIGNUM *p,
123                              const BIGNUM *m, BN_CTX *ctx,
124                              BN_MONT_CTX *m_ctx);
125 #  endif
126
127 /* The definitions for control commands specific to this engine */
128 #  define ATALLA_CMD_SO_PATH              ENGINE_CMD_BASE
129 static const ENGINE_CMD_DEFN atalla_cmd_defns[] = {
130     {ATALLA_CMD_SO_PATH,
131      "SO_PATH",
132      "Specifies the path to the 'atasi' shared library",
133      ENGINE_CMD_FLAG_STRING},
134     {0, NULL, NULL, 0}
135 };
136
137 #  ifndef OPENSSL_NO_RSA
138 /* Our internal RSA_METHOD that we provide pointers to */
139 static RSA_METHOD atalla_rsa = {
140     "Atalla RSA method",
141     NULL,
142     NULL,
143     NULL,
144     NULL,
145     atalla_rsa_mod_exp,
146     atalla_mod_exp_mont,
147     NULL,
148     NULL,
149     0,
150     NULL,
151     NULL,
152     NULL,
153     NULL
154 };
155 #  endif
156
157 #  ifndef OPENSSL_NO_DSA
158 /* Our internal DSA_METHOD that we provide pointers to */
159 static DSA_METHOD atalla_dsa = {
160     "Atalla DSA method",
161     NULL,                       /* dsa_do_sign */
162     NULL,                       /* dsa_sign_setup */
163     NULL,                       /* dsa_do_verify */
164     atalla_dsa_mod_exp,         /* dsa_mod_exp */
165     atalla_mod_exp_dsa,         /* bn_mod_exp */
166     NULL,                       /* init */
167     NULL,                       /* finish */
168     0,                          /* flags */
169     NULL,                       /* app_data */
170     NULL,                       /* dsa_paramgen */
171     NULL                        /* dsa_keygen */
172 };
173 #  endif
174
175 #  ifndef OPENSSL_NO_DH
176 /* Our internal DH_METHOD that we provide pointers to */
177 static DH_METHOD atalla_dh = {
178     "Atalla DH method",
179     NULL,
180     NULL,
181     atalla_mod_exp_dh,
182     NULL,
183     NULL,
184     0,
185     NULL,
186     NULL
187 };
188 #  endif
189
190 /* Constants used when creating the ENGINE */
191 static const char *engine_atalla_id = "atalla";
192 static const char *engine_atalla_name = "Atalla hardware engine support";
193
194 /*
195  * This internal function is used by ENGINE_atalla() and possibly by the
196  * "dynamic" ENGINE support too
197  */
198 static int bind_helper(ENGINE *e)
199 {
200 #  ifndef OPENSSL_NO_RSA
201     const RSA_METHOD *meth1;
202 #  endif
203 #  ifndef OPENSSL_NO_DSA
204     const DSA_METHOD *meth2;
205 #  endif
206 #  ifndef OPENSSL_NO_DH
207     const DH_METHOD *meth3;
208 #  endif
209     if (!ENGINE_set_id(e, engine_atalla_id) ||
210         !ENGINE_set_name(e, engine_atalla_name) ||
211 #  ifndef OPENSSL_NO_RSA
212         !ENGINE_set_RSA(e, &atalla_rsa) ||
213 #  endif
214 #  ifndef OPENSSL_NO_DSA
215         !ENGINE_set_DSA(e, &atalla_dsa) ||
216 #  endif
217 #  ifndef OPENSSL_NO_DH
218         !ENGINE_set_DH(e, &atalla_dh) ||
219 #  endif
220         !ENGINE_set_destroy_function(e, atalla_destroy) ||
221         !ENGINE_set_init_function(e, atalla_init) ||
222         !ENGINE_set_finish_function(e, atalla_finish) ||
223         !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
224         !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
225         return 0;
226
227 #  ifndef OPENSSL_NO_RSA
228     /*
229      * We know that the "PKCS1_SSLeay()" functions hook properly to the
230      * atalla-specific mod_exp and mod_exp_crt so we use those functions. NB:
231      * We don't use ENGINE_openssl() or anything "more generic" because
232      * something like the RSAref code may not hook properly, and if you own
233      * one of these cards then you have the right to do RSA operations on it
234      * anyway!
235      */
236     meth1 = RSA_PKCS1_SSLeay();
237     atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
238     atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
239     atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
240     atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
241 #  endif
242
243 #  ifndef OPENSSL_NO_DSA
244     /*
245      * Use the DSA_OpenSSL() method and just hook the mod_exp-ish bits.
246      */
247     meth2 = DSA_OpenSSL();
248     atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
249     atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
250     atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
251 #  endif
252
253 #  ifndef OPENSSL_NO_DH
254     /* Much the same for Diffie-Hellman */
255     meth3 = DH_OpenSSL();
256     atalla_dh.generate_key = meth3->generate_key;
257     atalla_dh.compute_key = meth3->compute_key;
258 #  endif
259
260     /* Ensure the atalla error handling is set up */
261     ERR_load_ATALLA_strings();
262     return 1;
263 }
264
265 #  ifdef OPENSSL_NO_DYNAMIC_ENGINE
266 static ENGINE *engine_atalla(void)
267 {
268     ENGINE *ret = ENGINE_new();
269     if (!ret)
270         return NULL;
271     if (!bind_helper(ret)) {
272         ENGINE_free(ret);
273         return NULL;
274     }
275     return ret;
276 }
277
278 void ENGINE_load_atalla(void)
279 {
280     /* Copied from eng_[openssl|dyn].c */
281     ENGINE *toadd = engine_atalla();
282     if (!toadd)
283         return;
284     ENGINE_add(toadd);
285     ENGINE_free(toadd);
286     ERR_clear_error();
287 }
288 #  endif
289
290 /*
291  * This is a process-global DSO handle used for loading and unloading the
292  * Atalla library. NB: This is only set (or unset) during an init() or
293  * finish() call (reference counts permitting) and they're operating with
294  * global locks, so this should be thread-safe implicitly.
295  */
296 static DSO *atalla_dso = NULL;
297
298 /*
299  * These are the function pointers that are (un)set when the library has
300  * successfully (un)loaded.
301  */
302 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
303 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
304 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics =
305     NULL;
306
307 /*
308  * These are the static string constants for the DSO file name and the
309  * function symbol names to bind to. Regrettably, the DSO name on *nix
310  * appears to be "atasi.so" rather than something more consistent like
311  * "libatasi.so". At the time of writing, I'm not sure what the file name on
312  * win32 is but clearly native name translation is not possible (eg
313  * libatasi.so on *nix, and atasi.dll on win32). For the purposes of testing,
314  * I have created a symbollic link called "libatasi.so" so that we can use
315  * native name-translation - a better solution will be needed.
316  */
317 static const char *ATALLA_LIBNAME = NULL;
318 static const char *get_ATALLA_LIBNAME(void)
319 {
320     if (ATALLA_LIBNAME)
321         return ATALLA_LIBNAME;
322     return "atasi";
323 }
324
325 static void free_ATALLA_LIBNAME(void)
326 {
327     OPENSSL_free(ATALLA_LIBNAME);
328     ATALLA_LIBNAME = NULL;
329 }
330
331 static long set_ATALLA_LIBNAME(const char *name)
332 {
333     free_ATALLA_LIBNAME();
334     return (((ATALLA_LIBNAME = BUF_strdup(name)) != NULL) ? 1 : 0);
335 }
336
337 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
338 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
339 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
340
341 /* Destructor (complements the "ENGINE_atalla()" constructor) */
342 static int atalla_destroy(ENGINE *e)
343 {
344     free_ATALLA_LIBNAME();
345     /*
346      * Unload the atalla error strings so any error state including our
347      * functs or reasons won't lead to a segfault (they simply get displayed
348      * without corresponding string data because none will be found).
349      */
350     ERR_unload_ATALLA_strings();
351     return 1;
352 }
353
354 /* (de)initialisation functions. */
355 static int atalla_init(ENGINE *e)
356 {
357     tfnASI_GetHardwareConfig *p1;
358     tfnASI_RSAPrivateKeyOpFn *p2;
359     tfnASI_GetPerformanceStatistics *p3;
360     /*
361      * Not sure of the origin of this magic value, but Ben's code had it and
362      * it seemed to have been working for a few people. :-)
363      */
364     unsigned int config_buf[1024];
365
366     if (atalla_dso != NULL) {
367         ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_ALREADY_LOADED);
368         goto err;
369     }
370     /*
371      * Attempt to load libatasi.so/atasi.dll/whatever. Needs to be changed
372      * unfortunately because the Atalla drivers don't have standard library
373      * names that can be platform-translated well.
374      */
375     /*
376      * TODO: Work out how to actually map to the names the Atalla drivers
377      * really use - for now a symbollic link needs to be created on the host
378      * system from libatasi.so to atasi.so on unix variants.
379      */
380     atalla_dso = DSO_load(NULL, get_ATALLA_LIBNAME(), NULL, 0);
381     if (atalla_dso == NULL) {
382         ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED);
383         goto err;
384     }
385 #define BINDIT(t, name) (t *)DSO_bind_func(atalla_dso, name)
386     if ((p1 = BINDIT(tfnASI_GetHardwareConfig, ATALLA_F1)) == NULL
387         || (p2 = BINDIT(tfnASI_RSAPrivateKeyOpFn, ATALLA_F2)) == NULL
388         || (p3 = BINDIT(tfnASI_GetPerformanceStatistics, ATALLA_F3)) == NULL) {
389         ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_NOT_LOADED);
390         goto err;
391     }
392     /* Copy the pointers */
393     p_Atalla_GetHardwareConfig = p1;
394     p_Atalla_RSAPrivateKeyOpFn = p2;
395     p_Atalla_GetPerformanceStatistics = p3;
396     /*
397      * Perform a basic test to see if there's actually any unit running.
398      */
399     if (p1(0L, config_buf) != 0) {
400         ATALLAerr(ATALLA_F_ATALLA_INIT, ATALLA_R_UNIT_FAILURE);
401         goto err;
402     }
403     /* Everything's fine. */
404     return 1;
405  err:
406     DSO_free(atalla_dso);
407     atalla_dso = NULL;
408     p_Atalla_GetHardwareConfig = NULL;
409     p_Atalla_RSAPrivateKeyOpFn = NULL;
410     p_Atalla_GetPerformanceStatistics = NULL;
411     return 0;
412 }
413
414 static int atalla_finish(ENGINE *e)
415 {
416     free_ATALLA_LIBNAME();
417     if (atalla_dso == NULL) {
418         ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_NOT_LOADED);
419         return 0;
420     }
421     if (!DSO_free(atalla_dso)) {
422         ATALLAerr(ATALLA_F_ATALLA_FINISH, ATALLA_R_UNIT_FAILURE);
423         return 0;
424     }
425     atalla_dso = NULL;
426     p_Atalla_GetHardwareConfig = NULL;
427     p_Atalla_RSAPrivateKeyOpFn = NULL;
428     p_Atalla_GetPerformanceStatistics = NULL;
429     return 1;
430 }
431
432 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f) (void))
433 {
434     int initialised = ((atalla_dso == NULL) ? 0 : 1);
435     switch (cmd) {
436     case ATALLA_CMD_SO_PATH:
437         if (p == NULL) {
438             ATALLAerr(ATALLA_F_ATALLA_CTRL, ERR_R_PASSED_NULL_PARAMETER);
439             return 0;
440         }
441         if (initialised) {
442             ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_ALREADY_LOADED);
443             return 0;
444         }
445         return set_ATALLA_LIBNAME((const char *)p);
446     default:
447         break;
448     }
449     ATALLAerr(ATALLA_F_ATALLA_CTRL, ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
450     return 0;
451 }
452
453 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
454                           const BIGNUM *m, BN_CTX *ctx)
455 {
456     /*
457      * I need somewhere to store temporary serialised values for use with the
458      * Atalla API calls. A neat cheat - I'll use BIGNUMs from the BN_CTX but
459      * access their arrays directly as byte arrays <grin>. This way I don't
460      * have to clean anything up.
461      */
462     BIGNUM *modulus;
463     BIGNUM *exponent;
464     BIGNUM *argument;
465     BIGNUM *result;
466     RSAPrivateKey keydata;
467     int to_return, numbytes;
468
469     modulus = exponent = argument = result = NULL;
470     to_return = 0;              /* expect failure */
471
472     if (!atalla_dso) {
473         ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_NOT_LOADED);
474         goto err;
475     }
476     /* Prepare the params */
477     BN_CTX_start(ctx);
478     modulus = BN_CTX_get(ctx);
479     exponent = BN_CTX_get(ctx);
480     argument = BN_CTX_get(ctx);
481     result = BN_CTX_get(ctx);
482     if (!result) {
483         ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_CTX_FULL);
484         goto err;
485     }
486     if (!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
487         !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top)) {
488         ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_BN_EXPAND_FAIL);
489         goto err;
490     }
491     /* Prepare the key-data */
492     memset(&keydata, 0, sizeof(keydata));
493     numbytes = BN_num_bytes(m);
494     memset(exponent->d, 0, numbytes);
495     memset(modulus->d, 0, numbytes);
496     BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
497     BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
498     keydata.privateExponent.data = (unsigned char *)exponent->d;
499     keydata.privateExponent.len = numbytes;
500     keydata.modulus.data = (unsigned char *)modulus->d;
501     keydata.modulus.len = numbytes;
502     /* Prepare the argument */
503     memset(argument->d, 0, numbytes);
504     memset(result->d, 0, numbytes);
505     BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
506     /* Perform the operation */
507     if (p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
508                                    (unsigned char *)argument->d,
509                                    keydata.modulus.len) != 0) {
510         ATALLAerr(ATALLA_F_ATALLA_MOD_EXP, ATALLA_R_REQUEST_FAILED);
511         goto err;
512     }
513     /* Convert the response */
514     BN_bin2bn((unsigned char *)result->d, numbytes, r);
515     to_return = 1;
516  err:
517     BN_CTX_end(ctx);
518     return to_return;
519 }
520
521 #  ifndef OPENSSL_NO_RSA
522 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa,
523                               BN_CTX *ctx)
524 {
525     int to_return = 0;
526
527     if (!atalla_dso) {
528         ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP, ATALLA_R_NOT_LOADED);
529         goto err;
530     }
531     if (!rsa->d || !rsa->n) {
532         ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,
533                   ATALLA_R_MISSING_KEY_COMPONENTS);
534         goto err;
535     }
536     to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
537  err:
538     return to_return;
539 }
540 #  endif
541
542 #  ifndef OPENSSL_NO_DSA
543 /*
544  * This code was liberated and adapted from the commented-out code in
545  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration (it
546  * doesn't have a CRT form for RSA), this function means that an Atalla
547  * system running with a DSA server certificate can handshake around 5 or 6
548  * times faster/more than an equivalent system running with RSA. Just check
549  * out the "signs" statistics from the RSA and DSA parts of "openssl speed
550  * -engine atalla dsa1024 rsa1024".
551  */
552 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
553                               BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
554                               BN_CTX *ctx, BN_MONT_CTX *in_mont)
555 {
556     BIGNUM t;
557     int to_return = 0;
558
559     BN_init(&t);
560     /* let rr = a1 ^ p1 mod m */
561     if (!atalla_mod_exp(rr, a1, p1, m, ctx))
562         goto end;
563     /* let t = a2 ^ p2 mod m */
564     if (!atalla_mod_exp(&t, a2, p2, m, ctx))
565         goto end;
566     /* let rr = rr * t mod m */
567     if (!BN_mod_mul(rr, rr, &t, m, ctx))
568         goto end;
569     to_return = 1;
570  end:
571     BN_free(&t);
572     return to_return;
573 }
574
575 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
576                               const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
577                               BN_MONT_CTX *m_ctx)
578 {
579     return atalla_mod_exp(r, a, p, m, ctx);
580 }
581 #  endif
582
583 #  ifndef OPENSSL_NO_RSA
584 /* This function is aliased to mod_exp (with the mont stuff dropped). */
585 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
586                                const BIGNUM *m, BN_CTX *ctx,
587                                BN_MONT_CTX *m_ctx)
588 {
589     return atalla_mod_exp(r, a, p, m, ctx);
590 }
591 #  endif
592
593 #  ifndef OPENSSL_NO_DH
594 /* This function is aliased to mod_exp (with the dh and mont dropped). */
595 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
596                              const BIGNUM *a, const BIGNUM *p,
597                              const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
598 {
599     return atalla_mod_exp(r, a, p, m, ctx);
600 }
601 #  endif
602
603 /*
604  * This stuff is needed if this ENGINE is being compiled into a
605  * self-contained shared-library.
606  */
607 #  ifndef OPENSSL_NO_DYNAMIC_ENGINE
608 static int bind_fn(ENGINE *e, const char *id)
609 {
610     if (id && (strcmp(id, engine_atalla_id) != 0))
611         return 0;
612     if (!bind_helper(e))
613         return 0;
614     return 1;
615 }
616
617 IMPLEMENT_DYNAMIC_CHECK_FN()
618     IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
619 #  endif                        /* OPENSSL_NO_DYNAMIC_ENGINE */
620 # endif                         /* !OPENSSL_NO_HW_ATALLA */
621 #endif                          /* !OPENSSL_NO_HW */