ecs_ossl.c 12.6 KB
Newer Older
B
Bodo Möller 已提交
1
/* crypto/ecdsa/ecs_ossl.c */
2 3 4
/*
 * Written by Nils Larsch for the OpenSSL project
 */
B
Bodo Möller 已提交
5
/* ====================================================================
6
 * Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.
B
Bodo Möller 已提交
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
 *
 * 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
 *    openssl-core@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).
 *
 */
58

59 60
#define OPENSSL_FIPSAPI

61
#include "ecs_locl.h"
62
#include <openssl/err.h>
63
#include <openssl/obj_mac.h>
64
#include <openssl/bn.h>
B
Bodo Möller 已提交
65

66
static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, 
N
Nils Larsch 已提交
67
		const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
68 69 70
static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, 
		BIGNUM **rp);
static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len, 
N
Nils Larsch 已提交
71
		const ECDSA_SIG *sig, EC_KEY *eckey);
B
Bodo Möller 已提交
72 73

static ECDSA_METHOD openssl_ecdsa_meth = {
74 75 76 77 78 79 80 81 82 83
	"OpenSSL ECDSA method",
	ecdsa_do_sign,
	ecdsa_sign_setup,
	ecdsa_do_verify,
#if 0
	NULL, /* init     */
	NULL, /* finish   */
#endif
	0,    /* flags    */
	NULL  /* app_data */
B
Bodo Möller 已提交
84 85 86 87 88 89 90
};

const ECDSA_METHOD *ECDSA_OpenSSL(void)
{
	return &openssl_ecdsa_meth;
}

91 92
static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
		BIGNUM **rp)
B
Bodo Möller 已提交
93 94
{
	BN_CTX   *ctx = NULL;
95
	BIGNUM	 *k = NULL, *r = NULL, *order = NULL, *X = NULL;
B
Bodo Möller 已提交
96
	EC_POINT *tmp_point=NULL;
N
Nils Larsch 已提交
97
	const EC_GROUP *group;
98
	int 	 ret = 0;
N
Nils Larsch 已提交
99 100

	if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
B
Bodo Möller 已提交
101
	{
102
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
B
Bodo Möller 已提交
103 104
		return 0;
	}
105

B
Bodo Möller 已提交
106 107
	if (ctx_in == NULL) 
	{
108
		if ((ctx = BN_CTX_new()) == NULL)
109
		{
110 111
			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
			return 0;
112
		}
B
Bodo Möller 已提交
113 114
	}
	else
115
		ctx = ctx_in;
B
Bodo Möller 已提交
116

117 118 119 120 121
	k     = BN_new();	/* this value is later returned in *kinvp */
	r     = BN_new();	/* this value is later returned in *rp    */
	order = BN_new();
	X     = BN_new();
	if (!k || !r || !order || !X)
B
Bodo Möller 已提交
122
	{
123 124
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
		goto err;
125
	}
126
	if ((tmp_point = EC_POINT_new(group)) == NULL)
127 128
	{
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
B
Bodo Möller 已提交
129 130
		goto err;
	}
131
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
132
	{
133
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
B
Bodo Möller 已提交
134 135 136 137 138 139 140
		goto err;
	}
	
	do
	{
		/* get random k */	
		do
141
			if (!BN_rand_range(k, order))
B
Bodo Möller 已提交
142
			{
143 144
				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
				 ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);	
B
Bodo Möller 已提交
145 146
				goto err;
			}
147
		while (BN_is_zero(k));
B
Bodo Möller 已提交
148 149

		/* compute r the x-coordinate of generator * k */
150
		if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx))
151 152 153 154
		{
			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
			goto err;
		}
155
		if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
156
		{
157
			if (!EC_POINT_get_affine_coordinates_GFp(group,
158 159
				tmp_point, X, NULL, ctx))
			{
160
				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
161 162 163
				goto err;
			}
		}
164
#ifndef OPENSSL_NO_EC2M
165 166
		else /* NID_X9_62_characteristic_two_field */
		{
167
			if (!EC_POINT_get_affine_coordinates_GF2m(group,
168 169
				tmp_point, X, NULL, ctx))
			{
170
				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
171 172 173
				goto err;
			}
		}
174
#endif
175
		if (!BN_nnmod(r, X, order, ctx))
B
Bodo Möller 已提交
176
		{
177
			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
B
Bodo Möller 已提交
178 179 180 181 182 183
			goto err;
		}
	}
	while (BN_is_zero(r));

	/* compute the inverse of k */
184
	if (!BN_mod_inverse(k, k, order, ctx))
185 186 187 188
	{
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
		goto err;	
	}
189 190
	/* clear old values if necessary */
	if (*rp != NULL)
B
Bodo Möller 已提交
191
		BN_clear_free(*rp);
192
	if (*kinvp != NULL) 
B
Bodo Möller 已提交
193
		BN_clear_free(*kinvp);
194 195 196
	/* save the pre-computed values  */
	*rp    = r;
	*kinvp = k;
B
Bodo Möller 已提交
197 198 199 200
	ret = 1;
err:
	if (!ret)
	{
201
		if (k != NULL) BN_clear_free(k);
B
Bodo Möller 已提交
202 203 204 205 206
		if (r != NULL) BN_clear_free(r);
	}
	if (ctx_in == NULL) 
		BN_CTX_free(ctx);
	if (order != NULL)
207
		BN_free(order);
B
Bodo Möller 已提交
208 209
	if (tmp_point != NULL) 
		EC_POINT_free(tmp_point);
210 211
	if (X)
		BN_clear_free(X);
B
Bodo Möller 已提交
212 213 214 215
	return(ret);
}


216
static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len, 
N
Nils Larsch 已提交
217
		const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
B
Bodo Möller 已提交
218
{
D
Dr. Stephen Henson 已提交
219
	int     ok = 0, i;
N
Nils Larsch 已提交
220 221
	BIGNUM *kinv=NULL, *s, *m=NULL,*tmp=NULL,*order=NULL;
	const BIGNUM *ckinv;
222
	BN_CTX     *ctx = NULL;
N
Nils Larsch 已提交
223
	const EC_GROUP   *group;
224
	ECDSA_SIG  *ret;
225
	ECDSA_DATA *ecdsa;
N
Nils Larsch 已提交
226
	const BIGNUM *priv_key;
227

N
Nils Larsch 已提交
228 229 230 231 232
	ecdsa    = ecdsa_check(eckey);
	group    = EC_KEY_get0_group(eckey);
	priv_key = EC_KEY_get0_private_key(eckey);
	
	if (group == NULL || priv_key == NULL || ecdsa == NULL)
B
Bodo Möller 已提交
233
	{
234
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
235
		return NULL;
B
Bodo Möller 已提交
236 237
	}

238 239 240 241 242 243 244 245
	ret = ECDSA_SIG_new();
	if (!ret)
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
		return NULL;
	}
	s = ret->s;

246
	if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
247
		(tmp = BN_new()) == NULL || (m = BN_new()) == NULL)
248 249 250 251
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
		goto err;
	}
B
Bodo Möller 已提交
252

253
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
254
	{
255
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
B
Bodo Möller 已提交
256 257
		goto err;
	}
D
Dr. Stephen Henson 已提交
258 259 260 261 262 263 264
	i = BN_num_bits(order);
	/* Need to truncate digest if it is too long: first truncate whole
	 * bytes.
	 */
	if (8 * dgst_len > i)
		dgst_len = (i + 7)/8;
	if (!BN_bin2bn(dgst, dgst_len, m))
B
Bodo Möller 已提交
265
	{
D
Dr. Stephen Henson 已提交
266
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
B
Bodo Möller 已提交
267 268
		goto err;
	}
D
Dr. Stephen Henson 已提交
269 270
	/* If still too long truncate remaining bits with a shift */
	if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
271 272 273 274
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
275 276
	do
	{
N
Nils Larsch 已提交
277
		if (in_kinv == NULL || in_r == NULL)
B
Bodo Möller 已提交
278
		{
279 280
			if (!ecdsa->meth->ecdsa_sign_setup(eckey, ctx,
								&kinv, &ret->r))
281
			{
282
				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
283 284
				goto err;
			}
N
Nils Larsch 已提交
285
			ckinv = kinv;
B
Bodo Möller 已提交
286 287 288
		}
		else
		{
N
Nils Larsch 已提交
289 290 291 292 293 294
			ckinv  = in_kinv;
			if (BN_copy(ret->r, in_r) == NULL)
			{
				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
				goto err;
			}
B
Bodo Möller 已提交
295 296
		}

N
Nils Larsch 已提交
297
		if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx))
298 299 300 301
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
302
		if (!BN_mod_add_quick(s, tmp, m, order))
303 304 305 306
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
N
Nils Larsch 已提交
307
		if (!BN_mod_mul(s, s, ckinv, order, ctx))
308 309 310 311
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
312 313 314 315 316 317 318 319 320 321 322 323 324
		if (BN_is_zero(s))
		{
			/* if kinv and r have been supplied by the caller
			 * don't to generate new kinv and r values */
			if (in_kinv != NULL && in_r != NULL)
			{
				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ECDSA_R_NEED_NEW_SETUP_VALUES);
				goto err;
			}
		}
		else
			/* s != 0 => we have a valid signature */
			break;
B
Bodo Möller 已提交
325
	}
326
	while (1);
B
Bodo Möller 已提交
327

328 329 330
	ok = 1;
err:
	if (!ok)
B
Bodo Möller 已提交
331 332 333 334
	{
		ECDSA_SIG_free(ret);
		ret = NULL;
	}
335 336 337 338 339 340 341
	if (ctx)
		BN_CTX_free(ctx);
	if (m)
		BN_clear_free(m);
	if (tmp)
		BN_clear_free(tmp);
	if (order)
342
		BN_free(order);
343 344
	if (kinv)
		BN_clear_free(kinv);
345
	return ret;
B
Bodo Möller 已提交
346 347
}

348
static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
N
Nils Larsch 已提交
349
		const ECDSA_SIG *sig, EC_KEY *eckey)
B
Bodo Möller 已提交
350
{
D
Dr. Stephen Henson 已提交
351
	int ret = -1, i;
352 353 354
	BN_CTX   *ctx;
	BIGNUM   *order, *u1, *u2, *m, *X;
	EC_POINT *point = NULL;
N
Nils Larsch 已提交
355 356 357
	const EC_GROUP *group;
	const EC_POINT *pub_key;

358
	/* check input values */
N
Nils Larsch 已提交
359 360
	if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
	    (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)
B
Bodo Möller 已提交
361
	{
362
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
B
Bodo Möller 已提交
363 364 365
		return -1;
	}

366 367
	ctx = BN_CTX_new();
	if (!ctx)
368 369
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
370
		return -1;
371
	}
372 373 374 375 376 377 378
	BN_CTX_start(ctx);
	order = BN_CTX_get(ctx);	
	u1    = BN_CTX_get(ctx);
	u2    = BN_CTX_get(ctx);
	m     = BN_CTX_get(ctx);
	X     = BN_CTX_get(ctx);
	if (!X)
379 380 381 382
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
383 384
	
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
385
	{
386
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
B
Bodo Möller 已提交
387 388
		goto err;
	}
389

390
	if (BN_is_zero(sig->r)          || BN_is_negative(sig->r) || 
391
	    BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s)  ||
392
	    BN_is_negative(sig->s)      || BN_ucmp(sig->s, order) >= 0)
B
Bodo Möller 已提交
393
	{
394
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
395
		ret = 0;	/* signature is invalid */
B
Bodo Möller 已提交
396 397 398
		goto err;
	}
	/* calculate tmp1 = inv(S) mod order */
399
	if (!BN_mod_inverse(u2, sig->s, order, ctx))
400 401 402 403
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
404
	/* digest -> m */
D
Dr. Stephen Henson 已提交
405 406 407 408 409 410
	i = BN_num_bits(order);
	/* Need to truncate digest if it is too long: first truncate whole
	 * bytes.
	 */
	if (8 * dgst_len > i)
		dgst_len = (i + 7)/8;
411
	if (!BN_bin2bn(dgst, dgst_len, m))
412 413 414 415
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
D
Dr. Stephen Henson 已提交
416 417 418 419 420 421
	/* If still too long truncate remaining bits with a shift */
	if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7)))
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
422
	/* u1 = m * tmp mod order */
423
	if (!BN_mod_mul(u1, m, u2, order, ctx))
424 425 426 427
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
428
	/* u2 = r * w mod q */
429
	if (!BN_mod_mul(u2, sig->r, u2, order, ctx))
430 431 432 433
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
434

435
	if ((point = EC_POINT_new(group)) == NULL)
B
Bodo Möller 已提交
436
	{
437
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
B
Bodo Möller 已提交
438 439
		goto err;
	}
N
Nils Larsch 已提交
440
	if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx))
B
Bodo Möller 已提交
441
	{
442 443 444
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
		goto err;
	}
445
	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
446
	{
447
		if (!EC_POINT_get_affine_coordinates_GFp(group,
448 449
			point, X, NULL, ctx))
		{
450
			ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
451 452 453
			goto err;
		}
	}
454
#ifndef OPENSSL_NO_EC2M
455 456
	else /* NID_X9_62_characteristic_two_field */
	{
457
		if (!EC_POINT_get_affine_coordinates_GF2m(group,
458 459
			point, X, NULL, ctx))
		{
460
			ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
461 462 463
			goto err;
		}
	}
464
#endif	
465
	if (!BN_nnmod(u1, X, order, ctx))
466 467
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
B
Bodo Möller 已提交
468 469
		goto err;
	}
470 471 472 473 474
	/*  if the signature is correct u1 is equal to sig->r */
	ret = (BN_ucmp(u1, sig->r) == 0);
err:
	BN_CTX_end(ctx);
	BN_CTX_free(ctx);
475 476
	if (point)
		EC_POINT_free(point);
477
	return ret;
B
Bodo Möller 已提交
478
}
479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507

#ifdef OPENSSL_FIPSCANISTER
/* FIPS stanadlone version of ecdsa_check: just return FIPS method */
ECDSA_DATA *fips_ecdsa_check(EC_KEY *key)
	{
	static ECDSA_DATA rv = {
		0,0,0,
		&openssl_ecdsa_meth
		};
	return &rv;
	}
/* Standalone digest sign and verify */
int FIPS_ecdsa_verify_digest(EC_KEY *key,
			const unsigned char *dig, int dlen, ECDSA_SIG *s)
	{
	ECDSA_DATA *ecdsa = ecdsa_check(key);
	if (ecdsa == NULL)
		return 0;
	return ecdsa->meth->ecdsa_do_verify(dig, dlen, s, key);
	}
ECDSA_SIG * FIPS_ecdsa_sign_digest(EC_KEY *key,
					const unsigned char *dig, int dlen)
	{
	ECDSA_DATA *ecdsa = ecdsa_check(key);
	if (ecdsa == NULL)
		return NULL;
	return ecdsa->meth->ecdsa_do_sign(dig, dlen, NULL, NULL, key);
	}
#endif