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));
108 /* compute r the x-coordinate of generator * k */
109 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
110 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
113 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
114 NID_X9_62_prime_field) {
115 if (!EC_POINT_get_affine_coordinates_GFp
116 (group, tmp_point, X, NULL, ctx)) {
117 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
121 #ifndef OPENSSL_NO_EC2M
122 else { /* NID_X9_62_characteristic_two_field */
124 if (!EC_POINT_get_affine_coordinates_GF2m(group,
127 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
132 if (!BN_nnmod(r, X, order, ctx)) {
133 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
137 while (BN_is_zero(r));
139 /* compute the inverse of k */
140 if (!ec_group_do_inverse_ord(group, k, k, ctx)) {
141 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
145 /* clear old values if necessary */
147 BN_clear_free(*kinvp);
148 /* save the pre-computed values */
159 EC_POINT_free(tmp_point);
164 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
167 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
170 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
171 const BIGNUM *in_kinv, const BIGNUM *in_r,
175 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
176 const BIGNUM *order, *ckinv;
178 const EC_GROUP *group;
180 const BIGNUM *priv_key;
182 group = EC_KEY_get0_group(eckey);
183 priv_key = EC_KEY_get0_private_key(eckey);
185 if (group == NULL || priv_key == NULL) {
186 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
190 if (!EC_KEY_can_sign(eckey)) {
191 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
195 ret = ECDSA_SIG_new();
197 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
202 if (ret->r == NULL || ret->s == NULL) {
203 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
208 if ((ctx = BN_CTX_new()) == NULL ||
209 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
210 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
214 order = EC_GROUP_get0_order(group);
216 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
219 i = BN_num_bits(order);
221 * Need to truncate digest if it is too long: first truncate whole bytes.
223 if (8 * dgst_len > i)
224 dgst_len = (i + 7) / 8;
225 if (!BN_bin2bn(dgst, dgst_len, m)) {
226 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
229 /* If still too long truncate remaining bits with a shift */
230 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
231 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
235 if (in_kinv == NULL || in_r == NULL) {
236 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
237 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
243 if (BN_copy(ret->r, in_r) == NULL) {
244 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
249 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
250 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
253 if (!BN_mod_add_quick(s, tmp, m, order)) {
254 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
257 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
258 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
263 * if kinv and r have been supplied by the caller, don't
264 * generate new kinv and r values
266 if (in_kinv != NULL && in_r != NULL) {
267 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
271 /* s != 0 => we have a valid signature */
291 * 1: correct signature
292 * 0: incorrect signature
295 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
296 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
299 const unsigned char *p = sigbuf;
300 unsigned char *der = NULL;
307 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
309 /* Ensure signature uses DER and doesn't have trailing garbage */
310 derlen = i2d_ECDSA_SIG(s, &der);
311 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
313 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
315 OPENSSL_clear_free(der, derlen);
320 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
321 const ECDSA_SIG *sig, EC_KEY *eckey)
326 BIGNUM *u1, *u2, *m, *X;
327 EC_POINT *point = NULL;
328 const EC_GROUP *group;
329 const EC_POINT *pub_key;
331 /* check input values */
332 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
333 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
334 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
338 if (!EC_KEY_can_sign(eckey)) {
339 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
345 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
349 u1 = BN_CTX_get(ctx);
350 u2 = BN_CTX_get(ctx);
354 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
358 order = EC_GROUP_get0_order(group);
360 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
364 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
365 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
366 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
367 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
368 ret = 0; /* signature is invalid */
371 /* calculate tmp1 = inv(S) mod order */
372 if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
373 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
377 i = BN_num_bits(order);
379 * Need to truncate digest if it is too long: first truncate whole bytes.
381 if (8 * dgst_len > i)
382 dgst_len = (i + 7) / 8;
383 if (!BN_bin2bn(dgst, dgst_len, m)) {
384 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
387 /* If still too long truncate remaining bits with a shift */
388 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
389 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
392 /* u1 = m * tmp mod order */
393 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
394 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
397 /* u2 = r * w mod q */
398 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
399 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
403 if ((point = EC_POINT_new(group)) == NULL) {
404 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
407 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
408 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
411 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
412 NID_X9_62_prime_field) {
413 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
414 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
418 #ifndef OPENSSL_NO_EC2M
419 else { /* NID_X9_62_characteristic_two_field */
421 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
422 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
427 if (!BN_nnmod(u1, X, order, ctx)) {
428 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
431 /* if the signature is correct u1 is equal to sig->r */
432 ret = (BN_ucmp(u1, sig->r) == 0);
436 EC_POINT_free(point);