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
55 #include <assert.h>
56 #include <string.h>
57
58 #include <openssl/bn.h>
59 #include <openssl/err.h>
60 #include <openssl/mem.h>
61 #include <openssl/sha.h>
62 #include <openssl/type_check.h>
63
64 #include "../bn/internal.h"
65 #include "../ec/internal.h"
66 #include "../../internal.h"
67
68
69 // EC_LOOSE_SCALAR is like |EC_SCALAR| but is bounded by 2^|BN_num_bits(order)|
70 // rather than |order|.
71 typedef union {
72 // bytes is the representation of the scalar in little-endian order.
73 uint8_t bytes[EC_MAX_SCALAR_BYTES];
74 BN_ULONG words[EC_MAX_SCALAR_WORDS];
75 } EC_LOOSE_SCALAR;
76
scalar_add_loose(const EC_GROUP * group,EC_LOOSE_SCALAR * r,const EC_LOOSE_SCALAR * a,const EC_SCALAR * b)77 static void scalar_add_loose(const EC_GROUP *group, EC_LOOSE_SCALAR *r,
78 const EC_LOOSE_SCALAR *a, const EC_SCALAR *b) {
79 // Add and subtract one copy of |order| if necessary. We have:
80 // |a| + |b| < 2^BN_num_bits(order) + order
81 // so this leaves |r| < 2^BN_num_bits(order).
82 const BIGNUM *order = &group->order;
83 BN_ULONG carry = bn_add_words(r->words, a->words, b->words, order->top);
84 EC_LOOSE_SCALAR tmp;
85 BN_ULONG v = bn_sub_words(tmp.words, r->words, order->d, order->top) - carry;
86 v = 0u - v;
87 for (int i = 0; i < order->top; i++) {
88 OPENSSL_COMPILE_ASSERT(sizeof(BN_ULONG) <= sizeof(crypto_word_t),
89 crypto_word_t_too_small);
90 r->words[i] = constant_time_select_w(v, r->words[i], tmp.words[i]);
91 }
92 }
93
scalar_mod_mul_montgomery(const EC_GROUP * group,EC_SCALAR * r,const EC_SCALAR * a,const EC_SCALAR * b)94 static int scalar_mod_mul_montgomery(const EC_GROUP *group, EC_SCALAR *r,
95 const EC_SCALAR *a, const EC_SCALAR *b) {
96 const BIGNUM *order = &group->order;
97 return bn_mod_mul_montgomery_small(r->words, order->top, a->words, order->top,
98 b->words, order->top, group->order_mont);
99 }
100
scalar_mod_mul_montgomery_loose(const EC_GROUP * group,EC_SCALAR * r,const EC_LOOSE_SCALAR * a,const EC_SCALAR * b)101 static int scalar_mod_mul_montgomery_loose(const EC_GROUP *group, EC_SCALAR *r,
102 const EC_LOOSE_SCALAR *a,
103 const EC_SCALAR *b) {
104 // Although |a| is loose, |bn_mod_mul_montgomery_small| only requires the
105 // product not exceed R * |order|. |b| is fully reduced and |a| <
106 // 2^BN_num_bits(order) <= R, so this holds.
107 const BIGNUM *order = &group->order;
108 return bn_mod_mul_montgomery_small(r->words, order->top, a->words, order->top,
109 b->words, order->top, group->order_mont);
110 }
111
112 // digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for
113 // ECDSA. Note this value is not fully reduced modulo the order, only the
114 // correct number of bits.
digest_to_scalar(const EC_GROUP * group,EC_LOOSE_SCALAR * out,const uint8_t * digest,size_t digest_len)115 static void digest_to_scalar(const EC_GROUP *group, EC_LOOSE_SCALAR *out,
116 const uint8_t *digest, size_t digest_len) {
117 const BIGNUM *order = &group->order;
118 size_t num_bits = BN_num_bits(order);
119 // Need to truncate digest if it is too long: first truncate whole bytes.
120 if (8 * digest_len > num_bits) {
121 digest_len = (num_bits + 7) / 8;
122 }
123 OPENSSL_memset(out, 0, sizeof(EC_SCALAR));
124 for (size_t i = 0; i < digest_len; i++) {
125 out->bytes[i] = digest[digest_len - 1 - i];
126 }
127
128 // If still too long truncate remaining bits with a shift
129 if (8 * digest_len > num_bits) {
130 size_t shift = 8 - (num_bits & 0x7);
131 for (int i = 0; i < order->top - 1; i++) {
132 out->words[i] =
133 (out->words[i] >> shift) | (out->words[i + 1] << (BN_BITS2 - shift));
134 }
135 out->words[order->top - 1] >>= shift;
136 }
137 }
138
139 // field_element_to_scalar reduces |r| modulo |group->order|. |r| must
140 // previously have been reduced modulo |group->field|.
field_element_to_scalar(const EC_GROUP * group,BIGNUM * r)141 static int field_element_to_scalar(const EC_GROUP *group, BIGNUM *r) {
142 // We must have p < 2×order, assuming p is not tiny (p >= 17). Thus rather we
143 // can reduce by performing at most one subtraction.
144 //
145 // Proof: We only work with prime order curves, so the number of points on
146 // the curve is the order. Thus Hasse's theorem gives:
147 //
148 // |order - (p + 1)| <= 2×sqrt(p)
149 // p + 1 - order <= 2×sqrt(p)
150 // p + 1 - 2×sqrt(p) <= order
151 // p + 1 - 2×(p/4) < order (p/4 > sqrt(p) for p >= 17)
152 // p/2 < p/2 + 1 < order
153 // p < 2×order
154 //
155 // Additionally, one can manually check this property for built-in curves. It
156 // is enforced for legacy custom curves in |EC_GROUP_set_generator|.
157 //
158 // TODO(davidben): Introduce |EC_FIELD_ELEMENT|, make this a function from
159 // |EC_FIELD_ELEMENT| to |EC_SCALAR|, and cut out the |BIGNUM|. Does this need
160 // to be constant-time for signing? |r| is the x-coordinate for kG, which is
161 // public unless k was rerolled because |s| was zero.
162 assert(!BN_is_negative(r));
163 assert(BN_cmp(r, &group->field) < 0);
164 if (BN_cmp(r, &group->order) >= 0 &&
165 !BN_sub(r, r, &group->order)) {
166 return 0;
167 }
168 assert(!BN_is_negative(r));
169 assert(BN_cmp(r, &group->order) < 0);
170 return 1;
171 }
172
ECDSA_SIG_new(void)173 ECDSA_SIG *ECDSA_SIG_new(void) {
174 ECDSA_SIG *sig = OPENSSL_malloc(sizeof(ECDSA_SIG));
175 if (sig == NULL) {
176 return NULL;
177 }
178 sig->r = BN_new();
179 sig->s = BN_new();
180 if (sig->r == NULL || sig->s == NULL) {
181 ECDSA_SIG_free(sig);
182 return NULL;
183 }
184 return sig;
185 }
186
ECDSA_SIG_free(ECDSA_SIG * sig)187 void ECDSA_SIG_free(ECDSA_SIG *sig) {
188 if (sig == NULL) {
189 return;
190 }
191
192 BN_free(sig->r);
193 BN_free(sig->s);
194 OPENSSL_free(sig);
195 }
196
ECDSA_SIG_get0(const ECDSA_SIG * sig,const BIGNUM ** out_r,const BIGNUM ** out_s)197 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r,
198 const BIGNUM **out_s) {
199 if (out_r != NULL) {
200 *out_r = sig->r;
201 }
202 if (out_s != NULL) {
203 *out_s = sig->s;
204 }
205 }
206
ECDSA_SIG_set0(ECDSA_SIG * sig,BIGNUM * r,BIGNUM * s)207 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
208 if (r == NULL || s == NULL) {
209 return 0;
210 }
211 BN_free(sig->r);
212 BN_free(sig->s);
213 sig->r = r;
214 sig->s = s;
215 return 1;
216 }
217
ECDSA_do_verify(const uint8_t * digest,size_t digest_len,const ECDSA_SIG * sig,const EC_KEY * eckey)218 int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
219 const ECDSA_SIG *sig, const EC_KEY *eckey) {
220 const EC_GROUP *group = EC_KEY_get0_group(eckey);
221 const EC_POINT *pub_key = EC_KEY_get0_public_key(eckey);
222 if (group == NULL || pub_key == NULL || sig == NULL) {
223 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
224 return 0;
225 }
226
227 BN_CTX *ctx = BN_CTX_new();
228 if (!ctx) {
229 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
230 return 0;
231 }
232 int ret = 0;
233 EC_POINT *point = NULL;
234 BN_CTX_start(ctx);
235 BIGNUM *X = BN_CTX_get(ctx);
236 if (X == NULL) {
237 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
238 goto err;
239 }
240
241 EC_SCALAR r, s, u1, u2, s_inv_mont;
242 EC_LOOSE_SCALAR m;
243 const BIGNUM *order = EC_GROUP_get0_order(group);
244 if (BN_is_zero(sig->r) ||
245 !ec_bignum_to_scalar(group, &r, sig->r) ||
246 BN_is_zero(sig->s) ||
247 !ec_bignum_to_scalar(group, &s, sig->s)) {
248 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
249 goto err;
250 }
251 // s_inv_mont = s^-1 mod order. We convert the result to Montgomery form for
252 // the products below.
253 int no_inverse;
254 if (!BN_mod_inverse_odd(X, &no_inverse, sig->s, order, ctx) ||
255 // TODO(davidben): Add a words version of |BN_mod_inverse_odd| and write
256 // into |s_inv_mont| directly.
257 !ec_bignum_to_scalar_unchecked(group, &s_inv_mont, X) ||
258 !bn_to_montgomery_small(s_inv_mont.words, order->top, s_inv_mont.words,
259 order->top, group->order_mont)) {
260 goto err;
261 }
262 // u1 = m * s^-1 mod order
263 // u2 = r * s^-1 mod order
264 //
265 // |s_inv_mont| is in Montgomery form while |m| and |r| are not, so |u1| and
266 // |u2| will be taken out of Montgomery form, as desired.
267 digest_to_scalar(group, &m, digest, digest_len);
268 if (!scalar_mod_mul_montgomery_loose(group, &u1, &m, &s_inv_mont) ||
269 !scalar_mod_mul_montgomery(group, &u2, &r, &s_inv_mont)) {
270 goto err;
271 }
272
273 point = EC_POINT_new(group);
274 if (point == NULL) {
275 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
276 goto err;
277 }
278 if (!ec_point_mul_scalar_public(group, point, &u1, pub_key, &u2, ctx)) {
279 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
280 goto err;
281 }
282 if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
283 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
284 goto err;
285 }
286 if (!field_element_to_scalar(group, X)) {
287 OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
288 goto err;
289 }
290 // The signature is correct iff |X| is equal to |sig->r|.
291 if (BN_ucmp(X, sig->r) != 0) {
292 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
293 goto err;
294 }
295
296 ret = 1;
297
298 err:
299 BN_CTX_end(ctx);
300 BN_CTX_free(ctx);
301 EC_POINT_free(point);
302 return ret;
303 }
304
ecdsa_sign_setup(const EC_KEY * eckey,BN_CTX * ctx,EC_SCALAR * out_kinv_mont,BIGNUM ** rp,const uint8_t * digest,size_t digest_len,const EC_SCALAR * priv_key)305 static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx,
306 EC_SCALAR *out_kinv_mont, BIGNUM **rp,
307 const uint8_t *digest, size_t digest_len,
308 const EC_SCALAR *priv_key) {
309 EC_POINT *tmp_point = NULL;
310 int ret = 0;
311 EC_SCALAR k;
312 BIGNUM *r = BN_new(); // this value is later returned in *rp
313 if (r == NULL) {
314 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
315 goto err;
316 }
317 const EC_GROUP *group = EC_KEY_get0_group(eckey);
318 const BIGNUM *order = EC_GROUP_get0_order(group);
319 tmp_point = EC_POINT_new(group);
320 if (tmp_point == NULL) {
321 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
322 goto err;
323 }
324
325 // Check that the size of the group order is FIPS compliant (FIPS 186-4
326 // B.5.2).
327 if (BN_num_bits(order) < 160) {
328 OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER);
329 goto err;
330 }
331
332 do {
333 // Include the private key and message digest in the k generation.
334 if (eckey->fixed_k != NULL) {
335 if (!ec_bignum_to_scalar(group, &k, eckey->fixed_k)) {
336 goto err;
337 }
338 } else {
339 // Pass a SHA512 hash of the private key and digest as additional data
340 // into the RBG. This is a hardening measure against entropy failure.
341 OPENSSL_COMPILE_ASSERT(SHA512_DIGEST_LENGTH >= 32,
342 additional_data_is_too_large_for_sha512);
343 SHA512_CTX sha;
344 uint8_t additional_data[SHA512_DIGEST_LENGTH];
345 SHA512_Init(&sha);
346 SHA512_Update(&sha, priv_key->words, order->top * sizeof(BN_ULONG));
347 SHA512_Update(&sha, digest, digest_len);
348 SHA512_Final(additional_data, &sha);
349 if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
350 goto err;
351 }
352 }
353
354 // Compute k^-1. We leave it in the Montgomery domain as an optimization for
355 // later operations.
356 if (!bn_to_montgomery_small(out_kinv_mont->words, order->top, k.words,
357 order->top, group->order_mont) ||
358 !bn_mod_inverse_prime_mont_small(out_kinv_mont->words, order->top,
359 out_kinv_mont->words, order->top,
360 group->order_mont)) {
361 goto err;
362 }
363
364 // Compute r, the x-coordinate of generator * k.
365 if (!ec_point_mul_scalar(group, tmp_point, &k, NULL, NULL, ctx) ||
366 !EC_POINT_get_affine_coordinates_GFp(group, tmp_point, r, NULL,
367 ctx)) {
368 goto err;
369 }
370
371 if (!field_element_to_scalar(group, r)) {
372 goto err;
373 }
374 } while (BN_is_zero(r));
375
376 BN_clear_free(*rp);
377 *rp = r;
378 r = NULL;
379 ret = 1;
380
381 err:
382 OPENSSL_cleanse(&k, sizeof(k));
383 BN_clear_free(r);
384 EC_POINT_free(tmp_point);
385 return ret;
386 }
387
ECDSA_do_sign(const uint8_t * digest,size_t digest_len,const EC_KEY * eckey)388 ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
389 const EC_KEY *eckey) {
390 if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
391 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
392 return NULL;
393 }
394
395 const EC_GROUP *group = EC_KEY_get0_group(eckey);
396 const BIGNUM *priv_key_bn = EC_KEY_get0_private_key(eckey);
397 if (group == NULL || priv_key_bn == NULL) {
398 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
399 return NULL;
400 }
401 const BIGNUM *order = EC_GROUP_get0_order(group);
402
403 int ok = 0;
404 ECDSA_SIG *ret = ECDSA_SIG_new();
405 BN_CTX *ctx = BN_CTX_new();
406 EC_SCALAR kinv_mont, priv_key, r_mont, s;
407 EC_LOOSE_SCALAR m, tmp;
408 if (ret == NULL || ctx == NULL) {
409 OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
410 return NULL;
411 }
412
413 digest_to_scalar(group, &m, digest, digest_len);
414 // TODO(davidben): Store the private key as an |EC_SCALAR| so this is obvious
415 // via the type system.
416 if (!ec_bignum_to_scalar_unchecked(group, &priv_key, priv_key_bn)) {
417 goto err;
418 }
419 for (;;) {
420 if (!ecdsa_sign_setup(eckey, ctx, &kinv_mont, &ret->r, digest, digest_len,
421 &priv_key)) {
422 goto err;
423 }
424
425 // Compute priv_key * r (mod order). Note if only one parameter is in the
426 // Montgomery domain, |scalar_mod_mul_montgomery| will compute the answer in
427 // the normal domain.
428 if (!ec_bignum_to_scalar(group, &r_mont, ret->r) ||
429 !bn_to_montgomery_small(r_mont.words, order->top, r_mont.words,
430 order->top, group->order_mont) ||
431 !scalar_mod_mul_montgomery(group, &s, &priv_key, &r_mont)) {
432 goto err;
433 }
434
435 // Compute tmp = m + priv_key * r.
436 scalar_add_loose(group, &tmp, &m, &s);
437
438 // Finally, multiply s by k^-1. That was retained in Montgomery form, so the
439 // same technique as the previous multiplication works.
440 if (!scalar_mod_mul_montgomery_loose(group, &s, &tmp, &kinv_mont) ||
441 !bn_set_words(ret->s, s.words, order->top)) {
442 goto err;
443 }
444 if (!BN_is_zero(ret->s)) {
445 // s != 0 => we have a valid signature
446 break;
447 }
448 }
449
450 ok = 1;
451
452 err:
453 if (!ok) {
454 ECDSA_SIG_free(ret);
455 ret = NULL;
456 }
457 BN_CTX_free(ctx);
458 OPENSSL_cleanse(&kinv_mont, sizeof(kinv_mont));
459 OPENSSL_cleanse(&priv_key, sizeof(priv_key));
460 OPENSSL_cleanse(&r_mont, sizeof(r_mont));
461 OPENSSL_cleanse(&s, sizeof(s));
462 OPENSSL_cleanse(&tmp, sizeof(tmp));
463 OPENSSL_cleanse(&m, sizeof(m));
464 return ret;
465 }
466