ecs_ossl.c 11.4 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
#include "ecs_locl.h"
60
#include <openssl/err.h>
61
#include <openssl/obj_mac.h>
62
#include <openssl/bn.h>
B
Bodo Möller 已提交
63

64
static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, 
N
Nils Larsch 已提交
65
		const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
66 67 68
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 已提交
69
		const ECDSA_SIG *sig, EC_KEY *eckey);
B
Bodo Möller 已提交
70 71

static ECDSA_METHOD openssl_ecdsa_meth = {
72 73 74 75 76 77 78 79 80 81
	"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 已提交
82 83 84 85 86 87 88
};

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

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

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

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

115 116 117 118 119
	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 已提交
120
	{
121 122
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
		goto err;
123
	}
124
	if ((tmp_point = EC_POINT_new(group)) == NULL)
125 126
	{
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
B
Bodo Möller 已提交
127 128
		goto err;
	}
129
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
130
	{
131
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
B
Bodo Möller 已提交
132 133 134 135 136 137 138
		goto err;
	}
	
	do
	{
		/* get random k */	
		do
139
			if (!BN_rand_range(k, order))
B
Bodo Möller 已提交
140
			{
141 142
				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
				 ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);	
B
Bodo Möller 已提交
143 144
				goto err;
			}
145
		while (BN_is_zero(k));
B
Bodo Möller 已提交
146 147

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

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


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

N
Nils Larsch 已提交
224 225 226 227 228
	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 已提交
229
	{
230
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
231
		return NULL;
B
Bodo Möller 已提交
232 233
	}

234 235 236 237 238 239 240 241
	ret = ECDSA_SIG_new();
	if (!ret)
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
		return NULL;
	}
	s = ret->s;

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

249
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
250
	{
251
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
B
Bodo Möller 已提交
252 253
		goto err;
	}
254
	if (8 * dgst_len > BN_num_bits(order))
B
Bodo Möller 已提交
255
	{
256 257 258 259 260 261 262 263
		/* XXX
		 * 
		 * Should provide for optional hash truncation:
		 * Keep the BN_num_bits(order) leftmost bits of dgst
		 * (see March 2006 FIPS 186-3 draft, which has a few
		 * confusing errors in this part though)
		 */

264 265
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
			ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
B
Bodo Möller 已提交
266 267 268
		goto err;
	}

269
	if (!BN_bin2bn(dgst, dgst_len, m))
270 271 272 273
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
274 275
	do
	{
N
Nils Larsch 已提交
276
		if (in_kinv == NULL || in_r == NULL)
B
Bodo Möller 已提交
277
		{
278
			if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r))
279
			{
280
				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
281 282
				goto err;
			}
N
Nils Larsch 已提交
283
			ckinv = kinv;
B
Bodo Möller 已提交
284 285 286
		}
		else
		{
N
Nils Larsch 已提交
287 288 289 290 291 292
			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 已提交
293 294
		}

N
Nils Larsch 已提交
295
		if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx))
296 297 298 299
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
300
		if (!BN_mod_add_quick(s, tmp, m, order))
301 302 303 304
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
N
Nils Larsch 已提交
305
		if (!BN_mod_mul(s, s, ckinv, order, ctx))
306 307 308 309
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
310 311 312 313 314 315 316 317 318 319 320 321 322
		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 已提交
323
	}
324
	while (1);
B
Bodo Möller 已提交
325

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

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

356
	/* check input values */
N
Nils Larsch 已提交
357 358
	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 已提交
359
	{
360
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
B
Bodo Möller 已提交
361 362 363
		return -1;
	}

364 365
	ctx = BN_CTX_new();
	if (!ctx)
366 367
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
368
		return -1;
369
	}
370 371 372 373 374 375 376
	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)
377 378 379 380
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
381 382
	
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
383
	{
384
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
B
Bodo Möller 已提交
385 386
		goto err;
	}
387

388
	if (BN_is_zero(sig->r)          || BN_is_negative(sig->r) || 
389
	    BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s)  ||
390
	    BN_is_negative(sig->s)      || BN_ucmp(sig->s, order) >= 0)
B
Bodo Möller 已提交
391
	{
392
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
393
		ret = 0;	/* signature is invalid */
B
Bodo Möller 已提交
394 395 396
		goto err;
	}
	/* calculate tmp1 = inv(S) mod order */
397
	if (!BN_mod_inverse(u2, sig->s, order, ctx))
398 399 400 401
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
402
	/* digest -> m */
403
	if (!BN_bin2bn(dgst, dgst_len, m))
404 405 406 407
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
408
	/* u1 = m * tmp mod order */
409
	if (!BN_mod_mul(u1, m, u2, order, ctx))
410 411 412 413
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
414
	/* u2 = r * w mod q */
415
	if (!BN_mod_mul(u2, sig->r, u2, order, ctx))
416 417 418 419
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
420

421
	if ((point = EC_POINT_new(group)) == NULL)
B
Bodo Möller 已提交
422
	{
423
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
B
Bodo Möller 已提交
424 425
		goto err;
	}
N
Nils Larsch 已提交
426
	if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx))
B
Bodo Möller 已提交
427
	{
428 429 430
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
		goto err;
	}
431
	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
432
	{
433
		if (!EC_POINT_get_affine_coordinates_GFp(group,
434 435
			point, X, NULL, ctx))
		{
436
			ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
437 438 439 440 441
			goto err;
		}
	}
	else /* NID_X9_62_characteristic_two_field */
	{
442
		if (!EC_POINT_get_affine_coordinates_GF2m(group,
443 444
			point, X, NULL, ctx))
		{
445
			ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
446 447 448 449
			goto err;
		}
	}
	
450
	if (!BN_nnmod(u1, X, order, ctx))
451 452
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
B
Bodo Möller 已提交
453 454
		goto err;
	}
455 456 457 458 459
	/*  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);
460 461
	if (point)
		EC_POINT_free(point);
462
	return ret;
B
Bodo Möller 已提交
463
}