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