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