blob: 99b11775c35ec87f6c5b6a984d424de0083ee8d4 [file] [log] [blame]
Adam Langleyd9e397b2015-01-22 14:27:53 -08001/* ====================================================================
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 Rootb8494592015-09-25 02:29:14 +000055#include <assert.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080056#include <string.h>
57
58#include <openssl/bn.h>
Kenny Rootb8494592015-09-25 02:29:14 +000059#include <openssl/bytestring.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080060#include <openssl/err.h>
61#include <openssl/mem.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080062
Robert Sloan69939df2017-01-09 10:53:07 -080063#include "../bn/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080064#include "../ec/internal.h"
Robert Sloan69939df2017-01-09 10:53:07 -080065#include "../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080066
67
68int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
Robert Sloana94fe052017-02-21 08:49:28 -080069 unsigned int *sig_len, const EC_KEY *eckey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080070 if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
Robert Sloana94fe052017-02-21 08:49:28 -080071 return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len,
72 (EC_KEY*) eckey /* cast away const */);
Adam Langleyd9e397b2015-01-22 14:27:53 -080073 }
74
75 return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
76 eckey);
77}
78
79int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
Robert Sloana94fe052017-02-21 08:49:28 -080080 const uint8_t *sig, size_t sig_len, const EC_KEY *eckey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080081 ECDSA_SIG *s;
82 int ret = 0;
83 uint8_t *der = NULL;
84
Kenny Rootb8494592015-09-25 02:29:14 +000085 /* Decode the ECDSA signature. */
86 s = ECDSA_SIG_from_bytes(sig, sig_len);
87 if (s == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -080088 goto err;
89 }
90
Kenny Rootb8494592015-09-25 02:29:14 +000091 /* Defend against potential laxness in the DER parser. */
92 size_t der_len;
93 if (!ECDSA_SIG_to_bytes(&der, &der_len, s) ||
Robert Sloan69939df2017-01-09 10:53:07 -080094 der_len != sig_len || OPENSSL_memcmp(sig, der, sig_len) != 0) {
Kenny Rootb8494592015-09-25 02:29:14 +000095 /* This should never happen. crypto/bytestring is strictly DER. */
96 OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
Adam Langleyd9e397b2015-01-22 14:27:53 -080097 goto err;
98 }
99
100 ret = ECDSA_do_verify(digest, digest_len, s, eckey);
101
102err:
Adam Langleye9ada862015-05-11 17:20:37 -0700103 OPENSSL_free(der);
104 ECDSA_SIG_free(s);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800105 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. */
111static 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 Rootb8494592015-09-25 02:29:14 +0000122 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800123 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 Rootb8494592015-09-25 02:29:14 +0000129 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800130 return 0;
131 }
132
133 return 1;
134}
135
136ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
Robert Sloana94fe052017-02-21 08:49:28 -0800137 const EC_KEY *key) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800138 return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
139}
140
141int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
Robert Sloana94fe052017-02-21 08:49:28 -0800142 const ECDSA_SIG *sig, const EC_KEY *eckey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800143 int ret = 0;
144 BN_CTX *ctx;
Adam Langley4139edb2016-01-13 15:00:54 -0800145 BIGNUM *u1, *u2, *m, *X;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800146 EC_POINT *point = NULL;
147 const EC_GROUP *group;
148 const EC_POINT *pub_key;
149
Adam Langleyd9e397b2015-01-22 14:27:53 -0800150 /* 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 Rootb8494592015-09-25 02:29:14 +0000154 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800155 return 0;
156 }
157
158 ctx = BN_CTX_new();
159 if (!ctx) {
Kenny Rootb8494592015-09-25 02:29:14 +0000160 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161 return 0;
162 }
163 BN_CTX_start(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800164 u1 = BN_CTX_get(ctx);
165 u2 = BN_CTX_get(ctx);
166 m = BN_CTX_get(ctx);
167 X = BN_CTX_get(ctx);
Adam Langley4139edb2016-01-13 15:00:54 -0800168 if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000169 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800170 goto err;
171 }
172
Adam Langley4139edb2016-01-13 15:00:54 -0800173 const BIGNUM *order = EC_GROUP_get0_order(group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800174 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 Rootb8494592015-09-25 02:29:14 +0000177 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800178 goto err;
179 }
180 /* calculate tmp1 = inv(S) mod order */
David Benjaminc895d6b2016-08-11 13:26:41 -0400181 int no_inverse;
182 if (!BN_mod_inverse_odd(u2, &no_inverse, sig->s, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000183 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800184 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 Rootb8494592015-09-25 02:29:14 +0000191 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800192 goto err;
193 }
194 /* u2 = r * w mod q */
195 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000196 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800197 goto err;
198 }
199
200 point = EC_POINT_new(group);
201 if (point == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000202 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800203 goto err;
204 }
205 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000206 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800207 goto err;
208 }
209 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000210 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800211 goto err;
212 }
213 if (!BN_nnmod(u1, X, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000214 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800215 goto err;
216 }
217 /* if the signature is correct u1 is equal to sig->r */
Robert Sloan2424d842017-05-01 07:46:28 -0700218 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 Langleyd9e397b2015-01-22 14:27:53 -0800224
225err:
David Benjamin4969cc92016-04-22 15:02:23 -0400226 BN_CTX_end(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800227 BN_CTX_free(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700228 EC_POINT_free(point);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800229 return ret;
230}
231
Robert Sloana94fe052017-02-21 08:49:28 -0800232static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800233 BIGNUM **rp, const uint8_t *digest,
234 size_t digest_len) {
235 BN_CTX *ctx = NULL;
Robert Sloan572a4e22017-04-17 10:52:19 -0700236 BIGNUM *k = NULL, *kinv = NULL, *r = NULL, *tmp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800237 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 Rootb8494592015-09-25 02:29:14 +0000242 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800243 return 0;
244 }
245
246 if (ctx_in == NULL) {
247 if ((ctx = BN_CTX_new()) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000248 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800249 return 0;
250 }
251 } else {
252 ctx = ctx_in;
253 }
254
Robert Sloan572a4e22017-04-17 10:52:19 -0700255 k = BN_new();
256 kinv = BN_new(); /* this value is later returned in *kinvp */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800257 r = BN_new(); /* this value is later returned in *rp */
David Benjaminc895d6b2016-08-11 13:26:41 -0400258 tmp = BN_new();
Robert Sloan572a4e22017-04-17 10:52:19 -0700259 if (k == NULL || kinv == NULL || r == NULL || tmp == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000260 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800261 goto err;
262 }
263 tmp_point = EC_POINT_new(group);
264 if (tmp_point == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000265 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800266 goto err;
267 }
Adam Langley4139edb2016-01-13 15:00:54 -0800268
269 const BIGNUM *order = EC_GROUP_get0_order(group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800270
Robert Sloan572a4e22017-04-17 10:52:19 -0700271 /* 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 Langleyd9e397b2015-01-22 14:27:53 -0800278 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 Benjaminc895d6b2016-08-11 13:26:41 -0400282 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 Langleyd9e397b2015-01-22 14:27:53 -0800294
Robert Sloan572a4e22017-04-17 10:52:19 -0700295 /* 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 Langleyd9e397b2015-01-22 14:27:53 -0800304 /* 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 Rootb8494592015-09-25 02:29:14 +0000319 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800320 goto err;
321 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400322 if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, tmp, NULL,
323 ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000324 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800325 goto err;
326 }
327
David Benjaminc895d6b2016-08-11 13:26:41 -0400328 if (!BN_nnmod(r, tmp, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000329 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800330 goto err;
331 }
332 } while (BN_is_zero(r));
333
Adam Langleyd9e397b2015-01-22 14:27:53 -0800334 /* clear old values if necessary */
Adam Langleye9ada862015-05-11 17:20:37 -0700335 BN_clear_free(*rp);
336 BN_clear_free(*kinvp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800337
338 /* save the pre-computed values */
339 *rp = r;
Robert Sloan572a4e22017-04-17 10:52:19 -0700340 *kinvp = kinv;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341 ret = 1;
342
343err:
Robert Sloan572a4e22017-04-17 10:52:19 -0700344 BN_clear_free(k);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800345 if (!ret) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700346 BN_clear_free(kinv);
Adam Langleye9ada862015-05-11 17:20:37 -0700347 BN_clear_free(r);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800348 }
Adam Langleye9ada862015-05-11 17:20:37 -0700349 if (ctx_in == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800350 BN_CTX_free(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700351 }
Adam Langleye9ada862015-05-11 17:20:37 -0700352 EC_POINT_free(tmp_point);
David Benjaminc895d6b2016-08-11 13:26:41 -0400353 BN_clear_free(tmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800354 return ret;
355}
356
Robert Sloana94fe052017-02-21 08:49:28 -0800357int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
358 BIGNUM **rp) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800359 return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
360}
361
362ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
363 const BIGNUM *in_kinv, const BIGNUM *in_r,
Robert Sloana94fe052017-02-21 08:49:28 -0800364 const EC_KEY *eckey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800365 int ok = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800366 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800367 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 Rootb8494592015-09-25 02:29:14 +0000374 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800375 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 Rootb8494592015-09-25 02:29:14 +0000382 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800383 return NULL;
384 }
385
386 ret = ECDSA_SIG_new();
387 if (!ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000388 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800389 return NULL;
390 }
391 s = ret->s;
392
Adam Langley4139edb2016-01-13 15:00:54 -0800393 if ((ctx = BN_CTX_new()) == NULL ||
394 (tmp = BN_new()) == NULL ||
395 (m = BN_new()) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000396 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800397 goto err;
398 }
399
Adam Langley4139edb2016-01-13 15:00:54 -0800400 const BIGNUM *order = EC_GROUP_get0_order(group);
401
Adam Langleyd9e397b2015-01-22 14:27:53 -0800402 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 Rootb8494592015-09-25 02:29:14 +0000408 OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409 goto err;
410 }
411 ckinv = kinv;
412 } else {
413 ckinv = in_kinv;
414 if (BN_copy(ret->r, in_r) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000415 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800416 goto err;
417 }
418 }
419
420 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000421 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800422 goto err;
423 }
424 if (!BN_mod_add_quick(s, tmp, m, order)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000425 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800426 goto err;
427 }
428 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000429 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800430 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 Rootb8494592015-09-25 02:29:14 +0000436 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800437 goto err;
438 }
439 } else {
440 /* s != 0 => we have a valid signature */
441 break;
442 }
443 }
444
445 ok = 1;
446
447err:
448 if (!ok) {
449 ECDSA_SIG_free(ret);
450 ret = NULL;
451 }
Adam Langleye9ada862015-05-11 17:20:37 -0700452 BN_CTX_free(ctx);
453 BN_clear_free(m);
454 BN_clear_free(tmp);
Adam Langleye9ada862015-05-11 17:20:37 -0700455 BN_clear_free(kinv);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800456 return ret;
457}
458
459int 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 Sloana94fe052017-02-21 08:49:28 -0800461 const BIGNUM *r, const EC_KEY *eckey) {
Kenny Rootb8494592015-09-25 02:29:14 +0000462 int ret = 0;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800463 ECDSA_SIG *s = NULL;
464
465 if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
Kenny Rootb8494592015-09-25 02:29:14 +0000466 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800467 *sig_len = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000468 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800469 }
470
471 s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
472 if (s == NULL) {
473 *sig_len = 0;
Kenny Rootb8494592015-09-25 02:29:14 +0000474 goto err;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800475 }
Kenny Rootb8494592015-09-25 02:29:14 +0000476
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
491err:
Adam Langleyd9e397b2015-01-22 14:27:53 -0800492 ECDSA_SIG_free(s);
Kenny Rootb8494592015-09-25 02:29:14 +0000493 return ret;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800494}