Certain reasons aren't really part of the engine framework, so let's
[openssl.git] / crypto / engine / hw_ubsec.c
1 /* crypto/engine/hw_ubsec.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  *
5  * Cloned shamelessly by Joe Tardo. 
6  */
7 /* ====================================================================
8  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer. 
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in
19  *    the documentation and/or other materials provided with the
20  *    distribution.
21  *
22  * 3. All advertising materials mentioning features or use of this
23  *    software must display the following acknowledgment:
24  *    "This product includes software developed by the OpenSSL Project
25  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26  *
27  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28  *    endorse or promote products derived from this software without
29  *    prior written permission. For written permission, please contact
30  *    licensing@OpenSSL.org.
31  *
32  * 5. Products derived from this software may not be called "OpenSSL"
33  *    nor may "OpenSSL" appear in their names without prior written
34  *    permission of the OpenSSL Project.
35  *
36  * 6. Redistributions of any form whatsoever must retain the following
37  *    acknowledgment:
38  *    "This product includes software developed by the OpenSSL Project
39  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52  * OF THE POSSIBILITY OF SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This product includes cryptographic software written by Eric Young
56  * (eay@cryptsoft.com).  This product includes software written by Tim
57  * Hudson (tjh@cryptsoft.com).
58  *
59  */
60
61 #include <stdio.h>
62 #include <openssl/crypto.h>
63 #include "cryptlib.h"
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
66
67 #ifndef OPENSSL_NO_HW
68 #ifndef OPENSSL_NO_HW_UBSEC
69
70 #ifdef FLAT_INC
71 #include "hw_ubsec.h"
72 #else
73 #include "vendor_defns/hw_ubsec.h"
74 #endif
75
76 #define UBSEC_LIB_NAME "ubsec engine"
77 #include "hw_ubsec_err.c"
78
79 #define FAIL_TO_SOFTWARE -15
80
81 static int ubsec_destroy(ENGINE *e);
82 static int ubsec_init(ENGINE *e);
83 static int ubsec_finish(ENGINE *e);
84 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
85 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
86                 const BIGNUM *m, BN_CTX *ctx);
87 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
88                         const BIGNUM *q, const BIGNUM *dp,
89                         const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
90 #ifndef OPENSSL_NO_RSA
91 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
92 #endif
93 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
94                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
95 #ifndef OPENSSL_NO_DSA
96 #if NOT_USED
97 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
98                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
99                 BN_CTX *ctx, BN_MONT_CTX *in_mont);
100 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
101                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
102                 BN_MONT_CTX *m_ctx);
103 #endif
104 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
105 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
106                                 DSA_SIG *sig, DSA *dsa);
107 #endif
108 #ifndef OPENSSL_NO_DH
109 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
110                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
111                 BN_MONT_CTX *m_ctx);
112 static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
113 static int ubsec_dh_generate_key(DH *dh);
114 #endif
115
116 #if NOT_USED
117 static int ubsec_rand_bytes(unsigned char *buf, int num);
118 static int ubsec_rand_status(void);
119 #endif
120
121 #define UBSEC_CMD_SO_PATH               ENGINE_CMD_BASE
122 static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
123         {UBSEC_CMD_SO_PATH,
124                 "SO_PATH",
125                 "Specifies the path to the 'ubsec' shared library",
126                 ENGINE_CMD_FLAG_STRING},
127         {0, NULL, NULL, 0}
128         };
129
130 #ifndef OPENSSL_NO_RSA
131 /* Our internal RSA_METHOD that we provide pointers to */
132 static RSA_METHOD ubsec_rsa =
133         {
134         "UBSEC RSA method",
135         NULL,
136         NULL,
137         NULL,
138         NULL,
139         ubsec_rsa_mod_exp,
140         ubsec_mod_exp_mont,
141         NULL,
142         NULL,
143         0,
144         NULL,
145         NULL,
146         NULL
147         };
148 #endif
149
150 #ifndef OPENSSL_NO_DSA
151 /* Our internal DSA_METHOD that we provide pointers to */
152 static DSA_METHOD ubsec_dsa =
153         {
154         "UBSEC DSA method",
155         ubsec_dsa_do_sign, /* dsa_do_sign */
156         NULL, /* dsa_sign_setup */
157         ubsec_dsa_verify, /* dsa_do_verify */
158         NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
159         NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
160         NULL, /* init */
161         NULL, /* finish */
162         0, /* flags */
163         NULL /* app_data */
164         };
165 #endif
166
167 #ifndef OPENSSL_NO_DH
168 /* Our internal DH_METHOD that we provide pointers to */
169 static DH_METHOD ubsec_dh =
170         {
171         "UBSEC DH method",
172         ubsec_dh_generate_key,
173         ubsec_dh_compute_key,
174         ubsec_mod_exp_dh,
175         NULL,
176         NULL,
177         0,
178         NULL
179         };
180 #endif
181
182 /* Constants used when creating the ENGINE */
183 static const char *engine_ubsec_id = "ubsec";
184 static const char *engine_ubsec_name = "UBSEC hardware engine support";
185
186 /* This internal function is used by ENGINE_ubsec() and possibly by the
187  * "dynamic" ENGINE support too */
188 static int bind_helper(ENGINE *e)
189         {
190 #ifndef OPENSSL_NO_RSA
191         const RSA_METHOD *meth1;
192 #endif
193 #ifndef OPENSSL_NO_DH
194 #ifndef HAVE_UBSEC_DH
195         const DH_METHOD *meth3;
196 #endif /* HAVE_UBSEC_DH */
197 #endif
198         if(!ENGINE_set_id(e, engine_ubsec_id) ||
199                         !ENGINE_set_name(e, engine_ubsec_name) ||
200 #ifndef OPENSSL_NO_RSA
201                         !ENGINE_set_RSA(e, &ubsec_rsa) ||
202 #endif
203 #ifndef OPENSSL_NO_DSA
204                         !ENGINE_set_DSA(e, &ubsec_dsa) ||
205 #endif
206 #ifndef OPENSSL_NO_DH
207                         !ENGINE_set_DH(e, &ubsec_dh) ||
208 #endif
209                         !ENGINE_set_destroy_function(e, ubsec_destroy) ||
210                         !ENGINE_set_init_function(e, ubsec_init) ||
211                         !ENGINE_set_finish_function(e, ubsec_finish) ||
212                         !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
213                         !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
214                 return 0;
215
216 #ifndef OPENSSL_NO_RSA
217         /* We know that the "PKCS1_SSLeay()" functions hook properly
218          * to the Broadcom-specific mod_exp and mod_exp_crt so we use
219          * those functions. NB: We don't use ENGINE_openssl() or
220          * anything "more generic" because something like the RSAref
221          * code may not hook properly, and if you own one of these
222          * cards then you have the right to do RSA operations on it
223          * anyway! */ 
224         meth1 = RSA_PKCS1_SSLeay();
225         ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
226         ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
227         ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
228         ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
229 #endif
230
231 #ifndef OPENSSL_NO_DH
232 #ifndef HAVE_UBSEC_DH
233         /* Much the same for Diffie-Hellman */
234         meth3 = DH_OpenSSL();
235         ubsec_dh.generate_key = meth3->generate_key;
236         ubsec_dh.compute_key = meth3->compute_key;
237 #endif /* HAVE_UBSEC_DH */
238 #endif
239
240         /* Ensure the ubsec error handling is set up */
241         ERR_load_UBSEC_strings();
242         return 1;
243         }
244
245 static ENGINE *engine_ubsec(void)
246         {
247         ENGINE *ret = ENGINE_new();
248         if(!ret)
249                 return NULL;
250         if(!bind_helper(ret))
251                 {
252                 ENGINE_free(ret);
253                 return NULL;
254                 }
255         return ret;
256         }
257
258 void ENGINE_load_ubsec(void)
259         {
260         /* Copied from eng_[openssl|dyn].c */
261         ENGINE *toadd = engine_ubsec();
262         if(!toadd) return;
263         ENGINE_add(toadd);
264         ENGINE_free(toadd);
265         ERR_clear_error();
266         }
267
268 /* This is a process-global DSO handle used for loading and unloading
269  * the UBSEC library. NB: This is only set (or unset) during an
270  * init() or finish() call (reference counts permitting) and they're
271  * operating with global locks, so this should be thread-safe
272  * implicitly. */
273
274 static DSO *ubsec_dso = NULL;
275
276 /* These are the function pointers that are (un)set when the library has
277  * successfully (un)loaded. */
278
279 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
280 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
281 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
282 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
283 #ifndef OPENSSL_NO_DH
284 static t_UBSEC_diffie_hellman_generate_ioctl 
285         *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
286 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
287 #endif
288 /* #ifndef OPENSSL_NO_RSA */
289 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
290 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
291 /* #endif */
292 #ifndef OPENSSL_NO_DSA
293 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
294 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
295 #endif
296 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
297 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
298 static t_UBSEC_max_key_len_ioctl *p_UBSEC_max_key_len_ioctl = NULL;
299
300 static int max_key_len = 1024;  /* ??? */
301
302 /* 
303  * These are the static string constants for the DSO file name and the function
304  * symbol names to bind to. 
305  */
306
307 static const char *UBSEC_LIBNAME = "ubsec";
308 static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
309 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
310 static const char *UBSEC_F3 = "ubsec_open";
311 static const char *UBSEC_F4 = "ubsec_close";
312 #ifndef OPENSSL_NO_DH
313 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
314 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
315 #endif
316 /* #ifndef OPENSSL_NO_RSA */
317 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
318 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
319 /* #endif */
320 #ifndef OPENSSL_NO_DSA
321 static const char *UBSEC_F9 = "dsa_sign_ioctl";
322 static const char *UBSEC_F10 = "dsa_verify_ioctl";
323 #endif
324 static const char *UBSEC_F11 = "math_accelerate_ioctl";
325 static const char *UBSEC_F12 = "rng_ioctl";
326 static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl";
327
328 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
329 static int ubsec_destroy(ENGINE *e)
330         {
331         ERR_unload_UBSEC_strings();
332         return 1;
333         }
334
335 /* (de)initialisation functions. */
336 static int ubsec_init(ENGINE *e)
337         {
338         t_UBSEC_ubsec_bytes_to_bits *p1;
339         t_UBSEC_ubsec_bits_to_bytes *p2;
340         t_UBSEC_ubsec_open *p3;
341         t_UBSEC_ubsec_close *p4;
342 #ifndef OPENSSL_NO_DH
343         t_UBSEC_diffie_hellman_generate_ioctl *p5;
344         t_UBSEC_diffie_hellman_agree_ioctl *p6;
345 #endif
346 /* #ifndef OPENSSL_NO_RSA */
347         t_UBSEC_rsa_mod_exp_ioctl *p7;
348         t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
349 /* #endif */
350 #ifndef OPENSSL_NO_DSA
351         t_UBSEC_dsa_sign_ioctl *p9;
352         t_UBSEC_dsa_verify_ioctl *p10;
353 #endif
354         t_UBSEC_math_accelerate_ioctl *p11;
355         t_UBSEC_rng_ioctl *p12;
356         t_UBSEC_max_key_len_ioctl *p13;
357         int fd = 0;
358
359         if(ubsec_dso != NULL)
360                 {
361                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
362                 goto err;
363                 }
364         /* 
365          * Attempt to load libubsec.so/ubsec.dll/whatever. 
366          */
367         ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
368         if(ubsec_dso == NULL)
369                 {
370                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
371                 goto err;
372                 }
373
374         if (
375         !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
376         !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
377         !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
378         !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
379 #ifndef OPENSSL_NO_DH
380         !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) 
381                                 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
382         !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) 
383                                 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
384 #endif
385 /* #ifndef OPENSSL_NO_RSA */
386         !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
387         !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
388 /* #endif */
389 #ifndef OPENSSL_NO_DSA
390         !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
391         !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
392 #endif
393         !(p11 = (t_UBSEC_math_accelerate_ioctl *) 
394                                 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
395         !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) ||
396         !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13)))
397                 {
398                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
399                 goto err;
400                 }
401
402         /* Copy the pointers */
403         p_UBSEC_ubsec_bytes_to_bits = p1;
404         p_UBSEC_ubsec_bits_to_bytes = p2;
405         p_UBSEC_ubsec_open = p3;
406         p_UBSEC_ubsec_close = p4;
407 #ifndef OPENSSL_NO_DH
408         p_UBSEC_diffie_hellman_generate_ioctl = p5;
409         p_UBSEC_diffie_hellman_agree_ioctl = p6;
410 #endif
411 #ifndef OPENSSL_NO_RSA
412         p_UBSEC_rsa_mod_exp_ioctl = p7;
413         p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
414 #endif
415 #ifndef OPENSSL_NO_DSA
416         p_UBSEC_dsa_sign_ioctl = p9;
417         p_UBSEC_dsa_verify_ioctl = p10;
418 #endif
419         p_UBSEC_math_accelerate_ioctl = p11;
420         p_UBSEC_rng_ioctl = p12;
421         p_UBSEC_max_key_len_ioctl = p13;
422
423         /* Perform an open to see if there's actually any unit running. */
424         if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
425         {
426            p_UBSEC_ubsec_close(fd);
427            return 1;
428         }
429         else
430         {
431           UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
432         }
433
434 err:
435         if(ubsec_dso)
436                 DSO_free(ubsec_dso);
437         p_UBSEC_ubsec_bytes_to_bits = NULL;
438         p_UBSEC_ubsec_bits_to_bytes = NULL;
439         p_UBSEC_ubsec_open = NULL;
440         p_UBSEC_ubsec_close = NULL;
441 #ifndef OPENSSL_NO_DH
442         p_UBSEC_diffie_hellman_generate_ioctl = NULL;
443         p_UBSEC_diffie_hellman_agree_ioctl = NULL;
444 #endif
445 #ifndef OPENSSL_NO_RSA
446         p_UBSEC_rsa_mod_exp_ioctl = NULL;
447         p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
448 #endif
449 #ifndef OPENSSL_NO_DSA
450         p_UBSEC_dsa_sign_ioctl = NULL;
451         p_UBSEC_dsa_verify_ioctl = NULL;
452 #endif
453         p_UBSEC_math_accelerate_ioctl = NULL;
454         p_UBSEC_rng_ioctl = NULL;
455         p_UBSEC_max_key_len_ioctl = NULL;
456
457         return 0;
458         }
459
460 static int ubsec_finish(ENGINE *e)
461         {
462         if(ubsec_dso == NULL)
463                 {
464                 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
465                 return 0;
466                 }
467         if(!DSO_free(ubsec_dso))
468                 {
469                 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
470                 return 0;
471                 }
472         ubsec_dso = NULL;
473         p_UBSEC_ubsec_bytes_to_bits = NULL;
474         p_UBSEC_ubsec_bits_to_bytes = NULL;
475         p_UBSEC_ubsec_open = NULL;
476         p_UBSEC_ubsec_close = NULL;
477 #ifndef OPENSSL_NO_DH
478         p_UBSEC_diffie_hellman_generate_ioctl = NULL;
479         p_UBSEC_diffie_hellman_agree_ioctl = NULL;
480 #endif
481 #ifndef OPENSSL_NO_RSA
482         p_UBSEC_rsa_mod_exp_ioctl = NULL;
483         p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
484 #endif
485 #ifndef OPENSSL_NO_DSA
486         p_UBSEC_dsa_sign_ioctl = NULL;
487         p_UBSEC_dsa_verify_ioctl = NULL;
488 #endif
489         p_UBSEC_math_accelerate_ioctl = NULL;
490         p_UBSEC_rng_ioctl = NULL;
491         p_UBSEC_max_key_len_ioctl = NULL;
492         return 1;
493         }
494
495 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
496         {
497         int initialised = ((ubsec_dso == NULL) ? 0 : 1);
498         switch(cmd)
499                 {
500         case UBSEC_CMD_SO_PATH:
501                 if(p == NULL)
502                         {
503                         UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
504                         return 0;
505                         }
506                 if(initialised)
507                         {
508                         UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
509                         return 0;
510                         }
511                 UBSEC_LIBNAME = (const char *)p;
512                 return 1;
513         default:
514                 break;
515                 }
516         UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
517         return 0;
518         }
519
520 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
521                 const BIGNUM *m, BN_CTX *ctx)
522         {
523         int     y_len = 0;
524         int     fd;
525
526         if(ubsec_dso == NULL)
527         {
528                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
529                 return 0;
530         }
531
532         /* Check if hardware can't handle this argument. */
533         y_len = BN_num_bits(m);
534         if (y_len > max_key_len) {
535                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
536                 return BN_mod_exp(r, a, p, m, ctx);
537         } 
538
539         if(!bn_wexpand(r, m->top))
540         {
541                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
542                 return 0;
543         }
544         memset(r->d, 0, BN_num_bytes(m));
545
546         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
547                 fd = 0;
548                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
549                 return BN_mod_exp(r, a, p, m, ctx);
550         }
551
552         if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
553                 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d, 
554                 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
555         {
556                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
557                 p_UBSEC_ubsec_close(fd);
558
559                 return BN_mod_exp(r, a, p, m, ctx);
560         }
561
562         p_UBSEC_ubsec_close(fd);
563
564         r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
565         return 1;
566         }
567
568 #ifndef OPENSSL_NO_RSA
569 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
570         {
571         BN_CTX *ctx;
572         int to_return = 0;
573
574         if((ctx = BN_CTX_new()) == NULL)
575                 goto err;
576
577         if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
578                 {
579                 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
580                 goto err;
581                 }
582
583         to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
584                     rsa->dmq1, rsa->iqmp, ctx);
585         if (to_return == FAIL_TO_SOFTWARE)
586         {
587           /*
588            * Do in software as hardware failed.
589            */
590            const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
591            to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
592         }
593 err:
594         if(ctx)
595                 BN_CTX_free(ctx);
596         return to_return;
597         }
598 #endif
599
600 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
601                         const BIGNUM *q, const BIGNUM *dp,
602                         const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
603         {
604         int     y_len,
605                 m_len,
606                 fd;
607
608         m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
609         y_len = BN_num_bits(p) + BN_num_bits(q);
610
611         /* Check if hardware can't handle this argument. */
612         if (y_len > max_key_len) {
613                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
614                 return FAIL_TO_SOFTWARE;
615         } 
616
617         if (!bn_wexpand(r, p->top + q->top + 1)) {
618                 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
619                 return 0;
620         }
621
622         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
623                 fd = 0;
624                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
625                 return FAIL_TO_SOFTWARE;
626         }
627
628         if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
629                 (unsigned char *)a->d, BN_num_bits(a), 
630                 (unsigned char *)qinv->d, BN_num_bits(qinv),
631                 (unsigned char *)dp->d, BN_num_bits(dp),
632                 (unsigned char *)p->d, BN_num_bits(p),
633                 (unsigned char *)dq->d, BN_num_bits(dq),
634                 (unsigned char *)q->d, BN_num_bits(q),
635                 (unsigned char *)r->d,  &y_len) != 0) {
636                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
637                 p_UBSEC_ubsec_close(fd);
638                 return FAIL_TO_SOFTWARE;
639         }
640
641         p_UBSEC_ubsec_close(fd);
642
643         r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
644         return 1;
645 }
646
647 #ifndef OPENSSL_NO_DSA
648 #if NOT_USED
649 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
650                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
651                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
652         {
653         BIGNUM t;
654         int to_return = 0;
655  
656         BN_init(&t);
657         /* let rr = a1 ^ p1 mod m */
658         if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
659         /* let t = a2 ^ p2 mod m */
660         if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
661         /* let rr = rr * t mod m */
662         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
663         to_return = 1;
664 end:
665         BN_free(&t);
666         return to_return;
667         }
668
669 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
670                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
671                 BN_MONT_CTX *m_ctx)
672         {
673         return ubsec_mod_exp(r, a, p, m, ctx);
674         }
675 #endif
676 #endif
677
678 /*
679  * This function is aliased to mod_exp (with the mont stuff dropped).
680  */
681 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
682                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
683         {
684         int ret = 0;
685
686 #ifndef OPENSSL_NO_RSA
687         /* Do in software if the key is too large for the hardware. */
688         if (BN_num_bits(m) > max_key_len)
689                 {
690                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
691                 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
692                 }
693         else
694 #endif
695                 {
696                 ret = ubsec_mod_exp(r, a, p, m, ctx);
697                 }
698         
699         return ret;
700         }
701
702 #ifndef OPENSSL_NO_DH
703 /* This function is aliased to mod_exp (with the dh and mont dropped). */
704 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
705                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
706                 BN_MONT_CTX *m_ctx)
707         {
708         return ubsec_mod_exp(r, a, p, m, ctx);
709         }
710 #endif
711
712 #ifndef OPENSSL_NO_DSA
713 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
714         {
715         DSA_SIG *to_return = NULL;
716         int s_len = 160, r_len = 160, d_len, fd;
717         BIGNUM m, *r=NULL, *s=NULL;
718
719         BN_init(&m);
720
721         s = BN_new();
722         r = BN_new();
723         if ((s == NULL) || (r==NULL))
724                 goto err;
725
726         d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
727
728         if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
729            (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
730                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
731                 goto err;
732         }
733
734         if (BN_bin2bn(dgst,dlen,&m) == NULL) {
735                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
736                 goto err;
737         } 
738
739         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
740                 const DSA_METHOD *meth;
741                 fd = 0;
742                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
743                 meth = DSA_OpenSSL();
744                 to_return =  meth->dsa_do_sign(dgst, dlen, dsa);
745                 goto err;
746         }
747
748         if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
749                 (unsigned char *)dgst, d_len,
750                 NULL, 0,  /* compute random value */
751                 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
752                 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
753                 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
754                 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
755                 (unsigned char *)r->d, &r_len,
756                 (unsigned char *)s->d, &s_len ) != 0) {
757                 const DSA_METHOD *meth;
758
759                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
760                 p_UBSEC_ubsec_close(fd);
761                 meth = DSA_OpenSSL();
762                 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
763
764                 goto err;
765         }
766
767         p_UBSEC_ubsec_close(fd);
768
769         r->top = (160+BN_BITS2-1)/BN_BITS2;
770         s->top = (160+BN_BITS2-1)/BN_BITS2;
771
772         to_return = DSA_SIG_new();
773         if(to_return == NULL) {
774                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
775                 goto err;
776         }
777
778         to_return->r = r;
779         to_return->s = s;
780
781 err:
782         if (!to_return) {
783                 if (r) BN_free(r);
784                 if (s) BN_free(s);
785         }                                 
786         BN_clear_free(&m);
787         return to_return;
788 }
789
790 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
791                                 DSA_SIG *sig, DSA *dsa)
792         {
793         int v_len, d_len;
794         int to_return = 0;
795         int fd;
796         BIGNUM v;
797
798         BN_init(&v);
799
800         if(!bn_wexpand(&v, dsa->p->top)) {
801                 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
802                 goto err;
803         }
804
805         v_len = BN_num_bits(dsa->p);
806
807         d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
808
809         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
810                 const DSA_METHOD *meth;
811                 fd = 0;
812                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
813                 meth = DSA_OpenSSL();
814                 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
815                 goto err;
816         }
817
818         if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
819                 (unsigned char *)dgst, d_len,
820                 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
821                 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
822                 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
823                 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
824                 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
825                 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
826                 (unsigned char *)v.d, &v_len) != 0) {
827                 const DSA_METHOD *meth;
828                 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
829                 p_UBSEC_ubsec_close(fd);
830
831                 meth = DSA_OpenSSL();
832                 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
833
834                 goto err;
835         }
836
837         p_UBSEC_ubsec_close(fd);
838
839         to_return = 1;
840 err:
841         BN_clear_free(&v);
842         return to_return;
843         }
844 #endif
845
846 #ifndef OPENSSL_NO_DH
847 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
848         {
849         int      ret      = -1,
850                  k_len,
851                  fd;
852
853         k_len = BN_num_bits(dh->p);
854
855         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
856                 {
857                 const DH_METHOD *meth;
858                 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
859                 meth = DH_OpenSSL();
860                 ret = meth->compute_key(key, pub_key, dh);
861                 goto err;
862                 }
863
864         if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
865                                                (unsigned char *)dh->priv_key->d, BN_num_bits(dh->priv_key),
866                                                (unsigned char *)pub_key->d, BN_num_bits(pub_key),
867                                                (unsigned char *)dh->p->d, BN_num_bits(dh->p),
868                                                key, &k_len) != 0)
869                 {
870                 /* Hardware's a no go, failover to software */
871                 const DH_METHOD *meth;
872                 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
873                 p_UBSEC_ubsec_close(fd);
874
875                 meth = DH_OpenSSL();
876                 ret = meth->compute_key(key, pub_key, dh);
877
878                 goto err;
879                 }
880
881         p_UBSEC_ubsec_close(fd);
882
883         ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
884 err:
885         return ret;
886         }
887
888 static int ubsec_dh_generate_key (DH *dh)
889         {
890         int      ret               = 0,
891                  random_bits       = 0,
892                  pub_key_len       = 0,
893                  priv_key_len      = 0,
894                  fd;
895         BIGNUM   *pub_key          = NULL;
896         BIGNUM   *priv_key         = NULL;
897
898         /* 
899          *  How many bits should Random x be? dh_key.c
900          *  sets the range from 0 to num_bits(modulus) ???
901          */
902
903         if (dh->priv_key == NULL)
904                 {
905                 priv_key = BN_new();
906                 if (priv_key == NULL) goto err;
907                 priv_key_len = BN_num_bits(dh->p);
908                 bn_wexpand(priv_key, dh->p->top);
909                 do
910                         if (!BN_rand_range(priv_key, dh->p)) goto err;
911                 while (BN_is_zero(priv_key));
912                 random_bits = BN_num_bits(priv_key);
913                 }
914         else
915                 {
916                 priv_key = dh->priv_key;
917                 }
918
919         if (dh->pub_key == NULL)
920                 {
921                 pub_key = BN_new();
922                 pub_key_len = BN_num_bits(dh->p);
923                 bn_wexpand(pub_key, dh->p->top);
924                 if(pub_key == NULL) goto err;
925                 }
926         else
927                 {
928                 pub_key = dh->pub_key;
929                 }
930
931         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
932                 {
933                 const DH_METHOD *meth;
934                 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
935                 meth = DH_OpenSSL();
936                 ret = meth->generate_key(dh);
937                 goto err;
938                 }
939
940         if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
941                                                   (unsigned char *)priv_key->d, &priv_key_len,
942                                                   (unsigned char *)pub_key->d,  &pub_key_len,
943                                                   (unsigned char *)dh->g->d, BN_num_bits(dh->g),
944                                                   (unsigned char *)dh->p->d, BN_num_bits(dh->p),
945                                                   0, 0, random_bits) != 0)
946                 {
947                 /* Hardware's a no go, failover to software */
948                 const DH_METHOD *meth;
949
950                 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
951                 p_UBSEC_ubsec_close(fd);
952
953                 meth = DH_OpenSSL();
954                 ret = meth->generate_key(dh);
955
956                 goto err;
957                 }
958
959         p_UBSEC_ubsec_close(fd);
960
961         dh->pub_key = pub_key;
962         dh->pub_key->top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
963         dh->priv_key = priv_key;
964         dh->priv_key->top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
965
966         ret = 1;
967 err:
968         return ret;
969         }
970 #endif
971
972 #if NOT_USED
973 static int ubsec_rand_bytes(unsigned char * buf,
974                             int num)
975         {
976         int      ret      = 0,
977                  fd;
978
979         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
980                 {
981                 const RAND_METHOD *meth;
982                 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
983                 num = p_UBSEC_ubsec_bits_to_bytes(num);
984                 meth = RAND_SSLeay();
985                 meth->seed(buf, num);
986                 ret = meth->bytes(buf, num);
987                 goto err;
988                 }
989
990         num *= 8; /* bytes to bits */
991
992         if (p_UBSEC_rng_ioctl(fd,
993                               UBSEC_RNG_DIRECT,
994                               buf,
995                               &num) != 0)
996                 {
997                 /* Hardware's a no go, failover to software */
998                 const RAND_METHOD *meth;
999
1000                 ENGINEerr(UBSEC_F_UBSEC_RNG_BYTES, UBSEC_R_REQUEST_FAILED);
1001                 p_UBSEC_ubsec_close(fd);
1002
1003                 num = p_UBSEC_ubsec_bits_to_bytes(num);
1004                 meth = RAND_SSLeay();
1005                 meth->seed(buf, num);
1006                 ret = meth->bytes(buf, num);
1007
1008                 goto err;
1009                 }
1010
1011         p_UBSEC_ubsec_close(fd);
1012
1013         ret = 1;
1014 err:
1015         return(ret);
1016         }
1017
1018
1019 static int ubsec_rand_status(void)
1020         {
1021         return 0;
1022         }
1023 #endif
1024
1025 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1026  * shared-library. */
1027 #ifdef ENGINE_DYNAMIC_SUPPORT
1028 static int bind_fn(ENGINE *e, const char *id)
1029         {
1030         if(id && (strcmp(id, engine_ubsec_id) != 0))
1031                 return 0;
1032         if(!bind_helper(e))
1033                 return 0;
1034         return 1;
1035         }
1036 IMPLEMENT_DYNAMIC_CHECK_FN()
1037 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1038 #endif /* ENGINE_DYNAMIC_SUPPORT */
1039
1040 #endif /* !OPENSSL_NO_HW_UBSEC */
1041 #endif /* !OPENSSL_NO_HW */