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 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
);
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
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
,
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
);
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
,
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
);
112 static int ubsec_rand_bytes(unsigned char *buf
, int num
);
113 static int ubsec_rand_status(void);
116 #define UBSEC_CMD_SO_PATH ENGINE_CMD_BASE
117 static const ENGINE_CMD_DEFN ubsec_cmd_defns
[] = {
120 "Specifies the path to the 'ubsec' shared library",
121 ENGINE_CMD_FLAG_STRING
},
125 #ifndef OPENSSL_NO_RSA
126 /* Our internal RSA_METHOD that we provide pointers to */
127 static RSA_METHOD ubsec_rsa
=
145 #ifndef OPENSSL_NO_DSA
146 /* Our internal DSA_METHOD that we provide pointers to */
147 static DSA_METHOD ubsec_dsa
=
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 */
162 #ifndef OPENSSL_NO_DH
163 /* Our internal DH_METHOD that we provide pointers to */
164 static DH_METHOD ubsec_dh
=
167 ubsec_dh_generate_key
,
168 ubsec_dh_compute_key
,
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
[] =
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"},
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)
226 if(ubsec_err_lib
< 0)
228 if((ubsec_err_lib
= ERR_get_next_error_library()) <= 0)
230 ubsec_str_functs
[0].error
= ERR_PACK(ubsec_err_lib
,0,0);
231 ERR_load_strings(ubsec_err_lib
, ubsec_str_functs
);
234 static void ubsec_unload_error_strings(void)
236 if(ubsec_err_lib
>= 0)
238 ERR_unload_strings(ubsec_err_lib
, ubsec_str_functs
);
243 #define UBSECerr(f,r) /* NOP */
244 static void ubsec_load_error_strings(void) { } /* NOP */
245 static void ubsec_unload_error_strings(void) { } /* NOP */
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";
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
)
256 #ifndef OPENSSL_NO_RSA
257 const RSA_METHOD
*meth1
;
259 #ifndef OPENSSL_NO_DH
260 #ifndef HAVE_UBSEC_DH
261 const DH_METHOD
*meth3
;
262 #endif /* HAVE_UBSEC_DH */
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
) ||
269 #ifndef OPENSSL_NO_DSA
270 !ENGINE_set_DSA(e
, &ubsec_dsa
) ||
272 #ifndef OPENSSL_NO_DH
273 !ENGINE_set_DH(e
, &ubsec_dh
) ||
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
))
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
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
;
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 */
306 /* Ensure the ubsec error handling is set up */
307 ubsec_load_error_strings();
311 static ENGINE
*engine_ubsec(void)
313 ENGINE
*ret
= ENGINE_new();
316 if(!bind_helper(ret
))
324 void ENGINE_load_ubsec(void)
326 /* Copied from eng_[openssl|dyn].c */
327 ENGINE
*toadd
= engine_ubsec();
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
340 static DSO
*ubsec_dso
= NULL
;
342 /* These are the function pointers that are (un)set when the library has
343 * successfully (un)loaded. */
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
;
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
;
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
;
362 static t_UBSEC_math_accelerate_ioctl
*p_UBSEC_math_accelerate_ioctl
= NULL
;
363 static t_UBSEC_rng_ioctl
*p_UBSEC_rng_ioctl
= NULL
;
366 * These are the static string constants for the DSO file name and the function
367 * symbol names to bind to.
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";
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";
383 #ifndef OPENSSL_NO_DSA
384 static const char *UBSEC_F9
= "dsa_sign_ioctl";
385 static const char *UBSEC_F10
= "dsa_verify_ioctl";
387 static const char *UBSEC_F11
= "math_accelerate_ioctl";
388 static const char *UBSEC_F12
= "rng_ioctl";
390 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
391 static int ubsec_destroy(ENGINE
*e
)
393 ubsec_unload_error_strings();
397 /* (de)initialisation functions. */
398 static int ubsec_init(ENGINE
*e
)
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
;
408 /* #ifndef OPENSSL_NO_RSA */
409 t_UBSEC_rsa_mod_exp_ioctl
*p7
;
410 t_UBSEC_rsa_mod_exp_crt_ioctl
*p8
;
412 #ifndef OPENSSL_NO_DSA
413 t_UBSEC_dsa_sign_ioctl
*p9
;
414 t_UBSEC_dsa_verify_ioctl
*p10
;
416 t_UBSEC_math_accelerate_ioctl
*p11
;
417 t_UBSEC_rng_ioctl
*p12
;
420 if(ubsec_dso
!= NULL
)
422 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_ALREADY_LOADED
);
426 * Attempt to load libubsec.so/ubsec.dll/whatever.
428 ubsec_dso
= DSO_load(NULL
, UBSEC_LIBNAME
, NULL
, 0);
429 if(ubsec_dso
== NULL
)
431 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_DSO_FAILURE
);
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
)) ||
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
)) ||
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
)) ||
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
)))
458 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_DSO_FAILURE
);
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
;
471 #ifndef OPENSSL_NO_RSA
472 p_UBSEC_rsa_mod_exp_ioctl
= p7
;
473 p_UBSEC_rsa_mod_exp_crt_ioctl
= p8
;
475 #ifndef OPENSSL_NO_DSA
476 p_UBSEC_dsa_sign_ioctl
= p9
;
477 p_UBSEC_dsa_verify_ioctl
= p10
;
479 p_UBSEC_math_accelerate_ioctl
= p11
;
480 p_UBSEC_rng_ioctl
= p12
;
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)
485 p_UBSEC_ubsec_close(fd
);
490 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_UNIT_FAILURE
);
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
;
504 #ifndef OPENSSL_NO_RSA
505 p_UBSEC_rsa_mod_exp_ioctl
= NULL
;
506 p_UBSEC_rsa_mod_exp_crt_ioctl
= NULL
;
508 #ifndef OPENSSL_NO_DSA
509 p_UBSEC_dsa_sign_ioctl
= NULL
;
510 p_UBSEC_dsa_verify_ioctl
= NULL
;
512 p_UBSEC_math_accelerate_ioctl
= NULL
;
513 p_UBSEC_rng_ioctl
= NULL
;
518 static int ubsec_finish(ENGINE
*e
)
520 if(ubsec_dso
== NULL
)
522 UBSECerr(UBSEC_F_UBSEC_FINISH
, UBSEC_R_NOT_LOADED
);
525 if(!DSO_free(ubsec_dso
))
527 UBSECerr(UBSEC_F_UBSEC_FINISH
, UBSEC_R_DSO_FAILURE
);
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
;
539 #ifndef OPENSSL_NO_RSA
540 p_UBSEC_rsa_mod_exp_ioctl
= NULL
;
541 p_UBSEC_rsa_mod_exp_crt_ioctl
= NULL
;
543 #ifndef OPENSSL_NO_DSA
544 p_UBSEC_dsa_sign_ioctl
= NULL
;
545 p_UBSEC_dsa_verify_ioctl
= NULL
;
547 p_UBSEC_math_accelerate_ioctl
= NULL
;
548 p_UBSEC_rng_ioctl
= NULL
;
552 static int ubsec_ctrl(ENGINE
*e
, int cmd
, long i
, void *p
, void (*f
)())
554 int initialised
= ((ubsec_dso
== NULL
) ? 0 : 1);
557 case UBSEC_CMD_SO_PATH
:
560 UBSECerr(UBSEC_F_UBSEC_CTRL
,ERR_R_PASSED_NULL_PARAMETER
);
565 UBSECerr(UBSEC_F_UBSEC_CTRL
,UBSEC_R_ALREADY_LOADED
);
568 UBSEC_LIBNAME
= (const char *)p
;
573 UBSECerr(UBSEC_F_UBSEC_CTRL
,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED
);
577 static int ubsec_mod_exp(BIGNUM
*r
, const BIGNUM
*a
, const BIGNUM
*p
,
578 const BIGNUM
*m
, BN_CTX
*ctx
)
583 if(ubsec_dso
== NULL
)
585 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_NOT_LOADED
);
589 /* Check if hardware can't handle this argument. */
590 y_len
= BN_num_bits(m
);
592 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
596 if(!bn_wexpand(r
, m
->top
))
598 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_BN_EXPAND_FAIL
);
601 memset(r
->d
, 0, BN_num_bytes(m
));
603 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
605 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_UNIT_FAILURE
);
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)
613 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_REQUEST_FAILED
);
617 p_UBSEC_ubsec_close(fd
);
619 r
->top
= (BN_num_bits(m
)+BN_BITS2
-1)/BN_BITS2
;
623 #ifndef OPENSSL_NO_RSA
624 static int ubsec_rsa_mod_exp(BIGNUM
*r0
, const BIGNUM
*I
, RSA
*rsa
)
629 if((ctx
= BN_CTX_new()) == NULL
)
632 if(!rsa
->p
|| !rsa
->q
|| !rsa
->dmp1
|| !rsa
->dmq1
|| !rsa
->iqmp
)
634 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP
, UBSEC_R_MISSING_KEY_COMPONENTS
);
639 * Do in software if argument is too large for hardware.
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
);
645 to_return
= ubsec_mod_exp_crt(r0
, I
, rsa
->p
, rsa
->q
, rsa
->dmp1
,
646 rsa
->dmq1
, rsa
->iqmp
, ctx
);
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
)
663 m_len
= BN_num_bytes(p
) + BN_num_bytes(q
) + 1;
664 y_len
= BN_num_bits(p
) + BN_num_bits(q
);
666 /* Check if hardware can't handle this argument. */
668 UBSECerr(UBSEC_F_UBSEC_MOD_EXP
, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL
);
672 if (!bn_wexpand(r
, p
->top
+ q
->top
+ 1)) {
673 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT
, UBSEC_R_BN_EXPAND_FAIL
);
677 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
679 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_UNIT_FAILURE
);
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
);
695 p_UBSEC_ubsec_close(fd
);
697 r
->top
= (BN_num_bits(p
) + BN_num_bits(q
) + BN_BITS2
- 1)/BN_BITS2
;
701 #ifndef OPENSSL_NO_DSA
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
)
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
;
723 static int ubsec_mod_exp_dsa(DSA
*dsa
, BIGNUM
*r
, BIGNUM
*a
,
724 const BIGNUM
*p
, const BIGNUM
*m
, BN_CTX
*ctx
,
727 return ubsec_mod_exp(r
, a
, p
, m
, ctx
);
733 * This function is aliased to mod_exp (with the mont stuff dropped).
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
)
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)
744 const RSA_METHOD
*meth
= RSA_PKCS1_SSLeay();
745 ret
= (*meth
->bn_mod_exp
)(r
, a
, p
, m
, ctx
, m_ctx
);
750 ret
= ubsec_mod_exp(r
, a
, p
, m
, ctx
);
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
,
762 return ubsec_mod_exp(r
, a
, p
, m
, ctx
);
766 #ifndef OPENSSL_NO_DSA
767 static DSA_SIG
*ubsec_dsa_do_sign(const unsigned char *dgst
, int dlen
, DSA
*dsa
)
769 DSA_SIG
*to_return
= NULL
;
770 int s_len
= 160, r_len
= 160, d_len
, fd
;
771 BIGNUM m
, *r
=NULL
, *s
=NULL
;
777 if ((s
== NULL
) || (r
==NULL
))
780 d_len
= p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst
, dlen
);
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
);
788 if (BN_bin2bn(dgst
,dlen
,&m
) == NULL
) {
789 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN
, UBSEC_R_BN_EXPAND_FAIL
);
793 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
795 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_UNIT_FAILURE
);
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
);
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_SIGN
, UBSEC_R_BN_EXPAND_FAIL
);
835 static int ubsec_dsa_verify(const unsigned char *dgst
, int dgst_len
,
836 DSA_SIG
*sig
, DSA
*dsa
)
845 if(!bn_wexpand(&v
, dsa
->p
->top
)) {
846 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY
,UBSEC_R_BN_EXPAND_FAIL
);
850 v_len
= BN_num_bits(dsa
->p
);
852 d_len
= p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst
, dgst_len
);
854 if ((fd
= p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME
)) <= 0) {
856 UBSECerr(UBSEC_F_UBSEC_INIT
, UBSEC_R_UNIT_FAILURE
);
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
);
873 p_UBSEC_ubsec_close(fd
);
882 #ifndef OPENSSL_NO_DH
883 static int ubsec_dh_compute_key (unsigned char *key
,const BIGNUM
*pub_key
,DH
*dh
)
888 static int ubsec_dh_generate_key (DH
*dh
)
895 static int ubsec_rand_bytes(unsigned char *buf
, int num
)
900 static int ubsec_rand_status(void)
906 /* This stuff is needed if this ENGINE is being compiled into a self-contained
908 #ifdef ENGINE_DYNAMIC_SUPPORT
909 static int bind_fn(ENGINE
*e
, const char *id
)
911 if(id
&& (strcmp(id
, engine_ubsec_id
) != 0))
917 IMPLEMENT_DYNAMIC_CHECK_FN()
918 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn
)
919 #endif /* ENGINE_DYNAMIC_SUPPORT */
921 #endif /* !OPENSSL_NO_HW_UBSEC */
922 #endif /* !OPENSSL_NO_HW */