]> git.ipfire.org Git - thirdparty/openssl.git/blame - crypto/ec/ecdsa_ossl.c
Add info on the CMP implementation and HTTP client to NEWS.md and CHANGES.md
[thirdparty/openssl.git] / crypto / ec / ecdsa_ossl.c
CommitLineData
c6700d27 1/*
3c7d0945 2 * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
4d94ae00 3 *
a7f182b7 4 * Licensed under the Apache License 2.0 (the "License"). You may not use
aa6bb135
RS
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
4d94ae00 8 */
0bee0e62 9
579422c8
P
10/*
11 * ECDSA low level APIs are deprecated for public use, but still ok for
12 * internal use.
13 */
14#include "internal/deprecated.h"
15
a200a817 16#include <string.h>
0bee0e62 17#include <openssl/err.h>
14a7cfb3 18#include <openssl/obj_mac.h>
8a99cb29 19#include <openssl/rand.h>
25f2138b 20#include "crypto/bn.h"
706457b7 21#include "ec_local.h"
190c615d 22
9bf682f6
PS
23int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
24 BIGNUM **rp)
25{
26 if (eckey->group->meth->ecdsa_sign_setup == NULL) {
27 ECerr(EC_F_OSSL_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
28 return 0;
29 }
30
31 return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
32}
33
34ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
35 const BIGNUM *in_kinv, const BIGNUM *in_r,
36 EC_KEY *eckey)
37{
38 if (eckey->group->meth->ecdsa_sign_sig == NULL) {
39 ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
40 return NULL;
41 }
42
43 return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
44 in_kinv, in_r, eckey);
45}
46
47int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
48 const ECDSA_SIG *sig, EC_KEY *eckey)
49{
50 if (eckey->group->meth->ecdsa_verify_sig == NULL) {
51 ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
52 return 0;
53 }
54
55 return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
56}
57
a200a817
DSH
58int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
59 unsigned char *sig, unsigned int *siglen,
60 const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
61{
62 ECDSA_SIG *s;
75e2c877 63
a200a817
DSH
64 s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
65 if (s == NULL) {
66 *siglen = 0;
67 return 0;
68 }
69 *siglen = i2d_ECDSA_SIG(s, &sig);
70 ECDSA_SIG_free(s);
71 return 1;
72}
73
8d6a75dc 74static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
0f113f3e
MC
75 BIGNUM **kinvp, BIGNUM **rp,
76 const unsigned char *dgst, int dlen)
4d94ae00 77{
0f113f3e 78 BN_CTX *ctx = NULL;
be2e334f
DSH
79 BIGNUM *k = NULL, *r = NULL, *X = NULL;
80 const BIGNUM *order;
0f113f3e
MC
81 EC_POINT *tmp_point = NULL;
82 const EC_GROUP *group;
83 int ret = 0;
4a089bbd 84 int order_bits;
7408f675 85 const BIGNUM *priv_key;
9dd84053 86
0f113f3e 87 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
6a47db45 88 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
89 return 0;
90 }
7408f675
DO
91 if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
92 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_MISSING_PRIVATE_KEY);
93 return 0;
94 }
a74333f9 95
4b0555ec
DSH
96 if (!EC_KEY_can_sign(eckey)) {
97 ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
98 return 0;
99 }
100
fff7a0dc 101 if ((ctx = ctx_in) == NULL) {
a9612d6c 102 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
6a47db45 103 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
104 return 0;
105 }
fff7a0dc 106 }
4d94ae00 107
bb315ca7 108 k = BN_secure_new(); /* this value is later returned in *kinvp */
0f113f3e 109 r = BN_new(); /* this value is later returned in *rp */
0f113f3e 110 X = BN_new();
be2e334f 111 if (k == NULL || r == NULL || X == NULL) {
6a47db45 112 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
113 goto err;
114 }
115 if ((tmp_point = EC_POINT_new(group)) == NULL) {
6a47db45 116 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
117 goto err;
118 }
be2e334f 119 order = EC_GROUP_get0_order(group);
cac4fb58 120
4a089bbd
P
121 /* Preallocate space */
122 order_bits = BN_num_bits(order);
123 if (!BN_set_bit(k, order_bits)
124 || !BN_set_bit(r, order_bits)
125 || !BN_set_bit(X, order_bits))
126 goto err;
127
0f113f3e
MC
128 do {
129 /* get random k */
fff7a0dc 130 do {
0f113f3e 131 if (dgst != NULL) {
7408f675 132 if (!BN_generate_dsa_nonce(k, order, priv_key,
fff7a0dc 133 dgst, dlen, ctx)) {
6a47db45 134 ECerr(EC_F_ECDSA_SIGN_SETUP,
fff7a0dc 135 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
0f113f3e
MC
136 goto err;
137 }
474e469b 138 } else {
a9612d6c 139 if (!BN_priv_rand_range_ex(k, order, ctx)) {
6a47db45 140 ECerr(EC_F_ECDSA_SIGN_SETUP,
fff7a0dc 141 EC_R_RANDOM_NUMBER_GENERATION_FAILED);
0f113f3e
MC
142 goto err;
143 }
144 }
fff7a0dc 145 } while (BN_is_zero(k));
4d94ae00 146
0f113f3e
MC
147 /* compute r the x-coordinate of generator * k */
148 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
6a47db45 149 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
0f113f3e
MC
150 goto err;
151 }
9cc570d4
MC
152
153 if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
154 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
155 goto err;
0f113f3e 156 }
9cc570d4 157
0f113f3e 158 if (!BN_nnmod(r, X, order, ctx)) {
6a47db45 159 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
0f113f3e
MC
160 goto err;
161 }
fff7a0dc 162 } while (BN_is_zero(r));
4d94ae00 163
c11d372b 164 /* compute the inverse of k */
792546eb 165 if (!ec_group_do_inverse_ord(group, k, k, ctx)) {
c11d372b
BB
166 ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
167 goto err;
0f113f3e 168 }
f54be179 169
0f113f3e 170 /* clear old values if necessary */
23a1d5e9
RS
171 BN_clear_free(*rp);
172 BN_clear_free(*kinvp);
0f113f3e
MC
173 /* save the pre-computed values */
174 *rp = r;
175 *kinvp = k;
176 ret = 1;
177 err:
178 if (!ret) {
23a1d5e9
RS
179 BN_clear_free(k);
180 BN_clear_free(r);
0f113f3e 181 }
23a1d5e9 182 if (ctx != ctx_in)
0f113f3e 183 BN_CTX_free(ctx);
8fdc3734 184 EC_POINT_free(tmp_point);
23a1d5e9 185 BN_clear_free(X);
26a7d938 186 return ret;
4d94ae00
BM
187}
188
9bf682f6
PS
189int ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
190 BIGNUM **rp)
6a47db45
DSH
191{
192 return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
193}
194
9bf682f6
PS
195ECDSA_SIG *ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
196 const BIGNUM *in_kinv, const BIGNUM *in_r,
197 EC_KEY *eckey)
4d94ae00 198{
0f113f3e 199 int ok = 0, i;
37132c97 200 BIGNUM *kinv = NULL, *s, *m = NULL;
be2e334f 201 const BIGNUM *order, *ckinv;
0f113f3e
MC
202 BN_CTX *ctx = NULL;
203 const EC_GROUP *group;
204 ECDSA_SIG *ret;
0f113f3e
MC
205 const BIGNUM *priv_key;
206
0f113f3e
MC
207 group = EC_KEY_get0_group(eckey);
208 priv_key = EC_KEY_get0_private_key(eckey);
14a7cfb3 209
7408f675 210 if (group == NULL) {
9bf682f6 211 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
0f113f3e
MC
212 return NULL;
213 }
7408f675 214 if (priv_key == NULL) {
9bf682f6 215 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, EC_R_MISSING_PRIVATE_KEY);
7408f675
DO
216 return NULL;
217 }
4d94ae00 218
4b0555ec 219 if (!EC_KEY_can_sign(eckey)) {
9bf682f6 220 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
4b0555ec
DSH
221 return NULL;
222 }
223
0f113f3e 224 ret = ECDSA_SIG_new();
90945fa3 225 if (ret == NULL) {
9bf682f6 226 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
227 return NULL;
228 }
8cc44d97
DSH
229 ret->r = BN_new();
230 ret->s = BN_new();
231 if (ret->r == NULL || ret->s == NULL) {
9bf682f6 232 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_MALLOC_FAILURE);
8cc44d97
DSH
233 goto err;
234 }
0f113f3e 235 s = ret->s;
c6700d27 236
a9612d6c 237 if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
37132c97 238 || (m = BN_new()) == NULL) {
9bf682f6 239 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
240 goto err;
241 }
4d94ae00 242
be2e334f 243 order = EC_GROUP_get0_order(group);
0f113f3e
MC
244 i = BN_num_bits(order);
245 /*
246 * Need to truncate digest if it is too long: first truncate whole bytes.
247 */
248 if (8 * dgst_len > i)
249 dgst_len = (i + 7) / 8;
250 if (!BN_bin2bn(dgst, dgst_len, m)) {
9bf682f6 251 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
252 goto err;
253 }
fff7a0dc 254 /* If still too long, truncate remaining bits with a shift */
0f113f3e 255 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
9bf682f6 256 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
257 goto err;
258 }
259 do {
260 if (in_kinv == NULL || in_r == NULL) {
261 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
9bf682f6 262 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_ECDSA_LIB);
0f113f3e
MC
263 goto err;
264 }
265 ckinv = kinv;
266 } else {
267 ckinv = in_kinv;
268 if (BN_copy(ret->r, in_r) == NULL) {
9bf682f6 269 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
270 goto err;
271 }
272 }
4d94ae00 273
37132c97
AP
274 /*
275 * With only one multiplicant being in Montgomery domain
276 * multiplication yields real result without post-conversion.
277 * Also note that all operations but last are performed with
278 * zero-padded vectors. Last operation, BN_mod_mul_montgomery
279 * below, returns user-visible value with removed zero padding.
280 */
281 if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
282 || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
9bf682f6 283 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
a3e9d5aa
MC
284 goto err;
285 }
37132c97 286 if (!bn_mod_add_fixed_top(s, s, m, order)) {
9bf682f6 287 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
0f113f3e
MC
288 goto err;
289 }
37132c97
AP
290 /*
291 * |s| can still be larger than modulus, because |m| can be. In
292 * such case we count on Montgomery reduction to tie it up.
293 */
294 if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
295 || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
9bf682f6 296 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, ERR_R_BN_LIB);
a3e9d5aa
MC
297 goto err;
298 }
37132c97 299
0f113f3e
MC
300 if (BN_is_zero(s)) {
301 /*
532b1183 302 * if kinv and r have been supplied by the caller, don't
0f113f3e
MC
303 * generate new kinv and r values
304 */
305 if (in_kinv != NULL && in_r != NULL) {
9bf682f6 306 ECerr(EC_F_ECDSA_SIMPLE_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
0f113f3e
MC
307 goto err;
308 }
fff7a0dc 309 } else {
0f113f3e
MC
310 /* s != 0 => we have a valid signature */
311 break;
fff7a0dc
AP
312 }
313 } while (1);
4d94ae00 314
0f113f3e
MC
315 ok = 1;
316 err:
317 if (!ok) {
318 ECDSA_SIG_free(ret);
319 ret = NULL;
320 }
23a1d5e9 321 BN_CTX_free(ctx);
3fc7a9b9 322 BN_clear_free(m);
23a1d5e9 323 BN_clear_free(kinv);
0f113f3e 324 return ret;
4d94ae00
BM
325}
326
a200a817
DSH
327/*-
328 * returns
329 * 1: correct signature
330 * 0: incorrect signature
331 * -1: error
332 */
333int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
334 const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
335{
336 ECDSA_SIG *s;
337 const unsigned char *p = sigbuf;
338 unsigned char *der = NULL;
339 int derlen = -1;
340 int ret = -1;
341
342 s = ECDSA_SIG_new();
343 if (s == NULL)
26a7d938 344 return ret;
a200a817
DSH
345 if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
346 goto err;
347 /* Ensure signature uses DER and doesn't have trailing garbage */
348 derlen = i2d_ECDSA_SIG(s, &der);
91e7bcc2 349 if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
a200a817
DSH
350 goto err;
351 ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
352 err:
cff7d199 353 OPENSSL_free(der);
a200a817 354 ECDSA_SIG_free(s);
26a7d938 355 return ret;
a200a817
DSH
356}
357
9bf682f6
PS
358int ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
359 const ECDSA_SIG *sig, EC_KEY *eckey)
4d94ae00 360{
0f113f3e
MC
361 int ret = -1, i;
362 BN_CTX *ctx;
be2e334f
DSH
363 const BIGNUM *order;
364 BIGNUM *u1, *u2, *m, *X;
0f113f3e
MC
365 EC_POINT *point = NULL;
366 const EC_GROUP *group;
367 const EC_POINT *pub_key;
9dd84053 368
0f113f3e
MC
369 /* check input values */
370 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
371 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
9bf682f6 372 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
0f113f3e
MC
373 return -1;
374 }
4d94ae00 375
4b0555ec 376 if (!EC_KEY_can_sign(eckey)) {
9bf682f6 377 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
4b0555ec
DSH
378 return -1;
379 }
380
a9612d6c 381 ctx = BN_CTX_new_ex(eckey->libctx);
90945fa3 382 if (ctx == NULL) {
9bf682f6 383 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
384 return -1;
385 }
386 BN_CTX_start(ctx);
0f113f3e
MC
387 u1 = BN_CTX_get(ctx);
388 u2 = BN_CTX_get(ctx);
389 m = BN_CTX_get(ctx);
390 X = BN_CTX_get(ctx);
91e7bcc2 391 if (X == NULL) {
9bf682f6 392 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
393 goto err;
394 }
c6700d27 395
be2e334f
DSH
396 order = EC_GROUP_get0_order(group);
397 if (order == NULL) {
9bf682f6 398 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
399 goto err;
400 }
4d94ae00 401
0f113f3e
MC
402 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
403 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
404 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
9bf682f6 405 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, EC_R_BAD_SIGNATURE);
0f113f3e
MC
406 ret = 0; /* signature is invalid */
407 goto err;
408 }
409 /* calculate tmp1 = inv(S) mod order */
792546eb 410 if (!ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
9bf682f6 411 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
c11d372b 412 goto err;
0f113f3e
MC
413 }
414 /* digest -> m */
415 i = BN_num_bits(order);
416 /*
417 * Need to truncate digest if it is too long: first truncate whole bytes.
418 */
419 if (8 * dgst_len > i)
420 dgst_len = (i + 7) / 8;
421 if (!BN_bin2bn(dgst, dgst_len, m)) {
9bf682f6 422 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
423 goto err;
424 }
425 /* If still too long truncate remaining bits with a shift */
426 if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
9bf682f6 427 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
428 goto err;
429 }
430 /* u1 = m * tmp mod order */
431 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
9bf682f6 432 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
433 goto err;
434 }
435 /* u2 = r * w mod q */
436 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
9bf682f6 437 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
438 goto err;
439 }
440
441 if ((point = EC_POINT_new(group)) == NULL) {
9bf682f6 442 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
0f113f3e
MC
443 goto err;
444 }
445 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
9bf682f6 446 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_EC_LIB);
0f113f3e
MC
447 goto err;
448 }
0f113f3e 449
9cc570d4 450 if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
9bf682f6 451 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_EC_LIB);
9cc570d4 452 goto err;
0f113f3e 453 }
9cc570d4 454
0f113f3e 455 if (!BN_nnmod(u1, X, order, ctx)) {
9bf682f6 456 ECerr(EC_F_ECDSA_SIMPLE_VERIFY_SIG, ERR_R_BN_LIB);
0f113f3e
MC
457 goto err;
458 }
459 /* if the signature is correct u1 is equal to sig->r */
460 ret = (BN_ucmp(u1, sig->r) == 0);
461 err:
462 BN_CTX_end(ctx);
463 BN_CTX_free(ctx);
8fdc3734 464 EC_POINT_free(point);
0f113f3e 465 return ret;
4d94ae00 466}