1 /* crypto/engine/hw_ubsec.c */
3 * Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL project
4 * 2000. Cloned shamelessly by Joe Tardo.
6 /* ====================================================================
7 * Copyright (c) 1999-2001 The OpenSSL Project. All rights reserved.
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in
18 * the documentation and/or other materials provided with the
21 * 3. All advertising materials mentioning features or use of this
22 * software must display the following acknowledgment:
23 * "This product includes software developed by the OpenSSL Project
24 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
26 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
27 * endorse or promote products derived from this software without
28 * prior written permission. For written permission, please contact
29 * licensing@OpenSSL.org.
31 * 5. Products derived from this software may not be called "OpenSSL"
32 * nor may "OpenSSL" appear in their names without prior written
33 * permission of the OpenSSL Project.
35 * 6. Redistributions of any form whatsoever must retain the following
37 * "This product includes software developed by the OpenSSL Project
38 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
40 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
41 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
42 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
43 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
44 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
45 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
46 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
47 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
48 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
49 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
50 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
51 * OF THE POSSIBILITY OF SUCH DAMAGE.
52 * ====================================================================
54 * This product includes cryptographic software written by Eric Young
55 * (eay@cryptsoft.com). This product includes software written by Tim
56 * Hudson (tjh@cryptsoft.com).
62 #include <openssl/crypto.h>
63 #include <openssl/buffer.h>
64 #include <openssl/dso.h>
65 #include <openssl/engine.h>
66 #ifndef OPENSSL_NO_RSA
67 # include <openssl/rsa.h>
69 #ifndef OPENSSL_NO_DSA
70 # include <openssl/dsa.h>
73 # include <openssl/dh.h>
75 #include <openssl/bn.h>
78 # ifndef OPENSSL_NO_HW_UBSEC
81 # include "hw_ubsec.h"
83 # include "vendor_defns/hw_ubsec.h"
86 # define UBSEC_LIB_NAME "ubsec engine"
87 # include "e_ubsec_err.c"
89 # define FAIL_TO_SOFTWARE -15
91 static int ubsec_destroy(ENGINE
*e
);
92 static int ubsec_init(ENGINE
*e
);
93 static int ubsec_finish(ENGINE
*e
);
94 static int ubsec_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void));
95 static int ubsec_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
96 const BIGNUM
*m
, BN_CTX
*ctx
);
97 # ifndef OPENSSL_NO_RSA
98 static int ubsec_mod_exp_crt(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
99 const BIGNUM
*q
, const BIGNUM
*dp
,
100 const BIGNUM
*dq
, const BIGNUM
*qinv
,
102 static int ubsec_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
,
104 static int ubsec_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
105 const BIGNUM
*m
, BN_CTX
*ctx
,
108 # ifndef OPENSSL_NO_DSA
110 static int ubsec_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
111 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
, BIGNUM
*m
,
112 BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
);
113 static int ubsec_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
114 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
117 static DSA_SIG
*ubsec_dsa_do_sign(const unsigned char *dgst
, int dlen
,
119 static int ubsec_dsa_verify(const unsigned char *dgst
, int dgst_len
,
120 DSA_SIG
*sig
, DSA
*dsa
);
122 # ifndef OPENSSL_NO_DH
123 static int ubsec_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
124 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
126 static int ubsec_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
,
128 static int ubsec_dh_generate_key(DH
*dh
);
132 static int ubsec_rand_bytes(unsigned char *buf
, int num
);
133 static int ubsec_rand_status(void);
136 # define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
137 static const ENGINE_CMD_DEFN ubsec_cmd_defns
[] = {
140 "Specifies the path to the 'ubsec' shared library",
141 ENGINE_CMD_FLAG_STRING
},
145 # ifndef OPENSSL_NO_RSA
146 /* Our internal RSA_METHOD that we provide pointers to */
147 static RSA_METHOD ubsec_rsa
= {
165 # ifndef OPENSSL_NO_DSA
166 /* Our internal DSA_METHOD that we provide pointers to */
167 static DSA_METHOD ubsec_dsa
= {
169 ubsec_dsa_do_sign
, /* dsa_do_sign */
170 NULL
, /* dsa_sign_setup */
171 ubsec_dsa_verify
, /* dsa_do_verify */
172 NULL
, /* ubsec_dsa_mod_exp *//* dsa_mod_exp */
173 NULL
, /* ubsec_mod_exp_dsa *//* bn_mod_exp */
178 NULL
, /* dsa_paramgen */
179 NULL
/* dsa_keygen */
183 # ifndef OPENSSL_NO_DH
184 /* Our internal DH_METHOD that we provide pointers to */
185 static DH_METHOD ubsec_dh
= {
187 ubsec_dh_generate_key
,
188 ubsec_dh_compute_key
,
198 /* Constants used when creating the ENGINE */
199 static const char *engine_ubsec_id
= "ubsec";
200 static const char *engine_ubsec_name
= "UBSEC hardware engine support";
203 * This internal function is used by ENGINE_ubsec() and possibly by the
204 * "dynamic" ENGINE support too
206 static int bind_helper(ENGINE
*e
)
208 # ifndef OPENSSL_NO_RSA
209 const RSA_METHOD
*meth1
;
211 # ifndef OPENSSL_NO_DH
212 # ifndef HAVE_UBSEC_DH
213 const DH_METHOD
*meth3
;
214 # endif /* HAVE_UBSEC_DH */
216 if (!ENGINE_set_id(e
, engine_ubsec_id
) ||
217 !ENGINE_set_name(e
, engine_ubsec_name
) ||
218 # ifndef OPENSSL_NO_RSA
219 !ENGINE_set_RSA(e
, &ubsec_rsa
) ||
221 # ifndef OPENSSL_NO_DSA
222 !ENGINE_set_DSA(e
, &ubsec_dsa
) ||
224 # ifndef OPENSSL_NO_DH
225 !ENGINE_set_DH(e
, &ubsec_dh
) ||
227 !ENGINE_set_destroy_function(e
, ubsec_destroy
) ||
228 !ENGINE_set_init_function(e
, ubsec_init
) ||
229 !ENGINE_set_finish_function(e
, ubsec_finish
) ||
230 !ENGINE_set_ctrl_function(e
, ubsec_ctrl
) ||
231 !ENGINE_set_cmd_defns(e
, ubsec_cmd_defns
))
234 # ifndef OPENSSL_NO_RSA
236 * We know that the "PKCS1_SSLeay()" functions hook properly to the
237 * Broadcom-specific mod_exp and mod_exp_crt so we use those functions.
238 * NB: We don't use ENGINE_openssl() or anything "more generic" because
239 * something like the RSAref code may not hook properly, and if you own
240 * one of these cards then you have the right to do RSA operations on it
243 meth1
= RSA_PKCS1_SSLeay();
244 ubsec_rsa
.rsa_pub_enc
= meth1
->rsa_pub_enc
;
245 ubsec_rsa
.rsa_pub_dec
= meth1
->rsa_pub_dec
;
246 ubsec_rsa
.rsa_priv_enc
= meth1
->rsa_priv_enc
;
247 ubsec_rsa
.rsa_priv_dec
= meth1
->rsa_priv_dec
;
250 # ifndef OPENSSL_NO_DH
251 # ifndef HAVE_UBSEC_DH
252 /* Much the same for Diffie-Hellman */
253 meth3
= DH_OpenSSL();
254 ubsec_dh
.generate_key
= meth3
->generate_key
;
255 ubsec_dh
.compute_key
= meth3
->compute_key
;
256 # endif /* HAVE_UBSEC_DH */
259 /* Ensure the ubsec error handling is set up */
260 ERR_load_UBSEC_strings();
264 # ifdef OPENSSL_NO_DYNAMIC_ENGINE
265 static ENGINE
*engine_ubsec(void)
267 ENGINE
*ret
= ENGINE_new();
270 if (!bind_helper(ret
)) {
277 void ENGINE_load_ubsec(void)
279 /* Copied from eng_[openssl|dyn].c */
280 ENGINE
*toadd
= engine_ubsec();
290 * This is a process-global DSO handle used for loading and unloading the
291 * UBSEC library. NB: This is only set (or unset) during an init() or
292 * finish() call (reference counts permitting) and they're operating with
293 * global locks, so this should be thread-safe implicitly.
296 static DSO
*ubsec_dso
= NULL
;
299 * These are the function pointers that are (un)set when the library has
300 * successfully (un)loaded.
303 static t_UBSEC_ubsec_bytes_to_bits
*p_UBSEC_ubsec_bytes_to_bits
= NULL
;
304 static t_UBSEC_ubsec_bits_to_bytes
*p_UBSEC_ubsec_bits_to_bytes
= NULL
;
305 static t_UBSEC_ubsec_open
*p_UBSEC_ubsec_open
= NULL
;
306 static t_UBSEC_ubsec_close
*p_UBSEC_ubsec_close
= NULL
;
307 # ifndef OPENSSL_NO_DH
308 static t_UBSEC_diffie_hellman_generate_ioctl
309 * p_UBSEC_diffie_hellman_generate_ioctl
= NULL
;
310 static t_UBSEC_diffie_hellman_agree_ioctl
*p_UBSEC_diffie_hellman_agree_ioctl
313 # ifndef OPENSSL_NO_RSA
314 static t_UBSEC_rsa_mod_exp_ioctl
*p_UBSEC_rsa_mod_exp_ioctl
= NULL
;
315 static t_UBSEC_rsa_mod_exp_crt_ioctl
*p_UBSEC_rsa_mod_exp_crt_ioctl
= NULL
;
317 # ifndef OPENSSL_NO_DSA
318 static t_UBSEC_dsa_sign_ioctl
*p_UBSEC_dsa_sign_ioctl
= NULL
;
319 static t_UBSEC_dsa_verify_ioctl
*p_UBSEC_dsa_verify_ioctl
= NULL
;
321 static t_UBSEC_math_accelerate_ioctl
*p_UBSEC_math_accelerate_ioctl
= NULL
;
322 static t_UBSEC_rng_ioctl
*p_UBSEC_rng_ioctl
= NULL
;
323 static t_UBSEC_max_key_len_ioctl
*p_UBSEC_max_key_len_ioctl
= NULL
;
325 static int max_key_len
= 1024; /* ??? */
328 * These are the static string constants for the DSO file name and the function
329 * symbol names to bind to.
332 static const char *UBSEC_LIBNAME
= NULL
;
333 static const char *get_UBSEC_LIBNAME(void)
336 return UBSEC_LIBNAME
;
340 static void free_UBSEC_LIBNAME(void)
343 OPENSSL_free((void *)UBSEC_LIBNAME
);
344 UBSEC_LIBNAME
= NULL
;
347 static long set_UBSEC_LIBNAME(const char *name
)
349 free_UBSEC_LIBNAME();
350 return (((UBSEC_LIBNAME
= BUF_strdup(name
)) != NULL
) ? 1 : 0);
353 static const char *UBSEC_F1
= "ubsec_bytes_to_bits";
354 static const char *UBSEC_F2
= "ubsec_bits_to_bytes";
355 static const char *UBSEC_F3
= "ubsec_open";
356 static const char *UBSEC_F4
= "ubsec_close";
357 # ifndef OPENSSL_NO_DH
358 static const char *UBSEC_F5
= "diffie_hellman_generate_ioctl";
359 static const char *UBSEC_F6
= "diffie_hellman_agree_ioctl";
361 /* #ifndef OPENSSL_NO_RSA */
362 static const char *UBSEC_F7
= "rsa_mod_exp_ioctl";
363 static const char *UBSEC_F8
= "rsa_mod_exp_crt_ioctl";
365 # ifndef OPENSSL_NO_DSA
366 static const char *UBSEC_F9
= "dsa_sign_ioctl";
367 static const char *UBSEC_F10
= "dsa_verify_ioctl";
369 static const char *UBSEC_F11
= "math_accelerate_ioctl";
370 static const char *UBSEC_F12
= "rng_ioctl";
371 static const char *UBSEC_F13
= "ubsec_max_key_len_ioctl";
373 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
374 static int ubsec_destroy(ENGINE
*e
)
376 free_UBSEC_LIBNAME();
377 ERR_unload_UBSEC_strings();
381 /* (de)initialisation functions. */
382 static int ubsec_init(ENGINE
*e
)
384 t_UBSEC_ubsec_bytes_to_bits
*p1
;
385 t_UBSEC_ubsec_bits_to_bytes
*p2
;
386 t_UBSEC_ubsec_open
*p3
;
387 t_UBSEC_ubsec_close
*p4
;
388 # ifndef OPENSSL_NO_DH
389 t_UBSEC_diffie_hellman_generate_ioctl
*p5
;
390 t_UBSEC_diffie_hellman_agree_ioctl
*p6
;
392 /* #ifndef OPENSSL_NO_RSA */
393 t_UBSEC_rsa_mod_exp_ioctl
*p7
;
394 t_UBSEC_rsa_mod_exp_crt_ioctl
*p8
;
396 # ifndef OPENSSL_NO_DSA
397 t_UBSEC_dsa_sign_ioctl
*p9
;
398 t_UBSEC_dsa_verify_ioctl
*p10
;
400 t_UBSEC_math_accelerate_ioctl
*p11
;
401 t_UBSEC_rng_ioctl
*p12
;
402 t_UBSEC_max_key_len_ioctl
*p13
;
405 if (ubsec_dso
!= NULL
) {
406 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_ALREADY_LOADED
);
410 * Attempt to load libubsec.so/ubsec.dll/whatever.
412 ubsec_dso
= DSO_load(NULL
, get_UBSEC_LIBNAME(), NULL
, 0);
413 if (ubsec_dso
== NULL
) {
414 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_DSO_FAILURE
);
418 if (!(p1
= (t_UBSEC_ubsec_bytes_to_bits
*)
419 DSO_bind_func(ubsec_dso
, UBSEC_F1
))
420 || !(p2
= (t_UBSEC_ubsec_bits_to_bytes
*)
421 DSO_bind_func(ubsec_dso
, UBSEC_F2
))
422 || !(p3
= (t_UBSEC_ubsec_open
*)
423 DSO_bind_func(ubsec_dso
, UBSEC_F3
))
424 || !(p4
= (t_UBSEC_ubsec_close
*)
425 DSO_bind_func(ubsec_dso
, UBSEC_F4
))
426 # ifndef OPENSSL_NO_DH
427 || !(p5
= (t_UBSEC_diffie_hellman_generate_ioctl
*)
428 DSO_bind_func(ubsec_dso
, UBSEC_F5
))
429 || !(p6
= (t_UBSEC_diffie_hellman_agree_ioctl
*)
430 DSO_bind_func(ubsec_dso
, UBSEC_F6
))
432 /* #ifndef OPENSSL_NO_RSA */
433 || !(p7
= (t_UBSEC_rsa_mod_exp_ioctl
*)
434 DSO_bind_func(ubsec_dso
, UBSEC_F7
))
435 || !(p8
= (t_UBSEC_rsa_mod_exp_crt_ioctl
*)
436 DSO_bind_func(ubsec_dso
, UBSEC_F8
))
438 # ifndef OPENSSL_NO_DSA
439 || !(p9
= (t_UBSEC_dsa_sign_ioctl
*)
440 DSO_bind_func(ubsec_dso
, UBSEC_F9
))
441 || !(p10
= (t_UBSEC_dsa_verify_ioctl
*)
442 DSO_bind_func(ubsec_dso
, UBSEC_F10
))
444 || !(p11
= (t_UBSEC_math_accelerate_ioctl
*)
445 DSO_bind_func(ubsec_dso
, UBSEC_F11
))
446 || !(p12
= (t_UBSEC_rng_ioctl
*)
447 DSO_bind_func(ubsec_dso
, UBSEC_F12
))
448 || !(p13
= (t_UBSEC_max_key_len_ioctl
*)
449 DSO_bind_func(ubsec_dso
, UBSEC_F13
))) {
450 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_DSO_FAILURE
);
454 /* Copy the pointers */
455 p_UBSEC_ubsec_bytes_to_bits
= p1
;
456 p_UBSEC_ubsec_bits_to_bytes
= p2
;
457 p_UBSEC_ubsec_open
= p3
;
458 p_UBSEC_ubsec_close
= p4
;
459 # ifndef OPENSSL_NO_DH
460 p_UBSEC_diffie_hellman_generate_ioctl
= p5
;
461 p_UBSEC_diffie_hellman_agree_ioctl
= p6
;
463 # ifndef OPENSSL_NO_RSA
464 p_UBSEC_rsa_mod_exp_ioctl
= p7
;
465 p_UBSEC_rsa_mod_exp_crt_ioctl
= p8
;
467 # ifndef OPENSSL_NO_DSA
468 p_UBSEC_dsa_sign_ioctl
= p9
;
469 p_UBSEC_dsa_verify_ioctl
= p10
;
471 p_UBSEC_math_accelerate_ioctl
= p11
;
472 p_UBSEC_rng_ioctl
= p12
;
473 p_UBSEC_max_key_len_ioctl
= p13
;
475 /* Perform an open to see if there's actually any unit running. */
476 if (((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) > 0)
477 && (p_UBSEC_max_key_len_ioctl(fd
, &max_key_len
) == 0)) {
478 p_UBSEC_ubsec_close(fd
);
481 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_UNIT_FAILURE
);
488 p_UBSEC_ubsec_bytes_to_bits
= NULL
;
489 p_UBSEC_ubsec_bits_to_bytes
= NULL
;
490 p_UBSEC_ubsec_open
= NULL
;
491 p_UBSEC_ubsec_close
= NULL
;
492 # ifndef OPENSSL_NO_DH
493 p_UBSEC_diffie_hellman_generate_ioctl
= NULL
;
494 p_UBSEC_diffie_hellman_agree_ioctl
= NULL
;
496 # ifndef OPENSSL_NO_RSA
497 p_UBSEC_rsa_mod_exp_ioctl
= NULL
;
498 p_UBSEC_rsa_mod_exp_crt_ioctl
= NULL
;
500 # ifndef OPENSSL_NO_DSA
501 p_UBSEC_dsa_sign_ioctl
= NULL
;
502 p_UBSEC_dsa_verify_ioctl
= NULL
;
504 p_UBSEC_math_accelerate_ioctl
= NULL
;
505 p_UBSEC_rng_ioctl
= NULL
;
506 p_UBSEC_max_key_len_ioctl
= NULL
;
511 static int ubsec_finish(ENGINE
*e
)
513 free_UBSEC_LIBNAME();
514 if (ubsec_dso
== NULL
) {
515 UBSECerr(UBSEC_F_UBSEC_FINISH
, UBSEC_R_NOT_LOADED
);
518 if (!DSO_free(ubsec_dso
)) {
519 UBSECerr(UBSEC_F_UBSEC_FINISH
, UBSEC_R_DSO_FAILURE
);
523 p_UBSEC_ubsec_bytes_to_bits
= NULL
;
524 p_UBSEC_ubsec_bits_to_bytes
= NULL
;
525 p_UBSEC_ubsec_open
= NULL
;
526 p_UBSEC_ubsec_close
= NULL
;
527 # ifndef OPENSSL_NO_DH
528 p_UBSEC_diffie_hellman_generate_ioctl
= NULL
;
529 p_UBSEC_diffie_hellman_agree_ioctl
= NULL
;
531 # ifndef OPENSSL_NO_RSA
532 p_UBSEC_rsa_mod_exp_ioctl
= NULL
;
533 p_UBSEC_rsa_mod_exp_crt_ioctl
= NULL
;
535 # ifndef OPENSSL_NO_DSA
536 p_UBSEC_dsa_sign_ioctl
= NULL
;
537 p_UBSEC_dsa_verify_ioctl
= NULL
;
539 p_UBSEC_math_accelerate_ioctl
= NULL
;
540 p_UBSEC_rng_ioctl
= NULL
;
541 p_UBSEC_max_key_len_ioctl
= NULL
;
545 static int ubsec_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
) (void))
547 int initialised
= ((ubsec_dso
== NULL
) ? 0 : 1);
549 case UBSEC_CMD_SO_PATH
:
551 UBSECerr(UBSEC_F_UBSEC_CTRL
, ERR_R_PASSED_NULL_PARAMETER
);
555 UBSECerr(UBSEC_F_UBSEC_CTRL
, UBSEC_R_ALREADY_LOADED
);
558 return set_UBSEC_LIBNAME((const char *)p
);
562 UBSECerr(UBSEC_F_UBSEC_CTRL
, UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
566 static int ubsec_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
567 const BIGNUM
*m
, BN_CTX
*ctx
)
572 if (ubsec_dso
== NULL
) {
573 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_NOT_LOADED
);
577 /* Check if hardware can't handle this argument. */
578 y_len
= BN_num_bits(m
);
579 if (y_len
> max_key_len
) {
580 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
581 return BN_mod_exp(r
, a
, p
, m
, ctx
);
584 if (!bn_wexpand(r
, m
->top
)) {
585 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_BN_EXPAND_FAIL
);
589 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
591 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_UNIT_FAILURE
);
592 return BN_mod_exp(r
, a
, p
, m
, ctx
);
595 if (p_UBSEC_rsa_mod_exp_ioctl(fd
, (unsigned char *)a
->d
, BN_num_bits(a
),
596 (unsigned char *)m
->d
, BN_num_bits(m
),
597 (unsigned char *)p
->d
, BN_num_bits(p
),
598 (unsigned char *)r
->d
, &y_len
) != 0) {
599 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_REQUEST_FAILED
);
600 p_UBSEC_ubsec_close(fd
);
602 return BN_mod_exp(r
, a
, p
, m
, ctx
);
605 p_UBSEC_ubsec_close(fd
);
607 r
->top
= (BN_num_bits(m
) + BN_BITS2
- 1) / BN_BITS2
;
611 # ifndef OPENSSL_NO_RSA
612 static int ubsec_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
,
617 if (!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
) {
618 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP
, UBSEC_R_MISSING_KEY_COMPONENTS
);
622 to_return
= ubsec_mod_exp_crt(r0
, I
, rsa
->p
, rsa
->q
, rsa
->dmp1
,
623 rsa
->dmq1
, rsa
->iqmp
, ctx
);
624 if (to_return
== FAIL_TO_SOFTWARE
) {
626 * Do in software as hardware failed.
628 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
629 to_return
= (*meth
->rsa_mod_exp
) (r0
, I
, rsa
, ctx
);
635 static int ubsec_mod_exp_crt(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
636 const BIGNUM
*q
, const BIGNUM
*dp
,
637 const BIGNUM
*dq
, const BIGNUM
*qinv
,
642 y_len
= BN_num_bits(p
) + BN_num_bits(q
);
644 /* Check if hardware can't handle this argument. */
645 if (y_len
> max_key_len
) {
646 UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT
,
647 UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
648 return FAIL_TO_SOFTWARE
;
651 if (!bn_wexpand(r
, p
->top
+ q
->top
+ 1)) {
652 UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT
, UBSEC_R_BN_EXPAND_FAIL
);
656 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
658 UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT
, UBSEC_R_UNIT_FAILURE
);
659 return FAIL_TO_SOFTWARE
;
662 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd
,
663 (unsigned char *)a
->d
, BN_num_bits(a
),
664 (unsigned char *)qinv
->d
,
666 (unsigned char *)dp
->d
, BN_num_bits(dp
),
667 (unsigned char *)p
->d
, BN_num_bits(p
),
668 (unsigned char *)dq
->d
, BN_num_bits(dq
),
669 (unsigned char *)q
->d
, BN_num_bits(q
),
670 (unsigned char *)r
->d
, &y_len
) != 0) {
671 UBSECerr(UBSEC_F_UBSEC_MOD_EXP_CRT
, UBSEC_R_REQUEST_FAILED
);
672 p_UBSEC_ubsec_close(fd
);
673 return FAIL_TO_SOFTWARE
;
676 p_UBSEC_ubsec_close(fd
);
678 r
->top
= (BN_num_bits(p
) + BN_num_bits(q
) + BN_BITS2
- 1) / BN_BITS2
;
683 # ifndef OPENSSL_NO_DSA
685 static int ubsec_dsa_mod_exp(DSA
*dsa
, BIGNUM
*rr
, BIGNUM
*a1
,
686 BIGNUM
*p1
, BIGNUM
*a2
, BIGNUM
*p2
, BIGNUM
*m
,
687 BN_CTX
*ctx
, BN_MONT_CTX
*in_mont
)
693 /* let rr = a1 ^ p1 mod m */
694 if (!ubsec_mod_exp(rr
, a1
, p1
, m
, ctx
))
696 /* let t = a2 ^ p2 mod m */
697 if (!ubsec_mod_exp(&t
, a2
, p2
, m
, ctx
))
699 /* let rr = rr * t mod m */
700 if (!BN_mod_mul(rr
, rr
, &t
, m
, ctx
))
708 static int ubsec_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
709 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
712 return ubsec_mod_exp(r
, a
, p
, m
, ctx
);
717 # ifndef OPENSSL_NO_RSA
720 * This function is aliased to mod_exp (with the mont stuff dropped).
722 static int ubsec_mod_exp_mont(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
723 const BIGNUM
*m
, BN_CTX
*ctx
,
728 /* Do in software if the key is too large for the hardware. */
729 if (BN_num_bits(m
) > max_key_len
) {
730 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
731 ret
= (*meth
->bn_mod_exp
) (r
, a
, p
, m
, ctx
, m_ctx
);
733 ret
= ubsec_mod_exp(r
, a
, p
, m
, ctx
);
740 # ifndef OPENSSL_NO_DH
741 /* This function is aliased to mod_exp (with the dh and mont dropped). */
742 static int ubsec_mod_exp_dh(const DH
*dh
, BIGNUM
*r
, const BIGNUM
*a
,
743 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
746 return ubsec_mod_exp(r
, a
, p
, m
, ctx
);
750 # ifndef OPENSSL_NO_DSA
751 static DSA_SIG
*ubsec_dsa_do_sign(const unsigned char *dgst
, int dlen
,
754 DSA_SIG
*to_return
= NULL
;
755 int s_len
= 160, r_len
= 160, d_len
, fd
;
756 BIGNUM m
, *r
= NULL
, *s
= NULL
;
762 if ((s
== NULL
) || (r
== NULL
))
765 d_len
= p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst
, dlen
);
767 if (!bn_wexpand(r
, (160 + BN_BITS2
- 1) / BN_BITS2
) ||
768 (!bn_wexpand(s
, (160 + BN_BITS2
- 1) / BN_BITS2
))) {
769 UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN
, UBSEC_R_BN_EXPAND_FAIL
);
773 if (BN_bin2bn(dgst
, dlen
, &m
) == NULL
) {
774 UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN
, UBSEC_R_BN_EXPAND_FAIL
);
778 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
779 const DSA_METHOD
*meth
;
781 UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN
, UBSEC_R_UNIT_FAILURE
);
782 meth
= DSA_OpenSSL();
783 to_return
= meth
->dsa_do_sign(dgst
, dlen
, dsa
);
787 if (p_UBSEC_dsa_sign_ioctl(fd
,
788 /* compute hash before signing */
789 0, (unsigned char *)dgst
, d_len
, NULL
,
790 /* compute random value */
792 (unsigned char *)dsa
->p
->d
,
794 (unsigned char *)dsa
->q
->d
,
796 (unsigned char *)dsa
->g
->d
,
798 (unsigned char *)dsa
->priv_key
->d
,
799 BN_num_bits(dsa
->priv_key
),
800 (unsigned char *)r
->d
, &r_len
,
801 (unsigned char *)s
->d
, &s_len
) != 0) {
802 const DSA_METHOD
*meth
;
804 UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN
, UBSEC_R_REQUEST_FAILED
);
805 p_UBSEC_ubsec_close(fd
);
806 meth
= DSA_OpenSSL();
807 to_return
= meth
->dsa_do_sign(dgst
, dlen
, dsa
);
812 p_UBSEC_ubsec_close(fd
);
814 r
->top
= (160 + BN_BITS2
- 1) / BN_BITS2
;
815 s
->top
= (160 + BN_BITS2
- 1) / BN_BITS2
;
817 to_return
= DSA_SIG_new();
818 if (to_return
== NULL
) {
819 UBSECerr(UBSEC_F_UBSEC_DSA_DO_SIGN
, UBSEC_R_BN_EXPAND_FAIL
);
837 static int ubsec_dsa_verify(const unsigned char *dgst
, int dgst_len
,
838 DSA_SIG
*sig
, DSA
*dsa
)
847 if (!bn_wexpand(pv
, dsa
->p
->top
)) {
848 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY
, UBSEC_R_BN_EXPAND_FAIL
);
852 v_len
= BN_num_bits(dsa
->p
);
854 d_len
= p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst
, dgst_len
);
856 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
857 const DSA_METHOD
*meth
;
859 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY
, UBSEC_R_UNIT_FAILURE
);
860 meth
= DSA_OpenSSL();
861 to_return
= meth
->dsa_do_verify(dgst
, dgst_len
, sig
, dsa
);
865 if (p_UBSEC_dsa_verify_ioctl(fd
, 0, /* compute hash before signing */
866 (unsigned char *)dgst
, d_len
,
867 (unsigned char *)dsa
->p
->d
,
869 (unsigned char *)dsa
->q
->d
,
871 (unsigned char *)dsa
->g
->d
,
873 (unsigned char *)dsa
->pub_key
->d
,
874 BN_num_bits(dsa
->pub_key
),
875 (unsigned char *)sig
->r
->d
,
877 (unsigned char *)sig
->s
->d
,
878 BN_num_bits(sig
->s
), (unsigned char *)v
.d
,
880 const DSA_METHOD
*meth
;
881 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY
, UBSEC_R_REQUEST_FAILED
);
882 p_UBSEC_ubsec_close(fd
);
884 meth
= DSA_OpenSSL();
885 to_return
= meth
->dsa_do_verify(dgst
, dgst_len
, sig
, dsa
);
890 p_UBSEC_ubsec_close(fd
);
899 # ifndef OPENSSL_NO_DH
900 static int ubsec_dh_compute_key(unsigned char *key
, const BIGNUM
*pub_key
,
903 int ret
= -1, k_len
, fd
;
905 k_len
= BN_num_bits(dh
->p
);
907 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
908 const DH_METHOD
*meth
;
909 UBSECerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY
, UBSEC_R_UNIT_FAILURE
);
911 ret
= meth
->compute_key(key
, pub_key
, dh
);
915 if (p_UBSEC_diffie_hellman_agree_ioctl(fd
,
916 (unsigned char *)dh
->priv_key
->d
,
917 BN_num_bits(dh
->priv_key
),
918 (unsigned char *)pub_key
->d
,
919 BN_num_bits(pub_key
),
920 (unsigned char *)dh
->p
->d
,
921 BN_num_bits(dh
->p
), key
,
923 /* Hardware's a no go, failover to software */
924 const DH_METHOD
*meth
;
925 UBSECerr(UBSEC_F_UBSEC_DH_COMPUTE_KEY
, UBSEC_R_REQUEST_FAILED
);
926 p_UBSEC_ubsec_close(fd
);
929 ret
= meth
->compute_key(key
, pub_key
, dh
);
934 p_UBSEC_ubsec_close(fd
);
936 ret
= p_UBSEC_ubsec_bits_to_bytes(k_len
);
941 static int ubsec_dh_generate_key(DH
*dh
)
943 int ret
= 0, random_bits
= 0, pub_key_len
= 0, priv_key_len
= 0, fd
;
944 BIGNUM
*pub_key
= NULL
;
945 BIGNUM
*priv_key
= NULL
;
948 * How many bits should Random x be? dh_key.c
949 * sets the range from 0 to num_bits(modulus) ???
952 if (dh
->priv_key
== NULL
) {
954 if (priv_key
== NULL
)
956 priv_key_len
= BN_num_bits(dh
->p
);
957 if (bn_wexpand(priv_key
, dh
->p
->top
) == NULL
)
960 if (!BN_rand_range(priv_key
, dh
->p
))
962 while (BN_is_zero(priv_key
)) ;
963 random_bits
= BN_num_bits(priv_key
);
965 priv_key
= dh
->priv_key
;
968 if (dh
->pub_key
== NULL
) {
972 pub_key_len
= BN_num_bits(dh
->p
);
973 if (bn_wexpand(pub_key
, dh
->p
->top
) == NULL
)
976 pub_key
= dh
->pub_key
;
979 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
980 const DH_METHOD
*meth
;
981 UBSECerr(UBSEC_F_UBSEC_DH_GENERATE_KEY
, UBSEC_R_UNIT_FAILURE
);
983 ret
= meth
->generate_key(dh
);
987 if (p_UBSEC_diffie_hellman_generate_ioctl(fd
,
988 (unsigned char *)priv_key
->d
,
990 (unsigned char *)pub_key
->d
,
992 (unsigned char *)dh
->g
->d
,
994 (unsigned char *)dh
->p
->d
,
995 BN_num_bits(dh
->p
), 0, 0,
997 /* Hardware's a no go, failover to software */
998 const DH_METHOD
*meth
;
1000 UBSECerr(UBSEC_F_UBSEC_DH_GENERATE_KEY
, UBSEC_R_REQUEST_FAILED
);
1001 p_UBSEC_ubsec_close(fd
);
1003 meth
= DH_OpenSSL();
1004 ret
= meth
->generate_key(dh
);
1009 p_UBSEC_ubsec_close(fd
);
1011 dh
->pub_key
= pub_key
;
1012 dh
->pub_key
->top
= (pub_key_len
+ BN_BITS2
- 1) / BN_BITS2
;
1013 dh
->priv_key
= priv_key
;
1014 dh
->priv_key
->top
= (priv_key_len
+ BN_BITS2
- 1) / BN_BITS2
;
1023 static int ubsec_rand_bytes(unsigned char *buf
, int num
)
1027 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
1028 const RAND_METHOD
*meth
;
1029 UBSECerr(UBSEC_F_UBSEC_RAND_BYTES
, UBSEC_R_UNIT_FAILURE
);
1030 num
= p_UBSEC_ubsec_bits_to_bytes(num
);
1031 meth
= RAND_SSLeay();
1032 meth
->seed(buf
, num
);
1033 ret
= meth
->bytes(buf
, num
);
1037 num
*= 8; /* bytes to bits */
1039 if (p_UBSEC_rng_ioctl(fd
, UBSEC_RNG_DIRECT
, buf
, &num
) != 0) {
1040 /* Hardware's a no go, failover to software */
1041 const RAND_METHOD
*meth
;
1043 UBSECerr(UBSEC_F_UBSEC_RAND_BYTES
, UBSEC_R_REQUEST_FAILED
);
1044 p_UBSEC_ubsec_close(fd
);
1046 num
= p_UBSEC_ubsec_bits_to_bytes(num
);
1047 meth
= RAND_SSLeay();
1048 meth
->seed(buf
, num
);
1049 ret
= meth
->bytes(buf
, num
);
1054 p_UBSEC_ubsec_close(fd
);
1061 static int ubsec_rand_status(void)
1068 * This stuff is needed if this ENGINE is being compiled into a
1069 * self-contained shared-library.
1071 # ifndef OPENSSL_NO_DYNAMIC_ENGINE
1072 static int bind_fn(ENGINE
*e
, const char *id
)
1074 if (id
&& (strcmp(id
, engine_ubsec_id
) != 0))
1076 if (!bind_helper(e
))
1081 IMPLEMENT_DYNAMIC_CHECK_FN()
1082 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
1083 # endif /* OPENSSL_NO_DYNAMIC_ENGINE */
1084 # endif /* !OPENSSL_NO_HW_UBSEC */
1085 #endif /* !OPENSSL_NO_HW */