Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [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 | |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 55 | #include <assert.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 56 | #include <string.h> |
| 57 | |
| 58 | #include <openssl/bn.h> |
| 59 | #include <openssl/err.h> |
| 60 | #include <openssl/mem.h> |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 61 | |
Robert Sloan | 69939df | 2017-01-09 10:53:07 -0800 | [diff] [blame] | 62 | #include "../bn/internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 63 | #include "../ec/internal.h" |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 64 | #include "../../internal.h" |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 65 | |
| 66 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 67 | /* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian |
| 68 | * number and sets |out| to that value. It then truncates |out| so that it's, |
| 69 | * at most, as long as |order|. It returns one on success and zero otherwise. */ |
| 70 | static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len, |
| 71 | const BIGNUM *order) { |
| 72 | size_t num_bits; |
| 73 | |
| 74 | num_bits = BN_num_bits(order); |
| 75 | /* Need to truncate digest if it is too long: first truncate whole |
| 76 | * bytes. */ |
| 77 | if (8 * digest_len > num_bits) { |
| 78 | digest_len = (num_bits + 7) / 8; |
| 79 | } |
| 80 | if (!BN_bin2bn(digest, digest_len, out)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 81 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 82 | return 0; |
| 83 | } |
| 84 | |
| 85 | /* If still too long truncate remaining bits with a shift */ |
| 86 | if ((8 * digest_len > num_bits) && |
| 87 | !BN_rshift(out, out, 8 - (num_bits & 0x7))) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 88 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 89 | return 0; |
| 90 | } |
| 91 | |
| 92 | return 1; |
| 93 | } |
| 94 | |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 95 | ECDSA_SIG *ECDSA_SIG_new(void) { |
| 96 | ECDSA_SIG *sig = OPENSSL_malloc(sizeof(ECDSA_SIG)); |
| 97 | if (sig == NULL) { |
| 98 | return NULL; |
| 99 | } |
| 100 | sig->r = BN_new(); |
| 101 | sig->s = BN_new(); |
| 102 | if (sig->r == NULL || sig->s == NULL) { |
| 103 | ECDSA_SIG_free(sig); |
| 104 | return NULL; |
| 105 | } |
| 106 | return sig; |
| 107 | } |
| 108 | |
| 109 | void ECDSA_SIG_free(ECDSA_SIG *sig) { |
| 110 | if (sig == NULL) { |
| 111 | return; |
| 112 | } |
| 113 | |
| 114 | BN_free(sig->r); |
| 115 | BN_free(sig->s); |
| 116 | OPENSSL_free(sig); |
| 117 | } |
| 118 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 119 | ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len, |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 120 | const EC_KEY *key) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 121 | return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key); |
| 122 | } |
| 123 | |
| 124 | int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 125 | const ECDSA_SIG *sig, const EC_KEY *eckey) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 126 | int ret = 0; |
| 127 | BN_CTX *ctx; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 128 | BIGNUM *u1, *u2, *m, *X; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 129 | EC_POINT *point = NULL; |
| 130 | const EC_GROUP *group; |
| 131 | const EC_POINT *pub_key; |
| 132 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 133 | /* check input values */ |
| 134 | if ((group = EC_KEY_get0_group(eckey)) == NULL || |
| 135 | (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || |
| 136 | sig == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 137 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 138 | return 0; |
| 139 | } |
| 140 | |
| 141 | ctx = BN_CTX_new(); |
| 142 | if (!ctx) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 143 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 144 | return 0; |
| 145 | } |
| 146 | BN_CTX_start(ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 147 | u1 = BN_CTX_get(ctx); |
| 148 | u2 = BN_CTX_get(ctx); |
| 149 | m = BN_CTX_get(ctx); |
| 150 | X = BN_CTX_get(ctx); |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 151 | if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 152 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 153 | goto err; |
| 154 | } |
| 155 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 156 | const BIGNUM *order = EC_GROUP_get0_order(group); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 157 | if (BN_is_zero(sig->r) || BN_is_negative(sig->r) || |
| 158 | BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) || |
| 159 | BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 160 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 161 | goto err; |
| 162 | } |
| 163 | /* calculate tmp1 = inv(S) mod order */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 164 | int no_inverse; |
| 165 | if (!BN_mod_inverse_odd(u2, &no_inverse, sig->s, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 166 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 167 | goto err; |
| 168 | } |
| 169 | if (!digest_to_bn(m, digest, digest_len, order)) { |
| 170 | goto err; |
| 171 | } |
| 172 | /* u1 = m * tmp mod order */ |
| 173 | if (!BN_mod_mul(u1, m, u2, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 174 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 175 | goto err; |
| 176 | } |
| 177 | /* u2 = r * w mod q */ |
| 178 | if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 179 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 180 | goto err; |
| 181 | } |
| 182 | |
| 183 | point = EC_POINT_new(group); |
| 184 | if (point == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 185 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 186 | goto err; |
| 187 | } |
| 188 | if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 189 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 190 | goto err; |
| 191 | } |
| 192 | if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 193 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 194 | goto err; |
| 195 | } |
| 196 | if (!BN_nnmod(u1, X, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 197 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 198 | goto err; |
| 199 | } |
| 200 | /* if the signature is correct u1 is equal to sig->r */ |
Robert Sloan | 2424d84 | 2017-05-01 07:46:28 -0700 | [diff] [blame] | 201 | if (BN_ucmp(u1, sig->r) != 0) { |
| 202 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
| 203 | goto err; |
| 204 | } |
| 205 | |
| 206 | ret = 1; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 207 | |
| 208 | err: |
David Benjamin | 4969cc9 | 2016-04-22 15:02:23 -0400 | [diff] [blame] | 209 | BN_CTX_end(ctx); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 210 | BN_CTX_free(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 211 | EC_POINT_free(point); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 212 | return ret; |
| 213 | } |
| 214 | |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 215 | static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp, |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 216 | BIGNUM **rp, const uint8_t *digest, |
| 217 | size_t digest_len) { |
| 218 | BN_CTX *ctx = NULL; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 219 | BIGNUM *k = NULL, *kinv = NULL, *r = NULL, *tmp = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 220 | EC_POINT *tmp_point = NULL; |
| 221 | const EC_GROUP *group; |
| 222 | int ret = 0; |
| 223 | |
| 224 | if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 225 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 226 | return 0; |
| 227 | } |
| 228 | |
| 229 | if (ctx_in == NULL) { |
| 230 | if ((ctx = BN_CTX_new()) == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 231 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 232 | return 0; |
| 233 | } |
| 234 | } else { |
| 235 | ctx = ctx_in; |
| 236 | } |
| 237 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 238 | k = BN_new(); |
| 239 | kinv = BN_new(); /* this value is later returned in *kinvp */ |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 240 | r = BN_new(); /* this value is later returned in *rp */ |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 241 | tmp = BN_new(); |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 242 | if (k == NULL || kinv == NULL || r == NULL || tmp == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 243 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 244 | goto err; |
| 245 | } |
| 246 | tmp_point = EC_POINT_new(group); |
| 247 | if (tmp_point == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 248 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 249 | goto err; |
| 250 | } |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 251 | |
| 252 | const BIGNUM *order = EC_GROUP_get0_order(group); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 253 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 254 | /* Check that the size of the group order is FIPS compliant (FIPS 186-4 |
| 255 | * B.5.2). */ |
| 256 | if (BN_num_bits(order) < 160) { |
| 257 | OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER); |
| 258 | goto err; |
| 259 | } |
| 260 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 261 | do { |
| 262 | /* If possible, we'll include the private key and message digest in the k |
| 263 | * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is |
| 264 | * being used. */ |
Robert Sloan | 8ff0355 | 2017-06-14 12:40:58 -0700 | [diff] [blame^] | 265 | if (eckey->fixed_k != NULL) { |
| 266 | if (!BN_copy(k, eckey->fixed_k)) { |
| 267 | goto err; |
| 268 | } |
| 269 | } else if (digest_len > 0) { |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 270 | do { |
| 271 | if (!BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey), |
| 272 | digest, digest_len, ctx)) { |
| 273 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED); |
| 274 | goto err; |
| 275 | } |
| 276 | } while (BN_is_zero(k)); |
| 277 | } else if (!BN_rand_range_ex(k, 1, order)) { |
| 278 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED); |
| 279 | goto err; |
| 280 | } |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 281 | |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 282 | /* Compute the inverse of k. The order is a prime, so use Fermat's Little |
| 283 | * Theorem. Note |ec_group_get_mont_data| may return NULL but |
| 284 | * |bn_mod_inverse_prime| allows this. */ |
| 285 | if (!bn_mod_inverse_prime(kinv, k, order, ctx, |
| 286 | ec_group_get_mont_data(group))) { |
| 287 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
| 288 | goto err; |
| 289 | } |
| 290 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 291 | /* We do not want timing information to leak the length of k, |
| 292 | * so we compute G*k using an equivalent scalar of fixed |
| 293 | * bit-length. */ |
| 294 | |
| 295 | if (!BN_add(k, k, order)) { |
| 296 | goto err; |
| 297 | } |
| 298 | if (BN_num_bits(k) <= BN_num_bits(order)) { |
| 299 | if (!BN_add(k, k, order)) { |
| 300 | goto err; |
| 301 | } |
| 302 | } |
| 303 | |
| 304 | /* compute r the x-coordinate of generator * k */ |
| 305 | if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 306 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 307 | goto err; |
| 308 | } |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 309 | if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, tmp, NULL, |
| 310 | ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 311 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 312 | goto err; |
| 313 | } |
| 314 | |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 315 | if (!BN_nnmod(r, tmp, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 316 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 317 | goto err; |
| 318 | } |
| 319 | } while (BN_is_zero(r)); |
| 320 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 321 | /* clear old values if necessary */ |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 322 | BN_clear_free(*rp); |
| 323 | BN_clear_free(*kinvp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 324 | |
| 325 | /* save the pre-computed values */ |
| 326 | *rp = r; |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 327 | *kinvp = kinv; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 328 | ret = 1; |
| 329 | |
| 330 | err: |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 331 | BN_clear_free(k); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 332 | if (!ret) { |
Robert Sloan | 572a4e2 | 2017-04-17 10:52:19 -0700 | [diff] [blame] | 333 | BN_clear_free(kinv); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 334 | BN_clear_free(r); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 335 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 336 | if (ctx_in == NULL) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 337 | BN_CTX_free(ctx); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 338 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 339 | EC_POINT_free(tmp_point); |
David Benjamin | c895d6b | 2016-08-11 13:26:41 -0400 | [diff] [blame] | 340 | BN_clear_free(tmp); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 341 | return ret; |
| 342 | } |
| 343 | |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 344 | int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, |
| 345 | BIGNUM **rp) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 346 | return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0); |
| 347 | } |
| 348 | |
| 349 | ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len, |
| 350 | const BIGNUM *in_kinv, const BIGNUM *in_r, |
Robert Sloan | a94fe05 | 2017-02-21 08:49:28 -0800 | [diff] [blame] | 351 | const EC_KEY *eckey) { |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 352 | int ok = 0; |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 353 | BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL; |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 354 | const BIGNUM *ckinv; |
| 355 | BN_CTX *ctx = NULL; |
| 356 | const EC_GROUP *group; |
| 357 | ECDSA_SIG *ret; |
| 358 | const BIGNUM *priv_key; |
| 359 | |
| 360 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 361 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 362 | return NULL; |
| 363 | } |
| 364 | |
| 365 | group = EC_KEY_get0_group(eckey); |
| 366 | priv_key = EC_KEY_get0_private_key(eckey); |
| 367 | |
| 368 | if (group == NULL || priv_key == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 369 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 370 | return NULL; |
| 371 | } |
| 372 | |
| 373 | ret = ECDSA_SIG_new(); |
| 374 | if (!ret) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 375 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 376 | return NULL; |
| 377 | } |
| 378 | s = ret->s; |
| 379 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 380 | if ((ctx = BN_CTX_new()) == NULL || |
| 381 | (tmp = BN_new()) == NULL || |
| 382 | (m = BN_new()) == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 383 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 384 | goto err; |
| 385 | } |
| 386 | |
Adam Langley | 4139edb | 2016-01-13 15:00:54 -0800 | [diff] [blame] | 387 | const BIGNUM *order = EC_GROUP_get0_order(group); |
| 388 | |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 389 | if (!digest_to_bn(m, digest, digest_len, order)) { |
| 390 | goto err; |
| 391 | } |
| 392 | for (;;) { |
| 393 | if (in_kinv == NULL || in_r == NULL) { |
| 394 | if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 395 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 396 | goto err; |
| 397 | } |
| 398 | ckinv = kinv; |
| 399 | } else { |
| 400 | ckinv = in_kinv; |
| 401 | if (BN_copy(ret->r, in_r) == NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 402 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 403 | goto err; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 408 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 409 | goto err; |
| 410 | } |
| 411 | if (!BN_mod_add_quick(s, tmp, m, order)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 412 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 413 | goto err; |
| 414 | } |
| 415 | if (!BN_mod_mul(s, s, ckinv, order, ctx)) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 416 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 417 | goto err; |
| 418 | } |
| 419 | if (BN_is_zero(s)) { |
| 420 | /* if kinv and r have been supplied by the caller |
| 421 | * don't to generate new kinv and r values */ |
| 422 | if (in_kinv != NULL && in_r != NULL) { |
Kenny Root | b849459 | 2015-09-25 02:29:14 +0000 | [diff] [blame] | 423 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 424 | goto err; |
| 425 | } |
| 426 | } else { |
| 427 | /* s != 0 => we have a valid signature */ |
| 428 | break; |
| 429 | } |
| 430 | } |
| 431 | |
| 432 | ok = 1; |
| 433 | |
| 434 | err: |
| 435 | if (!ok) { |
| 436 | ECDSA_SIG_free(ret); |
| 437 | ret = NULL; |
| 438 | } |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 439 | BN_CTX_free(ctx); |
| 440 | BN_clear_free(m); |
| 441 | BN_clear_free(tmp); |
Adam Langley | e9ada86 | 2015-05-11 17:20:37 -0700 | [diff] [blame] | 442 | BN_clear_free(kinv); |
Adam Langley | d9e397b | 2015-01-22 14:27:53 -0800 | [diff] [blame] | 443 | return ret; |
| 444 | } |