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