2 * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
4 * Licensed under the Apache License 2.0 (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 * ECDSA low level APIs are deprecated for public use, but still ok for
14 #include "internal/deprecated.h"
17 #include <openssl/err.h>
18 #include <openssl/obj_mac.h>
19 #include <openssl/rand.h>
20 #include "crypto/bn.h"
23 #define MIN_ECDSA_SIGN_ORDERBITS 64
25 * It is highly unlikely that a retry will happen,
26 * Multiple retries would indicate that something is wrong
27 * with the group parameters (which would normally only happen
28 * with a bad custom group).
30 #define MAX_ECDSA_SIGN_RETRIES 8
32 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
35 if (eckey->group->meth->ecdsa_sign_setup == NULL) {
36 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
40 return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
43 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
44 const BIGNUM *in_kinv, const BIGNUM *in_r,
47 if (eckey->group->meth->ecdsa_sign_sig == NULL) {
48 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
52 return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
53 in_kinv, in_r, eckey);
56 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
57 const ECDSA_SIG *sig, EC_KEY *eckey)
59 if (eckey->group->meth->ecdsa_verify_sig == NULL) {
60 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
64 return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
67 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
68 unsigned char *sig, unsigned int *siglen,
69 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
73 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
78 *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
83 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
84 BIGNUM **kinvp, BIGNUM **rp,
85 const unsigned char *dgst, int dlen)
88 BIGNUM *k = NULL, *r = NULL, *X = NULL;
90 EC_POINT *tmp_point = NULL;
91 const EC_GROUP *group;
94 const BIGNUM *priv_key;
96 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
97 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
100 if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
101 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
105 if (!EC_KEY_can_sign(eckey)) {
106 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
110 if ((ctx = ctx_in) == NULL) {
111 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
112 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
117 k = BN_secure_new(); /* this value is later returned in *kinvp */
118 r = BN_new(); /* this value is later returned in *rp */
120 if (k == NULL || r == NULL || X == NULL) {
121 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
124 if ((tmp_point = EC_POINT_new(group)) == NULL) {
125 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
128 order = EC_GROUP_get0_order(group);
130 /* Preallocate space */
131 order_bits = BN_num_bits(order);
132 /* Check the number of bits here so that an infinite loop is not possible */
133 if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
134 || !BN_set_bit(k, order_bits)
135 || !BN_set_bit(r, order_bits)
136 || !BN_set_bit(X, order_bits))
143 if (!BN_generate_dsa_nonce(k, order, priv_key,
145 ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
149 if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
150 ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
154 } while (BN_is_zero(k));
156 /* compute r the x-coordinate of generator * k */
157 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
158 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
162 if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
163 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
167 if (!BN_nnmod(r, X, order, ctx)) {
168 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
171 } while (BN_is_zero(r));
173 /* compute the inverse of k */
174 if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
175 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
179 /* clear old values if necessary */
181 BN_clear_free(*kinvp);
182 /* save the pre-computed values */
193 EC_POINT_free(tmp_point);
198 int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
201 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
204 ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
205 const BIGNUM *in_kinv, const BIGNUM *in_r,
210 BIGNUM *kinv = NULL, *s, *m = NULL;
211 const BIGNUM *order, *ckinv;
213 const EC_GROUP *group;
215 const BIGNUM *priv_key;
217 group = EC_KEY_get0_group(eckey);
218 priv_key = EC_KEY_get0_private_key(eckey);
221 ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
224 if (priv_key == NULL) {
225 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
229 if (!EC_KEY_can_sign(eckey)) {
230 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
234 ret = ECDSA_SIG_new();
236 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
241 if (ret->r == NULL || ret->s == NULL) {
242 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
247 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
248 || (m = BN_new()) == NULL) {
249 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
253 order = EC_GROUP_get0_order(group);
254 i = BN_num_bits(order);
256 * Need to truncate digest if it is too long: first truncate whole bytes.
258 if (8 * dgst_len > i)
259 dgst_len = (i + 7) / 8;
260 if (!BN_bin2bn(dgst, dgst_len, m)) {
261 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
264 /* If still too long, truncate remaining bits with a shift */
265 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
266 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
270 if (in_kinv == NULL || in_r == NULL) {
271 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
272 ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
278 if (BN_copy(ret->r, in_r) == NULL) {
279 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
285 * With only one multiplicant being in Montgomery domain
286 * multiplication yields real result without post-conversion.
287 * Also note that all operations but last are performed with
288 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
289 * below, returns user-visible value with removed zero padding.
291 if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
292 || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
293 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
296 if (!bn_mod_add_fixed_top(s, s, m, order)) {
297 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
301 * |s| can still be larger than modulus, because |m| can be. In
302 * such case we count on Montgomery reduction to tie it up.
304 if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
305 || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
306 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
312 * if kinv and r have been supplied by the caller, don't
313 * generate new kinv and r values
315 if (in_kinv != NULL && in_r != NULL) {
316 ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
319 /* Avoid infinite loops cause by invalid group parameters */
320 if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
321 ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
325 /* s != 0 => we have a valid signature */
344 * 1: correct signature
345 * 0: incorrect signature
348 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
349 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
352 const unsigned char *p = sigbuf;
353 unsigned char *der = NULL;
360 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
362 /* Ensure signature uses DER and doesn't have trailing garbage */
363 derlen = i2d_ECDSA_SIG(s, &der);
364 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
366 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
373 int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
374 const ECDSA_SIG *sig, EC_KEY *eckey)
379 BIGNUM *u1, *u2, *m, *X;
380 EC_POINT *point = NULL;
381 const EC_GROUP *group;
382 const EC_POINT *pub_key;
384 /* check input values */
385 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
386 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
387 ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
391 if (!EC_KEY_can_sign(eckey)) {
392 ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
396 ctx = BN_CTX_new_ex(eckey->libctx);
398 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
402 u1 = BN_CTX_get(ctx);
403 u2 = BN_CTX_get(ctx);
407 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
411 order = EC_GROUP_get0_order(group);
413 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
417 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
418 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
419 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
420 ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
421 ret = 0; /* signature is invalid */
424 /* calculate tmp1 = inv(S) mod order */
425 if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
426 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
430 i = BN_num_bits(order);
432 * Need to truncate digest if it is too long: first truncate whole bytes.
434 if (8 * dgst_len > i)
435 dgst_len = (i + 7) / 8;
436 if (!BN_bin2bn(dgst, dgst_len, m)) {
437 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
440 /* If still too long truncate remaining bits with a shift */
441 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
442 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
445 /* u1 = m * tmp mod order */
446 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
447 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
450 /* u2 = r * w mod q */
451 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
452 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
456 if ((point = EC_POINT_new(group)) == NULL) {
457 ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
460 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
461 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
465 if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
466 ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
470 if (!BN_nnmod(u1, X, order, ctx)) {
471 ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
474 /* if the signature is correct u1 is equal to sig->r */
475 ret = (BN_ucmp(u1, sig->r) == 0);
479 EC_POINT_free(point);