hw_ubsec.c 25.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66
/* crypto/engine/hw_ubsec.c */
/* Written by Geoff Thorpe (geoff@geoffthorpe.net) for the OpenSSL
 * project 2000.
 *
 * Cloned shamelessly by Joe Tardo. 
 */
/* ====================================================================
 * Copyright (c) 1999 The OpenSSL Project.  All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer. 
 *
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in
 *    the documentation and/or other materials provided with the
 *    distribution.
 *
 * 3. All advertising materials mentioning features or use of this
 *    software must display the following acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
 *
 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
 *    endorse or promote products derived from this software without
 *    prior written permission. For written permission, please contact
 *    licensing@OpenSSL.org.
 *
 * 5. Products derived from this software may not be called "OpenSSL"
 *    nor may "OpenSSL" appear in their names without prior written
 *    permission of the OpenSSL Project.
 *
 * 6. Redistributions of any form whatsoever must retain the following
 *    acknowledgment:
 *    "This product includes software developed by the OpenSSL Project
 *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
 *
 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
 * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 * ====================================================================
 *
 * This product includes cryptographic software written by Eric Young
 * (eay@cryptsoft.com).  This product includes software written by Tim
 * Hudson (tjh@cryptsoft.com).
 *
 */

#include <stdio.h>
#include <openssl/crypto.h>
#include "cryptlib.h"
#include <openssl/dso.h>
#include <openssl/engine.h>

67 68
#ifndef OPENSSL_NO_HW
#ifndef OPENSSL_NO_HW_UBSEC
69 70 71 72 73 74 75

#ifdef FLAT_INC
#include "hw_ubsec.h"
#else
#include "vendor_defns/hw_ubsec.h"
#endif

G
Geoff Thorpe 已提交
76 77
static int ubsec_init(ENGINE *e);
static int ubsec_finish(ENGINE *e);
G
Geoff Thorpe 已提交
78
static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)());
79 80 81 82 83
static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
		const BIGNUM *m, BN_CTX *ctx);
static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
			const BIGNUM *q, const BIGNUM *dp,
			const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx);
84
#ifndef OPENSSL_NO_RSA
85
static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa);
86
#endif
87 88
static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
		const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx);
89
#ifndef OPENSSL_NO_DSA
90
#if NOT_USED
91 92 93 94 95 96
static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
		BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
		BN_CTX *ctx, BN_MONT_CTX *in_mont);
static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
		const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
		BN_MONT_CTX *m_ctx);
97
#endif
98 99 100
static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa);
static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
                                DSA_SIG *sig, DSA *dsa);
101 102
#endif
#ifndef OPENSSL_NO_DH
103 104 105 106 107
static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
		const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
		BN_MONT_CTX *m_ctx);
static int ubsec_dh_compute_key(unsigned char *key,const BIGNUM *pub_key,DH *dh);
static int ubsec_dh_generate_key(DH *dh);
108
#endif
109 110

#if NOT_USED
111 112
static int ubsec_rand_bytes(unsigned char *buf, int num);
static int ubsec_rand_status(void);
113
#endif
G
Geoff Thorpe 已提交
114 115 116 117 118 119 120 121 122 123

#define UBSEC_CMD_SO_PATH		ENGINE_CMD_BASE
static const ENGINE_CMD_DEFN ubsec_cmd_defns[] = {
	{UBSEC_CMD_SO_PATH,
		"SO_PATH",
		"Specifies the path to the 'ubsec' shared library",
		ENGINE_CMD_FLAG_STRING},
	{0, NULL, NULL, 0}
	};

124
#ifndef OPENSSL_NO_RSA
125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
/* Our internal RSA_METHOD that we provide pointers to */
static RSA_METHOD ubsec_rsa =
	{
	"UBSEC RSA method",
	NULL,
	NULL,
	NULL,
	NULL,
	ubsec_rsa_mod_exp,
	ubsec_mod_exp_mont,
	NULL,
	NULL,
	0,
	NULL,
	NULL,
	NULL
	};
142
#endif
143

144
#ifndef OPENSSL_NO_DSA
145 146 147 148 149 150 151 152 153 154 155 156 157 158
/* Our internal DSA_METHOD that we provide pointers to */
static DSA_METHOD ubsec_dsa =
	{
	"UBSEC DSA method",
	ubsec_dsa_do_sign, /* dsa_do_sign */
	NULL, /* dsa_sign_setup */
	ubsec_dsa_verify, /* dsa_do_verify */
	NULL, /* ubsec_dsa_mod_exp */ /* dsa_mod_exp */
	NULL, /* ubsec_mod_exp_dsa */ /* bn_mod_exp */
	NULL, /* init */
	NULL, /* finish */
	0, /* flags */
	NULL /* app_data */
	};
159
#endif
160

161
#ifndef OPENSSL_NO_DH
162 163 164 165 166 167 168 169 170 171 172 173
/* Our internal DH_METHOD that we provide pointers to */
static DH_METHOD ubsec_dh =
	{
	"UBSEC DH method",
	ubsec_dh_generate_key,
	ubsec_dh_compute_key,
	ubsec_mod_exp_dh,
	NULL,
	NULL,
	0,
	NULL
	};
174
#endif
175

176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
#ifndef OPENSSL_NO_ERR
/* Error function codes for use in ubsec operation */
#define UBSEC_F_UBSEC_INIT			100
#define UBSEC_F_UBSEC_FINISH			101
#define UBSEC_F_UBSEC_CTRL			102
#define UBSEC_F_UBSEC_MOD_EXP			103
#define UBSEC_F_UBSEC_RSA_MOD_EXP		104
#define UBSEC_F_UBSEC_RSA_MOD_EXP_CRT		105
#define UBSEC_F_UBSEC_DSA_SIGN			106
#define UBSEC_F_UBSEC_DSA_VERIFY		107
/* Error reason codes */
#define UBSEC_R_ALREADY_LOADED			108
#define UBSEC_R_DSO_FAILURE			109
#define UBSEC_R_UNIT_FAILURE			110
#define UBSEC_R_NOT_LOADED			111
#define UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED	112
#define UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL	113
#define UBSEC_R_BN_EXPAND_FAIL			114
#define UBSEC_R_REQUEST_FAILED			115
#define UBSEC_R_MISSING_KEY_COMPONENTS		116
static ERR_STRING_DATA ubsec_str_functs[] =
	{
	/* This first element is changed to match the dynamic 'lib' number */
{ERR_PACK(0,0,0),				"ubsec engine code"},
{ERR_PACK(0,UBSEC_F_UBSEC_INIT,0),		"ubsec_init"},
{ERR_PACK(0,UBSEC_F_UBSEC_FINISH,0),		"ubsec_finish"},
{ERR_PACK(0,UBSEC_F_UBSEC_CTRL,0),		"ubsec_ctrl"},
{ERR_PACK(0,UBSEC_F_UBSEC_MOD_EXP,0),		"ubsec_mod_exp"},
{ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP,0),	"ubsec_rsa_mod_exp"},
{ERR_PACK(0,UBSEC_F_UBSEC_RSA_MOD_EXP_CRT,0),	"ubsec_rsa_mod_exp_crt"},
{ERR_PACK(0,UBSEC_F_UBSEC_DSA_SIGN,0),		"ubsec_dsa_sign"},
{ERR_PACK(0,UBSEC_F_UBSEC_DSA_VERIFY,0),	"ubsec_dsa_verify"},
/* Error reason codes */
{UBSEC_R_ALREADY_LOADED			,"already loaded"},
{UBSEC_R_DSO_FAILURE			,"DSO failure"},
{UBSEC_R_UNIT_FAILURE			,"unit failure"},
{UBSEC_R_NOT_LOADED			,"not loaded"},
{UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED	,"ctrl command not implemented"},
{UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL	,"size too large or too small"},
{UBSEC_R_BN_EXPAND_FAIL			,"bn_expand fail"},
{UBSEC_R_REQUEST_FAILED			,"request failed"},
{UBSEC_R_MISSING_KEY_COMPONENTS		,"missing key components"},
{0,NULL}
	};
/* The library number we obtain dynamically from the ERR code */
static int ubsec_err_lib = -1;
#define UBSECerr(f,r) ERR_PUT_error(ubsec_err_lib,(f),(r),__FILE__,__LINE__)
static void ubsec_load_error_strings(void)
	{
	if(ubsec_err_lib < 0)
		{
		if((ubsec_err_lib = ERR_get_next_error_library()) <= 0)
			return;
		ubsec_str_functs[0].error = ERR_PACK(ubsec_err_lib,0,0);
		ERR_load_strings(ubsec_err_lib, ubsec_str_functs);
		}
	}
#else
#define UBSECerr(f,r)				  /* NOP */
static void ubsec_load_error_strings(void) { }	 /* NOP */
#endif

G
Geoff Thorpe 已提交
238 239 240
/* Constants used when creating the ENGINE */
static const char *engine_ubsec_id = "ubsec";
static const char *engine_ubsec_name = "UBSEC hardware engine support";
241

242 243 244
/* This internal function is used by ENGINE_ubsec() and possibly by the
 * "dynamic" ENGINE support too */
static int bind_helper(ENGINE *e)
245
	{
246
#ifndef OPENSSL_NO_RSA
247
	const RSA_METHOD *meth1;
248 249
#endif
#ifndef OPENSSL_NO_DH
250 251 252
#ifndef HAVE_UBSEC_DH
	const DH_METHOD *meth3;
#endif /* HAVE_UBSEC_DH */
253
#endif
254 255
	if(!ENGINE_set_id(e, engine_ubsec_id) ||
			!ENGINE_set_name(e, engine_ubsec_name) ||
256
#ifndef OPENSSL_NO_RSA
257
			!ENGINE_set_RSA(e, &ubsec_rsa) ||
258 259
#endif
#ifndef OPENSSL_NO_DSA
260
			!ENGINE_set_DSA(e, &ubsec_dsa) ||
261 262
#endif
#ifndef OPENSSL_NO_DH
263 264 265 266 267 268 269 270 271
			!ENGINE_set_DH(e, &ubsec_dh) ||
#endif
			!ENGINE_set_BN_mod_exp(e, ubsec_mod_exp) ||
			!ENGINE_set_BN_mod_exp_crt(e, ubsec_mod_exp_crt) ||
			!ENGINE_set_init_function(e, ubsec_init) ||
			!ENGINE_set_finish_function(e, ubsec_finish) ||
			!ENGINE_set_ctrl_function(e, ubsec_ctrl) ||
			!ENGINE_set_cmd_defns(e, ubsec_cmd_defns))
		return 0;
272

273
#ifndef OPENSSL_NO_RSA
274 275 276 277 278 279 280 281 282 283 284 285
	/* We know that the "PKCS1_SSLeay()" functions hook properly
	 * to the Broadcom-specific mod_exp and mod_exp_crt so we use
	 * those functions. NB: We don't use ENGINE_openssl() or
	 * anything "more generic" because something like the RSAref
	 * code may not hook properly, and if you own one of these
	 * cards then you have the right to do RSA operations on it
	 * anyway! */ 
	meth1 = RSA_PKCS1_SSLeay();
	ubsec_rsa.rsa_pub_enc = meth1->rsa_pub_enc;
	ubsec_rsa.rsa_pub_dec = meth1->rsa_pub_dec;
	ubsec_rsa.rsa_priv_enc = meth1->rsa_priv_enc;
	ubsec_rsa.rsa_priv_dec = meth1->rsa_priv_dec;
286
#endif
287

288
#ifndef OPENSSL_NO_DH
289 290 291 292 293 294
#ifndef HAVE_UBSEC_DH
	/* Much the same for Diffie-Hellman */
	meth3 = DH_OpenSSL();
	ubsec_dh.generate_key = meth3->generate_key;
	ubsec_dh.compute_key = meth3->compute_key;
#endif /* HAVE_UBSEC_DH */
295
#endif
296

297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
	/* Ensure the ubsec error handling is set up */
	ubsec_load_error_strings();
	return 1;
	}

/* As this is only ever called once, there's no need for locking
 * (indeed - the lock will already be held by our caller!!!) */
ENGINE *ENGINE_ubsec(void)
	{
	ENGINE *ret = ENGINE_new();
	if(!ret)
		return NULL;
	if(!bind_helper(ret))
		{
		ENGINE_free(ret);
		return NULL;
		}
G
Geoff Thorpe 已提交
314
	return ret;
315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
	}

/* This is a process-global DSO handle used for loading and unloading
 * the UBSEC library. NB: This is only set (or unset) during an
 * init() or finish() call (reference counts permitting) and they're
 * operating with global locks, so this should be thread-safe
 * implicitly. */

static DSO *ubsec_dso = NULL;

/* These are the function pointers that are (un)set when the library has
 * successfully (un)loaded. */

static t_UBSEC_ubsec_bytes_to_bits *p_UBSEC_ubsec_bytes_to_bits = NULL;
static t_UBSEC_ubsec_bits_to_bytes *p_UBSEC_ubsec_bits_to_bytes = NULL;
static t_UBSEC_ubsec_open *p_UBSEC_ubsec_open = NULL;
static t_UBSEC_ubsec_close *p_UBSEC_ubsec_close = NULL;
332
#ifndef OPENSSL_NO_DH
333 334 335
static t_UBSEC_diffie_hellman_generate_ioctl 
	*p_UBSEC_diffie_hellman_generate_ioctl = NULL;
static t_UBSEC_diffie_hellman_agree_ioctl *p_UBSEC_diffie_hellman_agree_ioctl = NULL;
336 337
#endif
/* #ifndef OPENSSL_NO_RSA */
338 339
static t_UBSEC_rsa_mod_exp_ioctl *p_UBSEC_rsa_mod_exp_ioctl = NULL;
static t_UBSEC_rsa_mod_exp_crt_ioctl *p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
340 341
/* #endif */
#ifndef OPENSSL_NO_DSA
342 343
static t_UBSEC_dsa_sign_ioctl *p_UBSEC_dsa_sign_ioctl = NULL;
static t_UBSEC_dsa_verify_ioctl *p_UBSEC_dsa_verify_ioctl = NULL;
344
#endif
345 346 347
static t_UBSEC_math_accelerate_ioctl *p_UBSEC_math_accelerate_ioctl = NULL;
static t_UBSEC_rng_ioctl *p_UBSEC_rng_ioctl = NULL;

348 349 350 351 352 353 354 355 356 357
/* 
 * These are the static string constants for the DSO file name and the function
 * symbol names to bind to. 
 */

static const char *UBSEC_LIBNAME = "ubsec";
static const char *UBSEC_F1 = "ubsec_bytes_to_bits";
static const char *UBSEC_F2 = "ubsec_bits_to_bytes";
static const char *UBSEC_F3 = "ubsec_open";
static const char *UBSEC_F4 = "ubsec_close";
358
#ifndef OPENSSL_NO_DH
359 360
static const char *UBSEC_F5 = "diffie_hellman_generate_ioctl";
static const char *UBSEC_F6 = "diffie_hellman_agree_ioctl";
361 362
#endif
/* #ifndef OPENSSL_NO_RSA */
363 364
static const char *UBSEC_F7 = "rsa_mod_exp_ioctl";
static const char *UBSEC_F8 = "rsa_mod_exp_crt_ioctl";
365 366
/* #endif */
#ifndef OPENSSL_NO_DSA
367 368
static const char *UBSEC_F9 = "dsa_sign_ioctl";
static const char *UBSEC_F10 = "dsa_verify_ioctl";
369
#endif
370 371 372
static const char *UBSEC_F11 = "math_accelerate_ioctl";
static const char *UBSEC_F12 = "rng_ioctl";

373
/* (de)initialisation functions. */
G
Geoff Thorpe 已提交
374
static int ubsec_init(ENGINE *e)
375 376 377 378 379
	{
	t_UBSEC_ubsec_bytes_to_bits *p1;
	t_UBSEC_ubsec_bits_to_bytes *p2;
	t_UBSEC_ubsec_open *p3;
	t_UBSEC_ubsec_close *p4;
380
#ifndef OPENSSL_NO_DH
381 382
	t_UBSEC_diffie_hellman_generate_ioctl *p5;
	t_UBSEC_diffie_hellman_agree_ioctl *p6;
383 384
#endif
/* #ifndef OPENSSL_NO_RSA */
385 386
	t_UBSEC_rsa_mod_exp_ioctl *p7;
	t_UBSEC_rsa_mod_exp_crt_ioctl *p8;
387 388
/* #endif */
#ifndef OPENSSL_NO_DSA
389 390
	t_UBSEC_dsa_sign_ioctl *p9;
	t_UBSEC_dsa_verify_ioctl *p10;
391
#endif
392 393 394 395 396 397
	t_UBSEC_math_accelerate_ioctl *p11;
	t_UBSEC_rng_ioctl *p12;
	int fd = 0;

	if(ubsec_dso != NULL)
		{
398
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_ALREADY_LOADED);
399 400 401 402 403 404 405 406
		goto err;
		}
	/* 
	 * Attempt to load libubsec.so/ubsec.dll/whatever. 
	 */
	ubsec_dso = DSO_load(NULL, UBSEC_LIBNAME, NULL, 0);
	if(ubsec_dso == NULL)
		{
407
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
408 409 410 411 412 413 414 415
		goto err;
		}

	if (
	!(p1 = (t_UBSEC_ubsec_bytes_to_bits *) DSO_bind_func(ubsec_dso, UBSEC_F1)) ||
	!(p2 = (t_UBSEC_ubsec_bits_to_bytes *) DSO_bind_func(ubsec_dso, UBSEC_F2)) ||
	!(p3 = (t_UBSEC_ubsec_open *) DSO_bind_func(ubsec_dso, UBSEC_F3)) ||
	!(p4 = (t_UBSEC_ubsec_close *) DSO_bind_func(ubsec_dso, UBSEC_F4)) ||
416
#ifndef OPENSSL_NO_DH
417 418 419 420
	!(p5 = (t_UBSEC_diffie_hellman_generate_ioctl *) 
				DSO_bind_func(ubsec_dso, UBSEC_F5)) ||
	!(p6 = (t_UBSEC_diffie_hellman_agree_ioctl *) 
				DSO_bind_func(ubsec_dso, UBSEC_F6)) ||
421 422
#endif
/* #ifndef OPENSSL_NO_RSA */
423 424
	!(p7 = (t_UBSEC_rsa_mod_exp_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F7)) ||
	!(p8 = (t_UBSEC_rsa_mod_exp_crt_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F8)) ||
425 426
/* #endif */
#ifndef OPENSSL_NO_DSA
427 428
	!(p9 = (t_UBSEC_dsa_sign_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F9)) ||
	!(p10 = (t_UBSEC_dsa_verify_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F10)) ||
429
#endif
430 431 432 433
	!(p11 = (t_UBSEC_math_accelerate_ioctl *) 
				DSO_bind_func(ubsec_dso, UBSEC_F11)) ||
	!(p12 = (t_UBSEC_rng_ioctl *) DSO_bind_func(ubsec_dso, UBSEC_F12)))
		{
434
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_DSO_FAILURE);
435 436 437 438 439 440 441 442
		goto err;
		}

	/* Copy the pointers */
	p_UBSEC_ubsec_bytes_to_bits = p1;
	p_UBSEC_ubsec_bits_to_bytes = p2;
	p_UBSEC_ubsec_open = p3;
	p_UBSEC_ubsec_close = p4;
443
#ifndef OPENSSL_NO_DH
444 445
	p_UBSEC_diffie_hellman_generate_ioctl = p5;
	p_UBSEC_diffie_hellman_agree_ioctl = p6;
446 447
#endif
#ifndef OPENSSL_NO_RSA
448 449
	p_UBSEC_rsa_mod_exp_ioctl = p7;
	p_UBSEC_rsa_mod_exp_crt_ioctl = p8;
450 451
#endif
#ifndef OPENSSL_NO_DSA
452 453
	p_UBSEC_dsa_sign_ioctl = p9;
	p_UBSEC_dsa_verify_ioctl = p10;
454
#endif
455 456 457 458 459 460 461 462 463 464 465
	p_UBSEC_math_accelerate_ioctl = p11;
	p_UBSEC_rng_ioctl = p12;

	/* Perform an open to see if there's actually any unit running. */
	if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) > 0)
		{
		p_UBSEC_ubsec_close(fd);
		return 1;
		}
	else
		{
466
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
467 468 469 470 471 472 473 474 475
		}

err:
	if(ubsec_dso)
		DSO_free(ubsec_dso);
	p_UBSEC_ubsec_bytes_to_bits = NULL;
	p_UBSEC_ubsec_bits_to_bytes = NULL;
	p_UBSEC_ubsec_open = NULL;
	p_UBSEC_ubsec_close = NULL;
476
#ifndef OPENSSL_NO_DH
477 478
	p_UBSEC_diffie_hellman_generate_ioctl = NULL;
	p_UBSEC_diffie_hellman_agree_ioctl = NULL;
479 480
#endif
#ifndef OPENSSL_NO_RSA
481 482
	p_UBSEC_rsa_mod_exp_ioctl = NULL;
	p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
483 484
#endif
#ifndef OPENSSL_NO_DSA
485 486
	p_UBSEC_dsa_sign_ioctl = NULL;
	p_UBSEC_dsa_verify_ioctl = NULL;
487
#endif
488 489 490 491 492 493
	p_UBSEC_math_accelerate_ioctl = NULL;
	p_UBSEC_rng_ioctl = NULL;

	return 0;
	}

G
Geoff Thorpe 已提交
494
static int ubsec_finish(ENGINE *e)
495 496 497
	{
	if(ubsec_dso == NULL)
		{
498
		UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_NOT_LOADED);
499 500 501 502
		return 0;
		}
	if(!DSO_free(ubsec_dso))
		{
503
		UBSECerr(UBSEC_F_UBSEC_FINISH, UBSEC_R_DSO_FAILURE);
504 505 506 507 508 509 510
		return 0;
		}
	ubsec_dso = NULL;
	p_UBSEC_ubsec_bytes_to_bits = NULL;
	p_UBSEC_ubsec_bits_to_bytes = NULL;
	p_UBSEC_ubsec_open = NULL;
	p_UBSEC_ubsec_close = NULL;
511
#ifndef OPENSSL_NO_DH
512 513
	p_UBSEC_diffie_hellman_generate_ioctl = NULL;
	p_UBSEC_diffie_hellman_agree_ioctl = NULL;
514 515
#endif
#ifndef OPENSSL_NO_RSA
516 517
	p_UBSEC_rsa_mod_exp_ioctl = NULL;
	p_UBSEC_rsa_mod_exp_crt_ioctl = NULL;
518 519
#endif
#ifndef OPENSSL_NO_DSA
520 521
	p_UBSEC_dsa_sign_ioctl = NULL;
	p_UBSEC_dsa_verify_ioctl = NULL;
522
#endif
523 524 525 526 527
	p_UBSEC_math_accelerate_ioctl = NULL;
	p_UBSEC_rng_ioctl = NULL;
	return 1;
	}

G
Geoff Thorpe 已提交
528 529 530 531 532 533 534 535
static int ubsec_ctrl(ENGINE *e, int cmd, long i, void *p, void (*f)())
	{
	int initialised = ((ubsec_dso == NULL) ? 0 : 1);
	switch(cmd)
		{
	case UBSEC_CMD_SO_PATH:
		if(p == NULL)
			{
536
			UBSECerr(UBSEC_F_UBSEC_CTRL,ERR_R_PASSED_NULL_PARAMETER);
G
Geoff Thorpe 已提交
537 538 539 540
			return 0;
			}
		if(initialised)
			{
541
			UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_ALREADY_LOADED);
G
Geoff Thorpe 已提交
542 543 544 545 546 547 548
			return 0;
			}
		UBSEC_LIBNAME = (const char *)p;
		return 1;
	default:
		break;
		}
549
	UBSECerr(UBSEC_F_UBSEC_CTRL,UBSEC_R_CTRL_COMMAND_NOT_IMPLEMENTED);
G
Geoff Thorpe 已提交
550 551 552
	return 0;
	}

553 554 555 556 557 558 559 560
static int ubsec_mod_exp(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
		const BIGNUM *m, BN_CTX *ctx)
	{
	int 	y_len = 0;
	int 	fd;

	if(ubsec_dso == NULL)
	{
561
		UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_NOT_LOADED);
562 563 564 565 566 567
		return 0;
	}

	/* Check if hardware can't handle this argument. */
	y_len = BN_num_bits(m);
	if (y_len > 1024) {
568
		UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
569 570 571 572 573
		return 0;
	} 

	if(!bn_wexpand(r, m->top))
	{
574
		UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_BN_EXPAND_FAIL);
575 576 577 578 579 580
		return 0;
	}
	memset(r->d, 0, BN_num_bytes(m));

	if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
		fd = 0;
581
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
582 583 584 585 586 587 588
		return 0;
	}

	if (p_UBSEC_rsa_mod_exp_ioctl(fd, (unsigned char *)a->d, BN_num_bits(a),
		(unsigned char *)m->d, BN_num_bits(m), (unsigned char *)p->d, 
		BN_num_bits(p), (unsigned char *)r->d, &y_len) != 0)
	{
589
		UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
590 591 592 593 594 595 596 597 598
		return 0;
	}

	p_UBSEC_ubsec_close(fd);

	r->top = (BN_num_bits(m)+BN_BITS2-1)/BN_BITS2;
	return 1;
	}

599
#ifndef OPENSSL_NO_RSA
600 601 602 603 604 605 606 607 608 609
static int ubsec_rsa_mod_exp(BIGNUM *r0, const BIGNUM *I, RSA *rsa)
	{
	BN_CTX *ctx;
	int to_return = 0;

	if((ctx = BN_CTX_new()) == NULL)
		goto err;

	if(!rsa->p || !rsa->q || !rsa->dmp1 || !rsa->dmq1 || !rsa->iqmp)
		{
610
		UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP, UBSEC_R_MISSING_KEY_COMPONENTS);
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628
		goto err;
		}

	/*
	 * Do in software if argument is too large for hardware.
	 */
	if ((BN_num_bits(rsa->p)+BN_num_bits(rsa->q)) > 1024) {
		const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
		to_return = (*meth->rsa_mod_exp)(r0, I, rsa);
	} else {
		to_return = ubsec_mod_exp_crt(r0, I, rsa->p, rsa->q, rsa->dmp1,
			rsa->dmq1, rsa->iqmp, ctx);
	}
err:
	if(ctx)
		BN_CTX_free(ctx);
	return to_return;
	}
629
#endif
630 631 632 633 634 635 636 637 638 639 640 641 642 643

static int ubsec_mod_exp_crt(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
			const BIGNUM *q, const BIGNUM *dp,
			const BIGNUM *dq, const BIGNUM *qinv, BN_CTX *ctx)
	{
	int	y_len,
		m_len,
		fd;

	m_len = BN_num_bytes(p) + BN_num_bytes(q) + 1;
	y_len = BN_num_bits(p) + BN_num_bits(q);

	/* Check if hardware can't handle this argument. */
	if (y_len > 1024) {
644
		UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_SIZE_TOO_LARGE_OR_TOO_SMALL);
645 646 647 648
		return 0;
	} 

	if (!bn_wexpand(r, p->top + q->top + 1)) {
649
		UBSECerr(UBSEC_F_UBSEC_RSA_MOD_EXP_CRT, UBSEC_R_BN_EXPAND_FAIL);
650 651 652 653 654
		return 0;
	}

	if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
		fd = 0;
655
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
656 657 658 659 660 661 662 663 664 665 666
		return 0;
	}

	if (p_UBSEC_rsa_mod_exp_crt_ioctl(fd,
		(unsigned char *)a->d, BN_num_bits(a), 
		(unsigned char *)qinv->d, BN_num_bits(qinv),
		(unsigned char *)dp->d, BN_num_bits(dp),
		(unsigned char *)p->d, BN_num_bits(p),
		(unsigned char *)dq->d, BN_num_bits(dq),
		(unsigned char *)q->d, BN_num_bits(q),
		(unsigned char *)r->d,  &y_len) != 0) {
667
		UBSECerr(UBSEC_F_UBSEC_MOD_EXP, UBSEC_R_REQUEST_FAILED);
668 669 670 671 672 673 674 675 676
		return 0;
	}

	p_UBSEC_ubsec_close(fd);

	r->top = (BN_num_bits(p) + BN_num_bits(q) + BN_BITS2 - 1)/BN_BITS2;
	return 1;
}

677
#ifndef OPENSSL_NO_DSA
678
#if NOT_USED
679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704
static int ubsec_dsa_mod_exp(DSA *dsa, BIGNUM *rr, BIGNUM *a1,
		BIGNUM *p1, BIGNUM *a2, BIGNUM *p2, BIGNUM *m,
		BN_CTX *ctx, BN_MONT_CTX *in_mont)
	{
	BIGNUM t;
	int to_return = 0;
 
	BN_init(&t);
	/* let rr = a1 ^ p1 mod m */
	if (!ubsec_mod_exp(rr,a1,p1,m,ctx)) goto end;
	/* let t = a2 ^ p2 mod m */
	if (!ubsec_mod_exp(&t,a2,p2,m,ctx)) goto end;
	/* let rr = rr * t mod m */
	if (!BN_mod_mul(rr,rr,&t,m,ctx)) goto end;
	to_return = 1;
end:
	BN_free(&t);
	return to_return;
	}

static int ubsec_mod_exp_dsa(DSA *dsa, BIGNUM *r, BIGNUM *a,
		const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
		BN_MONT_CTX *m_ctx)
	{
	return ubsec_mod_exp(r, a, p, m, ctx);
	}
705
#endif
706
#endif
707 708 709 710 711 712

/*
 * This function is aliased to mod_exp (with the mont stuff dropped).
 */
static int ubsec_mod_exp_mont(BIGNUM *r, const BIGNUM *a, const BIGNUM *p,
		const BIGNUM *m, BN_CTX *ctx, BN_MONT_CTX *m_ctx)
713
        {
714 715
	int ret = 0;

716
#ifndef OPENSSL_NO_RSA
717
 	/* Do in software if the key is too large for the hardware. */
718 719
	if (BN_num_bits(m) > 1024)
                {
720 721
		const RSA_METHOD *meth = RSA_PKCS1_SSLeay();
		ret = (*meth->bn_mod_exp)(r, a, p, m, ctx, m_ctx);
722 723 724 725
                }
        else
#endif
                {
726
		ret = ubsec_mod_exp(r, a, p, m, ctx);
727
                }
728 729
	
	return ret;
730
        }
731

732
#ifndef OPENSSL_NO_DH
733 734 735 736 737 738 739
/* This function is aliased to mod_exp (with the dh and mont dropped). */
static int ubsec_mod_exp_dh(const DH *dh, BIGNUM *r, const BIGNUM *a,
		const BIGNUM *p, const BIGNUM *m, BN_CTX *ctx,
		BN_MONT_CTX *m_ctx)
	{
	return ubsec_mod_exp(r, a, p, m, ctx);
	}
740
#endif
741

742
#ifndef OPENSSL_NO_DSA
743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759
static DSA_SIG *ubsec_dsa_do_sign(const unsigned char *dgst, int dlen, DSA *dsa)
	{
	DSA_SIG *to_return = NULL;
	int s_len = 160, r_len = 160, d_len, fd;
	BIGNUM m, *r=NULL, *s=NULL;

	BN_init(&m);

	s = BN_new();
	r = BN_new();
	if ((s == NULL) || (r==NULL))
		goto err;

	d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dlen);

        if(!bn_wexpand(r, (160+BN_BITS2-1)/BN_BITS2) ||
       	   (!bn_wexpand(s, (160+BN_BITS2-1)/BN_BITS2))) {
760
		UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
761 762 763 764
		goto err;
	}

	if (BN_bin2bn(dgst,dlen,&m) == NULL) {
765
		UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
766 767 768 769 770
		goto err;
	} 

	if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
		fd = 0;
771
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
772 773 774 775 776 777 778 779 780 781 782 783
		return 0;
	}

	if (p_UBSEC_dsa_sign_ioctl(fd, 0, /* compute hash before signing */
		(unsigned char *)dgst, d_len,
		NULL, 0,  /* compute random value */
		(unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
		(unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
		(unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
		(unsigned char *)dsa->priv_key->d, BN_num_bits(dsa->priv_key),
		(unsigned char *)r->d, &r_len,
		(unsigned char *)s->d, &s_len ) != 0) {
784
		UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_REQUEST_FAILED);
785 786 787 788 789 790 791 792 793 794
		goto err;
	}

	p_UBSEC_ubsec_close(fd);

	r->top = (160+BN_BITS2-1)/BN_BITS2;
	s->top = (160+BN_BITS2-1)/BN_BITS2;

	to_return = DSA_SIG_new();
	if(to_return == NULL) {
795
		UBSECerr(UBSEC_F_UBSEC_DSA_SIGN, UBSEC_R_BN_EXPAND_FAIL);
796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821
		goto err;
	}

	to_return->r = r;
	to_return->s = s;

err:
	if (!to_return) {
		if (r) BN_free(r);
		if (s) BN_free(s);
	}                                 
	BN_clear_free(&m);
	return to_return;
}

static int ubsec_dsa_verify(const unsigned char *dgst, int dgst_len,
                                DSA_SIG *sig, DSA *dsa)
	{
	int v_len, d_len;
	int to_return = 0;
	int fd;
	BIGNUM v;

	BN_init(&v);

	if(!bn_wexpand(&v, dsa->p->top)) {
822
		UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY ,UBSEC_R_BN_EXPAND_FAIL);
823 824 825 826 827 828 829 830 831
		goto err;
	}

	v_len = BN_num_bits(dsa->p);

	d_len = p_UBSEC_ubsec_bytes_to_bits((unsigned char *)dgst, dgst_len);

	if ((fd = p_UBSEC_ubsec_open(UBSEC_KEY_DEVICE_NAME)) <= 0) {
		fd = 0;
832
		UBSECerr(UBSEC_F_UBSEC_INIT, UBSEC_R_UNIT_FAILURE);
833 834 835 836 837 838 839 840 841 842 843 844
		return 0;
	}

	if (p_UBSEC_dsa_verify_ioctl(fd, 0, /* compute hash before signing */
		(unsigned char *)dgst, d_len,
		(unsigned char *)dsa->p->d, BN_num_bits(dsa->p), 
		(unsigned char *)dsa->q->d, BN_num_bits(dsa->q),
		(unsigned char *)dsa->g->d, BN_num_bits(dsa->g),
		(unsigned char *)dsa->pub_key->d, BN_num_bits(dsa->pub_key),
		(unsigned char *)sig->r->d, BN_num_bits(sig->r),
		(unsigned char *)sig->s->d, BN_num_bits(sig->s),
		(unsigned char *)v.d, &v_len) != 0) {
845
		UBSECerr(UBSEC_F_UBSEC_DSA_VERIFY , UBSEC_R_REQUEST_FAILED);
846 847 848 849 850 851 852 853 854 855
		goto err;
	}

	p_UBSEC_ubsec_close(fd);

	to_return = 1;
err:
	BN_clear_free(&v);
	return to_return;
	}
856
#endif
857

858
#ifndef OPENSSL_NO_DH
859 860 861 862 863 864 865 866 867
static int ubsec_dh_compute_key (unsigned char *key,const BIGNUM *pub_key,DH *dh)
	{
	return 0;
	}

static int ubsec_dh_generate_key (DH *dh)
	{
	return 0;
	}
868
#endif
869

870
#ifdef NOT_USED
871 872 873 874 875 876 877 878 879
static int ubsec_rand_bytes(unsigned char *buf, int num)
	{
	return 0;
	}

static int ubsec_rand_status(void)
	{
	return 0;
	}
880
#endif
881

882 883 884 885 886 887 888 889 890 891 892 893 894 895 896
/* This stuff is needed if this ENGINE is being compiled into a self-contained
 * shared-library. */
#ifdef ENGINE_DYNAMIC_SUPPORT
static int bind_fn(ENGINE *e, const char *id)
	{
	if(id && (strcmp(id, engine_ubsec_id) != 0))
		return 0;
	if(!bind_helper(e))
		return 0;
	return 1;
	}
IMPLEMENT_DYNAMIC_CHECK_FN()
IMPLEMENT_DYNAMIC_BIND_FN(bind_fn)
#endif /* ENGINE_DYNAMIC_SUPPORT */

897 898
#endif /* !OPENSSL_NO_HW_UBSEC */
#endif /* !OPENSSL_NO_HW */