2 * Copyright 2002-2016 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the OpenSSL license (the "License"). You may not use
5 * this file except in compliance with the License. You can obtain a copy
6 * in the file LICENSE in the source distribution or at
7 * https://www.openssl.org/source/license.html
11 #include <openssl/err.h>
12 #include <openssl/obj_mac.h>
13 #include <openssl/bn.h>
14 #include <openssl/rand.h>
15 #include <openssl/ec.h>
18 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
19 unsigned char *sig, unsigned int *siglen,
20 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
23 RAND_seed(dgst, dlen);
24 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
29 *siglen = i2d_ECDSA_SIG(s, &sig);
34 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
35 BIGNUM **kinvp, BIGNUM **rp,
36 const unsigned char *dgst, int dlen)
39 BIGNUM *k = NULL, *r = NULL, *X = NULL;
41 EC_POINT *tmp_point = NULL;
42 const EC_GROUP *group;
45 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
46 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
50 if (!EC_KEY_can_sign(eckey)) {
51 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
56 if ((ctx = BN_CTX_new()) == NULL) {
57 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
63 k = BN_new(); /* this value is later returned in *kinvp */
64 r = BN_new(); /* this value is later returned in *rp */
66 if (k == NULL || r == NULL || X == NULL) {
67 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
70 if ((tmp_point = EC_POINT_new(group)) == NULL) {
71 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
74 order = EC_GROUP_get0_order(group);
76 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
84 if (!BN_generate_dsa_nonce
85 (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
87 ECerr(EC_F_ECDSA_SIGN_SETUP,
88 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
92 if (!BN_rand_range(k, order)) {
93 ECerr(EC_F_ECDSA_SIGN_SETUP,
94 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
98 while (BN_is_zero(k));
101 * We do not want timing information to leak the length of k, so we
102 * compute G*k using an equivalent scalar of fixed bit-length.
105 if (!BN_add(k, k, order))
107 if (BN_num_bits(k) <= BN_num_bits(order))
108 if (!BN_add(k, k, order))
111 /* compute r the x-coordinate of generator * k */
112 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
113 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
116 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
117 NID_X9_62_prime_field) {
118 if (!EC_POINT_get_affine_coordinates_GFp
119 (group, tmp_point, X, NULL, ctx)) {
120 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
124 #ifndef OPENSSL_NO_EC2M
125 else { /* NID_X9_62_characteristic_two_field */
127 if (!EC_POINT_get_affine_coordinates_GF2m(group,
130 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
135 if (!BN_nnmod(r, X, order, ctx)) {
136 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
140 while (BN_is_zero(r));
142 /* compute the inverse of k */
143 if (EC_GROUP_get_mont_data(group) != NULL) {
145 * We want inverse in constant time, therefore we utilize the fact
146 * order must be prime and use Fermats Little Theorem instead.
148 if (!BN_set_word(X, 2)) {
149 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
152 if (!BN_mod_sub(X, order, X, order, ctx)) {
153 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
156 BN_set_flags(X, BN_FLG_CONSTTIME);
157 if (!BN_mod_exp_mont_consttime
158 (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
159 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
163 if (!BN_mod_inverse(k, k, order, ctx)) {
164 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
169 /* clear old values if necessary */
171 BN_clear_free(*kinvp);
172 /* save the pre-computed values */
183 EC_POINT_free(tmp_point);
188 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
191 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
194 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
195 const BIGNUM *in_kinv, const BIGNUM *in_r,
199 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
200 const BIGNUM *order, *ckinv;
202 const EC_GROUP *group;
204 const BIGNUM *priv_key;
206 group = EC_KEY_get0_group(eckey);
207 priv_key = EC_KEY_get0_private_key(eckey);
209 if (group == NULL || priv_key == NULL) {
210 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
214 if (!EC_KEY_can_sign(eckey)) {
215 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
219 ret = ECDSA_SIG_new();
221 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
226 if (ret->r == NULL || ret->s == NULL) {
227 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
232 if ((ctx = BN_CTX_new()) == NULL ||
233 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
234 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
238 order = EC_GROUP_get0_order(group);
240 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
243 i = BN_num_bits(order);
245 * Need to truncate digest if it is too long: first truncate whole bytes.
247 if (8 * dgst_len > i)
248 dgst_len = (i + 7) / 8;
249 if (!BN_bin2bn(dgst, dgst_len, m)) {
250 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
253 /* If still too long truncate remaining bits with a shift */
254 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
255 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
259 if (in_kinv == NULL || in_r == NULL) {
260 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
261 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
267 if (BN_copy(ret->r, in_r) == NULL) {
268 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
273 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
274 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
277 if (!BN_mod_add_quick(s, tmp, m, order)) {
278 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
281 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
282 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
287 * if kinv and r have been supplied by the caller don't to
288 * generate new kinv and r values
290 if (in_kinv != NULL && in_r != NULL) {
291 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
295 /* s != 0 => we have a valid signature */
315 * 1: correct signature
316 * 0: incorrect signature
319 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
320 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
323 const unsigned char *p = sigbuf;
324 unsigned char *der = NULL;
331 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
333 /* Ensure signature uses DER and doesn't have trailing garbage */
334 derlen = i2d_ECDSA_SIG(s, &der);
335 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
337 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
339 OPENSSL_clear_free(der, derlen);
344 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
345 const ECDSA_SIG *sig, EC_KEY *eckey)
350 BIGNUM *u1, *u2, *m, *X;
351 EC_POINT *point = NULL;
352 const EC_GROUP *group;
353 const EC_POINT *pub_key;
355 /* check input values */
356 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
357 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
358 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
362 if (!EC_KEY_can_sign(eckey)) {
363 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
369 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
373 u1 = BN_CTX_get(ctx);
374 u2 = BN_CTX_get(ctx);
378 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
382 order = EC_GROUP_get0_order(group);
384 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
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) {
391 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
392 ret = 0; /* signature is invalid */
395 /* calculate tmp1 = inv(S) mod order */
396 if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
397 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
401 i = BN_num_bits(order);
403 * Need to truncate digest if it is too long: first truncate whole bytes.
405 if (8 * dgst_len > i)
406 dgst_len = (i + 7) / 8;
407 if (!BN_bin2bn(dgst, dgst_len, m)) {
408 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
411 /* If still too long truncate remaining bits with a shift */
412 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
413 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
416 /* u1 = m * tmp mod order */
417 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
418 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
421 /* u2 = r * w mod q */
422 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
423 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
427 if ((point = EC_POINT_new(group)) == NULL) {
428 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
431 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
432 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
435 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
436 NID_X9_62_prime_field) {
437 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
438 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
442 #ifndef OPENSSL_NO_EC2M
443 else { /* NID_X9_62_characteristic_two_field */
445 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
446 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
451 if (!BN_nnmod(u1, X, order, ctx)) {
452 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
455 /* if the signature is correct u1 is equal to sig->r */
456 ret = (BN_ucmp(u1, sig->r) == 0);
460 EC_POINT_free(point);