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