2 * Written by Nils Larsch for the OpenSSL project
4 /* ====================================================================
5 * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in
16 * the documentation and/or other materials provided with the
19 * 3. All advertising materials mentioning features or use of this
20 * software must display the following acknowledgment:
21 * "This product includes software developed by the OpenSSL Project
22 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
25 * endorse or promote products derived from this software without
26 * prior written permission. For written permission, please contact
27 * openssl-core@OpenSSL.org.
29 * 5. Products derived from this software may not be called "OpenSSL"
30 * nor may "OpenSSL" appear in their names without prior written
31 * permission of the OpenSSL Project.
33 * 6. Redistributions of any form whatsoever must retain the following
35 * "This product includes software developed by the OpenSSL Project
36 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
39 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
40 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
41 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
42 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
43 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
44 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
45 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
46 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
47 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
48 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
49 * OF THE POSSIBILITY OF SUCH DAMAGE.
50 * ====================================================================
52 * This product includes cryptographic software written by Eric Young
53 * (eay@cryptsoft.com). This product includes software written by Tim
54 * Hudson (tjh@cryptsoft.com).
59 #include <openssl/err.h>
60 #include <openssl/obj_mac.h>
61 #include <openssl/bn.h>
62 #include <openssl/rand.h>
63 #include <openssl/ec.h>
66 int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
67 unsigned char *sig, unsigned int *siglen,
68 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
71 RAND_seed(dgst, dlen);
72 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
77 *siglen = i2d_ECDSA_SIG(s, &sig);
82 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
83 BIGNUM **kinvp, BIGNUM **rp,
84 const unsigned char *dgst, int dlen)
87 BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
88 EC_POINT *tmp_point = NULL;
89 const EC_GROUP *group;
92 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
93 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
98 if ((ctx = BN_CTX_new()) == NULL) {
99 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
105 k = BN_new(); /* this value is later returned in *kinvp */
106 r = BN_new(); /* this value is later returned in *rp */
109 if (k == NULL || r == NULL || order == NULL || X == NULL) {
110 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
113 if ((tmp_point = EC_POINT_new(group)) == NULL) {
114 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
117 if (!EC_GROUP_get_order(group, order, ctx)) {
118 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
126 if (!BN_generate_dsa_nonce
127 (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
129 ECerr(EC_F_ECDSA_SIGN_SETUP,
130 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
134 if (!BN_rand_range(k, order)) {
135 ECerr(EC_F_ECDSA_SIGN_SETUP,
136 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
140 while (BN_is_zero(k));
143 * We do not want timing information to leak the length of k, so we
144 * compute G*k using an equivalent scalar of fixed bit-length.
147 if (!BN_add(k, k, order))
149 if (BN_num_bits(k) <= BN_num_bits(order))
150 if (!BN_add(k, k, order))
153 /* compute r the x-coordinate of generator * k */
154 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
155 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
158 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
159 NID_X9_62_prime_field) {
160 if (!EC_POINT_get_affine_coordinates_GFp
161 (group, tmp_point, X, NULL, ctx)) {
162 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
166 #ifndef OPENSSL_NO_EC2M
167 else { /* NID_X9_62_characteristic_two_field */
169 if (!EC_POINT_get_affine_coordinates_GF2m(group,
172 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
177 if (!BN_nnmod(r, X, order, ctx)) {
178 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
182 while (BN_is_zero(r));
184 /* compute the inverse of k */
185 if (EC_GROUP_get_mont_data(group) != NULL) {
187 * We want inverse in constant time, therefore we utilize the fact
188 * order must be prime and use Fermats Little Theorem instead.
190 if (!BN_set_word(X, 2)) {
191 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
194 if (!BN_mod_sub(X, order, X, order, ctx)) {
195 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
198 BN_set_flags(X, BN_FLG_CONSTTIME);
199 if (!BN_mod_exp_mont_consttime
200 (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
201 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
205 if (!BN_mod_inverse(k, k, order, ctx)) {
206 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
211 /* clear old values if necessary */
213 BN_clear_free(*kinvp);
214 /* save the pre-computed values */
226 EC_POINT_free(tmp_point);
231 int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
234 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
237 ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
238 const BIGNUM *in_kinv, const BIGNUM *in_r,
242 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
245 const EC_GROUP *group;
247 const BIGNUM *priv_key;
249 group = EC_KEY_get0_group(eckey);
250 priv_key = EC_KEY_get0_private_key(eckey);
252 if (group == NULL || priv_key == NULL) {
253 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
257 ret = ECDSA_SIG_new();
259 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
264 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
265 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
266 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
270 if (!EC_GROUP_get_order(group, order, ctx)) {
271 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
274 i = BN_num_bits(order);
276 * Need to truncate digest if it is too long: first truncate whole bytes.
278 if (8 * dgst_len > i)
279 dgst_len = (i + 7) / 8;
280 if (!BN_bin2bn(dgst, dgst_len, m)) {
281 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
284 /* If still too long truncate remaining bits with a shift */
285 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
286 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
290 if (in_kinv == NULL || in_r == NULL) {
291 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
292 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
298 if (BN_copy(ret->r, in_r) == NULL) {
299 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
304 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
305 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
308 if (!BN_mod_add_quick(s, tmp, m, order)) {
309 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
312 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
313 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
318 * if kinv and r have been supplied by the caller don't to
319 * generate new kinv and r values
321 if (in_kinv != NULL && in_r != NULL) {
322 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
326 /* s != 0 => we have a valid signature */
347 * 1: correct signature
348 * 0: incorrect signature
351 int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
352 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
355 const unsigned char *p = sigbuf;
356 unsigned char *der = NULL;
363 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
365 /* Ensure signature uses DER and doesn't have trailing garbage */
366 derlen = i2d_ECDSA_SIG(s, &der);
367 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
369 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
371 OPENSSL_clear_free(der, derlen);
376 int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
377 const ECDSA_SIG *sig, EC_KEY *eckey)
381 BIGNUM *order, *u1, *u2, *m, *X;
382 EC_POINT *point = NULL;
383 const EC_GROUP *group;
384 const EC_POINT *pub_key;
386 /* check input values */
387 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
388 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
389 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
395 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
399 order = BN_CTX_get(ctx);
400 u1 = BN_CTX_get(ctx);
401 u2 = BN_CTX_get(ctx);
405 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
409 if (!EC_GROUP_get_order(group, order, ctx)) {
410 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
414 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
415 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
416 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
417 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
418 ret = 0; /* signature is invalid */
421 /* calculate tmp1 = inv(S) mod order */
422 if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
423 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
427 i = BN_num_bits(order);
429 * Need to truncate digest if it is too long: first truncate whole bytes.
431 if (8 * dgst_len > i)
432 dgst_len = (i + 7) / 8;
433 if (!BN_bin2bn(dgst, dgst_len, m)) {
434 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
437 /* If still too long truncate remaining bits with a shift */
438 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
439 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
442 /* u1 = m * tmp mod order */
443 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
444 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
447 /* u2 = r * w mod q */
448 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
449 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
453 if ((point = EC_POINT_new(group)) == NULL) {
454 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
457 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
458 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
461 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
462 NID_X9_62_prime_field) {
463 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
464 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
468 #ifndef OPENSSL_NO_EC2M
469 else { /* NID_X9_62_characteristic_two_field */
471 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
472 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
477 if (!BN_nnmod(u1, X, order, ctx)) {
478 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
481 /* if the signature is correct u1 is equal to sig->r */
482 ret = (BN_ucmp(u1, sig->r) == 0);
486 EC_POINT_free(point);