blob: 9e719f23ad9a6eb289c3eb5ade0999a0caa7c5fa [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>
59#include <openssl/err.h>
60#include <openssl/mem.h>
Adam Langleyd9e397b2015-01-22 14:27:53 -080061
Robert Sloan69939df2017-01-09 10:53:07 -080062#include "../bn/internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080063#include "../ec/internal.h"
Robert Sloan8ff03552017-06-14 12:40:58 -070064#include "../../internal.h"
Adam Langleyd9e397b2015-01-22 14:27:53 -080065
66
Adam Langleyd9e397b2015-01-22 14:27:53 -080067/* 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. */
70static 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 Rootb8494592015-09-25 02:29:14 +000081 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -080082 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 Rootb8494592015-09-25 02:29:14 +000088 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -080089 return 0;
90 }
91
92 return 1;
93}
94
Robert Sloan8ff03552017-06-14 12:40:58 -070095ECDSA_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
109void 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 Langleyd9e397b2015-01-22 14:27:53 -0800119ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
Robert Sloana94fe052017-02-21 08:49:28 -0800120 const EC_KEY *key) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800121 return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
122}
123
124int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
Robert Sloana94fe052017-02-21 08:49:28 -0800125 const ECDSA_SIG *sig, const EC_KEY *eckey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800126 int ret = 0;
127 BN_CTX *ctx;
Adam Langley4139edb2016-01-13 15:00:54 -0800128 BIGNUM *u1, *u2, *m, *X;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800129 EC_POINT *point = NULL;
130 const EC_GROUP *group;
131 const EC_POINT *pub_key;
132
Adam Langleyd9e397b2015-01-22 14:27:53 -0800133 /* 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 Rootb8494592015-09-25 02:29:14 +0000137 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800138 return 0;
139 }
140
141 ctx = BN_CTX_new();
142 if (!ctx) {
Kenny Rootb8494592015-09-25 02:29:14 +0000143 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800144 return 0;
145 }
146 BN_CTX_start(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800147 u1 = BN_CTX_get(ctx);
148 u2 = BN_CTX_get(ctx);
149 m = BN_CTX_get(ctx);
150 X = BN_CTX_get(ctx);
Adam Langley4139edb2016-01-13 15:00:54 -0800151 if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000152 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800153 goto err;
154 }
155
Adam Langley4139edb2016-01-13 15:00:54 -0800156 const BIGNUM *order = EC_GROUP_get0_order(group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800157 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 Rootb8494592015-09-25 02:29:14 +0000160 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800161 goto err;
162 }
163 /* calculate tmp1 = inv(S) mod order */
David Benjaminc895d6b2016-08-11 13:26:41 -0400164 int no_inverse;
165 if (!BN_mod_inverse_odd(u2, &no_inverse, sig->s, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000166 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800167 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 Rootb8494592015-09-25 02:29:14 +0000174 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800175 goto err;
176 }
177 /* u2 = r * w mod q */
178 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000179 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800180 goto err;
181 }
182
183 point = EC_POINT_new(group);
184 if (point == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000185 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800186 goto err;
187 }
188 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000189 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800190 goto err;
191 }
192 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000193 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800194 goto err;
195 }
196 if (!BN_nnmod(u1, X, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000197 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800198 goto err;
199 }
200 /* if the signature is correct u1 is equal to sig->r */
Robert Sloan2424d842017-05-01 07:46:28 -0700201 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 Langleyd9e397b2015-01-22 14:27:53 -0800207
208err:
David Benjamin4969cc92016-04-22 15:02:23 -0400209 BN_CTX_end(ctx);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800210 BN_CTX_free(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700211 EC_POINT_free(point);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800212 return ret;
213}
214
Robert Sloana94fe052017-02-21 08:49:28 -0800215static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
Adam Langleyd9e397b2015-01-22 14:27:53 -0800216 BIGNUM **rp, const uint8_t *digest,
217 size_t digest_len) {
218 BN_CTX *ctx = NULL;
Robert Sloan572a4e22017-04-17 10:52:19 -0700219 BIGNUM *k = NULL, *kinv = NULL, *r = NULL, *tmp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800220 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 Rootb8494592015-09-25 02:29:14 +0000225 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800226 return 0;
227 }
228
229 if (ctx_in == NULL) {
230 if ((ctx = BN_CTX_new()) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000231 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800232 return 0;
233 }
234 } else {
235 ctx = ctx_in;
236 }
237
Robert Sloan572a4e22017-04-17 10:52:19 -0700238 k = BN_new();
239 kinv = BN_new(); /* this value is later returned in *kinvp */
Adam Langleyd9e397b2015-01-22 14:27:53 -0800240 r = BN_new(); /* this value is later returned in *rp */
David Benjaminc895d6b2016-08-11 13:26:41 -0400241 tmp = BN_new();
Robert Sloan572a4e22017-04-17 10:52:19 -0700242 if (k == NULL || kinv == NULL || r == NULL || tmp == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000243 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800244 goto err;
245 }
246 tmp_point = EC_POINT_new(group);
247 if (tmp_point == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000248 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800249 goto err;
250 }
Adam Langley4139edb2016-01-13 15:00:54 -0800251
252 const BIGNUM *order = EC_GROUP_get0_order(group);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800253
Robert Sloan572a4e22017-04-17 10:52:19 -0700254 /* 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 Langleyd9e397b2015-01-22 14:27:53 -0800261 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 Sloan8ff03552017-06-14 12:40:58 -0700265 if (eckey->fixed_k != NULL) {
266 if (!BN_copy(k, eckey->fixed_k)) {
267 goto err;
268 }
269 } else if (digest_len > 0) {
David Benjaminc895d6b2016-08-11 13:26:41 -0400270 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 Langleyd9e397b2015-01-22 14:27:53 -0800281
Robert Sloan572a4e22017-04-17 10:52:19 -0700282 /* 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 Langleyd9e397b2015-01-22 14:27:53 -0800291 /* 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 Rootb8494592015-09-25 02:29:14 +0000306 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800307 goto err;
308 }
David Benjaminc895d6b2016-08-11 13:26:41 -0400309 if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, tmp, NULL,
310 ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000311 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800312 goto err;
313 }
314
David Benjaminc895d6b2016-08-11 13:26:41 -0400315 if (!BN_nnmod(r, tmp, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000316 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800317 goto err;
318 }
319 } while (BN_is_zero(r));
320
Adam Langleyd9e397b2015-01-22 14:27:53 -0800321 /* clear old values if necessary */
Adam Langleye9ada862015-05-11 17:20:37 -0700322 BN_clear_free(*rp);
323 BN_clear_free(*kinvp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800324
325 /* save the pre-computed values */
326 *rp = r;
Robert Sloan572a4e22017-04-17 10:52:19 -0700327 *kinvp = kinv;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800328 ret = 1;
329
330err:
Robert Sloan572a4e22017-04-17 10:52:19 -0700331 BN_clear_free(k);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800332 if (!ret) {
Robert Sloan572a4e22017-04-17 10:52:19 -0700333 BN_clear_free(kinv);
Adam Langleye9ada862015-05-11 17:20:37 -0700334 BN_clear_free(r);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800335 }
Adam Langleye9ada862015-05-11 17:20:37 -0700336 if (ctx_in == NULL) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800337 BN_CTX_free(ctx);
Adam Langleye9ada862015-05-11 17:20:37 -0700338 }
Adam Langleye9ada862015-05-11 17:20:37 -0700339 EC_POINT_free(tmp_point);
David Benjaminc895d6b2016-08-11 13:26:41 -0400340 BN_clear_free(tmp);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800341 return ret;
342}
343
Robert Sloana94fe052017-02-21 08:49:28 -0800344int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
345 BIGNUM **rp) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800346 return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
347}
348
349ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
350 const BIGNUM *in_kinv, const BIGNUM *in_r,
Robert Sloana94fe052017-02-21 08:49:28 -0800351 const EC_KEY *eckey) {
Adam Langleyd9e397b2015-01-22 14:27:53 -0800352 int ok = 0;
Adam Langley4139edb2016-01-13 15:00:54 -0800353 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
Adam Langleyd9e397b2015-01-22 14:27:53 -0800354 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 Rootb8494592015-09-25 02:29:14 +0000361 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800362 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 Rootb8494592015-09-25 02:29:14 +0000369 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800370 return NULL;
371 }
372
373 ret = ECDSA_SIG_new();
374 if (!ret) {
Kenny Rootb8494592015-09-25 02:29:14 +0000375 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800376 return NULL;
377 }
378 s = ret->s;
379
Adam Langley4139edb2016-01-13 15:00:54 -0800380 if ((ctx = BN_CTX_new()) == NULL ||
381 (tmp = BN_new()) == NULL ||
382 (m = BN_new()) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000383 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800384 goto err;
385 }
386
Adam Langley4139edb2016-01-13 15:00:54 -0800387 const BIGNUM *order = EC_GROUP_get0_order(group);
388
Adam Langleyd9e397b2015-01-22 14:27:53 -0800389 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 Rootb8494592015-09-25 02:29:14 +0000395 OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800396 goto err;
397 }
398 ckinv = kinv;
399 } else {
400 ckinv = in_kinv;
401 if (BN_copy(ret->r, in_r) == NULL) {
Kenny Rootb8494592015-09-25 02:29:14 +0000402 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800403 goto err;
404 }
405 }
406
407 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000408 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800409 goto err;
410 }
411 if (!BN_mod_add_quick(s, tmp, m, order)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000412 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800413 goto err;
414 }
415 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
Kenny Rootb8494592015-09-25 02:29:14 +0000416 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800417 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 Rootb8494592015-09-25 02:29:14 +0000423 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800424 goto err;
425 }
426 } else {
427 /* s != 0 => we have a valid signature */
428 break;
429 }
430 }
431
432 ok = 1;
433
434err:
435 if (!ok) {
436 ECDSA_SIG_free(ret);
437 ret = NULL;
438 }
Adam Langleye9ada862015-05-11 17:20:37 -0700439 BN_CTX_free(ctx);
440 BN_clear_free(m);
441 BN_clear_free(tmp);
Adam Langleye9ada862015-05-11 17:20:37 -0700442 BN_clear_free(kinv);
Adam Langleyd9e397b2015-01-22 14:27:53 -0800443 return ret;
444}