/src/boringssl/crypto/fipsmodule/ecdsa/ecdsa.c
Line | Count | Source (jump to first uncovered line) |
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 | | |
63 | | #include "../../internal.h" |
64 | | #include "../bn/internal.h" |
65 | | #include "../ec/internal.h" |
66 | | #include "../service_indicator/internal.h" |
67 | | #include "internal.h" |
68 | | |
69 | | |
70 | | // digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for |
71 | | // ECDSA. |
72 | | static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out, |
73 | 1.34k | const uint8_t *digest, size_t digest_len) { |
74 | 1.34k | const BIGNUM *order = &group->order; |
75 | 1.34k | size_t num_bits = BN_num_bits(order); |
76 | | // Need to truncate digest if it is too long: first truncate whole bytes. |
77 | 1.34k | size_t num_bytes = (num_bits + 7) / 8; |
78 | 1.34k | if (digest_len > num_bytes) { |
79 | 226 | digest_len = num_bytes; |
80 | 226 | } |
81 | 1.34k | bn_big_endian_to_words(out->words, order->width, digest, digest_len); |
82 | | |
83 | | // If it is still too long, truncate remaining bits with a shift. |
84 | 1.34k | if (8 * digest_len > num_bits) { |
85 | 0 | bn_rshift_words(out->words, out->words, 8 - (num_bits & 0x7), order->width); |
86 | 0 | } |
87 | | |
88 | | // |out| now has the same bit width as |order|, but this only bounds by |
89 | | // 2*|order|. Subtract the order if out of range. |
90 | | // |
91 | | // Montgomery multiplication accepts the looser bounds, so this isn't strictly |
92 | | // necessary, but it is a cleaner abstraction and has no performance impact. |
93 | 1.34k | BN_ULONG tmp[EC_MAX_WORDS]; |
94 | 1.34k | bn_reduce_once_in_place(out->words, 0 /* no carry */, order->d, tmp, |
95 | 1.34k | order->width); |
96 | 1.34k | } |
97 | | |
98 | 16.2k | ECDSA_SIG *ECDSA_SIG_new(void) { |
99 | 16.2k | ECDSA_SIG *sig = OPENSSL_malloc(sizeof(ECDSA_SIG)); |
100 | 16.2k | if (sig == NULL) { |
101 | 0 | return NULL; |
102 | 0 | } |
103 | 16.2k | sig->r = BN_new(); |
104 | 16.2k | sig->s = BN_new(); |
105 | 16.2k | if (sig->r == NULL || sig->s == NULL) { |
106 | 0 | ECDSA_SIG_free(sig); |
107 | 0 | return NULL; |
108 | 0 | } |
109 | 16.2k | return sig; |
110 | 16.2k | } |
111 | | |
112 | 43.2k | void ECDSA_SIG_free(ECDSA_SIG *sig) { |
113 | 43.2k | if (sig == NULL) { |
114 | 26.9k | return; |
115 | 26.9k | } |
116 | | |
117 | 16.2k | BN_free(sig->r); |
118 | 16.2k | BN_free(sig->s); |
119 | 16.2k | OPENSSL_free(sig); |
120 | 16.2k | } |
121 | | |
122 | 0 | const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) { |
123 | 0 | return sig->r; |
124 | 0 | } |
125 | | |
126 | 0 | const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) { |
127 | 0 | return sig->s; |
128 | 0 | } |
129 | | |
130 | | void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r, |
131 | 0 | const BIGNUM **out_s) { |
132 | 0 | if (out_r != NULL) { |
133 | 0 | *out_r = sig->r; |
134 | 0 | } |
135 | 0 | if (out_s != NULL) { |
136 | 0 | *out_s = sig->s; |
137 | 0 | } |
138 | 0 | } |
139 | | |
140 | 0 | int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) { |
141 | 0 | if (r == NULL || s == NULL) { |
142 | 0 | return 0; |
143 | 0 | } |
144 | 0 | BN_free(sig->r); |
145 | 0 | BN_free(sig->s); |
146 | 0 | sig->r = r; |
147 | 0 | sig->s = s; |
148 | 0 | return 1; |
149 | 0 | } |
150 | | |
151 | | int ecdsa_do_verify_no_self_test(const uint8_t *digest, size_t digest_len, |
152 | 1.85k | const ECDSA_SIG *sig, const EC_KEY *eckey) { |
153 | 1.85k | const EC_GROUP *group = EC_KEY_get0_group(eckey); |
154 | 1.85k | const EC_POINT *pub_key = EC_KEY_get0_public_key(eckey); |
155 | 1.85k | if (group == NULL || pub_key == NULL || sig == NULL) { |
156 | 0 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS); |
157 | 0 | return 0; |
158 | 0 | } |
159 | | |
160 | 1.85k | EC_SCALAR r, s, u1, u2, s_inv_mont, m; |
161 | 1.85k | if (BN_is_zero(sig->r) || |
162 | 1.85k | !ec_bignum_to_scalar(group, &r, sig->r) || |
163 | 1.85k | BN_is_zero(sig->s) || |
164 | 1.85k | !ec_bignum_to_scalar(group, &s, sig->s)) { |
165 | 601 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
166 | 601 | return 0; |
167 | 601 | } |
168 | | |
169 | | // s_inv_mont = s^-1 in the Montgomery domain. |
170 | 1.25k | if (!ec_scalar_to_montgomery_inv_vartime(group, &s_inv_mont, &s)) { |
171 | 0 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR); |
172 | 0 | return 0; |
173 | 0 | } |
174 | | |
175 | | // u1 = m * s^-1 mod order |
176 | | // u2 = r * s^-1 mod order |
177 | | // |
178 | | // |s_inv_mont| is in Montgomery form while |m| and |r| are not, so |u1| and |
179 | | // |u2| will be taken out of Montgomery form, as desired. |
180 | 1.25k | digest_to_scalar(group, &m, digest, digest_len); |
181 | 1.25k | ec_scalar_mul_montgomery(group, &u1, &m, &s_inv_mont); |
182 | 1.25k | ec_scalar_mul_montgomery(group, &u2, &r, &s_inv_mont); |
183 | | |
184 | 1.25k | EC_JACOBIAN point; |
185 | 1.25k | if (!ec_point_mul_scalar_public(group, &point, &u1, &pub_key->raw, &u2)) { |
186 | 0 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB); |
187 | 0 | return 0; |
188 | 0 | } |
189 | | |
190 | 1.25k | if (!ec_cmp_x_coordinate(group, &point, &r)) { |
191 | 1.25k | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE); |
192 | 1.25k | return 0; |
193 | 1.25k | } |
194 | | |
195 | 1 | return 1; |
196 | 1.25k | } |
197 | | |
198 | | int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, |
199 | 1.85k | const ECDSA_SIG *sig, const EC_KEY *eckey) { |
200 | 1.85k | boringssl_ensure_ecc_self_test(); |
201 | | |
202 | 1.85k | return ecdsa_do_verify_no_self_test(digest, digest_len, sig, eckey); |
203 | 1.85k | } |
204 | | |
205 | | static ECDSA_SIG *ecdsa_sign_impl(const EC_GROUP *group, int *out_retry, |
206 | | const EC_SCALAR *priv_key, const EC_SCALAR *k, |
207 | 87 | const uint8_t *digest, size_t digest_len) { |
208 | 87 | *out_retry = 0; |
209 | | |
210 | | // Check that the size of the group order is FIPS compliant (FIPS 186-4 |
211 | | // B.5.2). |
212 | 87 | const BIGNUM *order = EC_GROUP_get0_order(group); |
213 | 87 | if (BN_num_bits(order) < 160) { |
214 | 0 | OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER); |
215 | 0 | return NULL; |
216 | 0 | } |
217 | | |
218 | | // Compute r, the x-coordinate of k * generator. |
219 | 87 | EC_JACOBIAN tmp_point; |
220 | 87 | EC_SCALAR r; |
221 | 87 | if (!ec_point_mul_scalar_base(group, &tmp_point, k) || |
222 | 87 | !ec_get_x_coordinate_as_scalar(group, &r, &tmp_point)) { |
223 | 0 | return NULL; |
224 | 0 | } |
225 | | |
226 | 87 | if (constant_time_declassify_int(ec_scalar_is_zero(group, &r))) { |
227 | 0 | *out_retry = 1; |
228 | 0 | return NULL; |
229 | 0 | } |
230 | | |
231 | | // s = priv_key * r. Note if only one parameter is in the Montgomery domain, |
232 | | // |ec_scalar_mod_mul_montgomery| will compute the answer in the normal |
233 | | // domain. |
234 | 87 | EC_SCALAR s; |
235 | 87 | ec_scalar_to_montgomery(group, &s, &r); |
236 | 87 | ec_scalar_mul_montgomery(group, &s, priv_key, &s); |
237 | | |
238 | | // s = m + priv_key * r. |
239 | 87 | EC_SCALAR tmp; |
240 | 87 | digest_to_scalar(group, &tmp, digest, digest_len); |
241 | 87 | ec_scalar_add(group, &s, &s, &tmp); |
242 | | |
243 | | // s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery |
244 | | // domain. This is |ec_scalar_to_montgomery| followed by |
245 | | // |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by |
246 | | // |ec_scalar_from_montgomery| is equivalent and slightly more efficient. |
247 | | // Then, as above, only one parameter is in the Montgomery domain, so the |
248 | | // result is in the normal domain. Finally, note k is non-zero (or computing r |
249 | | // would fail), so the inverse must exist. |
250 | 87 | ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2 |
251 | 87 | ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R |
252 | 87 | ec_scalar_mul_montgomery(group, &s, &s, &tmp); |
253 | 87 | if (constant_time_declassify_int(ec_scalar_is_zero(group, &s))) { |
254 | 0 | *out_retry = 1; |
255 | 0 | return NULL; |
256 | 0 | } |
257 | | |
258 | 87 | CONSTTIME_DECLASSIFY(r.words, sizeof(r.words)); |
259 | 87 | CONSTTIME_DECLASSIFY(s.words, sizeof(r.words)); |
260 | 87 | ECDSA_SIG *ret = ECDSA_SIG_new(); |
261 | 87 | if (ret == NULL || // |
262 | 87 | !bn_set_words(ret->r, r.words, order->width) || |
263 | 87 | !bn_set_words(ret->s, s.words, order->width)) { |
264 | 0 | ECDSA_SIG_free(ret); |
265 | 0 | return NULL; |
266 | 0 | } |
267 | 87 | return ret; |
268 | 87 | } |
269 | | |
270 | | ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest, |
271 | | size_t digest_len, |
272 | | const EC_KEY *eckey, |
273 | | const uint8_t *nonce, |
274 | 0 | size_t nonce_len) { |
275 | 0 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
276 | 0 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); |
277 | 0 | return NULL; |
278 | 0 | } |
279 | | |
280 | 0 | const EC_GROUP *group = EC_KEY_get0_group(eckey); |
281 | 0 | if (group == NULL || eckey->priv_key == NULL) { |
282 | 0 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
283 | 0 | return NULL; |
284 | 0 | } |
285 | 0 | const EC_SCALAR *priv_key = &eckey->priv_key->scalar; |
286 | |
|
287 | 0 | EC_SCALAR k; |
288 | 0 | if (!ec_scalar_from_bytes(group, &k, nonce, nonce_len)) { |
289 | 0 | return NULL; |
290 | 0 | } |
291 | 0 | int retry_ignored; |
292 | 0 | return ecdsa_sign_impl(group, &retry_ignored, priv_key, &k, digest, |
293 | 0 | digest_len); |
294 | 0 | } |
295 | | |
296 | | // This function is only exported for testing and is not called in production |
297 | | // code. |
298 | | ECDSA_SIG *ECDSA_sign_with_nonce_and_leak_private_key_for_testing( |
299 | | const uint8_t *digest, size_t digest_len, const EC_KEY *eckey, |
300 | 0 | const uint8_t *nonce, size_t nonce_len) { |
301 | 0 | boringssl_ensure_ecc_self_test(); |
302 | |
|
303 | 0 | return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey, |
304 | 0 | nonce, nonce_len); |
305 | 0 | } |
306 | | |
307 | | ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len, |
308 | 87 | const EC_KEY *eckey) { |
309 | 87 | boringssl_ensure_ecc_self_test(); |
310 | | |
311 | 87 | if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) { |
312 | 0 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED); |
313 | 0 | return NULL; |
314 | 0 | } |
315 | | |
316 | 87 | const EC_GROUP *group = EC_KEY_get0_group(eckey); |
317 | 87 | if (group == NULL || eckey->priv_key == NULL) { |
318 | 0 | OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER); |
319 | 0 | return NULL; |
320 | 0 | } |
321 | 87 | const BIGNUM *order = EC_GROUP_get0_order(group); |
322 | 87 | const EC_SCALAR *priv_key = &eckey->priv_key->scalar; |
323 | | |
324 | | // Pass a SHA512 hash of the private key and digest as additional data |
325 | | // into the RBG. This is a hardening measure against entropy failure. |
326 | 87 | static_assert(SHA512_DIGEST_LENGTH >= 32, |
327 | 87 | "additional_data is too large for SHA-512"); |
328 | | |
329 | 87 | FIPS_service_indicator_lock_state(); |
330 | | |
331 | 87 | SHA512_CTX sha; |
332 | 87 | uint8_t additional_data[SHA512_DIGEST_LENGTH]; |
333 | 87 | SHA512_Init(&sha); |
334 | 87 | SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG)); |
335 | 87 | SHA512_Update(&sha, digest, digest_len); |
336 | 87 | SHA512_Final(additional_data, &sha); |
337 | | |
338 | | // Cap iterations so callers who supply invalid values as custom groups do not |
339 | | // infinite loop. This does not impact valid parameters (e.g. those covered by |
340 | | // FIPS) because the probability of requiring even one retry is negligible, |
341 | | // let alone 32. |
342 | 87 | static const int kMaxIterations = 32; |
343 | 87 | ECDSA_SIG *ret = NULL; |
344 | 87 | int iters = 0; |
345 | 87 | for (;;) { |
346 | 87 | EC_SCALAR k; |
347 | 87 | if (!ec_random_nonzero_scalar(group, &k, additional_data)) { |
348 | 0 | ret = NULL; |
349 | 0 | goto out; |
350 | 0 | } |
351 | | |
352 | | // TODO(davidben): Move this inside |ec_random_nonzero_scalar| or lower, so |
353 | | // that all scalars we generate are, by default, secret. |
354 | 87 | CONSTTIME_SECRET(k.words, sizeof(k.words)); |
355 | | |
356 | 87 | int retry; |
357 | 87 | ret = ecdsa_sign_impl(group, &retry, priv_key, &k, digest, digest_len); |
358 | 87 | if (ret != NULL || !retry) { |
359 | 87 | goto out; |
360 | 87 | } |
361 | | |
362 | 0 | iters++; |
363 | 0 | if (iters > kMaxIterations) { |
364 | 0 | OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_TOO_MANY_ITERATIONS); |
365 | 0 | goto out; |
366 | 0 | } |
367 | 0 | } |
368 | | |
369 | 87 | out: |
370 | 87 | FIPS_service_indicator_unlock_state(); |
371 | 87 | return ret; |
372 | 87 | } |