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