]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecdsa_ossl.c
add block comment
[thirdparty/openssl.git] / crypto / ec / ecdsa_ossl.c
CommitLineData
6a47db45 1/* crypto/ec/ecdsa_ossl.c */
c6700d27
GT
2/*
3 * Written by Nils Larsch for the OpenSSL project
4 */
4d94ae00 5/* ====================================================================
c6700d27 6 * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
4d94ae00
BM
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
11 *
12 * 1. Redistributions of source code must retain the above copyright
0f113f3e 13 * notice, this list of conditions and the following disclaimer.
4d94ae00
BM
14 *
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in
17 * the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * 3. All advertising materials mentioning features or use of this
21 * software must display the following acknowledgment:
22 * "This product includes software developed by the OpenSSL Project
23 * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
24 *
25 * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
26 * endorse or promote products derived from this software without
27 * prior written permission. For written permission, please contact
28 * openssl-core@OpenSSL.org.
29 *
30 * 5. Products derived from this software may not be called "OpenSSL"
31 * nor may "OpenSSL" appear in their names without prior written
32 * permission of the OpenSSL Project.
33 *
34 * 6. Redistributions of any form whatsoever must retain the following
35 * acknowledgment:
36 * "This product includes software developed by the OpenSSL Project
37 * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
38 *
39 * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
40 * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
41 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
42 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
43 * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
45 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
46 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
47 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
48 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
49 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
50 * OF THE POSSIBILITY OF SUCH DAMAGE.
51 * ====================================================================
52 *
53 * This product includes cryptographic software written by Eric Young
54 * (eay@cryptsoft.com). This product includes software written by Tim
55 * Hudson (tjh@cryptsoft.com).
56 *
57 */
0bee0e62 58
a200a817 59#include <string.h>
0bee0e62 60#include <openssl/err.h>
14a7cfb3 61#include <openssl/obj_mac.h>
0f814687 62#include <openssl/bn.h>
8a99cb29 63#include <openssl/rand.h>
6a47db45
DSH
64#include <openssl/ec.h>
65#include "ec_lcl.h"
190c615d 66
a200a817
DSH
67int 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)
70{
71 ECDSA_SIG *s;
72 RAND_seed(dgst, dlen);
73 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
74 if (s == NULL) {
75 *siglen = 0;
76 return 0;
77 }
78 *siglen = i2d_ECDSA_SIG(s, &sig);
79 ECDSA_SIG_free(s);
80 return 1;
81}
82
8d6a75dc 83static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
0f113f3e
MC
84 BIGNUM **kinvp, BIGNUM **rp,
85 const unsigned char *dgst, int dlen)
4d94ae00 86{
0f113f3e
MC
87 BN_CTX *ctx = NULL;
88 BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
89 EC_POINT *tmp_point = NULL;
90 const EC_GROUP *group;
91 int ret = 0;
9dd84053 92
0f113f3e 93 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
6a47db45 94 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
95 return 0;
96 }
a74333f9 97
0f113f3e
MC
98 if (ctx_in == NULL) {
99 if ((ctx = BN_CTX_new()) == NULL) {
6a47db45 100 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
101 return 0;
102 }
103 } else
104 ctx = ctx_in;
4d94ae00 105
0f113f3e
MC
106 k = BN_new(); /* this value is later returned in *kinvp */
107 r = BN_new(); /* this value is later returned in *rp */
108 order = BN_new();
109 X = BN_new();
90945fa3 110 if (k == NULL || r == NULL || order == NULL || X == NULL) {
6a47db45 111 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
112 goto err;
113 }
114 if ((tmp_point = EC_POINT_new(group)) == NULL) {
6a47db45 115 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
116 goto err;
117 }
118 if (!EC_GROUP_get_order(group, order, ctx)) {
6a47db45 119 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
120 goto err;
121 }
cac4fb58 122
0f113f3e
MC
123 do {
124 /* get random k */
125 do
0f113f3e
MC
126 if (dgst != NULL) {
127 if (!BN_generate_dsa_nonce
128 (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
129 ctx)) {
6a47db45
DSH
130 ECerr(EC_F_ECDSA_SIGN_SETUP,
131 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
0f113f3e
MC
132 goto err;
133 }
474e469b 134 } else {
0f113f3e 135 if (!BN_rand_range(k, order)) {
6a47db45
DSH
136 ECerr(EC_F_ECDSA_SIGN_SETUP,
137 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
0f113f3e
MC
138 goto err;
139 }
140 }
141 while (BN_is_zero(k));
4d94ae00 142
0f113f3e
MC
143 /*
144 * We do not want timing information to leak the length of k, so we
145 * compute G*k using an equivalent scalar of fixed bit-length.
146 */
992bdde6 147
0f113f3e
MC
148 if (!BN_add(k, k, order))
149 goto err;
150 if (BN_num_bits(k) <= BN_num_bits(order))
151 if (!BN_add(k, k, order))
152 goto err;
992bdde6 153
0f113f3e
MC
154 /* compute r the x-coordinate of generator * k */
155 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
6a47db45 156 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
157 goto err;
158 }
159 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
160 NID_X9_62_prime_field) {
161 if (!EC_POINT_get_affine_coordinates_GFp
162 (group, tmp_point, X, NULL, ctx)) {
6a47db45 163 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
164 goto err;
165 }
166 }
b3310161 167#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
168 else { /* NID_X9_62_characteristic_two_field */
169
170 if (!EC_POINT_get_affine_coordinates_GF2m(group,
171 tmp_point, X, NULL,
172 ctx)) {
6a47db45 173 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
174 goto err;
175 }
176 }
b3310161 177#endif
0f113f3e 178 if (!BN_nnmod(r, X, order, ctx)) {
6a47db45 179 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
180 goto err;
181 }
182 }
183 while (BN_is_zero(r));
4d94ae00 184
0f113f3e
MC
185 /* compute the inverse of k */
186 if (EC_GROUP_get_mont_data(group) != NULL) {
187 /*
188 * We want inverse in constant time, therefore we utilize the fact
189 * order must be prime and use Fermats Little Theorem instead.
190 */
191 if (!BN_set_word(X, 2)) {
6a47db45 192 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
193 goto err;
194 }
195 if (!BN_mod_sub(X, order, X, order, ctx)) {
6a47db45 196 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
197 goto err;
198 }
199 BN_set_flags(X, BN_FLG_CONSTTIME);
200 if (!BN_mod_exp_mont_consttime
201 (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
6a47db45 202 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
203 goto err;
204 }
205 } else {
206 if (!BN_mod_inverse(k, k, order, ctx)) {
6a47db45 207 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
208 goto err;
209 }
210 }
f54be179 211
0f113f3e 212 /* clear old values if necessary */
23a1d5e9
RS
213 BN_clear_free(*rp);
214 BN_clear_free(*kinvp);
0f113f3e
MC
215 /* save the pre-computed values */
216 *rp = r;
217 *kinvp = k;
218 ret = 1;
219 err:
220 if (!ret) {
23a1d5e9
RS
221 BN_clear_free(k);
222 BN_clear_free(r);
0f113f3e 223 }
23a1d5e9 224 if (ctx != ctx_in)
0f113f3e 225 BN_CTX_free(ctx);
23a1d5e9 226 BN_free(order);
8fdc3734 227 EC_POINT_free(tmp_point);
23a1d5e9 228 BN_clear_free(X);
0f113f3e 229 return (ret);
4d94ae00
BM
230}
231
6a47db45
DSH
232int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
233 BIGNUM **rp)
234{
235 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
236}
237
238ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
239 const BIGNUM *in_kinv, const BIGNUM *in_r,
240 EC_KEY *eckey)
4d94ae00 241{
0f113f3e
MC
242 int ok = 0, i;
243 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
244 const BIGNUM *ckinv;
245 BN_CTX *ctx = NULL;
246 const EC_GROUP *group;
247 ECDSA_SIG *ret;
0f113f3e
MC
248 const BIGNUM *priv_key;
249
0f113f3e
MC
250 group = EC_KEY_get0_group(eckey);
251 priv_key = EC_KEY_get0_private_key(eckey);
14a7cfb3 252
6a47db45
DSH
253 if (group == NULL || priv_key == NULL) {
254 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
255 return NULL;
256 }
4d94ae00 257
0f113f3e 258 ret = ECDSA_SIG_new();
90945fa3 259 if (ret == NULL) {
6a47db45 260 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
261 return NULL;
262 }
263 s = ret->s;
c6700d27 264
0f113f3e
MC
265 if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
266 (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
6a47db45 267 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
268 goto err;
269 }
4d94ae00 270
0f113f3e 271 if (!EC_GROUP_get_order(group, order, ctx)) {
6a47db45 272 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
0f113f3e
MC
273 goto err;
274 }
275 i = BN_num_bits(order);
276 /*
277 * Need to truncate digest if it is too long: first truncate whole bytes.
278 */
279 if (8 * dgst_len > i)
280 dgst_len = (i + 7) / 8;
281 if (!BN_bin2bn(dgst, dgst_len, m)) {
6a47db45 282 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
283 goto err;
284 }
285 /* If still too long truncate remaining bits with a shift */
286 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
6a47db45 287 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
288 goto err;
289 }
290 do {
291 if (in_kinv == NULL || in_r == NULL) {
292 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
6a47db45 293 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
0f113f3e
MC
294 goto err;
295 }
296 ckinv = kinv;
297 } else {
298 ckinv = in_kinv;
299 if (BN_copy(ret->r, in_r) == NULL) {
6a47db45 300 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
301 goto err;
302 }
303 }
4d94ae00 304
0f113f3e 305 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
6a47db45 306 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
307 goto err;
308 }
309 if (!BN_mod_add_quick(s, tmp, m, order)) {
6a47db45 310 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
311 goto err;
312 }
313 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
6a47db45 314 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
315 goto err;
316 }
317 if (BN_is_zero(s)) {
318 /*
319 * if kinv and r have been supplied by the caller don't to
320 * generate new kinv and r values
321 */
322 if (in_kinv != NULL && in_r != NULL) {
6a47db45 323 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
0f113f3e
MC
324 goto err;
325 }
326 } else
327 /* s != 0 => we have a valid signature */
328 break;
329 }
330 while (1);
4d94ae00 331
0f113f3e
MC
332 ok = 1;
333 err:
334 if (!ok) {
335 ECDSA_SIG_free(ret);
336 ret = NULL;
337 }
23a1d5e9
RS
338 BN_CTX_free(ctx);
339 BN_clear_free(m);
340 BN_clear_free(tmp);
341 BN_free(order);
342 BN_clear_free(kinv);
0f113f3e 343 return ret;
4d94ae00
BM
344}
345
a200a817
DSH
346/*-
347 * returns
348 * 1: correct signature
349 * 0: incorrect signature
350 * -1: error
351 */
352int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
353 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
354{
355 ECDSA_SIG *s;
356 const unsigned char *p = sigbuf;
357 unsigned char *der = NULL;
358 int derlen = -1;
359 int ret = -1;
360
361 s = ECDSA_SIG_new();
362 if (s == NULL)
363 return (ret);
364 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
365 goto err;
366 /* Ensure signature uses DER and doesn't have trailing garbage */
367 derlen = i2d_ECDSA_SIG(s, &der);
368 if (derlen != sig_len || memcmp(sigbuf, der, derlen))
369 goto err;
370 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
371 err:
372 OPENSSL_clear_free(der, derlen);
373 ECDSA_SIG_free(s);
374 return (ret);
375}
376
6a47db45
DSH
377int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
378 const ECDSA_SIG *sig, EC_KEY *eckey)
4d94ae00 379{
0f113f3e
MC
380 int ret = -1, i;
381 BN_CTX *ctx;
382 BIGNUM *order, *u1, *u2, *m, *X;
383 EC_POINT *point = NULL;
384 const EC_GROUP *group;
385 const EC_POINT *pub_key;
9dd84053 386
0f113f3e
MC
387 /* check input values */
388 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
389 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
6a47db45 390 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
0f113f3e
MC
391 return -1;
392 }
4d94ae00 393
0f113f3e 394 ctx = BN_CTX_new();
90945fa3 395 if (ctx == NULL) {
6a47db45 396 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
397 return -1;
398 }
399 BN_CTX_start(ctx);
400 order = BN_CTX_get(ctx);
401 u1 = BN_CTX_get(ctx);
402 u2 = BN_CTX_get(ctx);
403 m = BN_CTX_get(ctx);
404 X = BN_CTX_get(ctx);
405 if (!X) {
6a47db45 406 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
407 goto err;
408 }
c6700d27 409
0f113f3e 410 if (!EC_GROUP_get_order(group, order, ctx)) {
6a47db45 411 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
412 goto err;
413 }
4d94ae00 414
0f113f3e
MC
415 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
416 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
417 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
6a47db45 418 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
0f113f3e
MC
419 ret = 0; /* signature is invalid */
420 goto err;
421 }
422 /* calculate tmp1 = inv(S) mod order */
423 if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
6a47db45 424 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
425 goto err;
426 }
427 /* digest -> m */
428 i = BN_num_bits(order);
429 /*
430 * Need to truncate digest if it is too long: first truncate whole bytes.
431 */
432 if (8 * dgst_len > i)
433 dgst_len = (i + 7) / 8;
434 if (!BN_bin2bn(dgst, dgst_len, m)) {
6a47db45 435 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
436 goto err;
437 }
438 /* If still too long truncate remaining bits with a shift */
439 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
6a47db45 440 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
441 goto err;
442 }
443 /* u1 = m * tmp mod order */
444 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
6a47db45 445 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
446 goto err;
447 }
448 /* u2 = r * w mod q */
449 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
6a47db45 450 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
451 goto err;
452 }
453
454 if ((point = EC_POINT_new(group)) == NULL) {
6a47db45 455 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
456 goto err;
457 }
458 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
6a47db45 459 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
460 goto err;
461 }
462 if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
463 NID_X9_62_prime_field) {
464 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
6a47db45 465 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
466 goto err;
467 }
468 }
b3310161 469#ifndef OPENSSL_NO_EC2M
0f113f3e
MC
470 else { /* NID_X9_62_characteristic_two_field */
471
472 if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
6a47db45 473 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
474 goto err;
475 }
476 }
477#endif
478 if (!BN_nnmod(u1, X, order, ctx)) {
6a47db45 479 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
480 goto err;
481 }
482 /* if the signature is correct u1 is equal to sig->r */
483 ret = (BN_ucmp(u1, sig->r) == 0);
484 err:
485 BN_CTX_end(ctx);
486 BN_CTX_free(ctx);
8fdc3734 487 EC_POINT_free(point);
0f113f3e 488 return ret;
4d94ae00 489}