b2c60bcd50268bcd7625e45710755ef4ec7b45a0
[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_destroy_function(e, ubsec_destroy) ||
276                         !ENGINE_set_init_function(e, ubsec_init) ||
277                         !ENGINE_set_finish_function(e, ubsec_finish) ||
278                         !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
279                         !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
280                 return 0;
281
282 #ifndef OPENSSL_NO_RSA
283         /* We know that the "PKCS1_SSLeay()" functions hook properly
284          * to the Broadcom-specific mod_exp and mod_exp_crt so we use
285          * those functions. NB: We don't use ENGINE_openssl() or
286          * anything "more generic" because something like the RSAref
287          * code may not hook properly, and if you own one of these
288          * cards then you have the right to do RSA operations on it
289          * anyway! */ 
290         meth1 = RSA_PKCS1_SSLeay();
291         ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
292         ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
293         ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
294         ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
295 #endif
296
297 #ifndef OPENSSL_NO_DH
298 #ifndef HAVE_UBSEC_DH
299         /* Much the same for Diffie-Hellman */
300         meth3 = DH_OpenSSL();
301         ubsec_dh.generate_key = meth3->generate_key;
302         ubsec_dh.compute_key = meth3->compute_key;
303 #endif /* HAVE_UBSEC_DH */
304 #endif
305
306         /* Ensure the ubsec error handling is set up */
307         ubsec_load_error_strings();
308         return 1;
309         }
310
311 static ENGINE *engine_ubsec(void)
312         {
313         ENGINE *ret = ENGINE_new();
314         if(!ret)
315                 return NULL;
316         if(!bind_helper(ret))
317                 {
318                 ENGINE_free(ret);
319                 return NULL;
320                 }
321         return ret;
322         }
323
324 void ENGINE_load_ubsec(void)
325         {
326         /* Copied from eng_[openssl|dyn].c */
327         ENGINE *toadd = engine_ubsec();
328         if(!toadd) return;
329         ENGINE_add(toadd);
330         ENGINE_free(toadd);
331         ERR_clear_error();
332         }
333
334 /* This is a process-global DSO handle used for loading and unloading
335  * the UBSEC library. NB: This is only set (or unset) during an
336  * init() or finish() call (reference counts permitting) and they're
337  * operating with global locks, so this should be thread-safe
338  * implicitly. */
339
340 static DSO *ubsec_dso = NULL;
341
342 /* These are the function pointers that are (un)set when the library has
343  * successfully (un)loaded. */
344
345 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
346 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
347 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
348 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
349 #ifndef OPENSSL_NO_DH
350 static t_UBSEC_diffie_hellman_generate_ioctl 
351         *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
352 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
353 #endif
354 /* #ifndef OPENSSL_NO_RSA */
355 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
356 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
357 /* #endif */
358 #ifndef OPENSSL_NO_DSA
359 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
360 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
361 #endif
362 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
363 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
364
365 /* 
366  * These are the static string constants for the DSO file name and the function
367  * symbol names to bind to. 
368  */
369
370 static const char *UBSEC_LIBNAME = "ubsec";
371 static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
372 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
373 static const char *UBSEC_F3 = "ubsec_open";
374 static const char *UBSEC_F4 = "ubsec_close";
375 #ifndef OPENSSL_NO_DH
376 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
377 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
378 #endif
379 /* #ifndef OPENSSL_NO_RSA */
380 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
381 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
382 /* #endif */
383 #ifndef OPENSSL_NO_DSA
384 static const char *UBSEC_F9 = "dsa_sign_ioctl";
385 static const char *UBSEC_F10 = "dsa_verify_ioctl";
386 #endif
387 static const char *UBSEC_F11 = "math_accelerate_ioctl";
388 static const char *UBSEC_F12 = "rng_ioctl";
389
390 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
391 static int ubsec_destroy(ENGINE *e)
392         {
393         ubsec_unload_error_strings();
394         return 1;
395         }
396
397 /* (de)initialisation functions. */
398 static int ubsec_init(ENGINE *e)
399         {
400         t_UBSEC_ubsec_bytes_to_bits *p1;
401         t_UBSEC_ubsec_bits_to_bytes *p2;
402         t_UBSEC_ubsec_open *p3;
403         t_UBSEC_ubsec_close *p4;
404 #ifndef OPENSSL_NO_DH
405         t_UBSEC_diffie_hellman_generate_ioctl *p5;
406         t_UBSEC_diffie_hellman_agree_ioctl *p6;
407 #endif
408 /* #ifndef OPENSSL_NO_RSA */
409         t_UBSEC_rsa_mod_exp_ioctl *p7;
410         t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
411 /* #endif */
412 #ifndef OPENSSL_NO_DSA
413         t_UBSEC_dsa_sign_ioctl *p9;
414         t_UBSEC_dsa_verify_ioctl *p10;
415 #endif
416         t_UBSEC_math_accelerate_ioctl *p11;
417         t_UBSEC_rng_ioctl *p12;
418         int fd = 0;
419
420         if(ubsec_dso != NULL)
421                 {
422                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
423                 goto err;
424                 }
425         /* 
426          * Attempt to load libubsec.so/ubsec.dll/whatever. 
427          */
428         ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
429         if(ubsec_dso == NULL)
430                 {
431                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
432                 goto err;
433                 }
434
435         if (
436         !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
437         !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
438         !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
439         !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
440 #ifndef OPENSSL_NO_DH
441         !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) 
442                                 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
443         !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) 
444                                 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
445 #endif
446 /* #ifndef OPENSSL_NO_RSA */
447         !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
448         !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
449 /* #endif */
450 #ifndef OPENSSL_NO_DSA
451         !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
452         !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
453 #endif
454         !(p11 = (t_UBSEC_math_accelerate_ioctl *) 
455                                 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
456         !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)))
457                 {
458                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
459                 goto err;
460                 }
461
462         /* Copy the pointers */
463         p_UBSEC_ubsec_bytes_to_bits = p1;
464         p_UBSEC_ubsec_bits_to_bytes = p2;
465         p_UBSEC_ubsec_open = p3;
466         p_UBSEC_ubsec_close = p4;
467 #ifndef OPENSSL_NO_DH
468         p_UBSEC_diffie_hellman_generate_ioctl = p5;
469         p_UBSEC_diffie_hellman_agree_ioctl = p6;
470 #endif
471 #ifndef OPENSSL_NO_RSA
472         p_UBSEC_rsa_mod_exp_ioctl = p7;
473         p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
474 #endif
475 #ifndef OPENSSL_NO_DSA
476         p_UBSEC_dsa_sign_ioctl = p9;
477         p_UBSEC_dsa_verify_ioctl = p10;
478 #endif
479         p_UBSEC_math_accelerate_ioctl = p11;
480         p_UBSEC_rng_ioctl = p12;
481
482         /* Perform an open to see if there's actually any unit running. */
483         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0)
484                 {
485                 p_UBSEC_ubsec_close(fd);
486                 return 1;
487                 }
488         else
489                 {
490                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
491                 }
492
493 err:
494         if(ubsec_dso)
495                 DSO_free(ubsec_dso);
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
515         return 0;
516         }
517
518 static int ubsec_finish(ENGINE *e)
519         {
520         if(ubsec_dso == NULL)
521                 {
522                 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
523                 return 0;
524                 }
525         if(!DSO_free(ubsec_dso))
526                 {
527                 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
528                 return 0;
529                 }
530         ubsec_dso = NULL;
531         p_UBSEC_ubsec_bytes_to_bits = NULL;
532         p_UBSEC_ubsec_bits_to_bytes = NULL;
533         p_UBSEC_ubsec_open = NULL;
534         p_UBSEC_ubsec_close = NULL;
535 #ifndef OPENSSL_NO_DH
536         p_UBSEC_diffie_hellman_generate_ioctl = NULL;
537         p_UBSEC_diffie_hellman_agree_ioctl = NULL;
538 #endif
539 #ifndef OPENSSL_NO_RSA
540         p_UBSEC_rsa_mod_exp_ioctl = NULL;
541         p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
542 #endif
543 #ifndef OPENSSL_NO_DSA
544         p_UBSEC_dsa_sign_ioctl = NULL;
545         p_UBSEC_dsa_verify_ioctl = NULL;
546 #endif
547         p_UBSEC_math_accelerate_ioctl = NULL;
548         p_UBSEC_rng_ioctl = NULL;
549         return 1;
550         }
551
552 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
553         {
554         int initialised = ((ubsec_dso == NULL) ? 0 : 1);
555         switch(cmd)
556                 {
557         case UBSEC_CMD_SO_PATH:
558                 if(p == NULL)
559                         {
560                         UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
561                         return 0;
562                         }
563                 if(initialised)
564                         {
565                         UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
566                         return 0;
567                         }
568                 UBSEC_LIBNAME = (const char *)p;
569                 return 1;
570         default:
571                 break;
572                 }
573         UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
574         return 0;
575         }
576
577 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
578                 const BIGNUM *m, BN_CTX *ctx)
579         {
580         int     y_len = 0;
581         int     fd;
582
583         if(ubsec_dso == NULL)
584         {
585                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
586                 return 0;
587         }
588
589         /* Check if hardware can't handle this argument. */
590         y_len = BN_num_bits(m);
591         if (y_len > 1024) {
592                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
593                 return 0;
594         } 
595
596         if(!bn_wexpand(r, m->top))
597         {
598                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
599                 return 0;
600         }
601         memset(r->d, 0, BN_num_bytes(m));
602
603         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
604                 fd = 0;
605                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
606                 return 0;
607         }
608
609         if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
610                 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d, 
611                 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
612         {
613                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
614                 return 0;
615         }
616
617         p_UBSEC_ubsec_close(fd);
618
619         r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
620         return 1;
621         }
622
623 #ifndef OPENSSL_NO_RSA
624 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
625         {
626         BN_CTX *ctx;
627         int to_return = 0;
628
629         if((ctx = BN_CTX_new()) == NULL)
630                 goto err;
631
632         if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
633                 {
634                 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
635                 goto err;
636                 }
637
638         /*
639          * Do in software if argument is too large for hardware.
640          */
641         if ((BN_num_bits(rsa->p)+BN_num_bits(rsa->q)) > 1024) {
642                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
643                 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
644         } else {
645                 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
646                         rsa->dmq1, rsa->iqmp, ctx);
647         }
648 err:
649         if(ctx)
650                 BN_CTX_free(ctx);
651         return to_return;
652         }
653 #endif
654
655 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
656                         const BIGNUM *q, const BIGNUM *dp,
657                         const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
658         {
659         int     y_len,
660                 m_len,
661                 fd;
662
663         m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
664         y_len = BN_num_bits(p) + BN_num_bits(q);
665
666         /* Check if hardware can't handle this argument. */
667         if (y_len > 1024) {
668                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
669                 return 0;
670         } 
671
672         if (!bn_wexpand(r, p->top + q->top + 1)) {
673                 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
674                 return 0;
675         }
676
677         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
678                 fd = 0;
679                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
680                 return 0;
681         }
682
683         if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
684                 (unsigned char *)a->d, BN_num_bits(a), 
685                 (unsigned char *)qinv->d, BN_num_bits(qinv),
686                 (unsigned char *)dp->d, BN_num_bits(dp),
687                 (unsigned char *)p->d, BN_num_bits(p),
688                 (unsigned char *)dq->d, BN_num_bits(dq),
689                 (unsigned char *)q->d, BN_num_bits(q),
690                 (unsigned char *)r->d,  &y_len) != 0) {
691                 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
692                 return 0;
693         }
694
695         p_UBSEC_ubsec_close(fd);
696
697         r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
698         return 1;
699 }
700
701 #ifndef OPENSSL_NO_DSA
702 #if NOT_USED
703 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
704                 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
705                 BN_CTX *ctx, BN_MONT_CTX *in_mont)
706         {
707         BIGNUM t;
708         int to_return = 0;
709  
710         BN_init(&t);
711         /* let rr = a1 ^ p1 mod m */
712         if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
713         /* let t = a2 ^ p2 mod m */
714         if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
715         /* let rr = rr * t mod m */
716         if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
717         to_return = 1;
718 end:
719         BN_free(&t);
720         return to_return;
721         }
722
723 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
724                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
725                 BN_MONT_CTX *m_ctx)
726         {
727         return ubsec_mod_exp(r, a, p, m, ctx);
728         }
729 #endif
730 #endif
731
732 /*
733  * This function is aliased to mod_exp (with the mont stuff dropped).
734  */
735 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
736                 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
737         {
738         int ret = 0;
739
740 #ifndef OPENSSL_NO_RSA
741         /* Do in software if the key is too large for the hardware. */
742         if (BN_num_bits(m) > 1024)
743                 {
744                 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
745                 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
746                 }
747         else
748 #endif
749                 {
750                 ret = ubsec_mod_exp(r, a, p, m, ctx);
751                 }
752         
753         return ret;
754         }
755
756 #ifndef OPENSSL_NO_DH
757 /* This function is aliased to mod_exp (with the dh and mont dropped). */
758 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
759                 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
760                 BN_MONT_CTX *m_ctx)
761         {
762         return ubsec_mod_exp(r, a, p, m, ctx);
763         }
764 #endif
765
766 #ifndef OPENSSL_NO_DSA
767 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
768         {
769         DSA_SIG *to_return = NULL;
770         int s_len = 160, r_len = 160, d_len, fd;
771         BIGNUM m, *r=NULL, *s=NULL;
772
773         BN_init(&m);
774
775         s = BN_new();
776         r = BN_new();
777         if ((s == NULL) || (r==NULL))
778                 goto err;
779
780         d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
781
782         if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
783            (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
784                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
785                 goto err;
786         }
787
788         if (BN_bin2bn(dgst,dlen,&m) == NULL) {
789                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
790                 goto err;
791         } 
792
793         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
794                 fd = 0;
795                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
796                 return 0;
797         }
798
799         if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
800                 (unsigned char *)dgst, d_len,
801                 NULL, 0,  /* compute random value */
802                 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
803                 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
804                 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
805                 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
806                 (unsigned char *)r->d, &r_len,
807                 (unsigned char *)s->d, &s_len ) != 0) {
808                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
809                 goto err;
810         }
811
812         p_UBSEC_ubsec_close(fd);
813
814         r->top = (160+BN_BITS2-1)/BN_BITS2;
815         s->top = (160+BN_BITS2-1)/BN_BITS2;
816
817         to_return = DSA_SIG_new();
818         if(to_return == NULL) {
819                 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
820                 goto err;
821         }
822
823         to_return->r = r;
824         to_return->s = s;
825
826 err:
827         if (!to_return) {
828                 if (r) BN_free(r);
829                 if (s) BN_free(s);
830         }                                 
831         BN_clear_free(&m);
832         return to_return;
833 }
834
835 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
836                                 DSA_SIG *sig, DSA *dsa)
837         {
838         int v_len, d_len;
839         int to_return = 0;
840         int fd;
841         BIGNUM v;
842
843         BN_init(&v);
844
845         if(!bn_wexpand(&v, dsa->p->top)) {
846                 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
847                 goto err;
848         }
849
850         v_len = BN_num_bits(dsa->p);
851
852         d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
853
854         if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
855                 fd = 0;
856                 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
857                 return 0;
858         }
859
860         if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
861                 (unsigned char *)dgst, d_len,
862                 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
863                 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
864                 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
865                 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
866                 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
867                 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
868                 (unsigned char *)v.d, &v_len) != 0) {
869                 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
870                 goto err;
871         }
872
873         p_UBSEC_ubsec_close(fd);
874
875         to_return = 1;
876 err:
877         BN_clear_free(&v);
878         return to_return;
879         }
880 #endif
881
882 #ifndef OPENSSL_NO_DH
883 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
884         {
885         return 0;
886         }
887
888 static int ubsec_dh_generate_key (DH *dh)
889         {
890         return 0;
891         }
892 #endif
893
894 #ifdef NOT_USED
895 static int ubsec_rand_bytes(unsigned char *buf, int num)
896         {
897         return 0;
898         }
899
900 static int ubsec_rand_status(void)
901         {
902         return 0;
903         }
904 #endif
905
906 /* This stuff is needed if this ENGINE is being compiled into a self-contained
907  * shared-library. */
908 #ifdef ENGINE_DYNAMIC_SUPPORT
909 static int bind_fn(ENGINE *e, const char *id)
910         {
911         if(id && (strcmp(id, engine_ubsec_id) != 0))
912                 return 0;
913         if(!bind_helper(e))
914                 return 0;
915         return 1;
916         }
917 IMPLEMENT_DYNAMIC_CHECK_FN()
918 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
919 #endif /* ENGINE_DYNAMIC_SUPPORT */
920
921 #endif /* !OPENSSL_NO_HW_UBSEC */
922 #endif /* !OPENSSL_NO_HW */