Some of the ENGINE file names were changed for 8.3 filename uniqueness
[openssl.git] / crypto / engine / hw_ubsec.c
1 /* crypto/engine/hw_ubsec.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
3  * project 2000.
4  *
5  * Cloned shamelessly by Joe Tardo. 
6  */
7 /* ====================================================================
8  * Copyright (c) 1999-2001 The OpenSSL Project.  All rights reserved.
9  *
10  * Redistribution and use in source and binary forms, with or without
11  * modification, are permitted provided that the following conditions
12  * are met:
13  *
14  * 1. Redistributions of source code must retain the above copyright
15  *    notice, this list of conditions and the following disclaimer. 
16  *
17  * 2. Redistributions in binary form must reproduce the above copyright
18  *    notice, this list of conditions and the following disclaimer in
19  *    the documentation and/or other materials provided with the
20  *    distribution.
21  *
22  * 3. All advertising materials mentioning features or use of this
23  *    software must display the following acknowledgment:
24  *    "This product includes software developed by the OpenSSL Project
25  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26  *
27  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
28  *    endorse or promote products derived from this software without
29  *    prior written permission. For written permission, please contact
30  *    licensing@OpenSSL.org.
31  *
32  * 5. Products derived from this software may not be called "OpenSSL"
33  *    nor may "OpenSSL" appear in their names without prior written
34  *    permission of the OpenSSL Project.
35  *
36  * 6. Redistributions of any form whatsoever must retain the following
37  *    acknowledgment:
38  *    "This product includes software developed by the OpenSSL Project
39  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40  *
41  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
42  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
43  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
44  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
45  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
46  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
47  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
48  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
49  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
50  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
51  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
52  * OF THE POSSIBILITY OF SUCH DAMAGE.
53  * ====================================================================
54  *
55  * This product includes cryptographic software written by Eric Young
56  * (eay@cryptsoft.com).  This product includes software written by Tim
57  * Hudson (tjh@cryptsoft.com).
58  *
59  */
60
61 #include <stdio.h>
62 #include <openssl/crypto.h>
63 #include "cryptlib.h"
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
66
67 #ifndef OPENSSL_NO_HW
68 #ifndef OPENSSL_NO_HW_UBSEC
69
70 #ifdef FLAT_INC
71 #include "hw_ubsec.h"
72 #else
73 #include "vendor_defns/hw_ubsec.h"
74 #endif
75
76 static int ubsec_destroy(ENGINE *e);
77 static int ubsec_init(ENGINE *e);
78 static int ubsec_finish(ENGINE *e);
79 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
80 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
81                 const BIGNUM *m, BN_CTX *ctx);
82 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
83                         const BIGNUM *q, const BIGNUM *dp,
84                         const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
85 #ifndef OPENSSL_NO_RSA
86 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
87 #endif
88 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
89                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
90 #ifndef OPENSSL_NO_DSA
91 #if NOT_USED
92 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
93                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
94                 BN_CTX *ctx, BN_MONT_CTX *in_mont);
95 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
96                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
97                 BN_MONT_CTX *m_ctx);
98 #endif
99 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
100 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
101                                 DSA_SIG *sig, DSA *dsa);
102 #endif
103 #ifndef OPENSSL_NO_DH
104 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
105                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
106                 BN_MONT_CTX *m_ctx);
107 static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
108 static int ubsec_dh_generate_key(DH *dh);
109 #endif
110
111 #if NOT_USED
112 static int ubsec_rand_bytes(unsigned char *buf, int num);
113 static int ubsec_rand_status(void);
114 #endif
115
116 #define UBSEC_CMD_SO_PATH               ENGINE_CMD_BASE
117 static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
118         {UBSEC_CMD_SO_PATH,
119                 "SO_PATH",
120                 "Specifies the path to the 'ubsec' shared library",
121                 ENGINE_CMD_FLAG_STRING},
122         {0, NULL, NULL, 0}
123         };
124
125 #ifndef OPENSSL_NO_RSA
126 /* Our internal RSA_METHOD that we provide pointers to */
127 static RSA_METHOD ubsec_rsa =
128         {
129         "UBSEC RSA method",
130         NULL,
131         NULL,
132         NULL,
133         NULL,
134         ubsec_rsa_mod_exp,
135         ubsec_mod_exp_mont,
136         NULL,
137         NULL,
138         0,
139         NULL,
140         NULL,
141         NULL
142         };
143 #endif
144
145 #ifndef OPENSSL_NO_DSA
146 /* Our internal DSA_METHOD that we provide pointers to */
147 static DSA_METHOD ubsec_dsa =
148         {
149         "UBSEC DSA method",
150         ubsec_dsa_do_sign, /* dsa_do_sign */
151         NULL, /* dsa_sign_setup */
152         ubsec_dsa_verify, /* dsa_do_verify */
153         NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
154         NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
155         NULL, /* init */
156         NULL, /* finish */
157         0, /* flags */
158         NULL /* app_data */
159         };
160 #endif
161
162 #ifndef OPENSSL_NO_DH
163 /* Our internal DH_METHOD that we provide pointers to */
164 static DH_METHOD ubsec_dh =
165         {
166         "UBSEC DH method",
167         ubsec_dh_generate_key,
168         ubsec_dh_compute_key,
169         ubsec_mod_exp_dh,
170         NULL,
171         NULL,
172         0,
173         NULL
174         };
175 #endif
176
177 #ifndef OPENSSL_NO_ERR
178 /* Error function codes for use in ubsec operation */
179 #define UBSEC_F_UBSEC_INIT                      100
180 #define UBSEC_F_UBSEC_FINISH                    101
181 #define UBSEC_F_UBSEC_CTRL                      102
182 #define UBSEC_F_UBSEC_MOD_EXP                   103
183 #define UBSEC_F_UBSEC_RSA_MOD_EXP               104
184 #define UBSEC_F_UBSEC_RSA_MOD_EXP_CRT           105
185 #define UBSEC_F_UBSEC_DSA_SIGN                  106
186 #define UBSEC_F_UBSEC_DSA_VERIFY                107
187 /* Error reason codes */
188 #define UBSEC_R_ALREADY_LOADED                  108
189 #define UBSEC_R_DSO_FAILURE                     109
190 #define UBSEC_R_UNIT_FAILURE                    110
191 #define UBSEC_R_NOT_LOADED                      111
192 #define UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED    112
193 #define UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL     113
194 #define UBSEC_R_BN_EXPAND_FAIL                  114
195 #define UBSEC_R_REQUEST_FAILED                  115
196 #define UBSEC_R_MISSING_KEY_COMPONENTS          116
197 static ERR_STRING_DATA ubsec_str_functs[] =
198         {
199         /* This first element is changed to match the dynamic 'lib' number */
200 {ERR_PACK(0,0,0),                               "ubsec engine code"},
201 {ERR_PACK(0,UBSEC_F_UBSEC_INIT,0),              "ubsec_init"},
202 {ERR_PACK(0,UBSEC_F_UBSEC_FINISH,0),            "ubsec_finish"},
203 {ERR_PACK(0,UBSEC_F_UBSEC_CTRL,0),              "ubsec_ctrl"},
204 {ERR_PACK(0,UBSEC_F_UBSEC_MOD_EXP,0),           "ubsec_mod_exp"},
205 {ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP,0),       "ubsec_rsa_mod_exp"},
206 {ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP_CRT,0),   "ubsec_rsa_mod_exp_crt"},
207 {ERR_PACK(0,UBSEC_F_UBSEC_DSA_SIGN,0),          "ubsec_dsa_sign"},
208 {ERR_PACK(0,UBSEC_F_UBSEC_DSA_VERIFY,0),        "ubsec_dsa_verify"},
209 /* Error reason codes */
210 {UBSEC_R_ALREADY_LOADED                 ,"already loaded"},
211 {UBSEC_R_DSO_FAILURE                    ,"DSO failure"},
212 {UBSEC_R_UNIT_FAILURE                   ,"unit failure"},
213 {UBSEC_R_NOT_LOADED                     ,"not loaded"},
214 {UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED   ,"ctrl command not implemented"},
215 {UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL    ,"size too large or too small"},
216 {UBSEC_R_BN_EXPAND_FAIL                 ,"bn_expand fail"},
217 {UBSEC_R_REQUEST_FAILED                 ,"request failed"},
218 {UBSEC_R_MISSING_KEY_COMPONENTS         ,"missing key components"},
219 {0,NULL}
220         };
221 /* The library number we obtain dynamically from the ERR code */
222 static int ubsec_err_lib = -1;
223 #define UBSECerr(f,r) ERR_PUT_error(ubsec_err_lib,(f),(r),__FILE__,__LINE__)
224 static void ubsec_load_error_strings(void)
225         {
226         if(ubsec_err_lib < 0)
227                 {
228                 if((ubsec_err_lib = ERR_get_next_error_library()) <= 0)
229                         return;
230                 ubsec_str_functs[0].error = ERR_PACK(ubsec_err_lib,0,0);
231                 ERR_load_strings(ubsec_err_lib, ubsec_str_functs);
232                 }
233         }
234 static void ubsec_unload_error_strings(void)
235         {
236         if(ubsec_err_lib >= 0)
237                 {
238                 ERR_unload_strings(ubsec_err_lib, ubsec_str_functs);
239                 ubsec_err_lib = -1;
240                 }
241         }
242 #else
243 #define UBSECerr(f,r)                                   /* NOP */
244 static void ubsec_load_error_strings(void) { }          /* NOP */
245 static void ubsec_unload_error_strings(void) { }        /* NOP */
246 #endif
247
248 /* Constants used when creating the ENGINE */
249 static const char *engine_ubsec_id = "ubsec";
250 static const char *engine_ubsec_name = "UBSEC hardware engine support";
251
252 /* This internal function is used by ENGINE_ubsec() and possibly by the
253  * "dynamic" ENGINE support too */
254 static int bind_helper(ENGINE *e)
255         {
256 #ifndef OPENSSL_NO_RSA
257         const RSA_METHOD *meth1;
258 #endif
259 #ifndef OPENSSL_NO_DH
260 #ifndef HAVE_UBSEC_DH
261         const DH_METHOD *meth3;
262 #endif /* HAVE_UBSEC_DH */
263 #endif
264         if(!ENGINE_set_id(e, engine_ubsec_id) ||
265                         !ENGINE_set_name(e, engine_ubsec_name) ||
266 #ifndef OPENSSL_NO_RSA
267                         !ENGINE_set_RSA(e, &ubsec_rsa) ||
268 #endif
269 #ifndef OPENSSL_NO_DSA
270                         !ENGINE_set_DSA(e, &ubsec_dsa) ||
271 #endif
272 #ifndef OPENSSL_NO_DH
273                         !ENGINE_set_DH(e, &ubsec_dh) ||
274 #endif
275                         !ENGINE_set_BN_mod_exp(e, ubsec_mod_exp) ||
276                         !ENGINE_set_BN_mod_exp_crt(e, ubsec_mod_exp_crt) ||
277                         !ENGINE_set_destroy_function(e, ubsec_destroy) ||
278                         !ENGINE_set_init_function(e, ubsec_init) ||
279                         !ENGINE_set_finish_function(e, ubsec_finish) ||
280                         !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
281                         !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
282                 return 0;
283
284 #ifndef OPENSSL_NO_RSA
285         /* We know that the "PKCS1_SSLeay()" functions hook properly
286          * to the Broadcom-specific mod_exp and mod_exp_crt so we use
287          * those functions. NB: We don't use ENGINE_openssl() or
288          * anything "more generic" because something like the RSAref
289          * code may not hook properly, and if you own one of these
290          * cards then you have the right to do RSA operations on it
291          * anyway! */ 
292         meth1 = RSA_PKCS1_SSLeay();
293         ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
294         ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
295         ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
296         ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
297 #endif
298
299 #ifndef OPENSSL_NO_DH
300 #ifndef HAVE_UBSEC_DH
301         /* Much the same for Diffie-Hellman */
302         meth3 = DH_OpenSSL();
303         ubsec_dh.generate_key = meth3->generate_key;
304         ubsec_dh.compute_key = meth3->compute_key;
305 #endif /* HAVE_UBSEC_DH */
306 #endif
307
308         /* Ensure the ubsec error handling is set up */
309         ubsec_load_error_strings();
310         return 1;
311         }
312
313 /* As this is only ever called once, there's no need for locking
314  * (indeed - the lock will already be held by our caller!!!) */
315 ENGINE *ENGINE_ubsec(void)
316         {
317         ENGINE *ret = ENGINE_new();
318         if(!ret)
319                 return NULL;
320         if(!bind_helper(ret))
321                 {
322                 ENGINE_free(ret);
323                 return NULL;
324                 }
325         return ret;
326         }
327
328 /* This is a process-global DSO handle used for loading and unloading
329  * the UBSEC library. NB: This is only set (or unset) during an
330  * init() or finish() call (reference counts permitting) and they're
331  * operating with global locks, so this should be thread-safe
332  * implicitly. */
333
334 static DSO *ubsec_dso = NULL;
335
336 /* These are the function pointers that are (un)set when the library has
337  * successfully (un)loaded. */
338
339 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
340 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
341 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
342 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
343 #ifndef OPENSSL_NO_DH
344 static t_UBSEC_diffie_hellman_generate_ioctl 
345         *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
346 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
347 #endif
348 /* #ifndef OPENSSL_NO_RSA */
349 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
350 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
351 /* #endif */
352 #ifndef OPENSSL_NO_DSA
353 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
354 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
355 #endif
356 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
357 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
358
359 /* 
360  * These are the static string constants for the DSO file name and the function
361  * symbol names to bind to. 
362  */
363
364 static const char *UBSEC_LIBNAME = "ubsec";
365 static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
366 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
367 static const char *UBSEC_F3 = "ubsec_open";
368 static const char *UBSEC_F4 = "ubsec_close";
369 #ifndef OPENSSL_NO_DH
370 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
371 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
372 #endif
373 /* #ifndef OPENSSL_NO_RSA */
374 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
375 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
376 /* #endif */
377 #ifndef OPENSSL_NO_DSA
378 static const char *UBSEC_F9 = "dsa_sign_ioctl";
379 static const char *UBSEC_F10 = "dsa_verify_ioctl";
380 #endif
381 static const char *UBSEC_F11 = "math_accelerate_ioctl";
382 static const char *UBSEC_F12 = "rng_ioctl";
383
384 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
385 static int ubsec_destroy(ENGINE *e)
386         {
387         ubsec_unload_error_strings();
388         return 1;
389         }
390
391 /* (de)initialisation functions. */
392 static int ubsec_init(ENGINE *e)
393         {
394         t_UBSEC_ubsec_bytes_to_bits *p1;
395         t_UBSEC_ubsec_bits_to_bytes *p2;
396         t_UBSEC_ubsec_open *p3;
397         t_UBSEC_ubsec_close *p4;
398 #ifndef OPENSSL_NO_DH
399         t_UBSEC_diffie_hellman_generate_ioctl *p5;
400         t_UBSEC_diffie_hellman_agree_ioctl *p6;
401 #endif
402 /* #ifndef OPENSSL_NO_RSA */
403         t_UBSEC_rsa_mod_exp_ioctl *p7;
404         t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
405 /* #endif */
406 #ifndef OPENSSL_NO_DSA
407         t_UBSEC_dsa_sign_ioctl *p9;
408         t_UBSEC_dsa_verify_ioctl *p10;
409 #endif
410         t_UBSEC_math_accelerate_ioctl *p11;
411         t_UBSEC_rng_ioctl *p12;
412         int fd = 0;
413
414         if(ubsec_dso != NULL)
415                 {
416                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
417                 goto err;
418                 }
419         /* 
420          * Attempt to load libubsec.so/ubsec.dll/whatever. 
421          */
422         ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
423         if(ubsec_dso == NULL)
424                 {
425                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
426                 goto err;
427                 }
428
429         if (
430         !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
431         !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
432         !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
433         !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
434 #ifndef OPENSSL_NO_DH
435         !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) 
436                                 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
437         !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) 
438                                 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
439 #endif
440 /* #ifndef OPENSSL_NO_RSA */
441         !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
442         !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
443 /* #endif */
444 #ifndef OPENSSL_NO_DSA
445         !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
446         !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
447 #endif
448         !(p11 = (t_UBSEC_math_accelerate_ioctl *) 
449                                 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
450         !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)))
451                 {
452                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
453                 goto err;
454                 }
455
456         /* Copy the pointers */
457         p_UBSEC_ubsec_bytes_to_bits = p1;
458         p_UBSEC_ubsec_bits_to_bytes = p2;
459         p_UBSEC_ubsec_open = p3;
460         p_UBSEC_ubsec_close = p4;
461 #ifndef OPENSSL_NO_DH
462         p_UBSEC_diffie_hellman_generate_ioctl = p5;
463         p_UBSEC_diffie_hellman_agree_ioctl = p6;
464 #endif
465 #ifndef OPENSSL_NO_RSA
466         p_UBSEC_rsa_mod_exp_ioctl = p7;
467         p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
468 #endif
469 #ifndef OPENSSL_NO_DSA
470         p_UBSEC_dsa_sign_ioctl = p9;
471         p_UBSEC_dsa_verify_ioctl = p10;
472 #endif
473         p_UBSEC_math_accelerate_ioctl = p11;
474         p_UBSEC_rng_ioctl = p12;
475
476         /* Perform an open to see if there's actually any unit running. */
477         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0)
478                 {
479                 p_UBSEC_ubsec_close(fd);
480                 return 1;
481                 }
482         else
483                 {
484                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
485                 }
486
487 err:
488         if(ubsec_dso)
489                 DSO_free(ubsec_dso);
490         p_UBSEC_ubsec_bytes_to_bits = NULL;
491         p_UBSEC_ubsec_bits_to_bytes = NULL;
492         p_UBSEC_ubsec_open = NULL;
493         p_UBSEC_ubsec_close = NULL;
494 #ifndef OPENSSL_NO_DH
495         p_UBSEC_diffie_hellman_generate_ioctl = NULL;
496         p_UBSEC_diffie_hellman_agree_ioctl = NULL;
497 #endif
498 #ifndef OPENSSL_NO_RSA
499         p_UBSEC_rsa_mod_exp_ioctl = NULL;
500         p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
501 #endif
502 #ifndef OPENSSL_NO_DSA
503         p_UBSEC_dsa_sign_ioctl = NULL;
504         p_UBSEC_dsa_verify_ioctl = NULL;
505 #endif
506         p_UBSEC_math_accelerate_ioctl = NULL;
507         p_UBSEC_rng_ioctl = NULL;
508
509         return 0;
510         }
511
512 static int ubsec_finish(ENGINE *e)
513         {
514         if(ubsec_dso == NULL)
515                 {
516                 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
517                 return 0;
518                 }
519         if(!DSO_free(ubsec_dso))
520                 {
521                 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
522                 return 0;
523                 }
524         ubsec_dso = NULL;
525         p_UBSEC_ubsec_bytes_to_bits = NULL;
526         p_UBSEC_ubsec_bits_to_bytes = NULL;
527         p_UBSEC_ubsec_open = NULL;
528         p_UBSEC_ubsec_close = NULL;
529 #ifndef OPENSSL_NO_DH
530         p_UBSEC_diffie_hellman_generate_ioctl = NULL;
531         p_UBSEC_diffie_hellman_agree_ioctl = NULL;
532 #endif
533 #ifndef OPENSSL_NO_RSA
534         p_UBSEC_rsa_mod_exp_ioctl = NULL;
535         p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
536 #endif
537 #ifndef OPENSSL_NO_DSA
538         p_UBSEC_dsa_sign_ioctl = NULL;
539         p_UBSEC_dsa_verify_ioctl = NULL;
540 #endif
541         p_UBSEC_math_accelerate_ioctl = NULL;
542         p_UBSEC_rng_ioctl = NULL;
543         return 1;
544         }
545
546 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
547         {
548         int initialised = ((ubsec_dso == NULL) ? 0 : 1);
549         switch(cmd)
550                 {
551         case UBSEC_CMD_SO_PATH:
552                 if(p == NULL)
553                         {
554                         UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
555                         return 0;
556                         }
557                 if(initialised)
558                         {
559                         UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
560                         return 0;
561                         }
562                 UBSEC_LIBNAME = (const char *)p;
563                 return 1;
564         default:
565                 break;
566                 }
567         UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
568         return 0;
569         }
570
571 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
572                 const BIGNUM *m, BN_CTX *ctx)
573         {
574         int     y_len = 0;
575         int     fd;
576
577         if(ubsec_dso == NULL)
578         {
579                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
580                 return 0;
581         }
582
583         /* Check if hardware can't handle this argument. */
584         y_len = BN_num_bits(m);
585         if (y_len > 1024) {
586                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
587                 return 0;
588         } 
589
590         if(!bn_wexpand(r, m->top))
591         {
592                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
593                 return 0;
594         }
595         memset(r->d, 0, BN_num_bytes(m));
596
597         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
598                 fd = 0;
599                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
600                 return 0;
601         }
602
603         if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
604                 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d, 
605                 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
606         {
607                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
608                 return 0;
609         }
610
611         p_UBSEC_ubsec_close(fd);
612
613         r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
614         return 1;
615         }
616
617 #ifndef OPENSSL_NO_RSA
618 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
619         {
620         BN_CTX *ctx;
621         int to_return = 0;
622
623         if((ctx = BN_CTX_new()) == NULL)
624                 goto err;
625
626         if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
627                 {
628                 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
629                 goto err;
630                 }
631
632         /*
633          * Do in software if argument is too large for hardware.
634          */
635         if ((BN_num_bits(rsa->p)+BN_num_bits(rsa->q)) > 1024) {
636                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
637                 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
638         } else {
639                 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
640                         rsa->dmq1, rsa->iqmp, ctx);
641         }
642 err:
643         if(ctx)
644                 BN_CTX_free(ctx);
645         return to_return;
646         }
647 #endif
648
649 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
650                         const BIGNUM *q, const BIGNUM *dp,
651                         const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
652         {
653         int     y_len,
654                 m_len,
655                 fd;
656
657         m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
658         y_len = BN_num_bits(p) + BN_num_bits(q);
659
660         /* Check if hardware can't handle this argument. */
661         if (y_len > 1024) {
662                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
663                 return 0;
664         } 
665
666         if (!bn_wexpand(r, p->top + q->top + 1)) {
667                 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
668                 return 0;
669         }
670
671         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
672                 fd = 0;
673                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
674                 return 0;
675         }
676
677         if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
678                 (unsigned char *)a->d, BN_num_bits(a), 
679                 (unsigned char *)qinv->d, BN_num_bits(qinv),
680                 (unsigned char *)dp->d, BN_num_bits(dp),
681                 (unsigned char *)p->d, BN_num_bits(p),
682                 (unsigned char *)dq->d, BN_num_bits(dq),
683                 (unsigned char *)q->d, BN_num_bits(q),
684                 (unsigned char *)r->d,  &y_len) != 0) {
685                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
686                 return 0;
687         }
688
689         p_UBSEC_ubsec_close(fd);
690
691         r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
692         return 1;
693 }
694
695 #ifndef OPENSSL_NO_DSA
696 #if NOT_USED
697 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
698                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
699                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
700         {
701         BIGNUM t;
702         int to_return = 0;
703  
704         BN_init(&t);
705         /* let rr = a1 ^ p1 mod m */
706         if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
707         /* let t = a2 ^ p2 mod m */
708         if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
709         /* let rr = rr * t mod m */
710         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
711         to_return = 1;
712 end:
713         BN_free(&t);
714         return to_return;
715         }
716
717 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
718                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
719                 BN_MONT_CTX *m_ctx)
720         {
721         return ubsec_mod_exp(r, a, p, m, ctx);
722         }
723 #endif
724 #endif
725
726 /*
727  * This function is aliased to mod_exp (with the mont stuff dropped).
728  */
729 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
730                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
731         {
732         int ret = 0;
733
734 #ifndef OPENSSL_NO_RSA
735         /* Do in software if the key is too large for the hardware. */
736         if (BN_num_bits(m) > 1024)
737                 {
738                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
739                 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
740                 }
741         else
742 #endif
743                 {
744                 ret = ubsec_mod_exp(r, a, p, m, ctx);
745                 }
746         
747         return ret;
748         }
749
750 #ifndef OPENSSL_NO_DH
751 /* This function is aliased to mod_exp (with the dh and mont dropped). */
752 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
753                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
754                 BN_MONT_CTX *m_ctx)
755         {
756         return ubsec_mod_exp(r, a, p, m, ctx);
757         }
758 #endif
759
760 #ifndef OPENSSL_NO_DSA
761 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
762         {
763         DSA_SIG *to_return = NULL;
764         int s_len = 160, r_len = 160, d_len, fd;
765         BIGNUM m, *r=NULL, *s=NULL;
766
767         BN_init(&m);
768
769         s = BN_new();
770         r = BN_new();
771         if ((s == NULL) || (r==NULL))
772                 goto err;
773
774         d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
775
776         if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
777            (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
778                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
779                 goto err;
780         }
781
782         if (BN_bin2bn(dgst,dlen,&m) == NULL) {
783                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
784                 goto err;
785         } 
786
787         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
788                 fd = 0;
789                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
790                 return 0;
791         }
792
793         if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
794                 (unsigned char *)dgst, d_len,
795                 NULL, 0,  /* compute random value */
796                 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
797                 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
798                 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
799                 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
800                 (unsigned char *)r->d, &r_len,
801                 (unsigned char *)s->d, &s_len ) != 0) {
802                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
803                 goto err;
804         }
805
806         p_UBSEC_ubsec_close(fd);
807
808         r->top = (160+BN_BITS2-1)/BN_BITS2;
809         s->top = (160+BN_BITS2-1)/BN_BITS2;
810
811         to_return = DSA_SIG_new();
812         if(to_return == NULL) {
813                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
814                 goto err;
815         }
816
817         to_return->r = r;
818         to_return->s = s;
819
820 err:
821         if (!to_return) {
822                 if (r) BN_free(r);
823                 if (s) BN_free(s);
824         }                                 
825         BN_clear_free(&m);
826         return to_return;
827 }
828
829 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
830                                 DSA_SIG *sig, DSA *dsa)
831         {
832         int v_len, d_len;
833         int to_return = 0;
834         int fd;
835         BIGNUM v;
836
837         BN_init(&v);
838
839         if(!bn_wexpand(&v, dsa->p->top)) {
840                 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
841                 goto err;
842         }
843
844         v_len = BN_num_bits(dsa->p);
845
846         d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
847
848         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
849                 fd = 0;
850                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
851                 return 0;
852         }
853
854         if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
855                 (unsigned char *)dgst, d_len,
856                 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
857                 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
858                 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
859                 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
860                 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
861                 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
862                 (unsigned char *)v.d, &v_len) != 0) {
863                 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
864                 goto err;
865         }
866
867         p_UBSEC_ubsec_close(fd);
868
869         to_return = 1;
870 err:
871         BN_clear_free(&v);
872         return to_return;
873         }
874 #endif
875
876 #ifndef OPENSSL_NO_DH
877 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
878         {
879         return 0;
880         }
881
882 static int ubsec_dh_generate_key (DH *dh)
883         {
884         return 0;
885         }
886 #endif
887
888 #ifdef NOT_USED
889 static int ubsec_rand_bytes(unsigned char *buf, int num)
890         {
891         return 0;
892         }
893
894 static int ubsec_rand_status(void)
895         {
896         return 0;
897         }
898 #endif
899
900 /* This stuff is needed if this ENGINE is being compiled into a self-contained
901  * shared-library. */
902 #ifdef ENGINE_DYNAMIC_SUPPORT
903 static int bind_fn(ENGINE *e, const char *id)
904         {
905         if(id && (strcmp(id, engine_ubsec_id) != 0))
906                 return 0;
907         if(!bind_helper(e))
908                 return 0;
909         return 1;
910         }
911 IMPLEMENT_DYNAMIC_CHECK_FN()
912 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
913 #endif /* ENGINE_DYNAMIC_SUPPORT */
914
915 #endif /* !OPENSSL_NO_HW_UBSEC */
916 #endif /* !OPENSSL_NO_HW */