2 * Copyright 2002-2018 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)
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;
46 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
47 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
51 if (!EC_KEY_can_sign(eckey)) {
52 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
57 if ((ctx = BN_CTX_new()) == NULL) {
58 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
64 k = BN_new(); /* this value is later returned in *kinvp */
65 r = BN_new(); /* this value is later returned in *rp */
67 if (k == NULL || r == NULL || X == NULL) {
68 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
71 if ((tmp_point = EC_POINT_new(group)) == NULL) {
72 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
75 order = EC_GROUP_get0_order(group);
77 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
81 /* Preallocate space */
82 order_bits = BN_num_bits(order);
83 if (!BN_set_bit(k, order_bits)
84 || !BN_set_bit(r, order_bits)
85 || !BN_set_bit(X, order_bits))
92 if (!BN_generate_dsa_nonce
93 (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
95 ECerr(EC_F_ECDSA_SIGN_SETUP,
96 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
100 if (!BN_priv_rand_range(k, order)) {
101 ECerr(EC_F_ECDSA_SIGN_SETUP,
102 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
106 while (BN_is_zero(k));
109 * We do not want timing information to leak the length of k, so we
110 * compute G*k using an equivalent scalar of fixed bit-length.
112 * We unconditionally perform both of these additions to prevent a
113 * small timing information leakage. We then choose the sum that is
114 * one bit longer than the order. This guarantees the code
115 * path used in the constant time implementations elsewhere.
117 * TODO: revisit the BN_copy aiming for a memory access agnostic
120 if (!BN_add(r, k, order)
121 || !BN_add(X, r, order)
122 || !BN_copy(k, BN_num_bits(r) > order_bits ? r : X))
125 /* compute r the x-coordinate of generator * k */
126 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
127 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
130 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
131 NID_X9_62_prime_field) {
132 if (!EC_POINT_get_affine_coordinates_GFp
133 (group, tmp_point, X, NULL, ctx)) {
134 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
138 #ifndef OPENSSL_NO_EC2M
139 else { /* NID_X9_62_characteristic_two_field */
141 if (!EC_POINT_get_affine_coordinates_GF2m(group,
144 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
149 if (!BN_nnmod(r, X, order, ctx)) {
150 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
154 while (BN_is_zero(r));
156 /* Check if optimized inverse is implemented */
157 if (EC_GROUP_do_inverse_ord(group, k, k, ctx) == 0) {
158 /* compute the inverse of k */
159 if (group->mont_data != NULL) {
161 * We want inverse in constant time, therefore we utilize the fact
162 * order must be prime and use Fermats Little Theorem instead.
164 if (!BN_set_word(X, 2)) {
165 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
168 if (!BN_mod_sub(X, order, X, order, ctx)) {
169 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
172 BN_set_flags(X, BN_FLG_CONSTTIME);
173 if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx,
175 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
179 if (!BN_mod_inverse(k, k, order, ctx)) {
180 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
186 /* clear old values if necessary */
188 BN_clear_free(*kinvp);
189 /* save the pre-computed values */
200 EC_POINT_free(tmp_point);
205 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
208 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
211 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
212 const BIGNUM *in_kinv, const BIGNUM *in_r,
216 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
217 const BIGNUM *order, *ckinv;
219 const EC_GROUP *group;
221 const BIGNUM *priv_key;
223 group = EC_KEY_get0_group(eckey);
224 priv_key = EC_KEY_get0_private_key(eckey);
226 if (group == NULL || priv_key == NULL) {
227 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
231 if (!EC_KEY_can_sign(eckey)) {
232 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
236 ret = ECDSA_SIG_new();
238 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
243 if (ret->r == NULL || ret->s == NULL) {
244 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
249 if ((ctx = BN_CTX_new()) == NULL ||
250 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
251 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
255 order = EC_GROUP_get0_order(group);
257 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
260 i = BN_num_bits(order);
262 * Need to truncate digest if it is too long: first truncate whole bytes.
264 if (8 * dgst_len > i)
265 dgst_len = (i + 7) / 8;
266 if (!BN_bin2bn(dgst, dgst_len, m)) {
267 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
270 /* If still too long truncate remaining bits with a shift */
271 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
272 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
276 if (in_kinv == NULL || in_r == NULL) {
277 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
278 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
284 if (BN_copy(ret->r, in_r) == NULL) {
285 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
290 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
291 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
294 if (!BN_mod_add_quick(s, tmp, m, order)) {
295 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
298 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
299 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
304 * if kinv and r have been supplied by the caller, don't
305 * generate new kinv and r values
307 if (in_kinv != NULL && in_r != NULL) {
308 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
312 /* s != 0 => we have a valid signature */
332 * 1: correct signature
333 * 0: incorrect signature
336 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
337 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
340 const unsigned char *p = sigbuf;
341 unsigned char *der = NULL;
348 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
350 /* Ensure signature uses DER and doesn't have trailing garbage */
351 derlen = i2d_ECDSA_SIG(s, &der);
352 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
354 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
356 OPENSSL_clear_free(der, derlen);
361 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
362 const ECDSA_SIG *sig, EC_KEY *eckey)
367 BIGNUM *u1, *u2, *m, *X;
368 EC_POINT *point = NULL;
369 const EC_GROUP *group;
370 const EC_POINT *pub_key;
372 /* check input values */
373 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
374 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
375 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
379 if (!EC_KEY_can_sign(eckey)) {
380 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
386 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
390 u1 = BN_CTX_get(ctx);
391 u2 = BN_CTX_get(ctx);
395 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
399 order = EC_GROUP_get0_order(group);
401 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
405 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
406 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
407 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
408 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
409 ret = 0; /* signature is invalid */
412 /* calculate tmp1 = inv(S) mod order */
413 /* Check if optimized inverse is implemented */
414 if (EC_GROUP_do_inverse_ord(group, u2, sig->s, ctx) == 0) {
415 if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
416 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
421 i = BN_num_bits(order);
423 * Need to truncate digest if it is too long: first truncate whole bytes.
425 if (8 * dgst_len > i)
426 dgst_len = (i + 7) / 8;
427 if (!BN_bin2bn(dgst, dgst_len, m)) {
428 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
431 /* If still too long truncate remaining bits with a shift */
432 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
433 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
436 /* u1 = m * tmp mod order */
437 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
438 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
441 /* u2 = r * w mod q */
442 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
443 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
447 if ((point = EC_POINT_new(group)) == NULL) {
448 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
451 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
452 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
455 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
456 NID_X9_62_prime_field) {
457 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
458 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
462 #ifndef OPENSSL_NO_EC2M
463 else { /* NID_X9_62_characteristic_two_field */
465 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
466 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
471 if (!BN_nnmod(u1, X, order, ctx)) {
472 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
475 /* if the signature is correct u1 is equal to sig->r */
476 ret = (BN_ucmp(u1, sig->r) == 0);
480 EC_POINT_free(point);