Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 1 | /* ==================================================================== |
| 2 | * Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved. |
| 3 | * |
| 4 | * Redistribution and use in source and binary forms, with or without |
| 5 | * modification, are permitted provided that the following conditions |
| 6 | * are met: |
| 7 | * |
| 8 | * 1. Redistributions of source code must retain the above copyright |
| 9 | * notice, this list of conditions and the following disclaimer. |
| 10 | * |
| 11 | * 2. Redistributions in binary form must reproduce the above copyright |
| 12 | * notice, this list of conditions and the following disclaimer in |
| 13 | * the documentation and/or other materials provided with the |
| 14 | * distribution. |
| 15 | * |
| 16 | * 3. All advertising materials mentioning features or use of this |
| 17 | * software must display the following acknowledgment: |
| 18 | * "This product includes software developed by the OpenSSL Project |
| 19 | * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)" |
| 20 | * |
| 21 | * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to |
| 22 | * endorse or promote products derived from this software without |
| 23 | * prior written permission. For written permission, please contact |
| 24 | * openssl-core@OpenSSL.org. |
| 25 | * |
| 26 | * 5. Products derived from this software may not be called "OpenSSL" |
| 27 | * nor may "OpenSSL" appear in their names without prior written |
| 28 | * permission of the OpenSSL Project. |
| 29 | * |
| 30 | * 6. Redistributions of any form whatsoever must retain the following |
| 31 | * acknowledgment: |
| 32 | * "This product includes software developed by the OpenSSL Project |
| 33 | * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)" |
| 34 | * |
| 35 | * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY |
| 36 | * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 37 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR |
| 38 | * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR |
| 39 | * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 40 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT |
| 41 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; |
| 42 | * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) |
| 43 | * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, |
| 44 | * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 45 | * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED |
| 46 | * OF THE POSSIBILITY OF SUCH DAMAGE. |
| 47 | * ==================================================================== |
| 48 | * |
| 49 | * This product includes cryptographic software written by Eric Young |
| 50 | * (eay@cryptsoft.com). This product includes software written by Tim |
| 51 | * Hudson (tjh@cryptsoft.com). */ |
| 52 | |
| 53 | #include <openssl/ecdsa.h> |
| 54 | |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 55 | #include <assert.h> |
Adam Langley | 2b2d66d | 2015-01-30 17:08:37 -0800 | [diff] [blame] | 56 | #include <string.h> |
| 57 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 58 | #include <openssl/bn.h> |
David Benjamin | 87897a8 | 2015-06-11 23:30:53 -0400 | [diff] [blame] | 59 | #include <openssl/bytestring.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 60 | #include <openssl/err.h> |
| 61 | #include <openssl/mem.h> |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 62 | |
Adam Langley | aacb72c | 2017-05-02 14:25:39 -0700 | [diff] [blame^] | 63 | #include "../bn/internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 64 | #include "../ec/internal.h" |
Adam Langley | aacb72c | 2017-05-02 14:25:39 -0700 | [diff] [blame^] | 65 | #include "../../internal.h" |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 66 | |
| 67 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 68 | /* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian |
| 69 | * number and sets |out| to that value. It then truncates |out| so that it's, |
| 70 | * at most, as long as |order|. It returns one on success and zero otherwise. */ |
| 71 | static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len, |
| 72 | const BIGNUM *order) { |
| 73 | size_t num_bits; |
| 74 | |
| 75 | num_bits = BN_num_bits(order); |
| 76 | /* Need to truncate digest if it is too long: first truncate whole |
| 77 | * bytes. */ |
| 78 | if (8 * digest_len > num_bits) { |
| 79 | digest_len = (num_bits + 7) / 8; |
| 80 | } |
| 81 | if (!BN_bin2bn(digest, digest_len, out)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 82 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 83 | return 0; |
| 84 | } |
| 85 | |
| 86 | /* If still too long truncate remaining bits with a shift */ |
| 87 | if ((8 * digest_len > num_bits) && |
| 88 | !BN_rshift(out, out, 8 - (num_bits & 0x7))) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 89 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 90 | return 0; |
| 91 | } |
| 92 | |
| 93 | return 1; |
| 94 | } |
| 95 | |
| 96 | ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 97 | const EC_KEY *key) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 98 | return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key); |
| 99 | } |
| 100 | |
| 101 | int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 102 | const ECDSA_SIG *sig, const EC_KEY *eckey) { |
Adam Langley | 5129e2d | 2014-07-25 10:06:44 -0700 | [diff] [blame] | 103 | int ret = 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 104 | BN_CTX *ctx; |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 105 | BIGNUM *u1, *u2, *m, *X; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 106 | EC_POINT *point = NULL; |
| 107 | const EC_GROUP *group; |
| 108 | const EC_POINT *pub_key; |
| 109 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 110 | /* check input values */ |
Adam Langley | b2cb0ec | 2014-09-02 14:28:49 -0700 | [diff] [blame] | 111 | if ((group = EC_KEY_get0_group(eckey)) == NULL || |
| 112 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || |
| 113 | sig == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 114 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS); |
Adam Langley | 5129e2d | 2014-07-25 10:06:44 -0700 | [diff] [blame] | 115 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | ctx = BN_CTX_new(); |
| 119 | if (!ctx) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 120 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 5129e2d | 2014-07-25 10:06:44 -0700 | [diff] [blame] | 121 | return 0; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 122 | } |
| 123 | BN_CTX_start(ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 124 | u1 = BN_CTX_get(ctx); |
| 125 | u2 = BN_CTX_get(ctx); |
| 126 | m = BN_CTX_get(ctx); |
| 127 | X = BN_CTX_get(ctx); |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 128 | if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 129 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 130 | goto err; |
| 131 | } |
| 132 | |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 133 | const BIGNUM *order = EC_GROUP_get0_order(group); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 134 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
| 135 | BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || |
| 136 | BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 137 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 138 | goto err; |
| 139 | } |
| 140 | /* calculate tmp1 = inv(S) mod order */ |
Brian Smith | a432757 | 2016-08-02 16:58:57 -1000 | [diff] [blame] | 141 | int no_inverse; |
| 142 | if (!BN_mod_inverse_odd(u2, &no_inverse, sig->s, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 143 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 144 | goto err; |
| 145 | } |
| 146 | if (!digest_to_bn(m, digest, digest_len, order)) { |
| 147 | goto err; |
| 148 | } |
| 149 | /* u1 = m * tmp mod order */ |
| 150 | if (!BN_mod_mul(u1, m, u2, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 151 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 152 | goto err; |
| 153 | } |
| 154 | /* u2 = r * w mod q */ |
| 155 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 156 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 157 | goto err; |
| 158 | } |
| 159 | |
| 160 | point = EC_POINT_new(group); |
| 161 | if (point == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 162 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 163 | goto err; |
| 164 | } |
| 165 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 166 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 167 | goto err; |
| 168 | } |
| 169 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 170 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 171 | goto err; |
| 172 | } |
| 173 | if (!BN_nnmod(u1, X, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 174 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 175 | goto err; |
| 176 | } |
| 177 | /* if the signature is correct u1 is equal to sig->r */ |
David Benjamin | 7ed2e82 | 2017-04-28 17:31:43 -0400 | [diff] [blame] | 178 | if (BN_ucmp(u1, sig->r) != 0) { |
| 179 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 180 | goto err; |
| 181 | } |
| 182 | |
| 183 | ret = 1; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 184 | |
| 185 | err: |
Matt Braithwaite | 0b553eb | 2016-01-15 16:55:41 -0800 | [diff] [blame] | 186 | BN_CTX_end(ctx); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 187 | BN_CTX_free(ctx); |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 188 | EC_POINT_free(point); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 189 | return ret; |
| 190 | } |
| 191 | |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 192 | static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, |
Adam Langley | d4b4f08 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 193 | BIGNUM **rp, const uint8_t *digest, |
| 194 | size_t digest_len) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 195 | BN_CTX *ctx = NULL; |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 196 | BIGNUM *k = NULL, *kinv = NULL, *r = NULL, *tmp = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 197 | EC_POINT *tmp_point = NULL; |
| 198 | const EC_GROUP *group; |
| 199 | int ret = 0; |
| 200 | |
| 201 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 202 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 203 | return 0; |
| 204 | } |
| 205 | |
| 206 | if (ctx_in == NULL) { |
| 207 | if ((ctx = BN_CTX_new()) == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 208 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 209 | return 0; |
| 210 | } |
| 211 | } else { |
| 212 | ctx = ctx_in; |
| 213 | } |
| 214 | |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 215 | k = BN_new(); |
| 216 | kinv = BN_new(); /* this value is later returned in *kinvp */ |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 217 | r = BN_new(); /* this value is later returned in *rp */ |
David Benjamin | 8cf79af | 2016-06-16 14:58:36 -0400 | [diff] [blame] | 218 | tmp = BN_new(); |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 219 | if (k == NULL || kinv == NULL || r == NULL || tmp == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 220 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 221 | goto err; |
| 222 | } |
| 223 | tmp_point = EC_POINT_new(group); |
| 224 | if (tmp_point == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 225 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 226 | goto err; |
| 227 | } |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 228 | |
| 229 | const BIGNUM *order = EC_GROUP_get0_order(group); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 230 | |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 231 | /* Check that the size of the group order is FIPS compliant (FIPS 186-4 |
| 232 | * B.5.2). */ |
| 233 | if (BN_num_bits(order) < 160) { |
| 234 | OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER); |
| 235 | goto err; |
| 236 | } |
| 237 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 238 | do { |
Adam Langley | d4b4f08 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 239 | /* If possible, we'll include the private key and message digest in the k |
| 240 | * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is |
| 241 | * being used. */ |
Brian Smith | 4edca0b | 2016-07-25 10:36:58 -1000 | [diff] [blame] | 242 | if (digest_len > 0) { |
| 243 | do { |
| 244 | if (!BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey), |
| 245 | digest, digest_len, ctx)) { |
| 246 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED); |
| 247 | goto err; |
| 248 | } |
| 249 | } while (BN_is_zero(k)); |
| 250 | } else if (!BN_rand_range_ex(k, 1, order)) { |
| 251 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED); |
| 252 | goto err; |
| 253 | } |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 254 | |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 255 | /* Compute the inverse of k. The order is a prime, so use Fermat's Little |
| 256 | * Theorem. Note |ec_group_get_mont_data| may return NULL but |
| 257 | * |bn_mod_inverse_prime| allows this. */ |
| 258 | if (!bn_mod_inverse_prime(kinv, k, order, ctx, |
| 259 | ec_group_get_mont_data(group))) { |
| 260 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
| 261 | goto err; |
| 262 | } |
| 263 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 264 | /* We do not want timing information to leak the length of k, |
| 265 | * so we compute G*k using an equivalent scalar of fixed |
| 266 | * bit-length. */ |
| 267 | |
| 268 | if (!BN_add(k, k, order)) { |
| 269 | goto err; |
| 270 | } |
| 271 | if (BN_num_bits(k) <= BN_num_bits(order)) { |
| 272 | if (!BN_add(k, k, order)) { |
| 273 | goto err; |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | /* compute r the x-coordinate of generator * k */ |
| 278 | if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 279 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 280 | goto err; |
| 281 | } |
David Benjamin | 8cf79af | 2016-06-16 14:58:36 -0400 | [diff] [blame] | 282 | if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, tmp, NULL, |
| 283 | ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 284 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 285 | goto err; |
| 286 | } |
| 287 | |
David Benjamin | 8cf79af | 2016-06-16 14:58:36 -0400 | [diff] [blame] | 288 | if (!BN_nnmod(r, tmp, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 289 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 290 | goto err; |
| 291 | } |
| 292 | } while (BN_is_zero(r)); |
| 293 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 294 | /* clear old values if necessary */ |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 295 | BN_clear_free(*rp); |
| 296 | BN_clear_free(*kinvp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 297 | |
| 298 | /* save the pre-computed values */ |
| 299 | *rp = r; |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 300 | *kinvp = kinv; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 301 | ret = 1; |
| 302 | |
| 303 | err: |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 304 | BN_clear_free(k); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 305 | if (!ret) { |
Steven Valdez | 89abf7a | 2017-04-12 15:56:14 -0400 | [diff] [blame] | 306 | BN_clear_free(kinv); |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 307 | BN_clear_free(r); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 308 | } |
David Benjamin | 9ab14e0 | 2015-02-11 01:17:18 -0500 | [diff] [blame] | 309 | if (ctx_in == NULL) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 310 | BN_CTX_free(ctx); |
David Benjamin | 9ab14e0 | 2015-02-11 01:17:18 -0500 | [diff] [blame] | 311 | } |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 312 | EC_POINT_free(tmp_point); |
David Benjamin | 8cf79af | 2016-06-16 14:58:36 -0400 | [diff] [blame] | 313 | BN_clear_free(tmp); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 314 | return ret; |
| 315 | } |
| 316 | |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 317 | int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, |
| 318 | BIGNUM **rp) { |
Adam Langley | d4b4f08 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 319 | return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0); |
| 320 | } |
| 321 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 322 | ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len, |
| 323 | const BIGNUM *in_kinv, const BIGNUM *in_r, |
Matthew Braithwaite | c4796c9 | 2017-02-16 16:49:54 -0800 | [diff] [blame] | 324 | const EC_KEY *eckey) { |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 325 | int ok = 0; |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 326 | BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL; |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 327 | const BIGNUM *ckinv; |
| 328 | BN_CTX *ctx = NULL; |
| 329 | const EC_GROUP *group; |
| 330 | ECDSA_SIG *ret; |
| 331 | const BIGNUM *priv_key; |
| 332 | |
| 333 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 334 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 335 | return NULL; |
| 336 | } |
| 337 | |
| 338 | group = EC_KEY_get0_group(eckey); |
| 339 | priv_key = EC_KEY_get0_private_key(eckey); |
| 340 | |
| 341 | if (group == NULL || priv_key == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 342 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 343 | return NULL; |
| 344 | } |
| 345 | |
| 346 | ret = ECDSA_SIG_new(); |
| 347 | if (!ret) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 348 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 349 | return NULL; |
| 350 | } |
| 351 | s = ret->s; |
| 352 | |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 353 | if ((ctx = BN_CTX_new()) == NULL || |
| 354 | (tmp = BN_new()) == NULL || |
| 355 | (m = BN_new()) == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 356 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 357 | goto err; |
| 358 | } |
| 359 | |
Brian Smith | a3d9de0 | 2015-11-18 17:07:14 -1000 | [diff] [blame] | 360 | const BIGNUM *order = EC_GROUP_get0_order(group); |
| 361 | |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 362 | if (!digest_to_bn(m, digest, digest_len, order)) { |
| 363 | goto err; |
| 364 | } |
| 365 | for (;;) { |
| 366 | if (in_kinv == NULL || in_r == NULL) { |
Adam Langley | d4b4f08 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 367 | if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 368 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 369 | goto err; |
| 370 | } |
| 371 | ckinv = kinv; |
| 372 | } else { |
| 373 | ckinv = in_kinv; |
| 374 | if (BN_copy(ret->r, in_r) == NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 375 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 376 | goto err; |
| 377 | } |
| 378 | } |
| 379 | |
| 380 | if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 381 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 382 | goto err; |
| 383 | } |
| 384 | if (!BN_mod_add_quick(s, tmp, m, order)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 385 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 386 | goto err; |
| 387 | } |
| 388 | if (!BN_mod_mul(s, s, ckinv, order, ctx)) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 389 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 390 | goto err; |
| 391 | } |
| 392 | if (BN_is_zero(s)) { |
| 393 | /* if kinv and r have been supplied by the caller |
| 394 | * don't to generate new kinv and r values */ |
| 395 | if (in_kinv != NULL && in_r != NULL) { |
David Benjamin | 3570d73 | 2015-06-29 00:28:17 -0400 | [diff] [blame] | 396 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 397 | goto err; |
| 398 | } |
| 399 | } else { |
| 400 | /* s != 0 => we have a valid signature */ |
| 401 | break; |
| 402 | } |
| 403 | } |
| 404 | |
| 405 | ok = 1; |
| 406 | |
| 407 | err: |
| 408 | if (!ok) { |
| 409 | ECDSA_SIG_free(ret); |
| 410 | ret = NULL; |
| 411 | } |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 412 | BN_CTX_free(ctx); |
| 413 | BN_clear_free(m); |
| 414 | BN_clear_free(tmp); |
David Benjamin | cca4ba7 | 2015-04-22 15:49:27 -0400 | [diff] [blame] | 415 | BN_clear_free(kinv); |
Adam Langley | 95c29f3 | 2014-06-20 12:00:00 -0700 | [diff] [blame] | 416 | return ret; |
| 417 | } |