88719beb791573f7e465e0ccf06a18ae75ea9779
[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_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_BN_mod_exp(e, atalla_mod_exp) ||
262                         !ENGINE_set_destroy_function(e, atalla_destroy) ||
263                         !ENGINE_set_init_function(e, atalla_init) ||
264                         !ENGINE_set_finish_function(e, atalla_finish) ||
265                         !ENGINE_set_ctrl_function(e, atalla_ctrl) ||
266                         !ENGINE_set_cmd_defns(e, atalla_cmd_defns))
267                 return 0;
268
269 #ifndef OPENSSL_NO_RSA
270         /* We know that the "PKCS1_SSLeay()" functions hook properly
271          * to the atalla-specific mod_exp and mod_exp_crt so we use
272          * those functions. NB: We don't use ENGINE_openssl() or
273          * anything "more generic" because something like the RSAref
274          * code may not hook properly, and if you own one of these
275          * cards then you have the right to do RSA operations on it
276          * anyway! */ 
277         meth1 = RSA_PKCS1_SSLeay();
278         atalla_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
279         atalla_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
280         atalla_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
281         atalla_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
282 #endif
283
284 #ifndef OPENSSL_NO_DSA
285         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
286          * bits. */
287         meth2 = DSA_OpenSSL();
288         atalla_dsa.dsa_do_sign = meth2->dsa_do_sign;
289         atalla_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
290         atalla_dsa.dsa_do_verify = meth2->dsa_do_verify;
291 #endif
292
293 #ifndef OPENSSL_NO_DH
294         /* Much the same for Diffie-Hellman */
295         meth3 = DH_OpenSSL();
296         atalla_dh.generate_key = meth3->generate_key;
297         atalla_dh.compute_key = meth3->compute_key;
298 #endif
299
300         /* Ensure the atalla error handling is set up */
301         atalla_load_error_strings();
302         return 1;
303         }
304
305 /* As this is only ever called once, there's no need for locking
306  * (indeed - the lock will already be held by our caller!!!) */
307 ENGINE *ENGINE_atalla(void)
308         {
309         ENGINE *ret = ENGINE_new();
310         if(!ret)
311                 return NULL;
312         if(!bind_helper(ret))
313                 {
314                 ENGINE_free(ret);
315                 return NULL;
316                 }
317         return ret;
318         }
319
320 /* This is a process-global DSO handle used for loading and unloading
321  * the Atalla library. NB: This is only set (or unset) during an
322  * init() or finish() call (reference counts permitting) and they're
323  * operating with global locks, so this should be thread-safe
324  * implicitly. */
325 static DSO *atalla_dso = NULL;
326
327 /* These are the function pointers that are (un)set when the library has
328  * successfully (un)loaded. */
329 static tfnASI_GetHardwareConfig *p_Atalla_GetHardwareConfig = NULL;
330 static tfnASI_RSAPrivateKeyOpFn *p_Atalla_RSAPrivateKeyOpFn = NULL;
331 static tfnASI_GetPerformanceStatistics *p_Atalla_GetPerformanceStatistics = NULL;
332
333 /* These are the static string constants for the DSO file name and the function
334  * symbol names to bind to. Regrettably, the DSO name on *nix appears to be
335  * "atasi.so" rather than something more consistent like "libatasi.so". At the
336  * time of writing, I'm not sure what the file name on win32 is but clearly
337  * native name translation is not possible (eg libatasi.so on *nix, and
338  * atasi.dll on win32). For the purposes of testing, I have created a symbollic
339  * link called "libatasi.so" so that we can use native name-translation - a
340  * better solution will be needed. */
341 static const char def_ATALLA_LIBNAME[] = "atasi";
342 static const char *ATALLA_LIBNAME = def_ATALLA_LIBNAME;
343 static const char *ATALLA_F1 = "ASI_GetHardwareConfig";
344 static const char *ATALLA_F2 = "ASI_RSAPrivateKeyOpFn";
345 static const char *ATALLA_F3 = "ASI_GetPerformanceStatistics";
346
347 /* Destructor (complements the "ENGINE_atalla()" constructor) */
348 static int atalla_destroy(ENGINE *e)
349         {
350         /* Unload the atalla error strings so any error state including our
351          * functs or reasons won't lead to a segfault (they simply get displayed
352          * without corresponding string data because none will be found). */
353         atalla_unload_error_strings();
354         return 1;
355         }
356
357 /* (de)initialisation functions. */
358 static int atalla_init(ENGINE *e)
359         {
360         tfnASI_GetHardwareConfig *p1;
361         tfnASI_RSAPrivateKeyOpFn *p2;
362         tfnASI_GetPerformanceStatistics *p3;
363         /* Not sure of the origin of this magic value, but Ben's code had it
364          * and it seemed to have been working for a few people. :-) */
365         unsigned int config_buf[1024];
366
367         if(atalla_dso != NULL)
368                 {
369                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_ALREADY_LOADED);
370                 goto err;
371                 }
372         /* Attempt to load libatasi.so/atasi.dll/whatever. Needs to be
373          * changed unfortunately because the Atalla drivers don't have
374          * standard library names that can be platform-translated well. */
375         /* TODO: Work out how to actually map to the names the Atalla
376          * drivers really use - for now a symbollic link needs to be
377          * created on the host system from libatasi.so to atasi.so on
378          * unix variants. */
379         atalla_dso = DSO_load(NULL, ATALLA_LIBNAME, NULL, 0);
380         if(atalla_dso == NULL)
381                 {
382                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
383                 goto err;
384                 }
385         if(!(p1 = (tfnASI_GetHardwareConfig *)DSO_bind_func(
386                                 atalla_dso, ATALLA_F1)) ||
387                         !(p2 = (tfnASI_RSAPrivateKeyOpFn *)DSO_bind_func(
388                                 atalla_dso, ATALLA_F2)) ||
389                         !(p3 = (tfnASI_GetPerformanceStatistics *)DSO_bind_func(
390                                 atalla_dso, ATALLA_F3)))
391                 {
392                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_NOT_LOADED);
393                 goto err;
394                 }
395         /* Copy the pointers */
396         p_Atalla_GetHardwareConfig = p1;
397         p_Atalla_RSAPrivateKeyOpFn = p2;
398         p_Atalla_GetPerformanceStatistics = p3;
399         /* Perform a basic test to see if there's actually any unit
400          * running. */
401         if(p1(0L, config_buf) != 0)
402                 {
403                 ATALLAerr(ATALLA_F_ATALLA_INIT,ATALLA_R_UNIT_FAILURE);
404                 goto err;
405                 }
406         /* Everything's fine. */
407         return 1;
408 err:
409         if(atalla_dso)
410                 DSO_free(atalla_dso);
411         p_Atalla_GetHardwareConfig = NULL;
412         p_Atalla_RSAPrivateKeyOpFn = NULL;
413         p_Atalla_GetPerformanceStatistics = NULL;
414         return 0;
415         }
416
417 static int atalla_finish(ENGINE *e)
418         {
419         if(atalla_dso == NULL)
420                 {
421                 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_NOT_LOADED);
422                 return 0;
423                 }
424         if(!DSO_free(atalla_dso))
425                 {
426                 ATALLAerr(ATALLA_F_ATALLA_FINISH,ATALLA_R_UNIT_FAILURE);
427                 return 0;
428                 }
429         atalla_dso = NULL;
430         p_Atalla_GetHardwareConfig = NULL;
431         p_Atalla_RSAPrivateKeyOpFn = NULL;
432         p_Atalla_GetPerformanceStatistics = NULL;
433         return 1;
434         }
435
436 static int atalla_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
437         {
438         int initialised = ((atalla_dso == NULL) ? 0 : 1);
439         switch(cmd)
440                 {
441         case ATALLA_CMD_SO_PATH:
442                 if(p == NULL)
443                         {
444                         ATALLAerr(ATALLA_F_ATALLA_CTRL,ERR_R_PASSED_NULL_PARAMETER);
445                         return 0;
446                         }
447                 if(initialised)
448                         {
449                         ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_ALREADY_LOADED);
450                         return 0;
451                         }
452                 ATALLA_LIBNAME = (const char *)p;
453                 return 1;
454         default:
455                 break;
456                 }
457         ATALLAerr(ATALLA_F_ATALLA_CTRL,ATALLA_R_CTRL_COMMAND_NOT_IMPLEMENTED);
458         return 0;
459         }
460
461 static int atalla_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
462                         const BIGNUM *m, BN_CTX *ctx)
463         {
464         /* I need somewhere to store temporary serialised values for
465          * use with the Atalla API calls. A neat cheat - I'll use
466          * BIGNUMs from the BN_CTX but access their arrays directly as
467          * byte arrays <grin>. This way I don't have to clean anything
468          * up. */
469         BIGNUM *modulus;
470         BIGNUM *exponent;
471         BIGNUM *argument;
472         BIGNUM *result;
473         RSAPrivateKey keydata;
474         int to_return, numbytes;
475
476         modulus = exponent = argument = result = NULL;
477         to_return = 0; /* expect failure */
478
479         if(!atalla_dso)
480                 {
481                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_NOT_LOADED);
482                 goto err;
483                 }
484         /* Prepare the params */
485         BN_CTX_start(ctx);
486         modulus = BN_CTX_get(ctx);
487         exponent = BN_CTX_get(ctx);
488         argument = BN_CTX_get(ctx);
489         result = BN_CTX_get(ctx);
490         if (!result)
491                 {
492                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_CTX_FULL);
493                 goto err;
494                 }
495         if(!bn_wexpand(modulus, m->top) || !bn_wexpand(exponent, m->top) ||
496            !bn_wexpand(argument, m->top) || !bn_wexpand(result, m->top))
497                 {
498                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_BN_EXPAND_FAIL);
499                 goto err;
500                 }
501         /* Prepare the key-data */
502         memset(&keydata, 0,sizeof keydata);
503         numbytes = BN_num_bytes(m);
504         memset(exponent->d, 0, numbytes);
505         memset(modulus->d, 0, numbytes);
506         BN_bn2bin(p, (unsigned char *)exponent->d + numbytes - BN_num_bytes(p));
507         BN_bn2bin(m, (unsigned char *)modulus->d + numbytes - BN_num_bytes(m));
508         keydata.privateExponent.data = (unsigned char *)exponent->d;
509         keydata.privateExponent.len = numbytes;
510         keydata.modulus.data = (unsigned char *)modulus->d;
511         keydata.modulus.len = numbytes;
512         /* Prepare the argument */
513         memset(argument->d, 0, numbytes);
514         memset(result->d, 0, numbytes);
515         BN_bn2bin(a, (unsigned char *)argument->d + numbytes - BN_num_bytes(a));
516         /* Perform the operation */
517         if(p_Atalla_RSAPrivateKeyOpFn(&keydata, (unsigned char *)result->d,
518                         (unsigned char *)argument->d,
519                         keydata.modulus.len) != 0)
520                 {
521                 ATALLAerr(ATALLA_F_ATALLA_MOD_EXP,ATALLA_R_REQUEST_FAILED);
522                 goto err;
523                 }
524         /* Convert the response */
525         BN_bin2bn((unsigned char *)result->d, numbytes, r);
526         to_return = 1;
527 err:
528         BN_CTX_end(ctx);
529         return to_return;
530         }
531
532 #ifndef OPENSSL_NO_RSA
533 static int atalla_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
534         {
535         BN_CTX *ctx = NULL;
536         int to_return = 0;
537
538         if(!atalla_dso)
539                 {
540                 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_NOT_LOADED);
541                 goto err;
542                 }
543         if((ctx = BN_CTX_new()) == NULL)
544                 goto err;
545         if(!rsa->d || !rsa->n)
546                 {
547                 ATALLAerr(ATALLA_F_ATALLA_RSA_MOD_EXP,ATALLA_R_MISSING_KEY_COMPONENTS);
548                 goto err;
549                 }
550         to_return = atalla_mod_exp(r0, I, rsa->d, rsa->n, ctx);
551 err:
552         if(ctx)
553                 BN_CTX_free(ctx);
554         return to_return;
555         }
556 #endif
557
558 #ifndef OPENSSL_NO_DSA
559 /* This code was liberated and adapted from the commented-out code in
560  * dsa_ossl.c. Because of the unoptimised form of the Atalla acceleration
561  * (it doesn't have a CRT form for RSA), this function means that an
562  * Atalla system running with a DSA server certificate can handshake
563  * around 5 or 6 times faster/more than an equivalent system running with
564  * RSA. Just check out the "signs" statistics from the RSA and DSA parts
565  * of "openssl speed -engine atalla dsa1024 rsa1024". */
566 static int atalla_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
567                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
568                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
569         {
570         BIGNUM t;
571         int to_return = 0;
572  
573         BN_init(&t);
574         /* let rr = a1 ^ p1 mod m */
575         if (!atalla_mod_exp(rr,a1,p1,m,ctx)) goto end;
576         /* let t = a2 ^ p2 mod m */
577         if (!atalla_mod_exp(&t,a2,p2,m,ctx)) goto end;
578         /* let rr = rr * t mod m */
579         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
580         to_return = 1;
581 end:
582         BN_free(&t);
583         return to_return;
584         }
585
586 static int atalla_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
587                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
588                 BN_MONT_CTX *m_ctx)
589         {
590         return atalla_mod_exp(r, a, p, m, ctx);
591         }
592 #endif
593
594 /* This function is aliased to mod_exp (with the mont stuff dropped). */
595 static int atalla_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
596                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
597         {
598         return atalla_mod_exp(r, a, p, m, ctx);
599         }
600
601 #ifndef OPENSSL_NO_DH
602 /* This function is aliased to mod_exp (with the dh and mont dropped). */
603 static int atalla_mod_exp_dh(const DH *dh, BIGNUM *r,
604                 const BIGNUM *a, const BIGNUM *p,
605                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
606         {
607         return atalla_mod_exp(r, a, p, m, ctx);
608         }
609 #endif
610
611 /* This stuff is needed if this ENGINE is being compiled into a self-contained
612  * shared-library. */
613 #ifdef ENGINE_DYNAMIC_SUPPORT
614 static int bind_fn(ENGINE *e, const char *id)
615         {
616         if(id && (strcmp(id, engine_atalla_id) != 0))
617                 return 0;
618         if(!bind_helper(e))
619                 return 0;
620         return 1;
621         }
622 IMPLEMENT_DYNAMIC_CHECK_FN()
623 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
624 #endif /* ENGINE_DYNAMIC_SUPPORT */
625
626 #endif /* !OPENSSL_NO_HW_ATALLA */
627 #endif /* !OPENSSL_NO_HW */