f25013f787cfd3bde1d9f1f12288922b01e0dd3c
[openssl.git] / crypto / engine / hw_aep.c
1 /* crypto/engine/hw_aep.c */
2 /*
3  */
4 /* ====================================================================
5  * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions
9  * are met:
10  *
11  * 1. Redistributions of source code must retain the above copyright
12  *    notice, this list of conditions and the following disclaimer. 
13  *
14  * 2. Redistributions in binary form must reproduce the above copyright
15  *    notice, this list of conditions and the following disclaimer in
16  *    the documentation and/or other materials provided with the
17  *    distribution.
18  *
19  * 3. All advertising materials mentioning features or use of this
20  *    software must display the following acknowledgment:
21  *    "This product includes software developed by the OpenSSL Project
22  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
23  *
24  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25  *    endorse or promote products derived from this software without
26  *    prior written permission. For written permission, please contact
27  *    licensing@OpenSSL.org.
28  *
29  * 5. Products derived from this software may not be called "OpenSSL"
30  *    nor may "OpenSSL" appear in their names without prior written
31  *    permission of the OpenSSL Project.
32  *
33  * 6. Redistributions of any form whatsoever must retain the following
34  *    acknowledgment:
35  *    "This product includes software developed by the OpenSSL Project
36  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
37  *
38  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
42  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49  * OF THE POSSIBILITY OF SUCH DAMAGE.
50  * ====================================================================
51  *
52  * This product includes cryptographic software written by Eric Young
53  * (eay@cryptsoft.com).  This product includes software written by Tim
54  * Hudson (tjh@cryptsoft.com).
55  *
56  */
57
58 #include <stdio.h>
59 #include <openssl/bn.h>
60 #include <string.h>
61
62 #include <openssl/e_os2.h>
63 #ifndef OPENSSL_SYS_MSDOS
64 #include <sys/types.h>
65 #include <unistd.h>
66 #else
67 #include <process.h>
68 typedef int pid_t;
69 #endif
70
71 #include <openssl/crypto.h>
72 #include <openssl/dso.h>
73 #include <openssl/engine.h>
74
75 #ifndef OPENSSL_NO_HW
76 #ifndef OPENSSL_NO_HW_AEP
77 #ifdef FLAT_INC
78 #include "aep.h"
79 #else
80 #include "vendor_defns/aep.h"
81 #endif
82
83 #define AEP_LIB_NAME "aep engine"
84 #define FAIL_TO_SW 0x10101010
85
86 #include "hw_aep_err.c"
87
88 static int aep_init(ENGINE *e);
89 static int aep_finish(ENGINE *e);
90 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
91 static int aep_destroy(ENGINE *e);
92
93 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR hConnection);
94 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection);
95 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection);
96 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use);
97
98 /* BIGNUM stuff */
99 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
100         const BIGNUM *m, BN_CTX *ctx);
101
102 static AEP_RV aep_mod_exp_crt(BIGNUM *r,const  BIGNUM *a, const BIGNUM *p,
103         const BIGNUM *q, const BIGNUM *dmp1,const BIGNUM *dmq1,
104         const BIGNUM *iqmp, BN_CTX *ctx);
105
106 /* RSA stuff */
107 #ifndef OPENSSL_NO_RSA
108 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
109 #endif
110
111 /* This function is aliased to mod_exp (with the mont stuff dropped). */
112 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
113         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
114
115 /* DSA stuff */
116 #ifndef OPENSSL_NO_DSA
117 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
118         BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
119         BN_CTX *ctx, BN_MONT_CTX *in_mont);
120
121 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
122         const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
123         BN_MONT_CTX *m_ctx);
124 #endif
125
126 /* DH stuff */
127 /* This function is aliased to mod_exp (with the DH and mont dropped). */
128 #ifndef OPENSSL_NO_DH
129 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
130         const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
131 #endif
132
133 /* rand stuff   */
134 #ifdef AEPRAND
135 static int aep_rand(unsigned char *buf, int num);
136 static int aep_rand_status(void);
137 #endif
138
139 /* Bignum conversion stuff */
140 static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize);
141 static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
142         unsigned char* AEP_BigNum);
143 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
144         unsigned char* AEP_BigNum);
145
146 /* The definitions for control commands specific to this engine */
147 #define AEP_CMD_SO_PATH         ENGINE_CMD_BASE
148 static const ENGINE_CMD_DEFN aep_cmd_defns[] =
149         {
150         { AEP_CMD_SO_PATH,
151           "SO_PATH",
152           "Specifies the path to the 'aep' shared library",
153           ENGINE_CMD_FLAG_STRING
154         },
155         {0, NULL, NULL, 0}
156         };
157
158 #ifndef OPENSSL_NO_RSA
159 /* Our internal RSA_METHOD that we provide pointers to */
160 static RSA_METHOD aep_rsa =
161         {
162         "Aep RSA method",
163         NULL,                /*rsa_pub_encrypt*/
164         NULL,                /*rsa_pub_decrypt*/
165         NULL,                /*rsa_priv_encrypt*/
166         NULL,                /*rsa_priv_encrypt*/
167         aep_rsa_mod_exp,     /*rsa_mod_exp*/
168         aep_mod_exp_mont,    /*bn_mod_exp*/
169         NULL,                /*init*/
170         NULL,                /*finish*/
171         0,                   /*flags*/
172         NULL,                /*app_data*/
173         NULL,                /*rsa_sign*/
174         NULL                 /*rsa_verify*/
175         };
176 #endif
177
178 #ifndef OPENSSL_NO_DSA
179 /* Our internal DSA_METHOD that we provide pointers to */
180 static DSA_METHOD aep_dsa =
181         {
182         "Aep DSA method",
183         NULL,                /* dsa_do_sign */
184         NULL,                /* dsa_sign_setup */
185         NULL,                /* dsa_do_verify */
186         aep_dsa_mod_exp,     /* dsa_mod_exp */
187         aep_mod_exp_dsa,     /* bn_mod_exp */
188         NULL,                /* init */
189         NULL,                /* finish */
190         0,                   /* flags */
191         NULL                 /* app_data */
192         };
193 #endif
194
195 #ifndef OPENSSL_NO_DH
196 /* Our internal DH_METHOD that we provide pointers to */
197 static DH_METHOD aep_dh =
198         {
199         "Aep DH method",
200         NULL,
201         NULL,
202         aep_mod_exp_dh,
203         NULL,
204         NULL,
205         0,
206         NULL
207         };
208 #endif
209
210 #ifdef AEPRAND
211 /* our internal RAND_method that we provide pointers to  */
212 static RAND_METHOD aep_random =
213         {
214         /*"AEP RAND method", */
215         NULL,
216         aep_rand,
217         NULL,
218         NULL,
219         aep_rand,
220         aep_rand_status,
221         };
222 #endif
223
224 /*Define an array of structures to hold connections*/
225 static AEP_CONNECTION_ENTRY aep_app_conn_table[MAX_PROCESS_CONNECTIONS];
226
227 /*Used to determine if this is a new process*/
228 static pid_t    recorded_pid = 0;
229
230 #ifdef AEPRAND
231 static AEP_U8   rand_block[RAND_BLK_SIZE];
232 static AEP_U32  rand_block_bytes = 0;
233 #endif
234
235 /* Constants used when creating the ENGINE */
236 static const char *engine_aep_id = "aep";
237 static const char *engine_aep_name = "Aep hardware engine support";
238
239 static int max_key_len = 2176;
240
241
242 /* This internal function is used by ENGINE_aep() and possibly by the
243  * "dynamic" ENGINE support too */
244 static int bind_aep(ENGINE *e)
245         {
246 #ifndef OPENSSL_NO_RSA
247         const RSA_METHOD  *meth1;
248 #endif
249 #ifndef OPENSSL_NO_DSA
250         const DSA_METHOD  *meth2;
251 #endif
252 #ifndef OPENSSL_NO_DH
253         const DH_METHOD   *meth3;
254 #endif
255
256         if(!ENGINE_set_id(e, engine_aep_id) ||
257                 !ENGINE_set_name(e, engine_aep_name) ||
258 #ifndef OPENSSL_NO_RSA
259                 !ENGINE_set_RSA(e, &aep_rsa) ||
260 #endif
261 #ifndef OPENSSL_NO_DSA
262                 !ENGINE_set_DSA(e, &aep_dsa) ||
263 #endif
264 #ifndef OPENSSL_NO_DH
265                 !ENGINE_set_DH(e, &aep_dh) ||
266 #endif
267 #ifdef AEPRAND
268                 !ENGINE_set_RAND(e, &aep_random) ||
269 #endif
270                 !ENGINE_set_init_function(e, aep_init) ||
271                 !ENGINE_set_destroy_function(e, aep_destroy) ||
272                 !ENGINE_set_finish_function(e, aep_finish) ||
273                 !ENGINE_set_ctrl_function(e, aep_ctrl) ||
274                 !ENGINE_set_cmd_defns(e, aep_cmd_defns))
275                 return 0;
276
277 #ifndef OPENSSL_NO_RSA
278         /* We know that the "PKCS1_SSLeay()" functions hook properly
279          * to the aep-specific mod_exp and mod_exp_crt so we use
280          * those functions. NB: We don't use ENGINE_openssl() or
281          * anything "more generic" because something like the RSAref
282          * code may not hook properly, and if you own one of these
283          * cards then you have the right to do RSA operations on it
284          * anyway! */
285         meth1 = RSA_PKCS1_SSLeay();
286         aep_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
287         aep_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
288         aep_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
289         aep_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
290 #endif
291
292
293 #ifndef OPENSSL_NO_DSA
294         /* Use the DSA_OpenSSL() method and just hook the mod_exp-ish
295          * bits. */
296         meth2 = DSA_OpenSSL();
297         aep_dsa.dsa_do_sign    = meth2->dsa_do_sign;
298         aep_dsa.dsa_sign_setup = meth2->dsa_sign_setup;
299         aep_dsa.dsa_do_verify  = meth2->dsa_do_verify;
300
301         aep_dsa = *DSA_get_default_method(); 
302         aep_dsa.dsa_mod_exp = aep_dsa_mod_exp; 
303         aep_dsa.bn_mod_exp = aep_mod_exp_dsa;
304 #endif
305
306 #ifndef OPENSSL_NO_DH
307         /* Much the same for Diffie-Hellman */
308         meth3 = DH_OpenSSL();
309         aep_dh.generate_key = meth3->generate_key;
310         aep_dh.compute_key  = meth3->compute_key;
311         aep_dh.bn_mod_exp   = meth3->bn_mod_exp;
312 #endif
313
314         /* Ensure the aep error handling is set up */
315         ERR_load_AEPHK_strings();
316
317         return 1;
318 }
319
320 #ifdef ENGINE_DYNAMIC_SUPPORT
321 static int bind_helper(ENGINE *e, const char *id)
322         {
323         if(id && (strcmp(id, engine_aep_id) != 0))
324                 return 0;
325         if(!bind_aep(e))
326                 return 0;
327         return 1;
328         }       
329 IMPLEMENT_DYNAMIC_CHECK_FN()
330 IMPLEMENT_DYNAMIC_BIND_FN(bind_helper)
331 #else
332 static ENGINE *engine_aep(void)
333         {
334         ENGINE *ret = ENGINE_new();
335         if(!ret)
336                 return NULL;
337         if(!bind_aep(ret))
338                 {
339                 ENGINE_free(ret);
340                 return NULL;
341                 }
342         return ret;
343         }
344
345 void ENGINE_load_aep(void)
346         {
347         /* Copied from eng_[openssl|dyn].c */
348         ENGINE *toadd = engine_aep();
349         if(!toadd) return;
350         ENGINE_add(toadd);
351         ENGINE_free(toadd);
352         ERR_clear_error();
353         }
354 #endif
355
356 /* This is a process-global DSO handle used for loading and unloading
357  * the Aep library. NB: This is only set (or unset) during an
358  * init() or finish() call (reference counts permitting) and they're
359  * operating with global locks, so this should be thread-safe
360  * implicitly. */
361 static DSO *aep_dso = NULL;
362
363 /* These are the static string constants for the DSO file name and the function
364  * symbol names to bind to. 
365 */
366 static const char *AEP_LIBNAME = "aep";
367
368 static const char *AEP_F1    = "AEP_ModExp";
369 static const char *AEP_F2    = "AEP_ModExpCrt";
370 #ifdef AEPRAND
371 static const char *AEP_F3    = "AEP_GenRandom";
372 #endif
373 static const char *AEP_F4    = "AEP_Finalize";
374 static const char *AEP_F5    = "AEP_Initialize";
375 static const char *AEP_F6    = "AEP_OpenConnection";
376 static const char *AEP_F7    = "AEP_SetBNCallBacks";
377 static const char *AEP_F8    = "AEP_CloseConnection";
378
379 /* These are the function pointers that are (un)set when the library has
380  * successfully (un)loaded. */
381 static t_AEP_OpenConnection    *p_AEP_OpenConnection  = NULL;
382 static t_AEP_CloseConnection   *p_AEP_CloseConnection = NULL;
383 static t_AEP_ModExp            *p_AEP_ModExp          = NULL;
384 static t_AEP_ModExpCrt         *p_AEP_ModExpCrt       = NULL;
385 #ifdef AEPRAND
386 static t_AEP_GenRandom         *p_AEP_GenRandom       = NULL;
387 #endif
388 static t_AEP_Initialize        *p_AEP_Initialize      = NULL;
389 static t_AEP_Finalize          *p_AEP_Finalize        = NULL;
390 static t_AEP_SetBNCallBacks    *p_AEP_SetBNCallBacks  = NULL;
391
392 /* (de)initialisation functions. */
393 static int aep_init(ENGINE *e)
394         {
395         t_AEP_ModExp          *p1;
396         t_AEP_ModExpCrt       *p2;
397 #ifdef AEPRAND
398         t_AEP_GenRandom       *p3;
399 #endif
400         t_AEP_Finalize        *p4;
401         t_AEP_Initialize      *p5;
402         t_AEP_OpenConnection  *p6;
403         t_AEP_SetBNCallBacks  *p7;
404         t_AEP_CloseConnection *p8;
405
406         int to_return = 0;
407  
408         if(aep_dso != NULL)
409                 {
410                 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_ALREADY_LOADED);
411                 goto err;
412                 }
413         /* Attempt to load libaep.so. */
414
415         aep_dso = DSO_load(NULL, AEP_LIBNAME, NULL, 0);
416   
417         if(aep_dso == NULL)
418                 {
419                 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
420                 goto err;
421                 }
422
423         if(     !(p1 = (t_AEP_ModExp *)     DSO_bind_func( aep_dso,AEP_F1))  ||
424                 !(p2 = (t_AEP_ModExpCrt*)   DSO_bind_func( aep_dso,AEP_F2))  ||
425 #ifdef AEPRAND
426                 !(p3 = (t_AEP_GenRandom*)   DSO_bind_func( aep_dso,AEP_F3))  ||
427 #endif
428                 !(p4 = (t_AEP_Finalize*)    DSO_bind_func( aep_dso,AEP_F4))  ||
429                 !(p5 = (t_AEP_Initialize*)  DSO_bind_func( aep_dso,AEP_F5))  ||
430                 !(p6 = (t_AEP_OpenConnection*) DSO_bind_func( aep_dso,AEP_F6))  ||
431                 !(p7 = (t_AEP_SetBNCallBacks*) DSO_bind_func( aep_dso,AEP_F7))  ||
432                 !(p8 = (t_AEP_CloseConnection*) DSO_bind_func( aep_dso,AEP_F8)))
433                 {
434                 AEPHKerr(AEPHK_F_AEP_INIT,AEPHK_R_NOT_LOADED);
435                 goto err;
436                 }
437
438         /* Copy the pointers */
439   
440         p_AEP_ModExp           = p1;
441         p_AEP_ModExpCrt        = p2;
442 #ifdef AEPRAND
443         p_AEP_GenRandom        = p3;
444 #endif
445         p_AEP_Finalize         = p4;
446         p_AEP_Initialize       = p5;
447         p_AEP_OpenConnection   = p6;
448         p_AEP_SetBNCallBacks   = p7;
449         p_AEP_CloseConnection  = p8;
450  
451         to_return = 1;
452  
453         return to_return;
454
455  err: 
456
457         if(aep_dso)
458                 DSO_free(aep_dso);
459                 
460         p_AEP_OpenConnection    = NULL;
461         p_AEP_ModExp            = NULL;
462         p_AEP_ModExpCrt         = NULL;
463 #ifdef AEPRAND
464         p_AEP_GenRandom         = NULL;
465 #endif
466         p_AEP_Initialize        = NULL;
467         p_AEP_Finalize          = NULL;
468         p_AEP_SetBNCallBacks    = NULL;
469         p_AEP_CloseConnection   = NULL;
470
471         return to_return;
472         }
473
474 /* Destructor (complements the "ENGINE_aep()" constructor) */
475 static int aep_destroy(ENGINE *e)
476         {
477         ERR_unload_AEPHK_strings();
478         return 1;
479         }
480
481 static int aep_finish(ENGINE *e)
482         {
483         int to_return = 0, in_use;
484         AEP_RV rv;
485
486         if(aep_dso == NULL)
487                 {
488                 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_NOT_LOADED);
489                 goto err;
490                 }
491
492         rv = aep_close_all_connections(0, &in_use);
493         if (rv != AEP_R_OK)
494                 {
495                 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CLOSE_HANDLES_FAILED);
496                 goto err;
497                 }
498         if (in_use)
499                 {
500                 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_CONNECTIONS_IN_USE);
501                 goto err;
502                 }
503
504         rv = p_AEP_Finalize();
505         if (rv != AEP_R_OK)
506                 {
507                 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_FINALIZE_FAILED);
508                 goto err;
509                 }
510
511         if(!DSO_free(aep_dso))
512                 {
513                 AEPHKerr(AEPHK_F_AEP_FINISH,AEPHK_R_UNIT_FAILURE);
514                 goto err;
515                 }
516
517         aep_dso = NULL;
518         p_AEP_CloseConnection   = NULL;
519         p_AEP_OpenConnection    = NULL;
520         p_AEP_ModExp            = NULL;
521         p_AEP_ModExpCrt         = NULL;
522 #ifdef AEPRAND
523         p_AEP_GenRandom         = NULL;
524 #endif
525         p_AEP_Initialize        = NULL;
526         p_AEP_Finalize          = NULL;
527         p_AEP_SetBNCallBacks    = NULL;
528
529         to_return = 1;
530  err:
531         return to_return;
532         }
533
534 static int aep_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
535         {
536         int initialised = ((aep_dso == NULL) ? 0 : 1);
537         switch(cmd)
538                 {
539         case AEP_CMD_SO_PATH:
540                 if(p == NULL)
541                         {
542                         AEPHKerr(AEPHK_F_AEP_CTRL,
543                                 ERR_R_PASSED_NULL_PARAMETER);
544                         return 0;
545                         }
546                 if(initialised)
547                         {
548                         AEPHKerr(AEPHK_F_AEP_CTRL,
549                                 AEPHK_R_ALREADY_LOADED);
550                         return 0;
551                         }
552                 AEP_LIBNAME = (const char *)p;
553                 return 1;
554         default:
555                 break;
556                 }
557         AEPHKerr(AEPHK_F_AEP_CTRL,AEPHK_R_CTRL_COMMAND_NOT_IMPLEMENTED);
558         return 0;
559         }
560
561 static int aep_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
562         const BIGNUM *m, BN_CTX *ctx)
563         {
564         int to_return = 0;
565         int     r_len = 0;
566         AEP_CONNECTION_HNDL hConnection;
567         AEP_RV rv;
568         
569         r_len = BN_num_bits(m);
570
571         /* Perform in software if modulus is too large for hardware. */
572
573         if (r_len > max_key_len){
574                 AEPHKerr(AEPHK_F_AEP_MOD_EXP, AEPHK_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
575                 return BN_mod_exp(r, a, p, m, ctx);
576         } 
577
578         /*Grab a connection from the pool*/
579         rv = aep_get_connection(&hConnection);
580         if (rv != AEP_R_OK)
581                 {     
582                 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_GET_HANDLE_FAILED);
583                 return BN_mod_exp(r, a, p, m, ctx);
584                 }
585
586         /*To the card with the mod exp*/
587         rv = p_AEP_ModExp(hConnection,(void*)a, (void*)p,(void*)m, (void*)r,NULL);
588
589         if (rv !=  AEP_R_OK)
590                 {
591                 AEPHKerr(AEPHK_F_AEP_MOD_EXP,AEPHK_R_MOD_EXP_FAILED);
592                 rv = aep_close_connection(hConnection);
593                 return BN_mod_exp(r, a, p, m, ctx);
594                 }
595
596         /*Return the connection to the pool*/
597         rv = aep_return_connection(hConnection);
598         if (rv != AEP_R_OK)
599                 {
600                 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED); 
601                 goto err;
602                 }
603
604         to_return = 1;
605  err:
606         return to_return;
607         }
608         
609 static AEP_RV aep_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
610         const BIGNUM *q, const BIGNUM *dmp1,
611         const BIGNUM *dmq1,const BIGNUM *iqmp, BN_CTX *ctx)
612         {
613         AEP_RV rv = AEP_R_OK;
614         AEP_CONNECTION_HNDL hConnection;
615
616         /*Grab a connection from the pool*/
617         rv = aep_get_connection(&hConnection);
618         if (rv != AEP_R_OK)
619                 {
620                 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_GET_HANDLE_FAILED);
621                 return FAIL_TO_SW;
622                 }
623
624         /*To the card with the mod exp*/
625         rv = p_AEP_ModExpCrt(hConnection,(void*)a, (void*)p, (void*)q, (void*)dmp1,(void*)dmq1,
626                 (void*)iqmp,(void*)r,NULL);
627         if (rv != AEP_R_OK)
628                 {
629                 AEPHKerr(AEPHK_F_AEP_MOD_EXP_CRT,AEPHK_R_MOD_EXP_CRT_FAILED);
630                 rv = aep_close_connection(hConnection);
631                 return FAIL_TO_SW;
632                 }
633
634         /*Return the connection to the pool*/
635         rv = aep_return_connection(hConnection);
636         if (rv != AEP_R_OK)
637                 {
638                 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED); 
639                 goto err;
640                 }
641  
642  err:
643         return rv;
644         }
645         
646
647 #ifdef AEPRAND
648 static int aep_rand(unsigned char *buf,int len )
649         {
650         AEP_RV rv = AEP_R_OK;
651         AEP_CONNECTION_HNDL hConnection;
652
653         int to_return = 0;
654
655   
656         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
657
658         /*Can the request be serviced with what's already in the buffer?*/
659         if (len <= rand_block_bytes)
660                 {
661                 memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
662                 rand_block_bytes -= len;
663                 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
664                 }
665         else
666                 /*If not the get another block of random bytes*/
667                 {
668                 CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
669
670                 rv = aep_get_connection(&hConnection);
671                 if (rv !=  AEP_R_OK)
672                         { 
673                         AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_HANDLE_FAILED);             
674                         goto err_nounlock;
675                         }
676
677                 if (len > RAND_BLK_SIZE)
678                         {
679                         rv = p_AEP_GenRandom(hConnection, len, 2, buf, NULL);
680                         if (rv !=  AEP_R_OK)
681                                 {  
682                                 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED); 
683                                 goto err_nounlock;
684                                 }
685                         }
686                 else
687                         {
688                         CRYPTO_w_lock(CRYPTO_LOCK_RAND);
689
690                         rv = p_AEP_GenRandom(hConnection, RAND_BLK_SIZE, 2, &rand_block[0], NULL);
691                         if (rv !=  AEP_R_OK)
692                                 {       
693                                 AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_GET_RANDOM_FAILED); 
694               
695                                 goto err;
696                                 }
697
698                         rand_block_bytes = RAND_BLK_SIZE;
699
700                         memcpy(buf, &rand_block[RAND_BLK_SIZE - rand_block_bytes], len);
701                         rand_block_bytes -= len;
702
703                         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
704                         }
705
706                 rv = aep_return_connection(hConnection);
707                 if (rv != AEP_R_OK)
708                         {
709                         AEPHKerr(AEPHK_F_AEP_RAND,AEPHK_R_RETURN_CONNECTION_FAILED); 
710           
711                         goto err_nounlock;
712                         }
713                 }
714   
715         return 1;
716  err:
717         CRYPTO_w_unlock(CRYPTO_LOCK_RAND);
718  err_nounlock:
719         return 0;
720         }
721         
722 static int aep_rand_status(void)
723 {
724         return 1;
725 }
726 #endif
727
728 #ifndef OPENSSL_NO_RSA
729 static int aep_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
730         {
731         BN_CTX *ctx = NULL;
732         int to_return = 0;
733         AEP_RV rv = AEP_R_OK;
734
735         if ((ctx = BN_CTX_new()) == NULL)
736                 goto err;
737
738         if (!aep_dso)
739                 {
740                 AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_NOT_LOADED);
741                 goto err;
742                 }
743
744         /*See if we have all the necessary bits for a crt*/
745         if (rsa->q && rsa->dmp1 && rsa->dmq1 && rsa->iqmp)
746                 {
747                 rv =  aep_mod_exp_crt(r0,I,rsa->p,rsa->q, rsa->dmp1,rsa->dmq1,rsa->iqmp,ctx);
748
749                 if (rv == FAIL_TO_SW){
750                         const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
751                         to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
752                         goto err;
753                 }
754                 else if (rv != AEP_R_OK)
755                         goto err;
756                 }
757         else
758                 {
759                 if (!rsa->d || !rsa->n)
760                         {
761                         AEPHKerr(AEPHK_F_AEP_RSA_MOD_EXP,AEPHK_R_MISSING_KEY_COMPONENTS);
762                         goto err;
763                         }
764  
765                 rv = aep_mod_exp(r0,I,rsa->d,rsa->n,ctx);
766                 if  (rv != AEP_R_OK)
767                         goto err;
768         
769                 }
770
771         to_return = 1;
772
773  err:
774         if(ctx)
775                 BN_CTX_free(ctx);
776         return to_return;
777 }
778 #endif
779
780 #ifndef OPENSSL_NO_DSA
781 static int aep_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
782         BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
783         BN_CTX *ctx, BN_MONT_CTX *in_mont)
784         {
785         BIGNUM t;
786         int to_return = 0;
787         BN_init(&t);
788
789         /* let rr = a1 ^ p1 mod m */
790         if (!aep_mod_exp(rr,a1,p1,m,ctx)) goto end;
791         /* let t = a2 ^ p2 mod m */
792         if (!aep_mod_exp(&t,a2,p2,m,ctx)) goto end;
793         /* let rr = rr * t mod m */
794         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
795         to_return = 1;
796  end: 
797         BN_free(&t);
798         return to_return;
799         }
800
801 static int aep_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
802         const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
803         BN_MONT_CTX *m_ctx)
804         {  
805         return aep_mod_exp(r, a, p, m, ctx); 
806         }
807 #endif
808
809 /* This function is aliased to mod_exp (with the mont stuff dropped). */
810 static int aep_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
811         const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
812         {
813         return aep_mod_exp(r, a, p, m, ctx);
814         }
815
816 #ifndef OPENSSL_NO_DH
817 /* This function is aliased to mod_exp (with the dh and mont dropped). */
818 static int aep_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
819         const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
820         BN_MONT_CTX *m_ctx)
821         {
822         return aep_mod_exp(r, a, p, m, ctx);
823         }
824 #endif
825
826 static AEP_RV aep_get_connection(AEP_CONNECTION_HNDL_PTR phConnection)
827         {
828         int count;
829         AEP_RV rv = AEP_R_OK;
830
831         /*Get the current process id*/
832         pid_t curr_pid;
833
834         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
835
836         curr_pid = getpid();
837
838         /*Check if this is the first time this is being called from the current
839           process*/
840         if (recorded_pid != curr_pid)
841                 {
842                 /*Remember our pid so we can check if we're in a new process*/
843                 recorded_pid = curr_pid;
844
845                 /*Call Finalize to make sure we have not inherited some data
846                   from a parent process*/
847                 p_AEP_Finalize();
848      
849                 /*Initialise the AEP API*/
850                 rv = p_AEP_Initialize(NULL);
851
852                 if (rv != AEP_R_OK)
853                         {
854                         AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_INIT_FAILURE);
855                         recorded_pid = 0;
856                         goto end;
857                         }
858
859                 /*Set the AEP big num call back functions*/
860                 rv = p_AEP_SetBNCallBacks(&GetBigNumSize, &MakeAEPBigNum,
861                         &ConvertAEPBigNum);
862
863                 if (rv != AEP_R_OK)
864                         {
865                         AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_SETBNCALLBACK_FAILURE);
866                         recorded_pid = 0;
867                         goto end;
868                         }
869
870 #ifdef AEPRAND
871                 /*Reset the rand byte count*/
872                 rand_block_bytes = 0;
873 #endif
874
875                 /*Init the structures*/
876                 for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
877                         {
878                         aep_app_conn_table[count].conn_state = NotConnected;
879                         aep_app_conn_table[count].conn_hndl  = 0;
880                         }
881
882                 /*Open a connection*/
883                 rv = p_AEP_OpenConnection(phConnection);
884
885                 if (rv != AEP_R_OK)
886                         {
887                         AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
888                         recorded_pid = 0;
889                         goto end;
890                         }
891
892                 aep_app_conn_table[0].conn_state = InUse;
893                 aep_app_conn_table[0].conn_hndl = *phConnection;
894                 goto end;
895                 }
896         /*Check the existing connections to see if we can find a free one*/
897         for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
898                 {
899                 if (aep_app_conn_table[count].conn_state == Connected)
900                         {
901                         aep_app_conn_table[count].conn_state = InUse;
902                         *phConnection = aep_app_conn_table[count].conn_hndl;
903                         goto end;
904                         }
905                 }
906         /*If no connections available, we're going to have to try
907           to open a new one*/
908         for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
909                 {
910                 if (aep_app_conn_table[count].conn_state == NotConnected)
911                         {
912                         /*Open a connection*/
913                         rv = p_AEP_OpenConnection(phConnection);
914
915                         if (rv != AEP_R_OK)
916                                 {             
917                                 AEPHKerr(AEPHK_F_AEP_GET_CONNECTION,AEPHK_R_UNIT_FAILURE);
918                                 goto end;
919                                 }
920
921                         aep_app_conn_table[count].conn_state = InUse;
922                         aep_app_conn_table[count].conn_hndl = *phConnection;
923                         goto end;
924                         }
925                 }
926         rv = AEP_R_GENERAL_ERROR;
927  end:
928         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
929         return rv;
930         }
931
932
933 static AEP_RV aep_return_connection(AEP_CONNECTION_HNDL hConnection)
934         {
935         int count;
936
937         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
938
939         /*Find the connection item that matches this connection handle*/
940         for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
941                 {
942                 if (aep_app_conn_table[count].conn_hndl == hConnection)
943                         {
944                         aep_app_conn_table[count].conn_state = Connected;
945                         break;
946                         }
947                 }
948
949         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
950
951         return AEP_R_OK;
952         }
953
954 static AEP_RV aep_close_connection(AEP_CONNECTION_HNDL hConnection)
955         {
956         int count;
957
958         CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
959
960         /*Find the connection item that matches this connection handle*/
961         for(count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
962                 {
963                 if (aep_app_conn_table[count].conn_hndl == hConnection)
964                         {
965                         aep_app_conn_table[count].conn_state = NotConnected;
966                         close(aep_app_conn_table[count].conn_hndl);
967                         break;
968                         }
969                 }
970
971         CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
972
973         return AEP_R_OK;
974         }
975
976 static AEP_RV aep_close_all_connections(int use_engine_lock, int *in_use)
977         {
978         int count;
979         AEP_RV rv;
980
981         *in_use = 0;
982         if (use_engine_lock) CRYPTO_w_lock(CRYPTO_LOCK_ENGINE);
983         for (count = 0;count < MAX_PROCESS_CONNECTIONS;count ++)
984                 {
985                 switch (aep_app_conn_table[count].conn_state)
986                         {
987                 case Connected:
988                         rv = p_AEP_CloseConnection(aep_app_conn_table[count].conn_hndl);
989                         if (rv != AEP_R_OK)
990                                 return rv;
991                         aep_app_conn_table[count].conn_state = NotConnected;
992                         aep_app_conn_table[count].conn_hndl  = 0;
993                         break;
994                 case InUse:
995                         (*in_use)++;
996                         break;
997                 case NotConnected:
998                         break;
999                         }
1000                 }
1001         if (use_engine_lock) CRYPTO_w_unlock(CRYPTO_LOCK_ENGINE);
1002         return AEP_R_OK;
1003         }
1004
1005 /*BigNum call back functions, used to convert OpenSSL bignums into AEP bignums.
1006   Note only 32bit Openssl build support*/
1007
1008 static AEP_RV GetBigNumSize(void* ArbBigNum, AEP_U32* BigNumSize)
1009         {
1010         BIGNUM* bn;
1011
1012         /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1013         bn = (BIGNUM*) ArbBigNum;
1014
1015 #ifdef SIXTY_FOUR_BIT_LONG
1016         *BigNumSize = bn->top << 3;
1017 #else
1018         /*Size of the bignum in bytes is equal to the bn->top (no of 32 bit
1019           words) multiplies by 4*/
1020         *BigNumSize = bn->top << 2;
1021 #endif
1022
1023         return AEP_R_OK;
1024         }
1025
1026 static AEP_RV MakeAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1027         unsigned char* AEP_BigNum)
1028         {
1029         BIGNUM* bn;
1030
1031 #ifndef SIXTY_FOUR_BIT_LONG
1032         unsigned char* buf;
1033         int i;
1034 #endif
1035
1036         /*Cast the ArbBigNum pointer to our BIGNUM struct*/
1037         bn = (BIGNUM*) ArbBigNum;
1038
1039 #ifdef SIXTY_FOUR_BIT_LONG
1040         memcpy(AEP_BigNum, bn->d, BigNumSize);
1041 #else
1042         /*Must copy data into a (monotone) least significant byte first format
1043           performing endian conversion if necessary*/
1044         for(i=0;i<bn->top;i++)
1045                 {
1046                 buf = (unsigned char*)&bn->d[i];
1047
1048                 *((AEP_U32*)AEP_BigNum) =
1049                         (AEP_U32) ((unsigned) buf[1] << 8 | buf[0]) |
1050                         ((unsigned) buf[3] << 8 | buf[2])  << 16;
1051
1052                 AEP_BigNum += 4;
1053                 }
1054 #endif
1055
1056         return AEP_R_OK;
1057         }
1058
1059 /*Turn an AEP Big Num back to a user big num*/
1060 static AEP_RV ConvertAEPBigNum(void* ArbBigNum, AEP_U32 BigNumSize,
1061         unsigned char* AEP_BigNum)
1062         {
1063         BIGNUM* bn;
1064 #ifndef SIXTY_FOUR_BIT_LONG
1065         int i;
1066 #endif
1067
1068         bn = (BIGNUM*)ArbBigNum;
1069
1070         /*Expand the result bn so that it can hold our big num.
1071           Size is in bits*/
1072         bn_expand(bn, (int)(BigNumSize << 3));
1073
1074 #ifdef SIXTY_FOUR_BIT_LONG
1075         bn->top = BigNumSize >> 3;
1076         
1077         if((BigNumSize & 7) != 0)
1078                 bn->top++;
1079
1080         memset(bn->d, 0, bn->top << 3); 
1081
1082         memcpy(bn->d, AEP_BigNum, BigNumSize);
1083 #else
1084         bn->top = BigNumSize >> 2;
1085  
1086         for(i=0;i<bn->top;i++)
1087                 {
1088                 bn->d[i] = (AEP_U32)
1089                         ((unsigned) AEP_BigNum[3] << 8 | AEP_BigNum[2]) << 16 |
1090                         ((unsigned) AEP_BigNum[1] << 8 | AEP_BigNum[0]);
1091                 AEP_BigNum += 4;
1092                 }
1093 #endif
1094
1095         return AEP_R_OK;
1096 }       
1097         
1098 #endif /* !OPENSSL_NO_HW_AEP */
1099 #endif /* !OPENSSL_NO_HW */