ecs_ossl.c 10.7 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

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

64 65 66 67 68 69
static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen, 
		EC_KEY *eckey);
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, 
		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;
95
	EC_GROUP *group;
96 97
	int 	 ret = 0;
	if (!eckey  || !eckey->group || !eckey->pub_key || !eckey->priv_key)
B
Bodo Möller 已提交
98
	{
99
		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
B
Bodo Möller 已提交
100 101
		return 0;
	}
102
	group = eckey->group;
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 213
static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len, 
		EC_KEY *eckey)
B
Bodo Möller 已提交
214
{
215 216 217 218 219
	int     ok = 0;
	BIGNUM *kinv=NULL, *r, *s, *m=NULL,*tmp=NULL,*order=NULL;
	BN_CTX     *ctx = NULL;
	EC_GROUP   *group;
	ECDSA_SIG  *ret;
220 221 222
	ECDSA_DATA *ecdsa;

	ecdsa = ecdsa_check(eckey);
B
Bodo Möller 已提交
223

224
	if (!eckey->group || !eckey->pub_key || !eckey->priv_key || !ecdsa)
B
Bodo Möller 已提交
225
	{
226
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
227
		return NULL;
B
Bodo Möller 已提交
228 229
	}

230 231 232 233 234 235 236 237 238
	group = eckey->group;
	ret = ECDSA_SIG_new();
	if (!ret)
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
		return NULL;
	}
	s = ret->s;

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

246
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
247
	{
248
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
B
Bodo Möller 已提交
249 250 251 252
		goto err;
	}
	if (dgst_len > BN_num_bytes(order))
	{
253 254
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
			ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
B
Bodo Möller 已提交
255 256 257
		goto err;
	}

258
	if (!BN_bin2bn(dgst, dgst_len, m))
259 260 261 262
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
263 264
	do
	{
265
		if (ecdsa->kinv == NULL || ecdsa->r == NULL)
B
Bodo Möller 已提交
266
		{
267
			if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r))
268
			{
269
				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
270 271
				goto err;
			}
272
			r = ret->r;
B
Bodo Möller 已提交
273 274 275
		}
		else
		{
276 277 278 279
			BN_free(ret->r);
			kinv   = ecdsa->kinv;
			r      = ecdsa->r;
			ret->r = r;
B
Bodo Möller 已提交
280
			ecdsa->kinv = NULL;
281
			ecdsa->r    = NULL;
B
Bodo Möller 已提交
282 283
		}

284
		if (!BN_mod_mul(tmp, eckey->priv_key, r, order, ctx))
285 286 287 288
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
289
		if (!BN_mod_add_quick(s, tmp, m, order))
290 291 292 293
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
294
		if (!BN_mod_mul(s, s, kinv, order, ctx))
295 296 297 298
		{
			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
			goto err;
		}
B
Bodo Möller 已提交
299 300 301
	}
	while (BN_is_zero(s));

302 303 304
	ok = 1;
err:
	if (!ok)
B
Bodo Möller 已提交
305 306 307 308
	{
		ECDSA_SIG_free(ret);
		ret = NULL;
	}
309 310 311 312 313 314 315
	if (ctx)
		BN_CTX_free(ctx);
	if (m)
		BN_clear_free(m);
	if (tmp)
		BN_clear_free(tmp);
	if (order)
316
		BN_free(order);
317 318
	if (kinv)
		BN_clear_free(kinv);
319
	return ret;
B
Bodo Möller 已提交
320 321
}

322 323
static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
		ECDSA_SIG *sig, EC_KEY *eckey)
B
Bodo Möller 已提交
324
{
325
	int ret = -1;
326 327 328 329 330
	BN_CTX   *ctx;
	BIGNUM   *order, *u1, *u2, *m, *X;
	EC_POINT *point = NULL;
	EC_GROUP *group;
	/* check input values */
331
	if (!eckey || !eckey->group || !eckey->pub_key || !sig)
B
Bodo Möller 已提交
332
	{
333
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
B
Bodo Möller 已提交
334 335 336
		return -1;
	}

337 338 339 340
	group = eckey->group;

	ctx = BN_CTX_new();
	if (!ctx)
341 342
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
343
		return -1;
344
	}
345 346 347 348 349 350 351
	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)
352 353 354 355
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
356 357
	
	if (!EC_GROUP_get_order(group, order, ctx))
B
Bodo Möller 已提交
358
	{
359
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
B
Bodo Möller 已提交
360 361
		goto err;
	}
362

363
	if (BN_is_zero(sig->r)          || BN_is_negative(sig->r) || 
364
	    BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s)  ||
365
	    BN_is_negative(sig->s)      || BN_ucmp(sig->s, order) >= 0)
B
Bodo Möller 已提交
366
	{
367
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
368
		ret = 0;	/* signature is invalid */
B
Bodo Möller 已提交
369 370 371
		goto err;
	}
	/* calculate tmp1 = inv(S) mod order */
372
	if (!BN_mod_inverse(u2, sig->s, order, ctx))
373 374 375 376
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
377
	/* digest -> m */
378
	if (!BN_bin2bn(dgst, dgst_len, m))
379 380 381 382
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
383
	/* u1 = m * tmp mod order */
384
	if (!BN_mod_mul(u1, m, u2, order, ctx))
385 386 387 388
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
389
	/* u2 = r * w mod q */
390
	if (!BN_mod_mul(u2, sig->r, u2, order, ctx))
391 392 393 394
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
		goto err;
	}
B
Bodo Möller 已提交
395

396
	if ((point = EC_POINT_new(group)) == NULL)
B
Bodo Möller 已提交
397
	{
398
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
B
Bodo Möller 已提交
399 400
		goto err;
	}
401
	if (!EC_POINT_mul(group, point, u1, eckey->pub_key, u2, ctx))
B
Bodo Möller 已提交
402
	{
403 404 405
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
		goto err;
	}
406
	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
407
	{
408
		if (!EC_POINT_get_affine_coordinates_GFp(group,
409 410 411 412 413 414 415 416
			point, X, NULL, ctx))
		{
			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
			goto err;
		}
	}
	else /* NID_X9_62_characteristic_two_field */
	{
417
		if (!EC_POINT_get_affine_coordinates_GF2m(group,
418 419 420 421 422 423 424
			point, X, NULL, ctx))
		{
			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
			goto err;
		}
	}
	
425
	if (!BN_nnmod(u1, X, order, ctx))
426 427
	{
		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
B
Bodo Möller 已提交
428 429
		goto err;
	}
430 431 432 433 434
	/*  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);
435 436
	if (point)
		EC_POINT_free(point);
437
	return ret;
B
Bodo Möller 已提交
438
}