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