1 /* crypto/ecdsa/ecs_ossl.c */
2 /* ====================================================================
3 * Copyright (c) 1998-2002 The OpenSSL Project. All rights reserved.
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
9 * 1. Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in
14 * the documentation and/or other materials provided with the
17 * 3. All advertising materials mentioning features or use of this
18 * software must display the following acknowledgment:
19 * "This product includes software developed by the OpenSSL Project
20 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
22 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
23 * endorse or promote products derived from this software without
24 * prior written permission. For written permission, please contact
25 * openssl-core@OpenSSL.org.
27 * 5. Products derived from this software may not be called "OpenSSL"
28 * nor may "OpenSSL" appear in their names without prior written
29 * permission of the OpenSSL Project.
31 * 6. Redistributions of any form whatsoever must retain the following
33 * "This product includes software developed by the OpenSSL Project
34 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
36 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
37 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
38 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
39 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
40 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
41 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
42 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
43 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
45 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
46 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
47 * OF THE POSSIBILITY OF SUCH DAMAGE.
48 * ====================================================================
50 * This product includes cryptographic software written by Eric Young
51 * (eay@cryptsoft.com). This product includes software written by Tim
52 * Hudson (tjh@cryptsoft.com).
57 #include <openssl/err.h>
58 #include <openssl/obj_mac.h>
60 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
62 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
64 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
65 ECDSA_SIG *sig, EC_KEY *eckey);
67 static ECDSA_METHOD openssl_ecdsa_meth = {
68 "OpenSSL ECDSA method",
80 const ECDSA_METHOD *ECDSA_OpenSSL(void)
82 return &openssl_ecdsa_meth;
85 static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
89 BIGNUM k,*kinv=NULL,*r=NULL,*order=NULL,*X=NULL;
90 EC_POINT *tmp_point=NULL;
92 if (!eckey || !eckey->group || !eckey->pub_key || !eckey->priv_key)
94 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
99 if ((ctx=BN_CTX_new()) == NULL)
101 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
108 if ((r = BN_new()) == NULL)
110 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
113 if ((order = BN_new()) == NULL)
115 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
118 if ((X = BN_new()) == NULL)
120 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
123 if ((tmp_point = EC_POINT_new(eckey->group)) == NULL)
125 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
128 if (!EC_GROUP_get_order(eckey->group,order,ctx))
130 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
139 if (!BN_rand_range(&k,order))
141 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
142 ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
145 while (BN_is_zero(&k));
147 /* compute r the x-coordinate of generator * k */
148 if (!EC_POINT_mul(eckey->group, tmp_point, &k, NULL, NULL, ctx))
150 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
153 if (EC_METHOD_get_field_type(EC_GROUP_method_of(eckey->group))
154 == NID_X9_62_prime_field)
156 if (!EC_POINT_get_affine_coordinates_GFp(eckey->group,
157 tmp_point, X, NULL, ctx))
159 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
164 else /* NID_X9_62_characteristic_two_field */
166 if (!EC_POINT_get_affine_coordinates_GF2m(eckey->group,
167 tmp_point, X, NULL, ctx))
169 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
174 if (!BN_nnmod(r,X,order,ctx))
176 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
180 while (BN_is_zero(r));
182 /* compute the inverse of k */
183 if ((kinv = BN_mod_inverse(NULL,&k,order,ctx)) == NULL)
185 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
193 BN_clear_free(*kinvp);
200 if (kinv != NULL) BN_clear_free(kinv);
201 if (r != NULL) BN_clear_free(r);
208 BN_clear_free(order);
209 if (tmp_point != NULL)
210 EC_POINT_free(tmp_point);
211 if (X) BN_clear_free(X);
217 static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
220 BIGNUM *kinv=NULL,*r=NULL,*s=NULL,*m=NULL,*tmp=NULL,*order=NULL;
226 ecdsa = ecdsa_check(eckey);
228 if (!eckey || !eckey->group || !eckey->pub_key || !eckey->priv_key
231 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
236 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
237 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL ||
238 (s = BN_new()) == NULL )
240 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
244 if (!EC_GROUP_get_order(eckey->group,order,ctx))
246 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
249 if (dgst_len > BN_num_bytes(order))
251 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
252 ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
256 if (BN_bin2bn(dgst,dgst_len,m) == NULL)
258 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
263 if (ecdsa->kinv == NULL || ecdsa->r == NULL)
265 if (!ECDSA_sign_setup(eckey,ctx,&kinv,&r))
267 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
280 if (!BN_mod_mul(tmp,eckey->priv_key,r,order,ctx))
282 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
285 if (!BN_add(s,tmp,m))
287 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
290 if (BN_cmp(s,order) > 0)
292 if (!BN_mod_mul(s,s,kinv,order,ctx))
294 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
298 while (BN_is_zero(s));
300 if ((ret = ECDSA_SIG_new()) == NULL)
302 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
305 if (BN_copy(ret->r, r) == NULL || BN_copy(ret->s, s) == NULL)
309 ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
324 BN_clear_free(order);
330 static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
331 ECDSA_SIG *sig, EC_KEY *eckey)
334 BIGNUM *order=NULL,*u1=NULL,*u2=NULL,*m=NULL,*X=NULL;
335 EC_POINT *point=NULL;
337 if (!eckey || !eckey->group || !eckey->pub_key || !sig)
339 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
343 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
344 (u1 = BN_new()) == NULL || (u2 = BN_new()) == NULL ||
345 (m = BN_new()) == NULL || (X = BN_new()) == NULL)
347 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
350 if (!EC_GROUP_get_order(eckey->group, order, ctx))
352 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
356 if (BN_is_zero(sig->r) || sig->r->neg || BN_ucmp(sig->r, order) >= 0)
358 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
362 if (BN_is_zero(sig->s) || sig->s->neg || BN_ucmp(sig->s, order) >= 0)
364 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
369 /* calculate tmp1 = inv(S) mod order */
370 if ((BN_mod_inverse(u2,sig->s,order,ctx)) == NULL)
372 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
376 if (BN_bin2bn(dgst,dgst_len,m) == NULL)
378 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
381 /* u1 = m * tmp mod order */
382 if (!BN_mod_mul(u1,m,u2,order,ctx))
384 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
387 /* u2 = r * w mod q */
388 if (!BN_mod_mul(u2,sig->r,u2,order,ctx))
390 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
394 if ((point = EC_POINT_new(eckey->group)) == NULL)
396 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
399 if (!EC_POINT_mul(eckey->group, point, u1, eckey->pub_key, u2, ctx))
401 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
404 if (EC_METHOD_get_field_type(EC_GROUP_method_of(eckey->group))
405 == NID_X9_62_prime_field)
407 if (!EC_POINT_get_affine_coordinates_GFp(eckey->group,
408 point, X, NULL, ctx))
410 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
414 else /* NID_X9_62_characteristic_two_field */
416 if (!EC_POINT_get_affine_coordinates_GF2m(eckey->group,
417 point, X, NULL, ctx))
419 ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
424 if (!BN_nnmod(u1,X,order,ctx))
426 ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
430 /* is now in u1. If the signature is correct, it will be
432 ret = (BN_ucmp(u1,sig->r) == 0);
446 BN_clear_free(order);
448 EC_POINT_free(point);