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