1 /* $OpenBSD: ecs_ossl.c,v 1.5 2014/07/12 16:03:37 miod Exp $ */
2 /*
3  * Written by Nils Larsch for the OpenSSL project
4  */
5 /* ====================================================================
6  * Copyright (c) 1998-2004 The OpenSSL Project.  All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions
10  * are met:
11  *
12  * 1. Redistributions of source code must retain the above copyright
13  *    notice, this list of conditions and the following disclaimer.
14  *
15  * 2. Redistributions in binary form must reproduce the above copyright
16  *    notice, this list of conditions and the following disclaimer in
17  *    the documentation and/or other materials provided with the
18  *    distribution.
19  *
20  * 3. All advertising materials mentioning features or use of this
21  *    software must display the following acknowledgment:
22  *    "This product includes software developed by the OpenSSL Project
23  *    for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24  *
25  * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26  *    endorse or promote products derived from this software without
27  *    prior written permission. For written permission, please contact
28  *    openssl-core@OpenSSL.org.
29  *
30  * 5. Products derived from this software may not be called "OpenSSL"
31  *    nor may "OpenSSL" appear in their names without prior written
32  *    permission of the OpenSSL Project.
33  *
34  * 6. Redistributions of any form whatsoever must retain the following
35  *    acknowledgment:
36  *    "This product includes software developed by the OpenSSL Project
37  *    for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38  *
39  * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40  * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42  * PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE OpenSSL PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46  * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50  * OF THE POSSIBILITY OF SUCH DAMAGE.
51  * ====================================================================
52  *
53  * This product includes cryptographic software written by Eric Young
54  * (eay@cryptsoft.com).  This product includes software written by Tim
55  * Hudson (tjh@cryptsoft.com).
56  *
57  */
58 
59 #include <openssl/opensslconf.h>
60 
61 #include "ecs_locl.h"
62 #include <openssl/err.h>
63 #include <openssl/obj_mac.h>
64 #include <openssl/bn.h>
65 
66 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
67     const BIGNUM *, const BIGNUM *, EC_KEY *eckey);
68 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
69     BIGNUM **rp);
70 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
71     const ECDSA_SIG *sig, EC_KEY *eckey);
72 
73 static ECDSA_METHOD openssl_ecdsa_meth = {
74 	.name = "OpenSSL ECDSA method",
75 	.ecdsa_do_sign = ecdsa_do_sign,
76 	.ecdsa_sign_setup = ecdsa_sign_setup,
77 	.ecdsa_do_verify = ecdsa_do_verify
78 };
79 
80 const ECDSA_METHOD *
81 ECDSA_OpenSSL(void)
82 {
83 	return &openssl_ecdsa_meth;
84 }
85 
86 static int
87 ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, BIGNUM **rp)
88 {
89 	BN_CTX   *ctx = NULL;
90 	BIGNUM	 *k = NULL, *r = NULL, *order = NULL, *X = NULL;
91 	EC_POINT *tmp_point = NULL;
92 	const EC_GROUP *group;
93 	int 	 ret = 0;
94 
95 	if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
96 		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
97 		return 0;
98 	}
99 
100 	if (ctx_in == NULL) {
101 		if ((ctx = BN_CTX_new()) == NULL) {
102 			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
103 			    ERR_R_MALLOC_FAILURE);
104 			return 0;
105 		}
106 	} else
107 		ctx = ctx_in;
108 
109 	k = BN_new();	/* this value is later returned in *kinvp */
110 	r = BN_new();	/* this value is later returned in *rp    */
111 	order = BN_new();
112 	X = BN_new();
113 	if (!k || !r || !order || !X) {
114 		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
115 		goto err;
116 	}
117 	if ((tmp_point = EC_POINT_new(group)) == NULL) {
118 		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
119 		goto err;
120 	}
121 	if (!EC_GROUP_get_order(group, order, ctx)) {
122 		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
123 		goto err;
124 	}
125 
126 	do {
127 		/* get random k */
128 		do
129 			if (!BN_rand_range(k, order)) {
130 				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
131 				    ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
132 				goto err;
133 			}
134 		while (BN_is_zero(k));
135 
136 		/* We do not want timing information to leak the length of k,
137 		 * so we compute G*k using an equivalent scalar of fixed
138 		 * bit-length. */
139 		if (!BN_add(k, k, order))
140 			goto err;
141 		if (BN_num_bits(k) <= BN_num_bits(order))
142 			if (!BN_add(k, k, order))
143 				goto err;
144 
145 		BN_set_flags(k, BN_FLG_CONSTTIME);
146 
147 		/* compute r the x-coordinate of generator * k */
148 		if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
149 			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
150 			goto err;
151 		}
152 		if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
153 		    NID_X9_62_prime_field) {
154 			if (!EC_POINT_get_affine_coordinates_GFp(group,
155 			    tmp_point, X, NULL, ctx)) {
156 				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
157 				    ERR_R_EC_LIB);
158 				goto err;
159 			}
160 		}
161 #ifndef OPENSSL_NO_EC2M
162 		else /* NID_X9_62_characteristic_two_field */
163 		{
164 			if (!EC_POINT_get_affine_coordinates_GF2m(group,
165 			    tmp_point, X, NULL, ctx)) {
166 				ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
167 				    ERR_R_EC_LIB);
168 				goto err;
169 			}
170 		}
171 #endif
172 		if (!BN_nnmod(r, X, order, ctx)) {
173 			ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
174 			goto err;
175 		}
176 	} while (BN_is_zero(r));
177 
178 	/* compute the inverse of k */
179 	if (!BN_mod_inverse(k, k, order, ctx)) {
180 		ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
181 		goto err;
182 	}
183 	/* clear old values if necessary */
184 	BN_clear_free(*rp);
185 	BN_clear_free(*kinvp);
186 	/* save the pre-computed values  */
187 	*rp = r;
188 	*kinvp = k;
189 	ret = 1;
190 
191 err:
192 	if (!ret) {
193 		BN_clear_free(k);
194 		BN_clear_free(r);
195 	}
196 	if (ctx_in == NULL)
197 		BN_CTX_free(ctx);
198 	BN_free(order);
199 	EC_POINT_free(tmp_point);
200 	BN_clear_free(X);
201 	return (ret);
202 }
203 
204 
205 static ECDSA_SIG *
206 ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
207     const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
208 {
209 	int     ok = 0, i;
210 	BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
211 	const BIGNUM *ckinv;
212 	BN_CTX     *ctx = NULL;
213 	const EC_GROUP   *group;
214 	ECDSA_SIG  *ret;
215 	ECDSA_DATA *ecdsa;
216 	const BIGNUM *priv_key;
217 
218 	ecdsa = ecdsa_check(eckey);
219 	group = EC_KEY_get0_group(eckey);
220 	priv_key = EC_KEY_get0_private_key(eckey);
221 
222 	if (group == NULL || priv_key == NULL || ecdsa == NULL) {
223 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
224 		return NULL;
225 	}
226 
227 	ret = ECDSA_SIG_new();
228 	if (!ret) {
229 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
230 		return NULL;
231 	}
232 	s = ret->s;
233 
234 	if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
235 	    (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
236 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
237 		goto err;
238 	}
239 
240 	if (!EC_GROUP_get_order(group, order, ctx)) {
241 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
242 		goto err;
243 	}
244 	i = BN_num_bits(order);
245 	/* Need to truncate digest if it is too long: first truncate whole
246 	 * bytes.
247 	 */
248 	if (8 * dgst_len > i)
249 		dgst_len = (i + 7)/8;
250 	if (!BN_bin2bn(dgst, dgst_len, m)) {
251 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
252 		goto err;
253 	}
254 	/* If still too long truncate remaining bits with a shift */
255 	if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
256 		ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
257 		goto err;
258 	}
259 	do {
260 		if (in_kinv == NULL || in_r == NULL) {
261 			if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
262 				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
263 				    ERR_R_ECDSA_LIB);
264 				goto err;
265 			}
266 			ckinv = kinv;
267 		} else {
268 			ckinv = in_kinv;
269 			if (BN_copy(ret->r, in_r) == NULL) {
270 				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
271 				    ERR_R_MALLOC_FAILURE);
272 				goto err;
273 			}
274 		}
275 
276 		if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
277 			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
278 			goto err;
279 		}
280 		if (!BN_mod_add_quick(s, tmp, m, order)) {
281 			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
282 			goto err;
283 		}
284 		if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
285 			ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
286 			goto err;
287 		}
288 		if (BN_is_zero(s)) {
289 			/* if kinv and r have been supplied by the caller
290 			 * don't to generate new kinv and r values */
291 			if (in_kinv != NULL && in_r != NULL) {
292 				ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
293 				    ECDSA_R_NEED_NEW_SETUP_VALUES);
294 				goto err;
295 			}
296 		} else
297 			/* s != 0 => we have a valid signature */
298 			break;
299 	} while (1);
300 
301 	ok = 1;
302 
303 err:
304 	if (!ok) {
305 		ECDSA_SIG_free(ret);
306 		ret = NULL;
307 	}
308 	BN_CTX_free(ctx);
309 	BN_clear_free(m);
310 	BN_clear_free(tmp);
311 	BN_free(order);
312 	BN_clear_free(kinv);
313 	return ret;
314 }
315 
316 static int
317 ecdsa_do_verify(const unsigned char *dgst, int dgst_len, const ECDSA_SIG *sig,
318     EC_KEY *eckey)
319 {
320 	int ret = -1, i;
321 	BN_CTX   *ctx;
322 	BIGNUM   *order, *u1, *u2, *m, *X;
323 	EC_POINT *point = NULL;
324 	const EC_GROUP *group;
325 	const EC_POINT *pub_key;
326 
327 	/* check input values */
328 	if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
329 	    (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
330 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
331 		return -1;
332 	}
333 
334 	ctx = BN_CTX_new();
335 	if (!ctx) {
336 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
337 		return -1;
338 	}
339 	BN_CTX_start(ctx);
340 	order = BN_CTX_get(ctx);
341 	u1 = BN_CTX_get(ctx);
342 	u2 = BN_CTX_get(ctx);
343 	m = BN_CTX_get(ctx);
344 	X = BN_CTX_get(ctx);
345 	if (!X) {
346 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
347 		goto err;
348 	}
349 
350 	if (!EC_GROUP_get_order(group, order, ctx)) {
351 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
352 		goto err;
353 	}
354 
355 	if (BN_is_zero(sig->r)          || BN_is_negative(sig->r) ||
356 	    BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s)  ||
357 	    BN_is_negative(sig->s)      || BN_ucmp(sig->s, order) >= 0) {
358 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
359 		ret = 0;	/* signature is invalid */
360 		goto err;
361 	}
362 	/* calculate tmp1 = inv(S) mod order */
363 	if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
364 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
365 		goto err;
366 	}
367 	/* digest -> m */
368 	i = BN_num_bits(order);
369 	/* Need to truncate digest if it is too long: first truncate whole
370 	 * bytes.
371 	 */
372 	if (8 * dgst_len > i)
373 		dgst_len = (i + 7)/8;
374 	if (!BN_bin2bn(dgst, dgst_len, m)) {
375 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
376 		goto err;
377 	}
378 	/* If still too long truncate remaining bits with a shift */
379 	if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
380 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
381 		goto err;
382 	}
383 	/* u1 = m * tmp mod order */
384 	if (!BN_mod_mul(u1, m, u2, order, ctx)) {
385 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
386 		goto err;
387 	}
388 	/* u2 = r * w mod q */
389 	if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
390 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
391 		goto err;
392 	}
393 
394 	if ((point = EC_POINT_new(group)) == NULL) {
395 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
396 		goto err;
397 	}
398 	if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
399 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
400 		goto err;
401 	}
402 	if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
403 	    NID_X9_62_prime_field) {
404 		if (!EC_POINT_get_affine_coordinates_GFp(group,
405 		    point, X, NULL, ctx)) {
406 			ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
407 			goto err;
408 		}
409 	}
410 #ifndef OPENSSL_NO_EC2M
411 	else /* NID_X9_62_characteristic_two_field */
412 	{
413 		if (!EC_POINT_get_affine_coordinates_GF2m(group,
414 		    point, X, NULL, ctx)) {
415 			ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
416 			goto err;
417 		}
418 	}
419 #endif
420 	if (!BN_nnmod(u1, X, order, ctx)) {
421 		ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
422 		goto err;
423 	}
424 	/*  if the signature is correct u1 is equal to sig->r */
425 	ret = (BN_ucmp(u1, sig->r) == 0);
426 
427 err:
428 	BN_CTX_end(ctx);
429 	BN_CTX_free(ctx);
430 	EC_POINT_free(point);
431 	return ret;
432 }
433