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_BN_mod_exp(e, ubsec_mod_exp) ||
276 !ENGINE_set_BN_mod_exp_crt(e, ubsec_mod_exp_crt) ||
277 !ENGINE_set_destroy_function(e, ubsec_destroy) ||
278 !ENGINE_set_init_function(e, ubsec_init) ||
279 !ENGINE_set_finish_function(e, ubsec_finish) ||
280 !ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
281 !ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
284 #ifndef OPENSSL_NO_RSA
285 /* We know that the "PKCS1_SSLeay()" functions hook properly
286 * to the Broadcom-specific mod_exp and mod_exp_crt so we use
287 * those functions. NB: We don't use ENGINE_openssl() or
288 * anything "more generic" because something like the RSAref
289 * code may not hook properly, and if you own one of these
290 * cards then you have the right to do RSA operations on it
292 meth1 = RSA_PKCS1_SSLeay();
293 ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
294 ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
295 ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
296 ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
299 #ifndef OPENSSL_NO_DH
300 #ifndef HAVE_UBSEC_DH
301 /* Much the same for Diffie-Hellman */
302 meth3 = DH_OpenSSL();
303 ubsec_dh.generate_key = meth3->generate_key;
304 ubsec_dh.compute_key = meth3->compute_key;
305 #endif /* HAVE_UBSEC_DH */
308 /* Ensure the ubsec error handling is set up */
309 ubsec_load_error_strings();
313 /* As this is only ever called once, there's no need for locking
314 * (indeed - the lock will already be held by our caller!!!) */
315 ENGINE *ENGINE_ubsec(void)
317 ENGINE *ret = ENGINE_new();
320 if(!bind_helper(ret))
328 /* This is a process-global DSO handle used for loading and unloading
329 * the UBSEC library. NB: This is only set (or unset) during an
330 * init() or finish() call (reference counts permitting) and they're
331 * operating with global locks, so this should be thread-safe
334 static DSO *ubsec_dso = NULL;
336 /* These are the function pointers that are (un)set when the library has
337 * successfully (un)loaded. */
339 static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
340 static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
341 static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
342 static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
343 #ifndef OPENSSL_NO_DH
344 static t_UBSEC_diffie_hellman_generate_ioctl
345 *p_UBSEC_diffie_hellman_generate_ioctl = NULL;
346 static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
348 /* #ifndef OPENSSL_NO_RSA */
349 static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
350 static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
352 #ifndef OPENSSL_NO_DSA
353 static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
354 static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
356 static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
357 static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;
360 * These are the static string constants for the DSO file name and the function
361 * symbol names to bind to.
364 static const char *UBSEC_LIBNAME = "ubsec";
365 static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
366 static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
367 static const char *UBSEC_F3 = "ubsec_open";
368 static const char *UBSEC_F4 = "ubsec_close";
369 #ifndef OPENSSL_NO_DH
370 static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
371 static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
373 /* #ifndef OPENSSL_NO_RSA */
374 static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
375 static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
377 #ifndef OPENSSL_NO_DSA
378 static const char *UBSEC_F9 = "dsa_sign_ioctl";
379 static const char *UBSEC_F10 = "dsa_verify_ioctl";
381 static const char *UBSEC_F11 = "math_accelerate_ioctl";
382 static const char *UBSEC_F12 = "rng_ioctl";
384 /* Destructor (complements the "ENGINE_ubsec()" constructor) */
385 static int ubsec_destroy(ENGINE *e)
387 ubsec_unload_error_strings();
391 /* (de)initialisation functions. */
392 static int ubsec_init(ENGINE *e)
394 t_UBSEC_ubsec_bytes_to_bits *p1;
395 t_UBSEC_ubsec_bits_to_bytes *p2;
396 t_UBSEC_ubsec_open *p3;
397 t_UBSEC_ubsec_close *p4;
398 #ifndef OPENSSL_NO_DH
399 t_UBSEC_diffie_hellman_generate_ioctl *p5;
400 t_UBSEC_diffie_hellman_agree_ioctl *p6;
402 /* #ifndef OPENSSL_NO_RSA */
403 t_UBSEC_rsa_mod_exp_ioctl *p7;
404 t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
406 #ifndef OPENSSL_NO_DSA
407 t_UBSEC_dsa_sign_ioctl *p9;
408 t_UBSEC_dsa_verify_ioctl *p10;
410 t_UBSEC_math_accelerate_ioctl *p11;
411 t_UBSEC_rng_ioctl *p12;
414 if(ubsec_dso != NULL)
416 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
420 * Attempt to load libubsec.so/ubsec.dll/whatever.
422 ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
423 if(ubsec_dso == NULL)
425 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
430 !(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
431 !(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
432 !(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
433 !(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
434 #ifndef OPENSSL_NO_DH
435 !(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *)
436 DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
437 !(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *)
438 DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
440 /* #ifndef OPENSSL_NO_RSA */
441 !(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
442 !(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
444 #ifndef OPENSSL_NO_DSA
445 !(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
446 !(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
448 !(p11 = (t_UBSEC_math_accelerate_ioctl *)
449 DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
450 !(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)))
452 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
456 /* Copy the pointers */
457 p_UBSEC_ubsec_bytes_to_bits = p1;
458 p_UBSEC_ubsec_bits_to_bytes = p2;
459 p_UBSEC_ubsec_open = p3;
460 p_UBSEC_ubsec_close = p4;
461 #ifndef OPENSSL_NO_DH
462 p_UBSEC_diffie_hellman_generate_ioctl = p5;
463 p_UBSEC_diffie_hellman_agree_ioctl = p6;
465 #ifndef OPENSSL_NO_RSA
466 p_UBSEC_rsa_mod_exp_ioctl = p7;
467 p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
469 #ifndef OPENSSL_NO_DSA
470 p_UBSEC_dsa_sign_ioctl = p9;
471 p_UBSEC_dsa_verify_ioctl = p10;
473 p_UBSEC_math_accelerate_ioctl = p11;
474 p_UBSEC_rng_ioctl = p12;
476 /* Perform an open to see if there's actually any unit running. */
477 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0)
479 p_UBSEC_ubsec_close(fd);
484 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
490 p_UBSEC_ubsec_bytes_to_bits = NULL;
491 p_UBSEC_ubsec_bits_to_bytes = NULL;
492 p_UBSEC_ubsec_open = NULL;
493 p_UBSEC_ubsec_close = NULL;
494 #ifndef OPENSSL_NO_DH
495 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
496 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
498 #ifndef OPENSSL_NO_RSA
499 p_UBSEC_rsa_mod_exp_ioctl = NULL;
500 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
502 #ifndef OPENSSL_NO_DSA
503 p_UBSEC_dsa_sign_ioctl = NULL;
504 p_UBSEC_dsa_verify_ioctl = NULL;
506 p_UBSEC_math_accelerate_ioctl = NULL;
507 p_UBSEC_rng_ioctl = NULL;
512 static int ubsec_finish(ENGINE *e)
514 if(ubsec_dso == NULL)
516 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
519 if(!DSO_free(ubsec_dso))
521 UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
525 p_UBSEC_ubsec_bytes_to_bits = NULL;
526 p_UBSEC_ubsec_bits_to_bytes = NULL;
527 p_UBSEC_ubsec_open = NULL;
528 p_UBSEC_ubsec_close = NULL;
529 #ifndef OPENSSL_NO_DH
530 p_UBSEC_diffie_hellman_generate_ioctl = NULL;
531 p_UBSEC_diffie_hellman_agree_ioctl = NULL;
533 #ifndef OPENSSL_NO_RSA
534 p_UBSEC_rsa_mod_exp_ioctl = NULL;
535 p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
537 #ifndef OPENSSL_NO_DSA
538 p_UBSEC_dsa_sign_ioctl = NULL;
539 p_UBSEC_dsa_verify_ioctl = NULL;
541 p_UBSEC_math_accelerate_ioctl = NULL;
542 p_UBSEC_rng_ioctl = NULL;
546 static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
548 int initialised = ((ubsec_dso == NULL) ? 0 : 1);
551 case UBSEC_CMD_SO_PATH:
554 UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
559 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
562 UBSEC_LIBNAME = (const char *)p;
567 UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
571 static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
572 const BIGNUM *m, BN_CTX *ctx)
577 if(ubsec_dso == NULL)
579 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
583 /* Check if hardware can't handle this argument. */
584 y_len = BN_num_bits(m);
586 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
590 if(!bn_wexpand(r, m->top))
592 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
595 memset(r->d, 0, BN_num_bytes(m));
597 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
599 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
603 if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
604 (unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d,
605 BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
607 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
611 p_UBSEC_ubsec_close(fd);
613 r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
617 #ifndef OPENSSL_NO_RSA
618 static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
623 if((ctx = BN_CTX_new()) == NULL)
626 if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
628 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
633 * Do in software if argument is too large for hardware.
635 if ((BN_num_bits(rsa->p)+BN_num_bits(rsa->q)) > 1024) {
636 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
637 to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
639 to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
640 rsa->dmq1, rsa->iqmp, ctx);
649 static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
650 const BIGNUM *q, const BIGNUM *dp,
651 const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
657 m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
658 y_len = BN_num_bits(p) + BN_num_bits(q);
660 /* Check if hardware can't handle this argument. */
662 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
666 if (!bn_wexpand(r, p->top + q->top + 1)) {
667 UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
671 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
673 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
677 if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
678 (unsigned char *)a->d, BN_num_bits(a),
679 (unsigned char *)qinv->d, BN_num_bits(qinv),
680 (unsigned char *)dp->d, BN_num_bits(dp),
681 (unsigned char *)p->d, BN_num_bits(p),
682 (unsigned char *)dq->d, BN_num_bits(dq),
683 (unsigned char *)q->d, BN_num_bits(q),
684 (unsigned char *)r->d, &y_len) != 0) {
685 UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
689 p_UBSEC_ubsec_close(fd);
691 r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
695 #ifndef OPENSSL_NO_DSA
697 static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
698 BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
699 BN_CTX *ctx, BN_MONT_CTX *in_mont)
705 /* let rr = a1 ^ p1 mod m */
706 if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
707 /* let t = a2 ^ p2 mod m */
708 if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
709 /* let rr = rr * t mod m */
710 if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
717 static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
718 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
721 return ubsec_mod_exp(r, a, p, m, ctx);
727 * This function is aliased to mod_exp (with the mont stuff dropped).
729 static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
730 const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
734 #ifndef OPENSSL_NO_RSA
735 /* Do in software if the key is too large for the hardware. */
736 if (BN_num_bits(m) > 1024)
738 const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
739 ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
744 ret = ubsec_mod_exp(r, a, p, m, ctx);
750 #ifndef OPENSSL_NO_DH
751 /* This function is aliased to mod_exp (with the dh and mont dropped). */
752 static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
753 const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
756 return ubsec_mod_exp(r, a, p, m, ctx);
760 #ifndef OPENSSL_NO_DSA
761 static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
763 DSA_SIG *to_return = NULL;
764 int s_len = 160, r_len = 160, d_len, fd;
765 BIGNUM m, *r=NULL, *s=NULL;
771 if ((s == NULL) || (r==NULL))
774 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);
776 if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
777 (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
778 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
782 if (BN_bin2bn(dgst,dlen,&m) == NULL) {
783 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
787 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
789 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
793 if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
794 (unsigned char *)dgst, d_len,
795 NULL, 0, /* compute random value */
796 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
797 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
798 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
799 (unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
800 (unsigned char *)r->d, &r_len,
801 (unsigned char *)s->d, &s_len ) != 0) {
802 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
806 p_UBSEC_ubsec_close(fd);
808 r->top = (160+BN_BITS2-1)/BN_BITS2;
809 s->top = (160+BN_BITS2-1)/BN_BITS2;
811 to_return = DSA_SIG_new();
812 if(to_return == NULL) {
813 UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
829 static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
830 DSA_SIG *sig, DSA *dsa)
839 if(!bn_wexpand(&v, dsa->p->top)) {
840 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
844 v_len = BN_num_bits(dsa->p);
846 d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);
848 if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
850 UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
854 if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
855 (unsigned char *)dgst, d_len,
856 (unsigned char *)dsa->p->d, BN_num_bits(dsa->p),
857 (unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
858 (unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
859 (unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
860 (unsigned char *)sig->r->d, BN_num_bits(sig->r),
861 (unsigned char *)sig->s->d, BN_num_bits(sig->s),
862 (unsigned char *)v.d, &v_len) != 0) {
863 UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
867 p_UBSEC_ubsec_close(fd);
876 #ifndef OPENSSL_NO_DH
877 static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
882 static int ubsec_dh_generate_key (DH *dh)
889 static int ubsec_rand_bytes(unsigned char *buf, int num)
894 static int ubsec_rand_status(void)
900 /* This stuff is needed if this ENGINE is being compiled into a self-contained
902 #ifdef ENGINE_DYNAMIC_SUPPORT
903 static int bind_fn(ENGINE *e, const char *id)
905 if(id && (strcmp(id, engine_ubsec_id) != 0))
911 IMPLEMENT_DYNAMIC_CHECK_FN()
912 IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
913 #endif /* ENGINE_DYNAMIC_SUPPORT */
915 #endif /* !OPENSSL_NO_HW_UBSEC */
916 #endif /* !OPENSSL_NO_HW */