blob: 13b1ea98c6bc294bbc4e51edb101d6f1bfcbd567 [file] [log] [blame]
Adam Langley95c29f32014-06-20 12:00:00 -07001/* ====================================================================
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 Benjamin87897a82015-06-11 23:30:53 -040055#include <assert.h>
Adam Langley2b2d66d2015-01-30 17:08:37 -080056#include <string.h>
57
Adam Langley95c29f32014-06-20 12:00:00 -070058#include <openssl/bn.h>
David Benjamin87897a82015-06-11 23:30:53 -040059#include <openssl/bytestring.h>
Adam Langley95c29f32014-06-20 12:00:00 -070060#include <openssl/err.h>
61#include <openssl/mem.h>
Adam Langley95c29f32014-06-20 12:00:00 -070062
Adam Langleyaacb72c2017-05-02 14:25:39 -070063#include "../bn/internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070064#include "../ec/internal.h"
Adam Langleyaacb72c2017-05-02 14:25:39 -070065#include "../../internal.h"
Adam Langley95c29f32014-06-20 12:00:00 -070066
67
Adam Langley95c29f32014-06-20 12:00:00 -070068/* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian
69 * number and sets |out| to that value. It then truncates |out| so that it's,
70 * at most, as long as |order|. It returns one on success and zero otherwise. */
71static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len,
72 const BIGNUM *order) {
73 size_t num_bits;
74
75 num_bits = BN_num_bits(order);
76 /* Need to truncate digest if it is too long: first truncate whole
77 * bytes. */
78 if (8 * digest_len > num_bits) {
79 digest_len = (num_bits + 7) / 8;
80 }
81 if (!BN_bin2bn(digest, digest_len, out)) {
David Benjamin3570d732015-06-29 00:28:17 -040082 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -070083 return 0;
84 }
85
86 /* If still too long truncate remaining bits with a shift */
87 if ((8 * digest_len > num_bits) &&
88 !BN_rshift(out, out, 8 - (num_bits & 0x7))) {
David Benjamin3570d732015-06-29 00:28:17 -040089 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -070090 return 0;
91 }
92
93 return 1;
94}
95
96ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
Matthew Braithwaitec4796c92017-02-16 16:49:54 -080097 const EC_KEY *key) {
Adam Langley95c29f32014-06-20 12:00:00 -070098 return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
99}
100
101int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
Matthew Braithwaitec4796c92017-02-16 16:49:54 -0800102 const ECDSA_SIG *sig, const EC_KEY *eckey) {
Adam Langley5129e2d2014-07-25 10:06:44 -0700103 int ret = 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700104 BN_CTX *ctx;
Brian Smitha3d9de02015-11-18 17:07:14 -1000105 BIGNUM *u1, *u2, *m, *X;
Adam Langley95c29f32014-06-20 12:00:00 -0700106 EC_POINT *point = NULL;
107 const EC_GROUP *group;
108 const EC_POINT *pub_key;
109
Adam Langley95c29f32014-06-20 12:00:00 -0700110 /* check input values */
Adam Langleyb2cb0ec2014-09-02 14:28:49 -0700111 if ((group = EC_KEY_get0_group(eckey)) == NULL ||
112 (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
113 sig == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400114 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
Adam Langley5129e2d2014-07-25 10:06:44 -0700115 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700116 }
117
118 ctx = BN_CTX_new();
119 if (!ctx) {
David Benjamin3570d732015-06-29 00:28:17 -0400120 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley5129e2d2014-07-25 10:06:44 -0700121 return 0;
Adam Langley95c29f32014-06-20 12:00:00 -0700122 }
123 BN_CTX_start(ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700124 u1 = BN_CTX_get(ctx);
125 u2 = BN_CTX_get(ctx);
126 m = BN_CTX_get(ctx);
127 X = BN_CTX_get(ctx);
Brian Smitha3d9de02015-11-18 17:07:14 -1000128 if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400129 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700130 goto err;
131 }
132
Brian Smitha3d9de02015-11-18 17:07:14 -1000133 const BIGNUM *order = EC_GROUP_get0_order(group);
Adam Langley95c29f32014-06-20 12:00:00 -0700134 if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
135 BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
136 BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
David Benjamin3570d732015-06-29 00:28:17 -0400137 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700138 goto err;
139 }
140 /* calculate tmp1 = inv(S) mod order */
Brian Smitha4327572016-08-02 16:58:57 -1000141 int no_inverse;
142 if (!BN_mod_inverse_odd(u2, &no_inverse, sig->s, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400143 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700144 goto err;
145 }
146 if (!digest_to_bn(m, digest, digest_len, order)) {
147 goto err;
148 }
149 /* u1 = m * tmp mod order */
150 if (!BN_mod_mul(u1, m, u2, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400151 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700152 goto err;
153 }
154 /* u2 = r * w mod q */
155 if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400156 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700157 goto err;
158 }
159
160 point = EC_POINT_new(group);
161 if (point == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400162 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700163 goto err;
164 }
165 if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400166 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700167 goto err;
168 }
169 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400170 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700171 goto err;
172 }
173 if (!BN_nnmod(u1, X, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400174 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700175 goto err;
176 }
177 /* if the signature is correct u1 is equal to sig->r */
David Benjamin7ed2e822017-04-28 17:31:43 -0400178 if (BN_ucmp(u1, sig->r) != 0) {
179 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
180 goto err;
181 }
182
183 ret = 1;
Adam Langley95c29f32014-06-20 12:00:00 -0700184
185err:
Matt Braithwaite0b553eb2016-01-15 16:55:41 -0800186 BN_CTX_end(ctx);
Adam Langley95c29f32014-06-20 12:00:00 -0700187 BN_CTX_free(ctx);
David Benjamincca4ba72015-04-22 15:49:27 -0400188 EC_POINT_free(point);
Adam Langley95c29f32014-06-20 12:00:00 -0700189 return ret;
190}
191
Matthew Braithwaitec4796c92017-02-16 16:49:54 -0800192static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
Adam Langleyd4b4f082014-06-20 12:00:00 -0700193 BIGNUM **rp, const uint8_t *digest,
194 size_t digest_len) {
Adam Langley95c29f32014-06-20 12:00:00 -0700195 BN_CTX *ctx = NULL;
Steven Valdez89abf7a2017-04-12 15:56:14 -0400196 BIGNUM *k = NULL, *kinv = NULL, *r = NULL, *tmp = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700197 EC_POINT *tmp_point = NULL;
198 const EC_GROUP *group;
199 int ret = 0;
200
201 if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400202 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
Adam Langley95c29f32014-06-20 12:00:00 -0700203 return 0;
204 }
205
206 if (ctx_in == NULL) {
207 if ((ctx = BN_CTX_new()) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400208 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700209 return 0;
210 }
211 } else {
212 ctx = ctx_in;
213 }
214
Steven Valdez89abf7a2017-04-12 15:56:14 -0400215 k = BN_new();
216 kinv = BN_new(); /* this value is later returned in *kinvp */
Adam Langley95c29f32014-06-20 12:00:00 -0700217 r = BN_new(); /* this value is later returned in *rp */
David Benjamin8cf79af2016-06-16 14:58:36 -0400218 tmp = BN_new();
Steven Valdez89abf7a2017-04-12 15:56:14 -0400219 if (k == NULL || kinv == NULL || r == NULL || tmp == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400220 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700221 goto err;
222 }
223 tmp_point = EC_POINT_new(group);
224 if (tmp_point == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400225 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700226 goto err;
227 }
Brian Smitha3d9de02015-11-18 17:07:14 -1000228
229 const BIGNUM *order = EC_GROUP_get0_order(group);
Adam Langley95c29f32014-06-20 12:00:00 -0700230
Steven Valdez89abf7a2017-04-12 15:56:14 -0400231 /* Check that the size of the group order is FIPS compliant (FIPS 186-4
232 * B.5.2). */
233 if (BN_num_bits(order) < 160) {
234 OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER);
235 goto err;
236 }
237
Adam Langley95c29f32014-06-20 12:00:00 -0700238 do {
Adam Langleyd4b4f082014-06-20 12:00:00 -0700239 /* If possible, we'll include the private key and message digest in the k
240 * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
241 * being used. */
Brian Smith4edca0b2016-07-25 10:36:58 -1000242 if (digest_len > 0) {
243 do {
244 if (!BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
245 digest, digest_len, ctx)) {
246 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
247 goto err;
248 }
249 } while (BN_is_zero(k));
250 } else if (!BN_rand_range_ex(k, 1, order)) {
251 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
252 goto err;
253 }
Adam Langley95c29f32014-06-20 12:00:00 -0700254
Steven Valdez89abf7a2017-04-12 15:56:14 -0400255 /* Compute the inverse of k. The order is a prime, so use Fermat's Little
256 * Theorem. Note |ec_group_get_mont_data| may return NULL but
257 * |bn_mod_inverse_prime| allows this. */
258 if (!bn_mod_inverse_prime(kinv, k, order, ctx,
259 ec_group_get_mont_data(group))) {
260 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
261 goto err;
262 }
263
Adam Langley95c29f32014-06-20 12:00:00 -0700264 /* We do not want timing information to leak the length of k,
265 * so we compute G*k using an equivalent scalar of fixed
266 * bit-length. */
267
268 if (!BN_add(k, k, order)) {
269 goto err;
270 }
271 if (BN_num_bits(k) <= BN_num_bits(order)) {
272 if (!BN_add(k, k, order)) {
273 goto err;
274 }
275 }
276
277 /* compute r the x-coordinate of generator * k */
278 if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400279 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700280 goto err;
281 }
David Benjamin8cf79af2016-06-16 14:58:36 -0400282 if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, tmp, NULL,
283 ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400284 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700285 goto err;
286 }
287
David Benjamin8cf79af2016-06-16 14:58:36 -0400288 if (!BN_nnmod(r, tmp, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400289 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700290 goto err;
291 }
292 } while (BN_is_zero(r));
293
Adam Langley95c29f32014-06-20 12:00:00 -0700294 /* clear old values if necessary */
David Benjamincca4ba72015-04-22 15:49:27 -0400295 BN_clear_free(*rp);
296 BN_clear_free(*kinvp);
Adam Langley95c29f32014-06-20 12:00:00 -0700297
298 /* save the pre-computed values */
299 *rp = r;
Steven Valdez89abf7a2017-04-12 15:56:14 -0400300 *kinvp = kinv;
Adam Langley95c29f32014-06-20 12:00:00 -0700301 ret = 1;
302
303err:
Steven Valdez89abf7a2017-04-12 15:56:14 -0400304 BN_clear_free(k);
Adam Langley95c29f32014-06-20 12:00:00 -0700305 if (!ret) {
Steven Valdez89abf7a2017-04-12 15:56:14 -0400306 BN_clear_free(kinv);
David Benjamincca4ba72015-04-22 15:49:27 -0400307 BN_clear_free(r);
Adam Langley95c29f32014-06-20 12:00:00 -0700308 }
David Benjamin9ab14e02015-02-11 01:17:18 -0500309 if (ctx_in == NULL) {
Adam Langley95c29f32014-06-20 12:00:00 -0700310 BN_CTX_free(ctx);
David Benjamin9ab14e02015-02-11 01:17:18 -0500311 }
David Benjamincca4ba72015-04-22 15:49:27 -0400312 EC_POINT_free(tmp_point);
David Benjamin8cf79af2016-06-16 14:58:36 -0400313 BN_clear_free(tmp);
Adam Langley95c29f32014-06-20 12:00:00 -0700314 return ret;
315}
316
Matthew Braithwaitec4796c92017-02-16 16:49:54 -0800317int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
318 BIGNUM **rp) {
Adam Langleyd4b4f082014-06-20 12:00:00 -0700319 return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
320}
321
Adam Langley95c29f32014-06-20 12:00:00 -0700322ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
323 const BIGNUM *in_kinv, const BIGNUM *in_r,
Matthew Braithwaitec4796c92017-02-16 16:49:54 -0800324 const EC_KEY *eckey) {
Adam Langley95c29f32014-06-20 12:00:00 -0700325 int ok = 0;
Brian Smitha3d9de02015-11-18 17:07:14 -1000326 BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
Adam Langley95c29f32014-06-20 12:00:00 -0700327 const BIGNUM *ckinv;
328 BN_CTX *ctx = NULL;
329 const EC_GROUP *group;
330 ECDSA_SIG *ret;
331 const BIGNUM *priv_key;
332
333 if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
David Benjamin3570d732015-06-29 00:28:17 -0400334 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
Adam Langley95c29f32014-06-20 12:00:00 -0700335 return NULL;
336 }
337
338 group = EC_KEY_get0_group(eckey);
339 priv_key = EC_KEY_get0_private_key(eckey);
340
341 if (group == NULL || priv_key == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400342 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
Adam Langley95c29f32014-06-20 12:00:00 -0700343 return NULL;
344 }
345
346 ret = ECDSA_SIG_new();
347 if (!ret) {
David Benjamin3570d732015-06-29 00:28:17 -0400348 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700349 return NULL;
350 }
351 s = ret->s;
352
Brian Smitha3d9de02015-11-18 17:07:14 -1000353 if ((ctx = BN_CTX_new()) == NULL ||
354 (tmp = BN_new()) == NULL ||
355 (m = BN_new()) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400356 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700357 goto err;
358 }
359
Brian Smitha3d9de02015-11-18 17:07:14 -1000360 const BIGNUM *order = EC_GROUP_get0_order(group);
361
Adam Langley95c29f32014-06-20 12:00:00 -0700362 if (!digest_to_bn(m, digest, digest_len, order)) {
363 goto err;
364 }
365 for (;;) {
366 if (in_kinv == NULL || in_r == NULL) {
Adam Langleyd4b4f082014-06-20 12:00:00 -0700367 if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
David Benjamin3570d732015-06-29 00:28:17 -0400368 OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700369 goto err;
370 }
371 ckinv = kinv;
372 } else {
373 ckinv = in_kinv;
374 if (BN_copy(ret->r, in_r) == NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400375 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
Adam Langley95c29f32014-06-20 12:00:00 -0700376 goto err;
377 }
378 }
379
380 if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400381 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700382 goto err;
383 }
384 if (!BN_mod_add_quick(s, tmp, m, order)) {
David Benjamin3570d732015-06-29 00:28:17 -0400385 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700386 goto err;
387 }
388 if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
David Benjamin3570d732015-06-29 00:28:17 -0400389 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
Adam Langley95c29f32014-06-20 12:00:00 -0700390 goto err;
391 }
392 if (BN_is_zero(s)) {
393 /* if kinv and r have been supplied by the caller
394 * don't to generate new kinv and r values */
395 if (in_kinv != NULL && in_r != NULL) {
David Benjamin3570d732015-06-29 00:28:17 -0400396 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES);
Adam Langley95c29f32014-06-20 12:00:00 -0700397 goto err;
398 }
399 } else {
400 /* s != 0 => we have a valid signature */
401 break;
402 }
403 }
404
405 ok = 1;
406
407err:
408 if (!ok) {
409 ECDSA_SIG_free(ret);
410 ret = NULL;
411 }
David Benjamincca4ba72015-04-22 15:49:27 -0400412 BN_CTX_free(ctx);
413 BN_clear_free(m);
414 BN_clear_free(tmp);
David Benjamincca4ba72015-04-22 15:49:27 -0400415 BN_clear_free(kinv);
Adam Langley95c29f32014-06-20 12:00:00 -0700416 return ret;
417}