1 /* crypto/engine/hw_ubsec.c */
2 /* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
5 * Cloned shamelessly by Joe Tardo.
7 /* ====================================================================
8 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
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
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/)"
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.
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.
36 * 6. Redistributions of any form whatsoever must retain the following
38 * "This product includes software developed by the OpenSSL Project
39 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
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 * ====================================================================
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).
62 #include <openssl/crypto.h>
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
68 #ifndef OPENSSL_NO_HW_UBSEC
73 #include "vendor_defns/hw_ubsec.h"
76 #define FAIL_TO_SOFTWARE -15
78 static int ubsec_destroy(ENGINE *e);
79 static int ubsec_init(ENGINE *e);
80 static int ubsec_finish(ENGINE *e);
81 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
82 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
83 const BIGNUM *m, BN_CTX *ctx);
84 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
85 const BIGNUM *q, const BIGNUM *dp,
86 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
87 #ifndef OPENSSL_NO_RSA
88 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
90 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
91 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
92 #ifndef OPENSSL_NO_DSA
94 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
95 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
96 BN_CTX *ctx, BN_MONT_CTX *in_mont);
97 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
98 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
101 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
102 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
103 DSA_SIG *sig, DSA *dsa);
105 #ifndef OPENSSL_NO_DH
106 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
107 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
109 static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
110 static int ubsec_dh_generate_key(DH *dh);
114 static int ubsec_rand_bytes(unsigned char *buf, int num);
115 static int ubsec_rand_status(void);
118 #define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
119 static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
122 "Specifies the path to the 'ubsec' shared library",
123 ENGINE_CMD_FLAG_STRING},
127 #ifndef OPENSSL_NO_RSA
128 /* Our internal RSA_METHOD that we provide pointers to */
129 static RSA_METHOD ubsec_rsa =
147 #ifndef OPENSSL_NO_DSA
148 /* Our internal DSA_METHOD that we provide pointers to */
149 static DSA_METHOD ubsec_dsa =
152 ubsec_dsa_do_sign, /* dsa_do_sign */
153 NULL, /* dsa_sign_setup */
154 ubsec_dsa_verify, /* dsa_do_verify */
155 NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
156 NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
164 #ifndef OPENSSL_NO_DH
165 /* Our internal DH_METHOD that we provide pointers to */
166 static DH_METHOD ubsec_dh =
169 ubsec_dh_generate_key,
170 ubsec_dh_compute_key,
179 #ifndef OPENSSL_NO_ERR
180 /* Error function codes for use in ubsec operation */
181 #define UBSEC_F_UBSEC_INIT 100
182 #define UBSEC_F_UBSEC_FINISH 101
183 #define UBSEC_F_UBSEC_CTRL 102
184 #define UBSEC_F_UBSEC_MOD_EXP 103
185 #define UBSEC_F_UBSEC_RSA_MOD_EXP 104
186 #define UBSEC_F_UBSEC_RSA_MOD_EXP_CRT 105
187 #define UBSEC_F_UBSEC_DSA_SIGN 106
188 #define UBSEC_F_UBSEC_DSA_VERIFY 107
189 #define UBSEC_F_UBSEC_DH_COMPUTE_KEY 117
190 /* Error reason codes */
191 #define UBSEC_R_ALREADY_LOADED 108
192 #define UBSEC_R_DSO_FAILURE 109
193 #define UBSEC_R_UNIT_FAILURE 110
194 #define UBSEC_R_NOT_LOADED 111
195 #define UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED 112
196 #define UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL 113
197 #define UBSEC_R_BN_EXPAND_FAIL 114
198 #define UBSEC_R_REQUEST_FAILED 115
199 #define UBSEC_R_MISSING_KEY_COMPONENTS 116
201 static ERR_STRING_DATA ubsec_str_functs[] =
203 /* This first element is changed to match the dynamic 'lib' number */
204 {ERR_PACK(0,0,0), "ubsec engine code"},
205 {ERR_PACK(0,UBSEC_F_UBSEC_INIT,0), "ubsec_init"},
206 {ERR_PACK(0,UBSEC_F_UBSEC_FINISH,0), "ubsec_finish"},
207 {ERR_PACK(0,UBSEC_F_UBSEC_CTRL,0), "ubsec_ctrl"},
208 {ERR_PACK(0,UBSEC_F_UBSEC_MOD_EXP,0), "ubsec_mod_exp"},
209 {ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP,0), "ubsec_rsa_mod_exp"},
210 {ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP_CRT,0), "ubsec_rsa_mod_exp_crt"},
211 {ERR_PACK(0,UBSEC_F_UBSEC_DSA_SIGN,0), "ubsec_dsa_sign"},
212 {ERR_PACK(0,UBSEC_F_UBSEC_DSA_VERIFY,0), "ubsec_dsa_verify"},
213 /* Error reason codes */
214 {UBSEC_R_ALREADY_LOADED ,"already loaded"},
215 {UBSEC_R_DSO_FAILURE ,"DSO failure"},
216 {UBSEC_R_UNIT_FAILURE ,"unit failure"},
217 {UBSEC_R_NOT_LOADED ,"not loaded"},
218 {UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED ,"ctrl command not implemented"},
219 {UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL ,"size too large or too small"},
220 {UBSEC_R_BN_EXPAND_FAIL ,"bn_expand fail"},
221 {UBSEC_R_REQUEST_FAILED ,"request failed"},
222 {UBSEC_R_MISSING_KEY_COMPONENTS ,"missing key components"},
225 /* The library number we obtain dynamically from the ERR code */
226 static int ubsec_err_lib = -1;
227 #define UBSECerr(f,r) ERR_PUT_error(ubsec_err_lib,(f),(r),__FILE__,__LINE__)
228 static void ubsec_load_error_strings(void)
230 if(ubsec_err_lib < 0)
232 if((ubsec_err_lib = ERR_get_next_error_library()) <= 0)
234 ubsec_str_functs[0].error = ERR_PACK(ubsec_err_lib,0,0);
235 ERR_load_strings(ubsec_err_lib, ubsec_str_functs);
238 static void ubsec_unload_error_strings(void)
240 if(ubsec_err_lib >= 0)
242 ERR_unload_strings(ubsec_err_lib, ubsec_str_functs);
247 #define UBSECerr(f,r) /* NOP */
248 static void ubsec_load_error_strings(void) { } /* NOP */
249 static void ubsec_unload_error_strings(void) { } /* NOP */
252 /* Constants used when creating the ENGINE */
253 static const char *engine_ubsec_id = "ubsec";
254 static const char *engine_ubsec_name = "UBSEC hardware engine support";
256 /* This internal function is used by ENGINE_ubsec() and possibly by the
257 * "dynamic" ENGINE support too */
258 static int bind_helper(ENGINE *e)
260 #ifndef OPENSSL_NO_RSA
261 const RSA_METHOD *meth1;
263 #ifndef OPENSSL_NO_DH
264 #ifndef HAVE_UBSEC_DH
265 const DH_METHOD *meth3;
266 #endif /* HAVE_UBSEC_DH */
268 if(!ENGINE_set_id(e, engine_ubsec_id) ||
269 !ENGINE_set_name(e, engine_ubsec_name) ||
270 #ifndef OPENSSL_NO_RSA
271 !ENGINE_set_RSA(e, &ubsec_rsa) ||
273 #ifndef OPENSSL_NO_DSA
274 !ENGINE_set_DSA(e, &ubsec_dsa) ||
276 #ifndef OPENSSL_NO_DH
277 !ENGINE_set_DH(e, &ubsec_dh) ||
279 !ENGINE_set_destroy_function(e, ubsec_destroy) ||
280 !ENGINE_set_init_function(e, ubsec_init) ||
281 !ENGINE_set_finish_function(e, ubsec_finish) ||
282 !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
283 !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
286 #ifndef OPENSSL_NO_RSA
287 /* We know that the "PKCS1_SSLeay()" functions hook properly
288 * to the Broadcom-specific mod_exp and mod_exp_crt so we use
289 * those functions. NB: We don't use ENGINE_openssl() or
290 * anything "more generic" because something like the RSAref
291 * code may not hook properly, and if you own one of these
292 * cards then you have the right to do RSA operations on it
294 meth1 = RSA_PKCS1_SSLeay();
295 ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
296 ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
297 ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
298 ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
301 #ifndef OPENSSL_NO_DH
302 #ifndef HAVE_UBSEC_DH
303 /* Much the same for Diffie-Hellman */
304 meth3 = DH_OpenSSL();
305 ubsec_dh.generate_key = meth3->generate_key;
306 ubsec_dh.compute_key = meth3->compute_key;
307 #endif /* HAVE_UBSEC_DH */
310 /* Ensure the ubsec error handling is set up */
311 ubsec_load_error_strings();
315 static ENGINE *engine_ubsec(void)
317 ENGINE *ret = ENGINE_new();
320 if(!bind_helper(ret))
328 void ENGINE_load_ubsec(void)
330 /* Copied from eng_[openssl|dyn].c */
331 ENGINE *toadd = engine_ubsec();
338 /* This is a process-global DSO handle used for loading and unloading
339 * the UBSEC library. NB: This is only set (or unset) during an
340 * init() or finish() call (reference counts permitting) and they're
341 * operating with global locks, so this should be thread-safe
344 static DSO *ubsec_dso = NULL;
346 /* These are the function pointers that are (un)set when the library has
347 * successfully (un)loaded. */
349 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
350 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
351 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
352 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
353 #ifndef OPENSSL_NO_DH
354 static t_UBSEC_diffie_hellman_generate_ioctl
355 *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
356 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
358 /* #ifndef OPENSSL_NO_RSA */
359 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
360 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
362 #ifndef OPENSSL_NO_DSA
363 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
364 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
366 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
367 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
368 static t_UBSEC_max_key_len_ioctl *p_UBSEC_max_key_len_ioctl = NULL;
370 static int max_key_len = 1024; /* ??? */
373 * These are the static string constants for the DSO file name and the function
374 * symbol names to bind to.
377 static const char *UBSEC_LIBNAME = "ubsec";
378 static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
379 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
380 static const char *UBSEC_F3 = "ubsec_open";
381 static const char *UBSEC_F4 = "ubsec_close";
382 #ifndef OPENSSL_NO_DH
383 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
384 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
386 /* #ifndef OPENSSL_NO_RSA */
387 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
388 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
390 #ifndef OPENSSL_NO_DSA
391 static const char *UBSEC_F9 = "dsa_sign_ioctl";
392 static const char *UBSEC_F10 = "dsa_verify_ioctl";
394 static const char *UBSEC_F11 = "math_accelerate_ioctl";
395 static const char *UBSEC_F12 = "rng_ioctl";
396 static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl";
398 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
399 static int ubsec_destroy(ENGINE *e)
401 ubsec_unload_error_strings();
405 /* (de)initialisation functions. */
406 static int ubsec_init(ENGINE *e)
408 t_UBSEC_ubsec_bytes_to_bits *p1;
409 t_UBSEC_ubsec_bits_to_bytes *p2;
410 t_UBSEC_ubsec_open *p3;
411 t_UBSEC_ubsec_close *p4;
412 #ifndef OPENSSL_NO_DH
413 t_UBSEC_diffie_hellman_generate_ioctl *p5;
414 t_UBSEC_diffie_hellman_agree_ioctl *p6;
416 /* #ifndef OPENSSL_NO_RSA */
417 t_UBSEC_rsa_mod_exp_ioctl *p7;
418 t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
420 #ifndef OPENSSL_NO_DSA
421 t_UBSEC_dsa_sign_ioctl *p9;
422 t_UBSEC_dsa_verify_ioctl *p10;
424 t_UBSEC_math_accelerate_ioctl *p11;
425 t_UBSEC_rng_ioctl *p12;
426 t_UBSEC_max_key_len_ioctl *p13;
429 if(ubsec_dso != NULL)
431 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
435 * Attempt to load libubsec.so/ubsec.dll/whatever.
437 ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
438 if(ubsec_dso == NULL)
440 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
445 !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
446 !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
447 !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
448 !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
449 #ifndef OPENSSL_NO_DH
450 !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *)
451 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
452 !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *)
453 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
455 /* #ifndef OPENSSL_NO_RSA */
456 !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
457 !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
459 #ifndef OPENSSL_NO_DSA
460 !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
461 !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
463 !(p11 = (t_UBSEC_math_accelerate_ioctl *)
464 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
465 !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) ||
466 !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13)))
468 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
472 /* Copy the pointers */
473 p_UBSEC_ubsec_bytes_to_bits = p1;
474 p_UBSEC_ubsec_bits_to_bytes = p2;
475 p_UBSEC_ubsec_open = p3;
476 p_UBSEC_ubsec_close = p4;
477 #ifndef OPENSSL_NO_DH
478 p_UBSEC_diffie_hellman_generate_ioctl = p5;
479 p_UBSEC_diffie_hellman_agree_ioctl = p6;
481 #ifndef OPENSSL_NO_RSA
482 p_UBSEC_rsa_mod_exp_ioctl = p7;
483 p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
485 #ifndef OPENSSL_NO_DSA
486 p_UBSEC_dsa_sign_ioctl = p9;
487 p_UBSEC_dsa_verify_ioctl = p10;
489 p_UBSEC_math_accelerate_ioctl = p11;
490 p_UBSEC_rng_ioctl = p12;
491 p_UBSEC_max_key_len_ioctl = p13;
493 /* Perform an open to see if there's actually any unit running. */
494 if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
496 p_UBSEC_ubsec_close(fd);
501 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
507 p_UBSEC_ubsec_bytes_to_bits = NULL;
508 p_UBSEC_ubsec_bits_to_bytes = NULL;
509 p_UBSEC_ubsec_open = NULL;
510 p_UBSEC_ubsec_close = NULL;
511 #ifndef OPENSSL_NO_DH
512 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
513 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
515 #ifndef OPENSSL_NO_RSA
516 p_UBSEC_rsa_mod_exp_ioctl = NULL;
517 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
519 #ifndef OPENSSL_NO_DSA
520 p_UBSEC_dsa_sign_ioctl = NULL;
521 p_UBSEC_dsa_verify_ioctl = NULL;
523 p_UBSEC_math_accelerate_ioctl = NULL;
524 p_UBSEC_rng_ioctl = NULL;
525 p_UBSEC_max_key_len_ioctl = NULL;
530 static int ubsec_finish(ENGINE *e)
532 if(ubsec_dso == NULL)
534 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
537 if(!DSO_free(ubsec_dso))
539 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
543 p_UBSEC_ubsec_bytes_to_bits = NULL;
544 p_UBSEC_ubsec_bits_to_bytes = NULL;
545 p_UBSEC_ubsec_open = NULL;
546 p_UBSEC_ubsec_close = NULL;
547 #ifndef OPENSSL_NO_DH
548 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
549 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
551 #ifndef OPENSSL_NO_RSA
552 p_UBSEC_rsa_mod_exp_ioctl = NULL;
553 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
555 #ifndef OPENSSL_NO_DSA
556 p_UBSEC_dsa_sign_ioctl = NULL;
557 p_UBSEC_dsa_verify_ioctl = NULL;
559 p_UBSEC_math_accelerate_ioctl = NULL;
560 p_UBSEC_rng_ioctl = NULL;
561 p_UBSEC_max_key_len_ioctl = NULL;
565 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
567 int initialised = ((ubsec_dso == NULL) ? 0 : 1);
570 case UBSEC_CMD_SO_PATH:
573 UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
578 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
581 UBSEC_LIBNAME = (const char *)p;
586 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
590 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
591 const BIGNUM *m, BN_CTX *ctx)
596 if(ubsec_dso == NULL)
598 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
602 /* Check if hardware can't handle this argument. */
603 y_len = BN_num_bits(m);
604 if (y_len > max_key_len) {
605 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
606 return BN_mod_exp(r, a, p, m, ctx);
609 if(!bn_wexpand(r, m->top))
611 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
614 memset(r->d, 0, BN_num_bytes(m));
616 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
618 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
619 return BN_mod_exp(r, a, p, m, ctx);
622 if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
623 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d,
624 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
626 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
627 p_UBSEC_ubsec_close(fd);
629 return BN_mod_exp(r, a, p, m, ctx);
632 p_UBSEC_ubsec_close(fd);
634 r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
638 #ifndef OPENSSL_NO_RSA
639 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
644 if((ctx = BN_CTX_new()) == NULL)
647 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
649 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
653 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
654 rsa->dmq1, rsa->iqmp, ctx);
655 if (to_return == FAIL_TO_SOFTWARE)
658 * Do in software as hardware failed.
660 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
661 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
670 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
671 const BIGNUM *q, const BIGNUM *dp,
672 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
678 m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
679 y_len = BN_num_bits(p) + BN_num_bits(q);
681 /* Check if hardware can't handle this argument. */
682 if (y_len > max_key_len) {
683 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
684 return FAIL_TO_SOFTWARE;
687 if (!bn_wexpand(r, p->top + q->top + 1)) {
688 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
692 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
694 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
695 return FAIL_TO_SOFTWARE;
698 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
699 (unsigned char *)a->d, BN_num_bits(a),
700 (unsigned char *)qinv->d, BN_num_bits(qinv),
701 (unsigned char *)dp->d, BN_num_bits(dp),
702 (unsigned char *)p->d, BN_num_bits(p),
703 (unsigned char *)dq->d, BN_num_bits(dq),
704 (unsigned char *)q->d, BN_num_bits(q),
705 (unsigned char *)r->d, &y_len) != 0) {
706 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
707 p_UBSEC_ubsec_close(fd);
708 return FAIL_TO_SOFTWARE;
711 p_UBSEC_ubsec_close(fd);
713 r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
717 #ifndef OPENSSL_NO_DSA
719 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
720 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
721 BN_CTX *ctx, BN_MONT_CTX *in_mont)
727 /* let rr = a1 ^ p1 mod m */
728 if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
729 /* let t = a2 ^ p2 mod m */
730 if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
731 /* let rr = rr * t mod m */
732 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
739 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
740 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
743 return ubsec_mod_exp(r, a, p, m, ctx);
749 * This function is aliased to mod_exp (with the mont stuff dropped).
751 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
752 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
756 #ifndef OPENSSL_NO_RSA
757 /* Do in software if the key is too large for the hardware. */
758 if (BN_num_bits(m) > max_key_len)
760 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
761 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
766 ret = ubsec_mod_exp(r, a, p, m, ctx);
772 #ifndef OPENSSL_NO_DH
773 /* This function is aliased to mod_exp (with the dh and mont dropped). */
774 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
775 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
778 return ubsec_mod_exp(r, a, p, m, ctx);
782 #ifndef OPENSSL_NO_DSA
783 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
785 DSA_SIG *to_return = NULL;
786 int s_len = 160, r_len = 160, d_len, fd;
787 BIGNUM m, *r=NULL, *s=NULL;
793 if ((s == NULL) || (r==NULL))
796 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
798 if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
799 (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
800 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
804 if (BN_bin2bn(dgst,dlen,&m) == NULL) {
805 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
809 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
810 const DSA_METHOD *meth;
812 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
813 meth = DSA_OpenSSL();
814 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
818 if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
819 (unsigned char *)dgst, d_len,
820 NULL, 0, /* compute random value */
821 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
822 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
823 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
824 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
825 (unsigned char *)r->d, &r_len,
826 (unsigned char *)s->d, &s_len ) != 0) {
827 const DSA_METHOD *meth;
829 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
830 p_UBSEC_ubsec_close(fd);
831 meth = DSA_OpenSSL();
832 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
837 p_UBSEC_ubsec_close(fd);
839 r->top = (160+BN_BITS2-1)/BN_BITS2;
840 s->top = (160+BN_BITS2-1)/BN_BITS2;
842 to_return = DSA_SIG_new();
843 if(to_return == NULL) {
844 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
860 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
861 DSA_SIG *sig, DSA *dsa)
870 if(!bn_wexpand(&v, dsa->p->top)) {
871 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
875 v_len = BN_num_bits(dsa->p);
877 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
879 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
880 const DSA_METHOD *meth;
882 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
883 meth = DSA_OpenSSL();
884 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
888 if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
889 (unsigned char *)dgst, d_len,
890 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
891 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
892 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
893 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
894 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
895 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
896 (unsigned char *)v.d, &v_len) != 0) {
897 const DSA_METHOD *meth;
898 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
899 p_UBSEC_ubsec_close(fd);
901 meth = DSA_OpenSSL();
902 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
907 p_UBSEC_ubsec_close(fd);
916 #ifndef OPENSSL_NO_DH
917 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
923 k_len = BN_num_bits(dh->p);
925 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
927 const DH_METHOD *meth;
928 ENGINEerr(UBSEC_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
930 ret = meth->compute_key(key, pub_key, dh);
934 if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
935 (unsigned char *)dh->priv_key->d, BN_num_bits(dh->priv_key),
936 (unsigned char *)pub_key->d, BN_num_bits(pub_key),
937 (unsigned char *)dh->p->d, BN_num_bits(dh->p),
940 /* Hardware's a no go, failover to software */
941 const DH_METHOD *meth;
942 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, ENGINE_R_REQUEST_FAILED);
943 p_UBSEC_ubsec_close(fd);
946 ret = meth->compute_key(key, pub_key, dh);
951 p_UBSEC_ubsec_close(fd);
953 ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
958 static int ubsec_dh_generate_key (DH *dh)
965 BIGNUM *pub_key = NULL;
966 BIGNUM *priv_key = NULL;
969 * How many bits should Random x be? dh_key.c
970 * sets the range from 0 to num_bits(modulus) ???
973 if (dh->priv_key == NULL)
976 if (priv_key == NULL) goto err;
977 priv_key_len = BN_num_bits(dh->p);
978 bn_wexpand(priv_key, dh->p->top);
980 if (!BN_rand_range(priv_key, dh->p)) goto err;
981 while (BN_is_zero(priv_key));
982 random_bits = BN_num_bits(priv_key);
986 priv_key = dh->priv_key;
989 if (dh->pub_key == NULL)
992 pub_key_len = BN_num_bits(dh->p);
993 bn_wexpand(pub_key, dh->p->top);
994 if(pub_key == NULL) goto err;
998 pub_key = dh->pub_key;
1001 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
1003 const DH_METHOD *meth;
1004 ENGINEerr(UBSEC_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
1005 meth = DH_OpenSSL();
1006 ret = meth->generate_key(dh);
1010 if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
1011 (unsigned char *)priv_key->d, &priv_key_len,
1012 (unsigned char *)pub_key->d, &pub_key_len,
1013 (unsigned char *)dh->g->d, BN_num_bits(dh->g),
1014 (unsigned char *)dh->p->d, BN_num_bits(dh->p),
1015 0, 0, random_bits) != 0)
1017 /* Hardware's a no go, failover to software */
1018 const DH_METHOD *meth;
1020 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, ENGINE_R_REQUEST_FAILED);
1021 p_UBSEC_ubsec_close(fd);
1023 meth = DH_OpenSSL();
1024 ret = meth->generate_key(dh);
1029 p_UBSEC_ubsec_close(fd);
1031 dh->pub_key = pub_key;
1032 dh->pub_key->top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
1033 dh->priv_key = priv_key;
1034 dh->priv_key->top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
1043 static int ubsec_rand_bytes(unsigned char * buf,
1049 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
1051 const RAND_METHOD *meth;
1052 ENGINEerr(UBSEC_F_UBSEC_INIT, ENGINE_R_UNIT_FAILURE);
1053 num = p_UBSEC_ubsec_bits_to_bytes(num);
1054 meth = RAND_SSLeay();
1055 meth->seed(buf, num);
1056 ret = meth->bytes(buf, num);
1060 num *= 8; /* bytes to bits */
1062 if (p_UBSEC_rng_ioctl(fd,
1067 /* Hardware's a no go, failover to software */
1068 const RAND_METHOD *meth;
1070 ENGINEerr(UBSEC_F_UBSEC_RNG_BYTES, ENGINE_R_REQUEST_FAILED);
1071 p_UBSEC_ubsec_close(fd);
1073 num = p_UBSEC_ubsec_bits_to_bytes(num);
1074 meth = RAND_SSLeay();
1075 meth->seed(buf, num);
1076 ret = meth->bytes(buf, num);
1081 p_UBSEC_ubsec_close(fd);
1089 static int ubsec_rand_status(void)
1095 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1096 * shared-library. */
1097 #ifdef ENGINE_DYNAMIC_SUPPORT
1098 static int bind_fn(ENGINE *e, const char *id)
1100 if(id && (strcmp(id, engine_ubsec_id) != 0))
1106 IMPLEMENT_DYNAMIC_CHECK_FN()
1107 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1108 #endif /* ENGINE_DYNAMIC_SUPPORT */
1110 #endif /* !OPENSSL_NO_HW_UBSEC */
1111 #endif /* !OPENSSL_NO_HW */