2 * Copyright 2002-2018 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 #include <openssl/err.h>
12 #include <openssl/obj_mac.h>
13 #include <openssl/rand.h>
14 #include "internal/bn_int.h"
17 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
18 unsigned char *sig, unsigned int *siglen,
19 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
23 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
28 *siglen = i2d_ECDSA_SIG(s, &sig);
33 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
34 BIGNUM **kinvp, BIGNUM **rp,
35 const unsigned char *dgst, int dlen)
38 BIGNUM *k = NULL, *r = NULL, *X = NULL;
40 EC_POINT *tmp_point = NULL;
41 const EC_GROUP *group;
44 const BIGNUM *priv_key;
46 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
47 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
50 if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
51 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_MISSING_PRIVATE_KEY);
55 if (!EC_KEY_can_sign(eckey)) {
56 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
60 if ((ctx = ctx_in) == NULL) {
61 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
62 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
67 k = BN_secure_new(); /* this value is later returned in *kinvp */
68 r = BN_new(); /* this value is later returned in *rp */
70 if (k == NULL || r == NULL || X == NULL) {
71 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
74 if ((tmp_point = EC_POINT_new(group)) == NULL) {
75 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
78 order = EC_GROUP_get0_order(group);
80 /* Preallocate space */
81 order_bits = BN_num_bits(order);
82 if (!BN_set_bit(k, order_bits)
83 || !BN_set_bit(r, order_bits)
84 || !BN_set_bit(X, order_bits))
91 if (!BN_generate_dsa_nonce(k, order, priv_key,
93 ECerr(EC_F_ECDSA_SIGN_SETUP,
94 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
98 if (!BN_priv_rand_range_ex(k, order, ctx)) {
99 ECerr(EC_F_ECDSA_SIGN_SETUP,
100 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
104 } while (BN_is_zero(k));
106 /* compute r the x-coordinate of generator * k */
107 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
108 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
112 if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
113 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
117 if (!BN_nnmod(r, X, order, ctx)) {
118 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
121 } while (BN_is_zero(r));
123 /* compute the inverse of k */
124 if (!ec_group_do_inverse_ord(group, k, k, ctx)) {
125 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
129 /* clear old values if necessary */
131 BN_clear_free(*kinvp);
132 /* save the pre-computed values */
143 EC_POINT_free(tmp_point);
148 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
151 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
154 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
155 const BIGNUM *in_kinv, const BIGNUM *in_r,
159 BIGNUM *kinv = NULL, *s, *m = NULL;
160 const BIGNUM *order, *ckinv;
162 const EC_GROUP *group;
164 const BIGNUM *priv_key;
166 group = EC_KEY_get0_group(eckey);
167 priv_key = EC_KEY_get0_private_key(eckey);
170 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
173 if (priv_key == NULL) {
174 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_MISSING_PRIVATE_KEY);
178 if (!EC_KEY_can_sign(eckey)) {
179 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
183 ret = ECDSA_SIG_new();
185 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
190 if (ret->r == NULL || ret->s == NULL) {
191 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
196 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
197 || (m = BN_new()) == NULL) {
198 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
202 order = EC_GROUP_get0_order(group);
203 i = BN_num_bits(order);
205 * Need to truncate digest if it is too long: first truncate whole bytes.
207 if (8 * dgst_len > i)
208 dgst_len = (i + 7) / 8;
209 if (!BN_bin2bn(dgst, dgst_len, m)) {
210 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
213 /* If still too long, truncate remaining bits with a shift */
214 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
215 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
219 if (in_kinv == NULL || in_r == NULL) {
220 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
221 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
227 if (BN_copy(ret->r, in_r) == NULL) {
228 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
234 * With only one multiplicant being in Montgomery domain
235 * multiplication yields real result without post-conversion.
236 * Also note that all operations but last are performed with
237 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
238 * below, returns user-visible value with removed zero padding.
240 if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
241 || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
242 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
245 if (!bn_mod_add_fixed_top(s, s, m, order)) {
246 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
250 * |s| can still be larger than modulus, because |m| can be. In
251 * such case we count on Montgomery reduction to tie it up.
253 if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
254 || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
255 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
261 * if kinv and r have been supplied by the caller, don't
262 * generate new kinv and r values
264 if (in_kinv != NULL && in_r != NULL) {
265 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
269 /* s != 0 => we have a valid signature */
288 * 1: correct signature
289 * 0: incorrect signature
292 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
293 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
296 const unsigned char *p = sigbuf;
297 unsigned char *der = NULL;
304 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
306 /* Ensure signature uses DER and doesn't have trailing garbage */
307 derlen = i2d_ECDSA_SIG(s, &der);
308 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
310 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
312 OPENSSL_clear_free(der, derlen);
317 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
318 const ECDSA_SIG *sig, EC_KEY *eckey)
323 BIGNUM *u1, *u2, *m, *X;
324 EC_POINT *point = NULL;
325 const EC_GROUP *group;
326 const EC_POINT *pub_key;
328 /* check input values */
329 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
330 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
331 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
335 if (!EC_KEY_can_sign(eckey)) {
336 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
340 ctx = BN_CTX_new_ex(eckey->libctx);
342 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
346 u1 = BN_CTX_get(ctx);
347 u2 = BN_CTX_get(ctx);
351 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
355 order = EC_GROUP_get0_order(group);
357 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
361 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
362 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
363 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
364 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
365 ret = 0; /* signature is invalid */
368 /* calculate tmp1 = inv(S) mod order */
369 if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
370 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
374 i = BN_num_bits(order);
376 * Need to truncate digest if it is too long: first truncate whole bytes.
378 if (8 * dgst_len > i)
379 dgst_len = (i + 7) / 8;
380 if (!BN_bin2bn(dgst, dgst_len, m)) {
381 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
384 /* If still too long truncate remaining bits with a shift */
385 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
386 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
389 /* u1 = m * tmp mod order */
390 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
391 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
394 /* u2 = r * w mod q */
395 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
396 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
400 if ((point = EC_POINT_new(group)) == NULL) {
401 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
404 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
405 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
409 if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
410 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
414 if (!BN_nnmod(u1, X, order, ctx)) {
415 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
418 /* if the signature is correct u1 is equal to sig->r */
419 ret = (BN_ucmp(u1, sig->r) == 0);
423 EC_POINT_free(point);