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 UBSEC_LIB_NAME "ubsec engine"
77 #include "hw_ubsec_err.c"
79 #define FAIL_TO_SOFTWARE -15
81 static int ubsec_destroy(ENGINE *e);
82 static int ubsec_init(ENGINE *e);
83 static int ubsec_finish(ENGINE *e);
84 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
85 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
86 const BIGNUM *m, BN_CTX *ctx);
87 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
88 const BIGNUM *q, const BIGNUM *dp,
89 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
90 #ifndef OPENSSL_NO_RSA
91 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
93 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
94 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
95 #ifndef OPENSSL_NO_DSA
97 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
98 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
99 BN_CTX *ctx, BN_MONT_CTX *in_mont);
100 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
101 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
104 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
105 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
106 DSA_SIG *sig, DSA *dsa);
108 #ifndef OPENSSL_NO_DH
109 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
110 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
112 static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
113 static int ubsec_dh_generate_key(DH *dh);
117 static int ubsec_rand_bytes(unsigned char *buf, int num);
118 static int ubsec_rand_status(void);
121 #define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
122 static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
125 "Specifies the path to the 'ubsec' shared library",
126 ENGINE_CMD_FLAG_STRING},
130 #ifndef OPENSSL_NO_RSA
131 /* Our internal RSA_METHOD that we provide pointers to */
132 static RSA_METHOD ubsec_rsa =
150 #ifndef OPENSSL_NO_DSA
151 /* Our internal DSA_METHOD that we provide pointers to */
152 static DSA_METHOD ubsec_dsa =
155 ubsec_dsa_do_sign, /* dsa_do_sign */
156 NULL, /* dsa_sign_setup */
157 ubsec_dsa_verify, /* dsa_do_verify */
158 NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
159 NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
167 #ifndef OPENSSL_NO_DH
168 /* Our internal DH_METHOD that we provide pointers to */
169 static DH_METHOD ubsec_dh =
172 ubsec_dh_generate_key,
173 ubsec_dh_compute_key,
182 /* Constants used when creating the ENGINE */
183 static const char *engine_ubsec_id = "ubsec";
184 static const char *engine_ubsec_name = "UBSEC hardware engine support";
186 /* This internal function is used by ENGINE_ubsec() and possibly by the
187 * "dynamic" ENGINE support too */
188 static int bind_helper(ENGINE *e)
190 #ifndef OPENSSL_NO_RSA
191 const RSA_METHOD *meth1;
193 #ifndef OPENSSL_NO_DH
194 #ifndef HAVE_UBSEC_DH
195 const DH_METHOD *meth3;
196 #endif /* HAVE_UBSEC_DH */
198 if(!ENGINE_set_id(e, engine_ubsec_id) ||
199 !ENGINE_set_name(e, engine_ubsec_name) ||
200 #ifndef OPENSSL_NO_RSA
201 !ENGINE_set_RSA(e, &ubsec_rsa) ||
203 #ifndef OPENSSL_NO_DSA
204 !ENGINE_set_DSA(e, &ubsec_dsa) ||
206 #ifndef OPENSSL_NO_DH
207 !ENGINE_set_DH(e, &ubsec_dh) ||
209 !ENGINE_set_destroy_function(e, ubsec_destroy) ||
210 !ENGINE_set_init_function(e, ubsec_init) ||
211 !ENGINE_set_finish_function(e, ubsec_finish) ||
212 !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
213 !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
216 #ifndef OPENSSL_NO_RSA
217 /* We know that the "PKCS1_SSLeay()" functions hook properly
218 * to the Broadcom-specific mod_exp and mod_exp_crt so we use
219 * those functions. NB: We don't use ENGINE_openssl() or
220 * anything "more generic" because something like the RSAref
221 * code may not hook properly, and if you own one of these
222 * cards then you have the right to do RSA operations on it
224 meth1 = RSA_PKCS1_SSLeay();
225 ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
226 ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
227 ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
228 ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
231 #ifndef OPENSSL_NO_DH
232 #ifndef HAVE_UBSEC_DH
233 /* Much the same for Diffie-Hellman */
234 meth3 = DH_OpenSSL();
235 ubsec_dh.generate_key = meth3->generate_key;
236 ubsec_dh.compute_key = meth3->compute_key;
237 #endif /* HAVE_UBSEC_DH */
240 /* Ensure the ubsec error handling is set up */
241 ERR_load_UBSEC_strings();
245 static ENGINE *engine_ubsec(void)
247 ENGINE *ret = ENGINE_new();
250 if(!bind_helper(ret))
258 void ENGINE_load_ubsec(void)
260 /* Copied from eng_[openssl|dyn].c */
261 ENGINE *toadd = engine_ubsec();
268 /* This is a process-global DSO handle used for loading and unloading
269 * the UBSEC library. NB: This is only set (or unset) during an
270 * init() or finish() call (reference counts permitting) and they're
271 * operating with global locks, so this should be thread-safe
274 static DSO *ubsec_dso = NULL;
276 /* These are the function pointers that are (un)set when the library has
277 * successfully (un)loaded. */
279 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
280 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
281 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
282 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
283 #ifndef OPENSSL_NO_DH
284 static t_UBSEC_diffie_hellman_generate_ioctl
285 *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
286 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
288 /* #ifndef OPENSSL_NO_RSA */
289 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
290 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
292 #ifndef OPENSSL_NO_DSA
293 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
294 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
296 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
297 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
298 static t_UBSEC_max_key_len_ioctl *p_UBSEC_max_key_len_ioctl = NULL;
300 static int max_key_len = 1024; /* ??? */
303 * These are the static string constants for the DSO file name and the function
304 * symbol names to bind to.
307 static const char *UBSEC_LIBNAME = "ubsec";
308 static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
309 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
310 static const char *UBSEC_F3 = "ubsec_open";
311 static const char *UBSEC_F4 = "ubsec_close";
312 #ifndef OPENSSL_NO_DH
313 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
314 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
316 /* #ifndef OPENSSL_NO_RSA */
317 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
318 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
320 #ifndef OPENSSL_NO_DSA
321 static const char *UBSEC_F9 = "dsa_sign_ioctl";
322 static const char *UBSEC_F10 = "dsa_verify_ioctl";
324 static const char *UBSEC_F11 = "math_accelerate_ioctl";
325 static const char *UBSEC_F12 = "rng_ioctl";
326 static const char *UBSEC_F13 = "ubsec_max_key_len_ioctl";
328 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
329 static int ubsec_destroy(ENGINE *e)
331 ERR_unload_UBSEC_strings();
335 /* (de)initialisation functions. */
336 static int ubsec_init(ENGINE *e)
338 t_UBSEC_ubsec_bytes_to_bits *p1;
339 t_UBSEC_ubsec_bits_to_bytes *p2;
340 t_UBSEC_ubsec_open *p3;
341 t_UBSEC_ubsec_close *p4;
342 #ifndef OPENSSL_NO_DH
343 t_UBSEC_diffie_hellman_generate_ioctl *p5;
344 t_UBSEC_diffie_hellman_agree_ioctl *p6;
346 /* #ifndef OPENSSL_NO_RSA */
347 t_UBSEC_rsa_mod_exp_ioctl *p7;
348 t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
350 #ifndef OPENSSL_NO_DSA
351 t_UBSEC_dsa_sign_ioctl *p9;
352 t_UBSEC_dsa_verify_ioctl *p10;
354 t_UBSEC_math_accelerate_ioctl *p11;
355 t_UBSEC_rng_ioctl *p12;
356 t_UBSEC_max_key_len_ioctl *p13;
359 if(ubsec_dso != NULL)
361 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
365 * Attempt to load libubsec.so/ubsec.dll/whatever.
367 ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
368 if(ubsec_dso == NULL)
370 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
375 !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
376 !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
377 !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
378 !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
379 #ifndef OPENSSL_NO_DH
380 !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *)
381 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
382 !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *)
383 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
385 /* #ifndef OPENSSL_NO_RSA */
386 !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
387 !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
389 #ifndef OPENSSL_NO_DSA
390 !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
391 !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
393 !(p11 = (t_UBSEC_math_accelerate_ioctl *)
394 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
395 !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)) ||
396 !(p13 = (t_UBSEC_max_key_len_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F13)))
398 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
402 /* Copy the pointers */
403 p_UBSEC_ubsec_bytes_to_bits = p1;
404 p_UBSEC_ubsec_bits_to_bytes = p2;
405 p_UBSEC_ubsec_open = p3;
406 p_UBSEC_ubsec_close = p4;
407 #ifndef OPENSSL_NO_DH
408 p_UBSEC_diffie_hellman_generate_ioctl = p5;
409 p_UBSEC_diffie_hellman_agree_ioctl = p6;
411 #ifndef OPENSSL_NO_RSA
412 p_UBSEC_rsa_mod_exp_ioctl = p7;
413 p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
415 #ifndef OPENSSL_NO_DSA
416 p_UBSEC_dsa_sign_ioctl = p9;
417 p_UBSEC_dsa_verify_ioctl = p10;
419 p_UBSEC_math_accelerate_ioctl = p11;
420 p_UBSEC_rng_ioctl = p12;
421 p_UBSEC_max_key_len_ioctl = p13;
423 /* Perform an open to see if there's actually any unit running. */
424 if (((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0) && (p_UBSEC_max_key_len_ioctl(fd, &max_key_len) == 0))
426 p_UBSEC_ubsec_close(fd);
431 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
437 p_UBSEC_ubsec_bytes_to_bits = NULL;
438 p_UBSEC_ubsec_bits_to_bytes = NULL;
439 p_UBSEC_ubsec_open = NULL;
440 p_UBSEC_ubsec_close = NULL;
441 #ifndef OPENSSL_NO_DH
442 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
443 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
445 #ifndef OPENSSL_NO_RSA
446 p_UBSEC_rsa_mod_exp_ioctl = NULL;
447 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
449 #ifndef OPENSSL_NO_DSA
450 p_UBSEC_dsa_sign_ioctl = NULL;
451 p_UBSEC_dsa_verify_ioctl = NULL;
453 p_UBSEC_math_accelerate_ioctl = NULL;
454 p_UBSEC_rng_ioctl = NULL;
455 p_UBSEC_max_key_len_ioctl = NULL;
460 static int ubsec_finish(ENGINE *e)
462 if(ubsec_dso == NULL)
464 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
467 if(!DSO_free(ubsec_dso))
469 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
473 p_UBSEC_ubsec_bytes_to_bits = NULL;
474 p_UBSEC_ubsec_bits_to_bytes = NULL;
475 p_UBSEC_ubsec_open = NULL;
476 p_UBSEC_ubsec_close = NULL;
477 #ifndef OPENSSL_NO_DH
478 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
479 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
481 #ifndef OPENSSL_NO_RSA
482 p_UBSEC_rsa_mod_exp_ioctl = NULL;
483 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
485 #ifndef OPENSSL_NO_DSA
486 p_UBSEC_dsa_sign_ioctl = NULL;
487 p_UBSEC_dsa_verify_ioctl = NULL;
489 p_UBSEC_math_accelerate_ioctl = NULL;
490 p_UBSEC_rng_ioctl = NULL;
491 p_UBSEC_max_key_len_ioctl = NULL;
495 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
497 int initialised = ((ubsec_dso == NULL) ? 0 : 1);
500 case UBSEC_CMD_SO_PATH:
503 UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
508 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
511 UBSEC_LIBNAME = (const char *)p;
516 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
520 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
521 const BIGNUM *m, BN_CTX *ctx)
526 if(ubsec_dso == NULL)
528 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
532 /* Check if hardware can't handle this argument. */
533 y_len = BN_num_bits(m);
534 if (y_len > max_key_len) {
535 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
536 return BN_mod_exp(r, a, p, m, ctx);
539 if(!bn_wexpand(r, m->top))
541 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
544 memset(r->d, 0, BN_num_bytes(m));
546 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
548 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
549 return BN_mod_exp(r, a, p, m, ctx);
552 if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
553 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d,
554 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
556 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
557 p_UBSEC_ubsec_close(fd);
559 return BN_mod_exp(r, a, p, m, ctx);
562 p_UBSEC_ubsec_close(fd);
564 r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
568 #ifndef OPENSSL_NO_RSA
569 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
574 if((ctx = BN_CTX_new()) == NULL)
577 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
579 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
583 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
584 rsa->dmq1, rsa->iqmp, ctx);
585 if (to_return == FAIL_TO_SOFTWARE)
588 * Do in software as hardware failed.
590 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
591 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
600 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
601 const BIGNUM *q, const BIGNUM *dp,
602 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
608 m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
609 y_len = BN_num_bits(p) + BN_num_bits(q);
611 /* Check if hardware can't handle this argument. */
612 if (y_len > max_key_len) {
613 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
614 return FAIL_TO_SOFTWARE;
617 if (!bn_wexpand(r, p->top + q->top + 1)) {
618 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
622 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
624 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
625 return FAIL_TO_SOFTWARE;
628 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
629 (unsigned char *)a->d, BN_num_bits(a),
630 (unsigned char *)qinv->d, BN_num_bits(qinv),
631 (unsigned char *)dp->d, BN_num_bits(dp),
632 (unsigned char *)p->d, BN_num_bits(p),
633 (unsigned char *)dq->d, BN_num_bits(dq),
634 (unsigned char *)q->d, BN_num_bits(q),
635 (unsigned char *)r->d, &y_len) != 0) {
636 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
637 p_UBSEC_ubsec_close(fd);
638 return FAIL_TO_SOFTWARE;
641 p_UBSEC_ubsec_close(fd);
643 r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
647 #ifndef OPENSSL_NO_DSA
649 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
650 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
651 BN_CTX *ctx, BN_MONT_CTX *in_mont)
657 /* let rr = a1 ^ p1 mod m */
658 if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
659 /* let t = a2 ^ p2 mod m */
660 if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
661 /* let rr = rr * t mod m */
662 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
669 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
670 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
673 return ubsec_mod_exp(r, a, p, m, ctx);
679 * This function is aliased to mod_exp (with the mont stuff dropped).
681 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
682 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
686 #ifndef OPENSSL_NO_RSA
687 /* Do in software if the key is too large for the hardware. */
688 if (BN_num_bits(m) > max_key_len)
690 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
691 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
696 ret = ubsec_mod_exp(r, a, p, m, ctx);
702 #ifndef OPENSSL_NO_DH
703 /* This function is aliased to mod_exp (with the dh and mont dropped). */
704 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
705 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
708 return ubsec_mod_exp(r, a, p, m, ctx);
712 #ifndef OPENSSL_NO_DSA
713 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
715 DSA_SIG *to_return = NULL;
716 int s_len = 160, r_len = 160, d_len, fd;
717 BIGNUM m, *r=NULL, *s=NULL;
723 if ((s == NULL) || (r==NULL))
726 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
728 if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
729 (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
730 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
734 if (BN_bin2bn(dgst,dlen,&m) == NULL) {
735 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
739 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
740 const DSA_METHOD *meth;
742 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
743 meth = DSA_OpenSSL();
744 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
748 if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
749 (unsigned char *)dgst, d_len,
750 NULL, 0, /* compute random value */
751 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
752 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
753 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
754 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
755 (unsigned char *)r->d, &r_len,
756 (unsigned char *)s->d, &s_len ) != 0) {
757 const DSA_METHOD *meth;
759 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
760 p_UBSEC_ubsec_close(fd);
761 meth = DSA_OpenSSL();
762 to_return = meth->dsa_do_sign(dgst, dlen, dsa);
767 p_UBSEC_ubsec_close(fd);
769 r->top = (160+BN_BITS2-1)/BN_BITS2;
770 s->top = (160+BN_BITS2-1)/BN_BITS2;
772 to_return = DSA_SIG_new();
773 if(to_return == NULL) {
774 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
790 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
791 DSA_SIG *sig, DSA *dsa)
800 if(!bn_wexpand(&v, dsa->p->top)) {
801 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
805 v_len = BN_num_bits(dsa->p);
807 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
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_verify(dgst, dgst_len, sig, dsa);
818 if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
819 (unsigned char *)dgst, d_len,
820 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
821 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
822 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
823 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
824 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
825 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
826 (unsigned char *)v.d, &v_len) != 0) {
827 const DSA_METHOD *meth;
828 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
829 p_UBSEC_ubsec_close(fd);
831 meth = DSA_OpenSSL();
832 to_return = meth->dsa_do_verify(dgst, dgst_len, sig, dsa);
837 p_UBSEC_ubsec_close(fd);
846 #ifndef OPENSSL_NO_DH
847 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
853 k_len = BN_num_bits(dh->p);
855 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
857 const DH_METHOD *meth;
858 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
860 ret = meth->compute_key(key, pub_key, dh);
864 if (p_UBSEC_diffie_hellman_agree_ioctl(fd,
865 (unsigned char *)dh->priv_key->d, BN_num_bits(dh->priv_key),
866 (unsigned char *)pub_key->d, BN_num_bits(pub_key),
867 (unsigned char *)dh->p->d, BN_num_bits(dh->p),
870 /* Hardware's a no go, failover to software */
871 const DH_METHOD *meth;
872 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
873 p_UBSEC_ubsec_close(fd);
876 ret = meth->compute_key(key, pub_key, dh);
881 p_UBSEC_ubsec_close(fd);
883 ret = p_UBSEC_ubsec_bits_to_bytes(k_len);
888 static int ubsec_dh_generate_key (DH *dh)
895 BIGNUM *pub_key = NULL;
896 BIGNUM *priv_key = NULL;
899 * How many bits should Random x be? dh_key.c
900 * sets the range from 0 to num_bits(modulus) ???
903 if (dh->priv_key == NULL)
906 if (priv_key == NULL) goto err;
907 priv_key_len = BN_num_bits(dh->p);
908 bn_wexpand(priv_key, dh->p->top);
910 if (!BN_rand_range(priv_key, dh->p)) goto err;
911 while (BN_is_zero(priv_key));
912 random_bits = BN_num_bits(priv_key);
916 priv_key = dh->priv_key;
919 if (dh->pub_key == NULL)
922 pub_key_len = BN_num_bits(dh->p);
923 bn_wexpand(pub_key, dh->p->top);
924 if(pub_key == NULL) goto err;
928 pub_key = dh->pub_key;
931 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
933 const DH_METHOD *meth;
934 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
936 ret = meth->generate_key(dh);
940 if (p_UBSEC_diffie_hellman_generate_ioctl(fd,
941 (unsigned char *)priv_key->d, &priv_key_len,
942 (unsigned char *)pub_key->d, &pub_key_len,
943 (unsigned char *)dh->g->d, BN_num_bits(dh->g),
944 (unsigned char *)dh->p->d, BN_num_bits(dh->p),
945 0, 0, random_bits) != 0)
947 /* Hardware's a no go, failover to software */
948 const DH_METHOD *meth;
950 ENGINEerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY, UBSEC_R_REQUEST_FAILED);
951 p_UBSEC_ubsec_close(fd);
954 ret = meth->generate_key(dh);
959 p_UBSEC_ubsec_close(fd);
961 dh->pub_key = pub_key;
962 dh->pub_key->top = (pub_key_len + BN_BITS2-1) / BN_BITS2;
963 dh->priv_key = priv_key;
964 dh->priv_key->top = (priv_key_len + BN_BITS2-1) / BN_BITS2;
973 static int ubsec_rand_bytes(unsigned char * buf,
979 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0)
981 const RAND_METHOD *meth;
982 ENGINEerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
983 num = p_UBSEC_ubsec_bits_to_bytes(num);
984 meth = RAND_SSLeay();
985 meth->seed(buf, num);
986 ret = meth->bytes(buf, num);
990 num *= 8; /* bytes to bits */
992 if (p_UBSEC_rng_ioctl(fd,
997 /* Hardware's a no go, failover to software */
998 const RAND_METHOD *meth;
1000 ENGINEerr(UBSEC_F_UBSEC_RNG_BYTES, UBSEC_R_REQUEST_FAILED);
1001 p_UBSEC_ubsec_close(fd);
1003 num = p_UBSEC_ubsec_bits_to_bytes(num);
1004 meth = RAND_SSLeay();
1005 meth->seed(buf, num);
1006 ret = meth->bytes(buf, num);
1011 p_UBSEC_ubsec_close(fd);
1019 static int ubsec_rand_status(void)
1025 /* This stuff is needed if this ENGINE is being compiled into a self-contained
1026 * shared-library. */
1027 #ifdef ENGINE_DYNAMIC_SUPPORT
1028 static int bind_fn(ENGINE *e, const char *id)
1030 if(id && (strcmp(id, engine_ubsec_id) != 0))
1036 IMPLEMENT_DYNAMIC_CHECK_FN()
1037 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
1038 #endif /* ENGINE_DYNAMIC_SUPPORT */
1040 #endif /* !OPENSSL_NO_HW_UBSEC */
1041 #endif /* !OPENSSL_NO_HW */