1*1c55417bStb /* $OpenBSD: ecdsa.c,v 1.10 2023/07/05 17:10:10 tb Exp $ */ 2b4a65d29Stb /* ==================================================================== 3b4a65d29Stb * Copyright (c) 2000-2002 The OpenSSL Project. All rights reserved. 4b4a65d29Stb * 5b4a65d29Stb * Redistribution and use in source and binary forms, with or without 6b4a65d29Stb * modification, are permitted provided that the following conditions 7b4a65d29Stb * are met: 8b4a65d29Stb * 9b4a65d29Stb * 1. Redistributions of source code must retain the above copyright 10b4a65d29Stb * notice, this list of conditions and the following disclaimer. 11b4a65d29Stb * 12b4a65d29Stb * 2. Redistributions in binary form must reproduce the above copyright 13b4a65d29Stb * notice, this list of conditions and the following disclaimer in 14b4a65d29Stb * the documentation and/or other materials provided with the 15b4a65d29Stb * distribution. 16b4a65d29Stb * 17b4a65d29Stb * 3. All advertising materials mentioning features or use of this 18b4a65d29Stb * software must display the following acknowledgment: 19b4a65d29Stb * "This product includes software developed by the OpenSSL Project 20b4a65d29Stb * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" 21b4a65d29Stb * 22b4a65d29Stb * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to 23b4a65d29Stb * endorse or promote products derived from this software without 24b4a65d29Stb * prior written permission. For written permission, please contact 25b4a65d29Stb * licensing@OpenSSL.org. 26b4a65d29Stb * 27b4a65d29Stb * 5. Products derived from this software may not be called "OpenSSL" 28b4a65d29Stb * nor may "OpenSSL" appear in their names without prior written 29b4a65d29Stb * permission of the OpenSSL Project. 30b4a65d29Stb * 31b4a65d29Stb * 6. Redistributions of any form whatsoever must retain the following 32b4a65d29Stb * acknowledgment: 33b4a65d29Stb * "This product includes software developed by the OpenSSL Project 34b4a65d29Stb * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" 35b4a65d29Stb * 36b4a65d29Stb * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY 37b4a65d29Stb * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 38b4a65d29Stb * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 39b4a65d29Stb * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR 40b4a65d29Stb * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 41b4a65d29Stb * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 42b4a65d29Stb * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; 43b4a65d29Stb * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 44b4a65d29Stb * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, 45b4a65d29Stb * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 46b4a65d29Stb * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED 47b4a65d29Stb * OF THE POSSIBILITY OF SUCH DAMAGE. 48b4a65d29Stb * ==================================================================== 49b4a65d29Stb * 50b4a65d29Stb * This product includes cryptographic software written by Eric Young 51b4a65d29Stb * (eay@cryptsoft.com). This product includes software written by Tim 52b4a65d29Stb * Hudson (tjh@cryptsoft.com). 53b4a65d29Stb * 54b4a65d29Stb */ 55b4a65d29Stb 565e179de8Stb #include <stddef.h> 575e179de8Stb #include <stdlib.h> 58b4a65d29Stb #include <string.h> 59b4a65d29Stb 60b4a65d29Stb #include <openssl/opensslconf.h> 61b4a65d29Stb 625e179de8Stb #include <openssl/asn1.h> 63b4a65d29Stb #include <openssl/asn1t.h> 64b4a65d29Stb #include <openssl/bn.h> 655e179de8Stb #include <openssl/ecdsa.h> 665e179de8Stb #include <openssl/ec.h> 67b4a65d29Stb #include <openssl/err.h> 68b4a65d29Stb #include <openssl/evp.h> 69b4a65d29Stb 70b4a65d29Stb #include "bn_local.h" 71b4a65d29Stb #include "ec_local.h" 72b4a65d29Stb #include "ecdsa_local.h" 73b4a65d29Stb 74b4a65d29Stb static const ASN1_TEMPLATE ECDSA_SIG_seq_tt[] = { 75b4a65d29Stb { 76b4a65d29Stb .flags = 0, 77b4a65d29Stb .tag = 0, 78b4a65d29Stb .offset = offsetof(ECDSA_SIG, r), 79b4a65d29Stb .field_name = "r", 80b4a65d29Stb .item = &BIGNUM_it, 81b4a65d29Stb }, 82b4a65d29Stb { 83b4a65d29Stb .flags = 0, 84b4a65d29Stb .tag = 0, 85b4a65d29Stb .offset = offsetof(ECDSA_SIG, s), 86b4a65d29Stb .field_name = "s", 87b4a65d29Stb .item = &BIGNUM_it, 88b4a65d29Stb }, 89b4a65d29Stb }; 90b4a65d29Stb 91b4a65d29Stb const ASN1_ITEM ECDSA_SIG_it = { 92b4a65d29Stb .itype = ASN1_ITYPE_SEQUENCE, 93b4a65d29Stb .utype = V_ASN1_SEQUENCE, 94b4a65d29Stb .templates = ECDSA_SIG_seq_tt, 95b4a65d29Stb .tcount = sizeof(ECDSA_SIG_seq_tt) / sizeof(ASN1_TEMPLATE), 96b4a65d29Stb .funcs = NULL, 97b4a65d29Stb .size = sizeof(ECDSA_SIG), 98b4a65d29Stb .sname = "ECDSA_SIG", 99b4a65d29Stb }; 100b4a65d29Stb 101b4a65d29Stb ECDSA_SIG * 102b4a65d29Stb d2i_ECDSA_SIG(ECDSA_SIG **a, const unsigned char **in, long len) 103b4a65d29Stb { 104b4a65d29Stb return (ECDSA_SIG *)ASN1_item_d2i((ASN1_VALUE **)a, in, len, 105b4a65d29Stb &ECDSA_SIG_it); 106b4a65d29Stb } 107b4a65d29Stb 108b4a65d29Stb int 109b4a65d29Stb i2d_ECDSA_SIG(const ECDSA_SIG *a, unsigned char **out) 110b4a65d29Stb { 111b4a65d29Stb return ASN1_item_i2d((ASN1_VALUE *)a, out, &ECDSA_SIG_it); 112b4a65d29Stb } 113b4a65d29Stb 114b4a65d29Stb ECDSA_SIG * 115b4a65d29Stb ECDSA_SIG_new(void) 116b4a65d29Stb { 117b4a65d29Stb return (ECDSA_SIG *)ASN1_item_new(&ECDSA_SIG_it); 118b4a65d29Stb } 119b4a65d29Stb 120b4a65d29Stb void 121b4a65d29Stb ECDSA_SIG_free(ECDSA_SIG *a) 122b4a65d29Stb { 123b4a65d29Stb ASN1_item_free((ASN1_VALUE *)a, &ECDSA_SIG_it); 124b4a65d29Stb } 125b4a65d29Stb 126b4a65d29Stb void 127b4a65d29Stb ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **pr, const BIGNUM **ps) 128b4a65d29Stb { 129b4a65d29Stb if (pr != NULL) 130b4a65d29Stb *pr = sig->r; 131b4a65d29Stb if (ps != NULL) 132b4a65d29Stb *ps = sig->s; 133b4a65d29Stb } 134b4a65d29Stb 135b4a65d29Stb const BIGNUM * 136b4a65d29Stb ECDSA_SIG_get0_r(const ECDSA_SIG *sig) 137b4a65d29Stb { 138b4a65d29Stb return sig->r; 139b4a65d29Stb } 140b4a65d29Stb 141b4a65d29Stb const BIGNUM * 142b4a65d29Stb ECDSA_SIG_get0_s(const ECDSA_SIG *sig) 143b4a65d29Stb { 144b4a65d29Stb return sig->s; 145b4a65d29Stb } 146b4a65d29Stb 147b4a65d29Stb int 148b4a65d29Stb ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) 149b4a65d29Stb { 150b4a65d29Stb if (r == NULL || s == NULL) 151b4a65d29Stb return 0; 152b4a65d29Stb 153b4a65d29Stb BN_free(sig->r); 154b4a65d29Stb BN_free(sig->s); 155b4a65d29Stb sig->r = r; 156b4a65d29Stb sig->s = s; 157b4a65d29Stb return 1; 158b4a65d29Stb } 159b4a65d29Stb 160c0ee283eStb int 161c0ee283eStb ECDSA_size(const EC_KEY *r) 162c0ee283eStb { 163c0ee283eStb const EC_GROUP *group; 164c0ee283eStb const BIGNUM *order = NULL; 165c0ee283eStb ECDSA_SIG sig; 166c0ee283eStb int ret = 0; 167c0ee283eStb 168c0ee283eStb if (r == NULL) 169c0ee283eStb goto err; 170c0ee283eStb 171c0ee283eStb if ((group = EC_KEY_get0_group(r)) == NULL) 172c0ee283eStb goto err; 173c0ee283eStb 174c0ee283eStb if ((order = EC_GROUP_get0_order(group)) == NULL) 175c0ee283eStb goto err; 176c0ee283eStb 177c0ee283eStb sig.r = (BIGNUM *)order; 178c0ee283eStb sig.s = (BIGNUM *)order; 179c0ee283eStb 180c0ee283eStb if ((ret = i2d_ECDSA_SIG(&sig, NULL)) < 0) 181c0ee283eStb ret = 0; 182c0ee283eStb 183c0ee283eStb err: 184c0ee283eStb return ret; 185c0ee283eStb } 186c0ee283eStb 187b4a65d29Stb /* 188b4a65d29Stb * FIPS 186-5, section 6.4.1, step 2: convert hashed message into an integer. 189b4a65d29Stb * Use the order_bits leftmost bits if it exceeds the group order. 190b4a65d29Stb */ 191b4a65d29Stb static int 192b4a65d29Stb ecdsa_prepare_digest(const unsigned char *digest, int digest_len, 193b4a65d29Stb const EC_KEY *key, BIGNUM *e) 194b4a65d29Stb { 195b4a65d29Stb const EC_GROUP *group; 196b4a65d29Stb int digest_bits, order_bits; 197b4a65d29Stb 198bd6cba7cStb if (BN_bin2bn(digest, digest_len, e) == NULL) { 19970458be1Stb ECerror(ERR_R_BN_LIB); 200b4a65d29Stb return 0; 201b4a65d29Stb } 202b4a65d29Stb 203b4a65d29Stb if ((group = EC_KEY_get0_group(key)) == NULL) 204b4a65d29Stb return 0; 205b4a65d29Stb order_bits = EC_GROUP_order_bits(group); 206b4a65d29Stb 207b4a65d29Stb digest_bits = 8 * digest_len; 208b4a65d29Stb if (digest_bits <= order_bits) 209b4a65d29Stb return 1; 210b4a65d29Stb 211b4a65d29Stb return BN_rshift(e, e, digest_bits - order_bits); 212b4a65d29Stb } 213b4a65d29Stb 214b4a65d29Stb int 215b4a65d29Stb ecdsa_sign(int type, const unsigned char *digest, int digest_len, 216b4a65d29Stb unsigned char *signature, unsigned int *signature_len, const BIGNUM *kinv, 217b4a65d29Stb const BIGNUM *r, EC_KEY *key) 218b4a65d29Stb { 219b4a65d29Stb ECDSA_SIG *sig; 220b4a65d29Stb int out_len = 0; 221b4a65d29Stb int ret = 0; 222b4a65d29Stb 223b4a65d29Stb if ((sig = ECDSA_do_sign_ex(digest, digest_len, kinv, r, key)) == NULL) 224b4a65d29Stb goto err; 225b4a65d29Stb 226b4a65d29Stb if ((out_len = i2d_ECDSA_SIG(sig, &signature)) < 0) { 227b4a65d29Stb out_len = 0; 228b4a65d29Stb goto err; 229b4a65d29Stb } 230b4a65d29Stb 231b4a65d29Stb ret = 1; 232b4a65d29Stb 233b4a65d29Stb err: 234b4a65d29Stb *signature_len = out_len; 235b4a65d29Stb ECDSA_SIG_free(sig); 236b4a65d29Stb 237b4a65d29Stb return ret; 238b4a65d29Stb } 239b4a65d29Stb 240b4a65d29Stb /* 241b4a65d29Stb * FIPS 186-5, section 6.4.1, steps 3-8 and 11: Generate k, calculate r and 242682ba1ecStb * kinv. If r == 0, try again with a new random k. 243b4a65d29Stb */ 244b4a65d29Stb 245b4a65d29Stb int 246b4a65d29Stb ecdsa_sign_setup(EC_KEY *key, BN_CTX *in_ctx, BIGNUM **out_kinv, BIGNUM **out_r) 247b4a65d29Stb { 248b4a65d29Stb const EC_GROUP *group; 249b4a65d29Stb EC_POINT *point = NULL; 250b4a65d29Stb BN_CTX *ctx = NULL; 251b4a65d29Stb BIGNUM *k = NULL, *r = NULL; 252b4a65d29Stb const BIGNUM *order; 253b4a65d29Stb BIGNUM *x; 254b4a65d29Stb int order_bits; 255b4a65d29Stb int ret = 0; 256b4a65d29Stb 257b4a65d29Stb BN_free(*out_kinv); 258b4a65d29Stb *out_kinv = NULL; 259b4a65d29Stb 260b4a65d29Stb BN_free(*out_r); 261b4a65d29Stb *out_r = NULL; 262b4a65d29Stb 263b4a65d29Stb if (key == NULL) { 2644f33f08fStb ECerror(ERR_R_PASSED_NULL_PARAMETER); 265b4a65d29Stb goto err; 266b4a65d29Stb } 267b4a65d29Stb if ((group = EC_KEY_get0_group(key)) == NULL) { 2684f33f08fStb ECerror(ERR_R_PASSED_NULL_PARAMETER); 269b4a65d29Stb goto err; 270b4a65d29Stb } 271b4a65d29Stb 272b4a65d29Stb if ((k = BN_new()) == NULL) 273b4a65d29Stb goto err; 274b4a65d29Stb if ((r = BN_new()) == NULL) 275b4a65d29Stb goto err; 276b4a65d29Stb 277b4a65d29Stb if ((ctx = in_ctx) == NULL) 278b4a65d29Stb ctx = BN_CTX_new(); 279b4a65d29Stb if (ctx == NULL) { 2804f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 281b4a65d29Stb goto err; 282b4a65d29Stb } 283b4a65d29Stb 284b4a65d29Stb BN_CTX_start(ctx); 285b4a65d29Stb 286b4a65d29Stb if ((x = BN_CTX_get(ctx)) == NULL) 287b4a65d29Stb goto err; 288b4a65d29Stb 289b4a65d29Stb if ((point = EC_POINT_new(group)) == NULL) { 2904f33f08fStb ECerror(ERR_R_EC_LIB); 291b4a65d29Stb goto err; 292b4a65d29Stb } 293b4a65d29Stb if ((order = EC_GROUP_get0_order(group)) == NULL) { 2944f33f08fStb ECerror(ERR_R_EC_LIB); 295b4a65d29Stb goto err; 296b4a65d29Stb } 297b4a65d29Stb 298b4a65d29Stb if (BN_cmp(order, BN_value_one()) <= 0) { 2994f33f08fStb ECerror(EC_R_INVALID_GROUP_ORDER); 300b4a65d29Stb goto err; 301b4a65d29Stb } 302b4a65d29Stb 303b4a65d29Stb /* Reject curves with an order that is smaller than 80 bits. */ 304b4a65d29Stb if ((order_bits = BN_num_bits(order)) < 80) { 3054f33f08fStb ECerror(EC_R_INVALID_GROUP_ORDER); 306b4a65d29Stb goto err; 307b4a65d29Stb } 308b4a65d29Stb 309b4a65d29Stb /* Preallocate space. */ 310b4a65d29Stb if (!BN_set_bit(k, order_bits) || 311b4a65d29Stb !BN_set_bit(r, order_bits) || 312b4a65d29Stb !BN_set_bit(x, order_bits)) 313b4a65d29Stb goto err; 314b4a65d29Stb 315b4a65d29Stb /* Step 11: repeat until r != 0. */ 316b4a65d29Stb do { 317b4a65d29Stb /* Step 3: generate random k. */ 318*1c55417bStb if (!bn_rand_interval(k, BN_value_one(), order)) 319b4a65d29Stb goto err; 320b4a65d29Stb 321b4a65d29Stb /* 322b4a65d29Stb * We do not want timing information to leak the length of k, 323b4a65d29Stb * so we compute G * k using an equivalent scalar of fixed 324b4a65d29Stb * bit-length. 325b4a65d29Stb * 326b4a65d29Stb * We unconditionally perform both of these additions to prevent 327b4a65d29Stb * a small timing information leakage. We then choose the sum 328b4a65d29Stb * that is one bit longer than the order. This guarantees the 329b4a65d29Stb * code path used in the constant time implementations 330b4a65d29Stb * elsewhere. 331b4a65d29Stb * 332b4a65d29Stb * TODO: revisit the bn_copy aiming for a memory access agnostic 333b4a65d29Stb * conditional copy. 334b4a65d29Stb */ 335b4a65d29Stb if (!BN_add(r, k, order) || 336b4a65d29Stb !BN_add(x, r, order) || 337b4a65d29Stb !bn_copy(k, BN_num_bits(r) > order_bits ? r : x)) 338b4a65d29Stb goto err; 339b4a65d29Stb 340b4a65d29Stb BN_set_flags(k, BN_FLG_CONSTTIME); 341b4a65d29Stb 342b4a65d29Stb /* Step 5: P = k * G. */ 343b4a65d29Stb if (!EC_POINT_mul(group, point, k, NULL, NULL, ctx)) { 3444f33f08fStb ECerror(ERR_R_EC_LIB); 345b4a65d29Stb goto err; 346b4a65d29Stb } 347b4a65d29Stb /* Steps 6 (and 7): from P = (x, y) retain the x-coordinate. */ 348b4a65d29Stb if (!EC_POINT_get_affine_coordinates(group, point, x, NULL, 349b4a65d29Stb ctx)) { 3504f33f08fStb ECerror(ERR_R_EC_LIB); 351b4a65d29Stb goto err; 352b4a65d29Stb } 353b4a65d29Stb /* Step 8: r = x (mod order). */ 354b4a65d29Stb if (!BN_nnmod(r, x, order, ctx)) { 3554f33f08fStb ECerror(ERR_R_BN_LIB); 356b4a65d29Stb goto err; 357b4a65d29Stb } 358b4a65d29Stb } while (BN_is_zero(r)); 359b4a65d29Stb 360b4a65d29Stb /* Step 4: calculate kinv. */ 361b4a65d29Stb if (BN_mod_inverse_ct(k, k, order, ctx) == NULL) { 3624f33f08fStb ECerror(ERR_R_BN_LIB); 363b4a65d29Stb goto err; 364b4a65d29Stb } 365b4a65d29Stb 366b4a65d29Stb *out_kinv = k; 367b4a65d29Stb k = NULL; 368b4a65d29Stb 369b4a65d29Stb *out_r = r; 370b4a65d29Stb r = NULL; 371b4a65d29Stb 372b4a65d29Stb ret = 1; 373b4a65d29Stb 374b4a65d29Stb err: 375b4a65d29Stb BN_CTX_end(ctx); 376b4a65d29Stb if (ctx != in_ctx) 377b4a65d29Stb BN_CTX_free(ctx); 378b4a65d29Stb BN_free(k); 379b4a65d29Stb BN_free(r); 380b4a65d29Stb EC_POINT_free(point); 381b4a65d29Stb 382b4a65d29Stb return ret; 383b4a65d29Stb } 384b4a65d29Stb 385b4a65d29Stb /* 386b4a65d29Stb * FIPS 186-5, section 6.4.1, step 9: compute s = inv(k)(e + xr) mod order. 387b4a65d29Stb * In order to reduce the possibility of a side-channel attack, the following 388b4a65d29Stb * is calculated using a random blinding value b in [1, order): 389b4a65d29Stb * s = inv(b)(be + bxr)inv(k) mod order. 390b4a65d29Stb */ 391b4a65d29Stb 392b4a65d29Stb static int 393b4a65d29Stb ecdsa_compute_s(BIGNUM **out_s, const BIGNUM *e, const BIGNUM *kinv, 394b4a65d29Stb const BIGNUM *r, const EC_KEY *key, BN_CTX *ctx) 395b4a65d29Stb { 396b4a65d29Stb const EC_GROUP *group; 397b4a65d29Stb const BIGNUM *order, *priv_key; 398b4a65d29Stb BIGNUM *b, *binv, *be, *bxr; 399b4a65d29Stb BIGNUM *s = NULL; 400b4a65d29Stb int ret = 0; 401b4a65d29Stb 402b4a65d29Stb *out_s = NULL; 403b4a65d29Stb 404b4a65d29Stb BN_CTX_start(ctx); 405b4a65d29Stb 406b4a65d29Stb if ((group = EC_KEY_get0_group(key)) == NULL) { 4074f33f08fStb ECerror(ERR_R_PASSED_NULL_PARAMETER); 408b4a65d29Stb goto err; 409b4a65d29Stb } 410b4a65d29Stb if ((order = EC_GROUP_get0_order(group)) == NULL) { 4114f33f08fStb ECerror(ERR_R_EC_LIB); 412b4a65d29Stb goto err; 413b4a65d29Stb } 414b4a65d29Stb if ((priv_key = EC_KEY_get0_private_key(key)) == NULL) { 4154f33f08fStb ECerror(ERR_R_PASSED_NULL_PARAMETER); 416b4a65d29Stb goto err; 417b4a65d29Stb } 418b4a65d29Stb 419b4a65d29Stb if ((b = BN_CTX_get(ctx)) == NULL) 420b4a65d29Stb goto err; 421b4a65d29Stb if ((binv = BN_CTX_get(ctx)) == NULL) 422b4a65d29Stb goto err; 423b4a65d29Stb if ((be = BN_CTX_get(ctx)) == NULL) 424b4a65d29Stb goto err; 425b4a65d29Stb if ((bxr = BN_CTX_get(ctx)) == NULL) 426b4a65d29Stb goto err; 427b4a65d29Stb 428b4a65d29Stb if ((s = BN_new()) == NULL) 429b4a65d29Stb goto err; 430b4a65d29Stb 431b4a65d29Stb /* 432b4a65d29Stb * In a valid ECDSA signature, r must be in [1, order). Since r can be 433b4a65d29Stb * caller provided - either directly or by replacing sign_setup() - we 434b4a65d29Stb * can't rely on this being the case. 435b4a65d29Stb */ 436b4a65d29Stb if (BN_cmp(r, BN_value_one()) < 0 || BN_cmp(r, order) >= 0) { 437*1c55417bStb ECerror(EC_R_BAD_SIGNATURE); 438b4a65d29Stb goto err; 439b4a65d29Stb } 440b4a65d29Stb 441b4a65d29Stb if (!bn_rand_interval(b, BN_value_one(), order)) { 4424f33f08fStb ECerror(ERR_R_BN_LIB); 443b4a65d29Stb goto err; 444b4a65d29Stb } 445b4a65d29Stb 446b4a65d29Stb if (BN_mod_inverse_ct(binv, b, order, ctx) == NULL) { 4474f33f08fStb ECerror(ERR_R_BN_LIB); 448b4a65d29Stb goto err; 449b4a65d29Stb } 450b4a65d29Stb 451b4a65d29Stb if (!BN_mod_mul(bxr, b, priv_key, order, ctx)) { 4524f33f08fStb ECerror(ERR_R_BN_LIB); 453b4a65d29Stb goto err; 454b4a65d29Stb } 455b4a65d29Stb if (!BN_mod_mul(bxr, bxr, r, order, ctx)) { 4564f33f08fStb ECerror(ERR_R_BN_LIB); 457b4a65d29Stb goto err; 458b4a65d29Stb } 459b4a65d29Stb if (!BN_mod_mul(be, b, e, order, ctx)) { 4604f33f08fStb ECerror(ERR_R_BN_LIB); 461b4a65d29Stb goto err; 462b4a65d29Stb } 463b4a65d29Stb if (!BN_mod_add(s, be, bxr, order, ctx)) { 4644f33f08fStb ECerror(ERR_R_BN_LIB); 465b4a65d29Stb goto err; 466b4a65d29Stb } 467b4a65d29Stb /* s = b(e + xr)k^-1 */ 468b4a65d29Stb if (!BN_mod_mul(s, s, kinv, order, ctx)) { 4694f33f08fStb ECerror(ERR_R_BN_LIB); 470b4a65d29Stb goto err; 471b4a65d29Stb } 472b4a65d29Stb /* s = (e + xr)k^-1 */ 473b4a65d29Stb if (!BN_mod_mul(s, s, binv, order, ctx)) { 4744f33f08fStb ECerror(ERR_R_BN_LIB); 475b4a65d29Stb goto err; 476b4a65d29Stb } 477b4a65d29Stb 478b4a65d29Stb /* Step 11: if s == 0 start over. */ 479b4a65d29Stb if (!BN_is_zero(s)) { 480b4a65d29Stb *out_s = s; 481b4a65d29Stb s = NULL; 482b4a65d29Stb } 483b4a65d29Stb 484b4a65d29Stb ret = 1; 485b4a65d29Stb 486b4a65d29Stb err: 487b4a65d29Stb BN_CTX_end(ctx); 488b4a65d29Stb BN_free(s); 489b4a65d29Stb 490b4a65d29Stb return ret; 491b4a65d29Stb } 492b4a65d29Stb 493b4a65d29Stb /* 494b4a65d29Stb * It is too expensive to check curve parameters on every sign operation. 495b4a65d29Stb * Instead, cap the number of retries. A single retry is very unlikely, so 496b4a65d29Stb * allowing 32 retries is amply enough. 497b4a65d29Stb */ 498b4a65d29Stb #define ECDSA_MAX_SIGN_ITERATIONS 32 499b4a65d29Stb 500b4a65d29Stb /* 501b4a65d29Stb * FIPS 186-5: Section 6.4.1: ECDSA signature generation, steps 2-12. 502b4a65d29Stb * The caller provides the hash of the message, thus performs step 1. 503b4a65d29Stb * Step 10, zeroing k and kinv, is done by BN_free(). 504b4a65d29Stb */ 505b4a65d29Stb 506b4a65d29Stb ECDSA_SIG * 507b4a65d29Stb ecdsa_sign_sig(const unsigned char *digest, int digest_len, 508b4a65d29Stb const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *key) 509b4a65d29Stb { 510b4a65d29Stb BN_CTX *ctx = NULL; 511b4a65d29Stb BIGNUM *kinv = NULL, *r = NULL, *s = NULL; 512b4a65d29Stb BIGNUM *e; 513b4a65d29Stb int caller_supplied_values = 0; 514b4a65d29Stb int attempts = 0; 515b4a65d29Stb ECDSA_SIG *sig = NULL; 516b4a65d29Stb 517b4a65d29Stb if ((ctx = BN_CTX_new()) == NULL) { 5184f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 519b4a65d29Stb goto err; 520b4a65d29Stb } 521b4a65d29Stb 522b4a65d29Stb BN_CTX_start(ctx); 523b4a65d29Stb 524b4a65d29Stb if ((e = BN_CTX_get(ctx)) == NULL) 525b4a65d29Stb goto err; 526b4a65d29Stb 527b4a65d29Stb /* Step 2: convert hash into an integer. */ 528b4a65d29Stb if (!ecdsa_prepare_digest(digest, digest_len, key, e)) 529b4a65d29Stb goto err; 530b4a65d29Stb 531b4a65d29Stb if (in_kinv != NULL && in_r != NULL) { 532b4a65d29Stb /* 533b4a65d29Stb * Use the caller's kinv and r. Don't call ECDSA_sign_setup(). 534b4a65d29Stb * If we're unable to compute a valid signature, the caller 535b4a65d29Stb * must provide new values. 536b4a65d29Stb */ 537b4a65d29Stb caller_supplied_values = 1; 538b4a65d29Stb 539b4a65d29Stb if ((kinv = BN_dup(in_kinv)) == NULL) { 5404f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 541b4a65d29Stb goto err; 542b4a65d29Stb } 543b4a65d29Stb if ((r = BN_dup(in_r)) == NULL) { 5444f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 545b4a65d29Stb goto err; 546b4a65d29Stb } 547b4a65d29Stb } 548b4a65d29Stb 549b4a65d29Stb do { 550b4a65d29Stb /* Steps 3-8: calculate kinv and r. */ 551b4a65d29Stb if (!caller_supplied_values) { 552b4a65d29Stb if (!ECDSA_sign_setup(key, ctx, &kinv, &r)) { 553*1c55417bStb ECerror(ERR_R_EC_LIB); 554b4a65d29Stb goto err; 555b4a65d29Stb } 556b4a65d29Stb } 557b4a65d29Stb 558b4a65d29Stb /* 559b4a65d29Stb * Steps 9 and 11: if s is non-NULL, we have a valid signature. 560b4a65d29Stb */ 561b4a65d29Stb if (!ecdsa_compute_s(&s, e, kinv, r, key, ctx)) 562b4a65d29Stb goto err; 563b4a65d29Stb if (s != NULL) 564b4a65d29Stb break; 565b4a65d29Stb 566b4a65d29Stb if (caller_supplied_values) { 567*1c55417bStb ECerror(EC_R_NEED_NEW_SETUP_VALUES); 568b4a65d29Stb goto err; 569b4a65d29Stb } 570b4a65d29Stb 571b4a65d29Stb if (++attempts > ECDSA_MAX_SIGN_ITERATIONS) { 5724f33f08fStb ECerror(EC_R_WRONG_CURVE_PARAMETERS); 573b4a65d29Stb goto err; 574b4a65d29Stb } 575b4a65d29Stb } while (1); 576b4a65d29Stb 577b4a65d29Stb /* Step 12: output (r, s). */ 578b4a65d29Stb if ((sig = ECDSA_SIG_new()) == NULL) { 5794f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 580b4a65d29Stb goto err; 581b4a65d29Stb } 582b4a65d29Stb if (!ECDSA_SIG_set0(sig, r, s)) { 583b4a65d29Stb ECDSA_SIG_free(sig); 584b4a65d29Stb goto err; 585b4a65d29Stb } 586b4a65d29Stb r = NULL; 587b4a65d29Stb s = NULL; 588b4a65d29Stb 589b4a65d29Stb err: 590b4a65d29Stb BN_CTX_end(ctx); 591b4a65d29Stb BN_CTX_free(ctx); 592b4a65d29Stb BN_free(kinv); 593b4a65d29Stb BN_free(r); 594b4a65d29Stb BN_free(s); 595b4a65d29Stb 596b4a65d29Stb return sig; 597b4a65d29Stb } 598b4a65d29Stb 599b4a65d29Stb int 600b4a65d29Stb ecdsa_verify(int type, const unsigned char *digest, int digest_len, 601b4a65d29Stb const unsigned char *sigbuf, int sig_len, EC_KEY *key) 602b4a65d29Stb { 603b4a65d29Stb ECDSA_SIG *s; 604b4a65d29Stb unsigned char *der = NULL; 605b4a65d29Stb const unsigned char *p; 606b4a65d29Stb int der_len = 0; 607b4a65d29Stb int ret = -1; 608b4a65d29Stb 609b4a65d29Stb if ((s = ECDSA_SIG_new()) == NULL) 610b4a65d29Stb goto err; 611b4a65d29Stb 612b4a65d29Stb p = sigbuf; 613b4a65d29Stb if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL) 614b4a65d29Stb goto err; 615b4a65d29Stb 6162979b3a0Stb /* Ensure signature uses DER and doesn't have trailing garbage. */ 617b4a65d29Stb if ((der_len = i2d_ECDSA_SIG(s, &der)) != sig_len) 618b4a65d29Stb goto err; 619b4a65d29Stb if (timingsafe_memcmp(sigbuf, der, der_len)) 620b4a65d29Stb goto err; 621b4a65d29Stb 622b4a65d29Stb ret = ECDSA_do_verify(digest, digest_len, s, key); 623b4a65d29Stb 624b4a65d29Stb err: 625b4a65d29Stb freezero(der, der_len); 626b4a65d29Stb ECDSA_SIG_free(s); 627b4a65d29Stb 628b4a65d29Stb return ret; 629b4a65d29Stb } 630b4a65d29Stb 631b4a65d29Stb /* 632b4a65d29Stb * FIPS 186-5, section 6.4.2: ECDSA signature verification. 633b4a65d29Stb * The caller provides us with the hash of the message, so has performed step 2. 634b4a65d29Stb */ 635b4a65d29Stb 636b4a65d29Stb int 637b4a65d29Stb ecdsa_verify_sig(const unsigned char *digest, int digest_len, 638b4a65d29Stb const ECDSA_SIG *sig, EC_KEY *key) 639b4a65d29Stb { 640b4a65d29Stb const EC_GROUP *group; 641b4a65d29Stb const EC_POINT *pub_key; 642b4a65d29Stb EC_POINT *point = NULL; 643b4a65d29Stb const BIGNUM *order; 644b4a65d29Stb BN_CTX *ctx = NULL; 645b4a65d29Stb BIGNUM *e, *sinv, *u, *v, *x; 646b4a65d29Stb int ret = -1; 647b4a65d29Stb 648b4a65d29Stb if (key == NULL || sig == NULL) { 649*1c55417bStb ECerror(EC_R_MISSING_PARAMETERS); 650b4a65d29Stb goto err; 651b4a65d29Stb } 652b4a65d29Stb if ((group = EC_KEY_get0_group(key)) == NULL) { 653*1c55417bStb ECerror(EC_R_MISSING_PARAMETERS); 654b4a65d29Stb goto err; 655b4a65d29Stb } 656b4a65d29Stb if ((pub_key = EC_KEY_get0_public_key(key)) == NULL) { 657*1c55417bStb ECerror(EC_R_MISSING_PARAMETERS); 658b4a65d29Stb goto err; 659b4a65d29Stb } 660b4a65d29Stb 661b4a65d29Stb if ((ctx = BN_CTX_new()) == NULL) { 6624f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 663b4a65d29Stb goto err; 664b4a65d29Stb } 665b4a65d29Stb 666b4a65d29Stb BN_CTX_start(ctx); 667b4a65d29Stb 668b4a65d29Stb if ((e = BN_CTX_get(ctx)) == NULL) 669b4a65d29Stb goto err; 670b4a65d29Stb if ((sinv = BN_CTX_get(ctx)) == NULL) 671b4a65d29Stb goto err; 672b4a65d29Stb if ((u = BN_CTX_get(ctx)) == NULL) 673b4a65d29Stb goto err; 674b4a65d29Stb if ((v = BN_CTX_get(ctx)) == NULL) 675b4a65d29Stb goto err; 676b4a65d29Stb if ((x = BN_CTX_get(ctx)) == NULL) 677b4a65d29Stb goto err; 678b4a65d29Stb 679b4a65d29Stb if ((order = EC_GROUP_get0_order(group)) == NULL) { 6804f33f08fStb ECerror(ERR_R_EC_LIB); 681b4a65d29Stb goto err; 682b4a65d29Stb } 683b4a65d29Stb 684b4a65d29Stb /* Step 1: verify that r and s are in the range [1, order). */ 685b4a65d29Stb if (BN_cmp(sig->r, BN_value_one()) < 0 || BN_cmp(sig->r, order) >= 0) { 686*1c55417bStb ECerror(EC_R_BAD_SIGNATURE); 687b4a65d29Stb ret = 0; 688b4a65d29Stb goto err; 689b4a65d29Stb } 690b4a65d29Stb if (BN_cmp(sig->s, BN_value_one()) < 0 || BN_cmp(sig->s, order) >= 0) { 691*1c55417bStb ECerror(EC_R_BAD_SIGNATURE); 692b4a65d29Stb ret = 0; 693b4a65d29Stb goto err; 694b4a65d29Stb } 695b4a65d29Stb 696b4a65d29Stb /* Step 3: convert the hash into an integer. */ 697b4a65d29Stb if (!ecdsa_prepare_digest(digest, digest_len, key, e)) 698b4a65d29Stb goto err; 699b4a65d29Stb 700b4a65d29Stb /* Step 4: compute the inverse of s modulo order. */ 701b4a65d29Stb if (BN_mod_inverse_ct(sinv, sig->s, order, ctx) == NULL) { 7024f33f08fStb ECerror(ERR_R_BN_LIB); 703b4a65d29Stb goto err; 704b4a65d29Stb } 705b4a65d29Stb /* Step 5: compute u = s^-1 * e and v = s^-1 * r (modulo order). */ 706b4a65d29Stb if (!BN_mod_mul(u, e, sinv, order, ctx)) { 7074f33f08fStb ECerror(ERR_R_BN_LIB); 708b4a65d29Stb goto err; 709b4a65d29Stb } 710b4a65d29Stb if (!BN_mod_mul(v, sig->r, sinv, order, ctx)) { 7114f33f08fStb ECerror(ERR_R_BN_LIB); 712b4a65d29Stb goto err; 713b4a65d29Stb } 714b4a65d29Stb 715b4a65d29Stb /* 716b4a65d29Stb * Steps 6 and 7: compute R = G * u + pub_key * v = (x, y). Reject if 717b4a65d29Stb * it's the point at infinity - getting affine coordinates fails. Keep 718b4a65d29Stb * the x coordinate. 719b4a65d29Stb */ 720b4a65d29Stb if ((point = EC_POINT_new(group)) == NULL) { 7214f33f08fStb ECerror(ERR_R_MALLOC_FAILURE); 722b4a65d29Stb goto err; 723b4a65d29Stb } 724b4a65d29Stb if (!EC_POINT_mul(group, point, u, pub_key, v, ctx)) { 7254f33f08fStb ECerror(ERR_R_EC_LIB); 726b4a65d29Stb goto err; 727b4a65d29Stb } 728b4a65d29Stb if (!EC_POINT_get_affine_coordinates(group, point, x, NULL, ctx)) { 7294f33f08fStb ECerror(ERR_R_EC_LIB); 730b4a65d29Stb goto err; 731b4a65d29Stb } 732b4a65d29Stb /* Step 8: convert x to a number in [0, order). */ 733b4a65d29Stb if (!BN_nnmod(x, x, order, ctx)) { 7344f33f08fStb ECerror(ERR_R_BN_LIB); 735b4a65d29Stb goto err; 736b4a65d29Stb } 737b4a65d29Stb 738b4a65d29Stb /* Step 9: the signature is valid iff the x-coordinate is equal to r. */ 739b4a65d29Stb ret = (BN_cmp(x, sig->r) == 0); 740b4a65d29Stb 741b4a65d29Stb err: 742b4a65d29Stb BN_CTX_end(ctx); 743b4a65d29Stb BN_CTX_free(ctx); 744b4a65d29Stb EC_POINT_free(point); 745b4a65d29Stb 746b4a65d29Stb return ret; 747b4a65d29Stb } 748b4a65d29Stb 749b4a65d29Stb ECDSA_SIG * 750b4a65d29Stb ECDSA_do_sign(const unsigned char *digest, int digest_len, EC_KEY *key) 751b4a65d29Stb { 752b4a65d29Stb return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key); 753b4a65d29Stb } 754b4a65d29Stb 755b4a65d29Stb ECDSA_SIG * 756b4a65d29Stb ECDSA_do_sign_ex(const unsigned char *digest, int digest_len, 757b4a65d29Stb const BIGNUM *kinv, const BIGNUM *out_r, EC_KEY *key) 758b4a65d29Stb { 759b4a65d29Stb if (key->meth->sign_sig == NULL) { 760*1c55417bStb ECerror(EC_R_NOT_IMPLEMENTED); 761b4a65d29Stb return 0; 762b4a65d29Stb } 763b4a65d29Stb return key->meth->sign_sig(digest, digest_len, kinv, out_r, key); 764b4a65d29Stb } 765b4a65d29Stb 766b4a65d29Stb int 767b4a65d29Stb ECDSA_sign(int type, const unsigned char *digest, int digest_len, 768b4a65d29Stb unsigned char *signature, unsigned int *signature_len, EC_KEY *key) 769b4a65d29Stb { 770b4a65d29Stb return ECDSA_sign_ex(type, digest, digest_len, signature, signature_len, 771b4a65d29Stb NULL, NULL, key); 772b4a65d29Stb } 773b4a65d29Stb 774b4a65d29Stb int 775b4a65d29Stb ECDSA_sign_ex(int type, const unsigned char *digest, int digest_len, 776b4a65d29Stb unsigned char *signature, unsigned int *signature_len, const BIGNUM *kinv, 777b4a65d29Stb const BIGNUM *r, EC_KEY *key) 778b4a65d29Stb { 779b4a65d29Stb if (key->meth->sign == NULL) { 780*1c55417bStb ECerror(EC_R_NOT_IMPLEMENTED); 781b4a65d29Stb return 0; 782b4a65d29Stb } 783b4a65d29Stb return key->meth->sign(type, digest, digest_len, signature, 784b4a65d29Stb signature_len, kinv, r, key); 785b4a65d29Stb } 786b4a65d29Stb 787b4a65d29Stb int 788b4a65d29Stb ECDSA_sign_setup(EC_KEY *key, BN_CTX *in_ctx, BIGNUM **out_kinv, 789b4a65d29Stb BIGNUM **out_r) 790b4a65d29Stb { 791b4a65d29Stb if (key->meth->sign_setup == NULL) { 792*1c55417bStb ECerror(EC_R_NOT_IMPLEMENTED); 793b4a65d29Stb return 0; 794b4a65d29Stb } 795b4a65d29Stb return key->meth->sign_setup(key, in_ctx, out_kinv, out_r); 796b4a65d29Stb } 797b4a65d29Stb 798b4a65d29Stb int 799b4a65d29Stb ECDSA_do_verify(const unsigned char *digest, int digest_len, 800b4a65d29Stb const ECDSA_SIG *sig, EC_KEY *key) 801b4a65d29Stb { 802b4a65d29Stb if (key->meth->verify_sig == NULL) { 803*1c55417bStb ECerror(EC_R_NOT_IMPLEMENTED); 804b4a65d29Stb return 0; 805b4a65d29Stb } 806b4a65d29Stb return key->meth->verify_sig(digest, digest_len, sig, key); 807b4a65d29Stb } 808b4a65d29Stb 809b4a65d29Stb int 810b4a65d29Stb ECDSA_verify(int type, const unsigned char *digest, int digest_len, 811b4a65d29Stb const unsigned char *sigbuf, int sig_len, EC_KEY *key) 812b4a65d29Stb { 813b4a65d29Stb if (key->meth->verify == NULL) { 814*1c55417bStb ECerror(EC_R_NOT_IMPLEMENTED); 815b4a65d29Stb return 0; 816b4a65d29Stb } 817b4a65d29Stb return key->meth->verify(type, digest, digest_len, sigbuf, sig_len, key); 818b4a65d29Stb } 819