Debian Codegraph @

debian/rust-vendor/aws-lc-sys/aws-lc/crypto/fipsmodule/ecdsa/ecdsa.c

raw
1 // Copyright (c) 1998-2005 The OpenSSL Project. All rights reserved.
2 // SPDX-License-Identifier: Apache-2.0
3
4 #include <openssl/ecdsa.h>
5
6 #include <assert.h>
7 #include <string.h>
8
9 #include <openssl/bn.h>
10 #include <openssl/bytestring.h>
11 #include <openssl/err.h>
12 #include <openssl/mem.h>
13 #include <openssl/sha.h>
14 #include <openssl/type_check.h>
15
16 #include "../../internal.h"
17 #include "../bn/internal.h"
18 #include "../ec/internal.h"
19 #include "internal.h"
20
21
22 // digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for
23 // ECDSA.
24 static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out,
25 const uint8_t *digest, size_t digest_len) {
26 const BIGNUM *order = EC_GROUP_get0_order(group);
27 size_t num_bits = BN_num_bits(order);
28 // Need to truncate digest if it is too long: first truncate whole bytes.
29 size_t num_bytes = (num_bits + 7) / 8;
30 if (digest_len > num_bytes) {
31 digest_len = num_bytes;
32 }
33 bn_big_endian_to_words(out->words, order->width, digest, digest_len);
34
35 // If it is still too long, truncate remaining bits with a shift.
36 if (8 * digest_len > num_bits) {
37 bn_rshift_words(out->words, out->words, 8 - (num_bits & 0x7), order->width);
38 }
39
40 // |out| now has the same bit width as |order|, but this only bounds by
41 // 2*|order|. Subtract the order if out of range.
42 //
43 // Montgomery multiplication accepts the looser bounds, so this isn't strictly
44 // necessary, but it is a cleaner abstraction and has no performance impact.
45 BN_ULONG tmp[EC_MAX_WORDS];
46 bn_reduce_once_in_place(out->words, 0 /* no carry */, order->d, tmp,
47 order->width);
48 }
49
50 ECDSA_SIG *ECDSA_SIG_new(void) {
51 ECDSA_SIG *sig = OPENSSL_malloc(sizeof(ECDSA_SIG));
52 if (sig == NULL) {
53 return NULL;
54 }
55 sig->r = BN_new();
56 sig->s = BN_new();
57 if (sig->r == NULL || sig->s == NULL) {
58 ECDSA_SIG_free(sig);
59 return NULL;
60 }
61 return sig;
62 }
63
64 void ECDSA_SIG_free(ECDSA_SIG *sig) {
65 if (sig == NULL) {
66 return;
67 }
68
69 BN_free(sig->r);
70 BN_free(sig->s);
71 OPENSSL_free(sig);
72 }
73
74 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) {
75 return sig->r;
76 }
77
78 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) {
79 return sig->s;
80 }
81
82 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r,
83 const BIGNUM **out_s) {
84 if (out_r != NULL) {
85 *out_r = sig->r;
86 }
87 if (out_s != NULL) {
88 *out_s = sig->s;
89 }
90 }
91
92 int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s) {
93 if (r == NULL || s == NULL) {
94 return 0;
95 }
96 BN_free(sig->r);
97 BN_free(sig->s);
98 sig->r = r;
99 sig->s = s;
100 return 1;
101 }
102
103 int ecdsa_do_verify_no_self_test(const uint8_t *digest, size_t digest_len,
104 const ECDSA_SIG *sig, const EC_KEY *eckey) {
105 const EC_GROUP *group = EC_KEY_get0_group(eckey);
106 const EC_POINT *pub_key = EC_KEY_get0_public_key(eckey);
107 if (group == NULL || pub_key == NULL || sig == NULL) {
108 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
109 return 0;
110 }
111
112 EC_SCALAR r, s, u1, u2, s_inv_mont, m;
113 if (BN_is_zero(sig->r) ||
114 !ec_bignum_to_scalar(group, &r, sig->r) ||
115 BN_is_zero(sig->s) ||
116 !ec_bignum_to_scalar(group, &s, sig->s)) {
117 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
118 return 0;
119 }
120
121 // s_inv_mont = s^-1 in the Montgomery domain.
122 if (!ec_scalar_to_montgomery_inv_vartime(group, &s_inv_mont, &s)) {
123 OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
124 return 0;
125 }
126
127 // u1 = m * s^-1 mod order
128 // u2 = r * s^-1 mod order
129 //
130 // |s_inv_mont| is in Montgomery form while |m| and |r| are not, so |u1| and
131 // |u2| will be taken out of Montgomery form, as desired.
132 digest_to_scalar(group, &m, digest, digest_len);
133 ec_scalar_mul_montgomery(group, &u1, &m, &s_inv_mont);
134 ec_scalar_mul_montgomery(group, &u2, &r, &s_inv_mont);
135
136 EC_JACOBIAN point;
137 if (!ec_point_mul_scalar_public(group, &point, &u1, &pub_key->raw, &u2)) {
138 OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
139 return 0;
140 }
141
142 if (!ec_cmp_x_coordinate(group, &point, &r)) {
143 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISMATCHED_SIGNATURE);
144 return 0;
145 }
146
147 return 1;
148 }
149
150 int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
151 const ECDSA_SIG *sig, const EC_KEY *eckey) {
152 boringssl_ensure_ecc_self_test();
153
154 return ecdsa_do_verify_no_self_test(digest, digest_len, sig, eckey);
155 }
156
157 static ECDSA_SIG *ecdsa_sign_impl(const EC_GROUP *group, int *out_retry,
158 const EC_SCALAR *priv_key, const EC_SCALAR *k,
159 const uint8_t *digest, size_t digest_len) {
160 *out_retry = 0;
161
162 // Check that the size of the group order is FIPS compliant (FIPS 186-4
163 // B.5.2).
164 const BIGNUM *order = EC_GROUP_get0_order(group);
165 if (BN_num_bits(order) < 160) {
166 OPENSSL_PUT_ERROR(ECDSA, EC_R_INVALID_GROUP_ORDER);
167 return NULL;
168 }
169
170 // Compute r, the x-coordinate of k * generator.
171 EC_JACOBIAN tmp_point;
172 EC_SCALAR r;
173 if (!ec_point_mul_scalar_base(group, &tmp_point, k) ||
174 !ec_get_x_coordinate_as_scalar(group, &r, &tmp_point)) {
175 return NULL;
176 }
177
178 if (constant_time_declassify_int(ec_scalar_is_zero(group, &r))) {
179 *out_retry = 1;
180 return NULL;
181 }
182
183 // s = priv_key * r. Note if only one parameter is in the Montgomery domain,
184 // |ec_scalar_mod_mul_montgomery| will compute the answer in the normal
185 // domain.
186 EC_SCALAR s;
187 ec_scalar_to_montgomery(group, &s, &r);
188 ec_scalar_mul_montgomery(group, &s, priv_key, &s);
189
190 // s = m + priv_key * r.
191 EC_SCALAR tmp;
192 digest_to_scalar(group, &tmp, digest, digest_len);
193 ec_scalar_add(group, &s, &s, &tmp);
194
195 // s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery
196 // domain. This is |ec_scalar_to_montgomery| followed by
197 // |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by
198 // |ec_scalar_from_montgomery| is equivalent and slightly more efficient.
199 // Then, as above, only one parameter is in the Montgomery domain, so the
200 // result is in the normal domain. Finally, note k is non-zero (or computing r
201 // would fail), so the inverse must exist.
202 ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2
203 ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R
204 ec_scalar_mul_montgomery(group, &s, &s, &tmp);
205 OPENSSL_cleanse(&tmp, sizeof(tmp));
206 if (constant_time_declassify_int(ec_scalar_is_zero(group, &s))) {
207 *out_retry = 1;
208 return NULL;
209 }
210
211 CONSTTIME_DECLASSIFY(r.words, sizeof(r.words));
212 CONSTTIME_DECLASSIFY(s.words, sizeof(r.words));
213 ECDSA_SIG *ret = ECDSA_SIG_new();
214 if (ret == NULL || //
215 !bn_set_words(ret->r, r.words, order->width) ||
216 !bn_set_words(ret->s, s.words, order->width)) {
217 ECDSA_SIG_free(ret);
218 return NULL;
219 }
220 return ret;
221 }
222
223 ECDSA_SIG *ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest,
224 size_t digest_len,
225 const EC_KEY *eckey,
226 const uint8_t *nonce,
227 size_t nonce_len) {
228 if (eckey->eckey_method && eckey->eckey_method->sign) {
229 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
230 return NULL;
231 }
232
233 const EC_GROUP *group = EC_KEY_get0_group(eckey);
234 if (group == NULL || eckey->priv_key == NULL) {
235 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
236 return NULL;
237 }
238 const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
239
240 EC_SCALAR k;
241 if (!ec_scalar_from_bytes(group, &k, nonce, nonce_len)) {
242 return NULL;
243 }
244 int retry_ignored;
245 return ecdsa_sign_impl(group, &retry_ignored, priv_key, &k, digest,
246 digest_len);
247 }
248
249 // This function is only exported for testing and is not called in production
250 // code.
251 ECDSA_SIG *ECDSA_sign_with_nonce_and_leak_private_key_for_testing(
252 const uint8_t *digest, size_t digest_len, const EC_KEY *eckey,
253 const uint8_t *nonce, size_t nonce_len) {
254 boringssl_ensure_ecc_self_test();
255
256 return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey,
257 nonce, nonce_len);
258 }
259
260 ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
261 const EC_KEY *eckey) {
262 boringssl_ensure_ecc_self_test();
263
264 if (eckey->eckey_method && eckey->eckey_method->sign_sig) {
265 return eckey->eckey_method->sign_sig(digest, (int)digest_len, NULL, NULL,
266 (EC_KEY *)eckey);
267 }
268
269 const EC_GROUP *group = EC_KEY_get0_group(eckey);
270 if (group == NULL || eckey->priv_key == NULL) {
271 OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
272 return NULL;
273 }
274 // We have to avoid the underlying |SHA512_Final| services updating the
275 // indicator state, so we lock the state here.
276 FIPS_service_indicator_lock_state();
277 const BIGNUM *order = EC_GROUP_get0_order(group);
278 const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
279
280 // Pass a SHA512 hash of the private key and digest as additional data
281 // into the RBG. This is a hardening measure against entropy failure.
282 OPENSSL_STATIC_ASSERT(SHA512_DIGEST_LENGTH >= 32,
283 additional_data_is_too_large_for_SHA_512)
284 SHA512_CTX sha;
285 uint8_t additional_data[SHA512_DIGEST_LENGTH];
286 SHA512_Init(&sha);
287 SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG));
288 SHA512_Update(&sha, digest, digest_len);
289 SHA512_Final(additional_data, &sha);
290
291 FIPS_service_indicator_unlock_state();
292 // Cap iterations so callers who supply invalid values as custom groups do not
293 // infinite loop. This does not impact valid parameters (e.g. those covered by
294 // FIPS) because the probability of requiring even one retry is negligible,
295 // let alone 32.
296 static const int kMaxIterations = 32;
297 int iters = 0;
298 for (;;) {
299 EC_SCALAR k;
300 if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
301 OPENSSL_cleanse(&k, sizeof(EC_SCALAR));
302 return NULL;
303 }
304
305 // TODO(davidben): Move this inside |ec_random_nonzero_scalar| or lower, so
306 // that all scalars we generate are, by default, secret.
307 CONSTTIME_SECRET(k.words, sizeof(k.words));
308
309 int retry;
310 ECDSA_SIG *sig =
311 ecdsa_sign_impl(group, &retry, priv_key, &k, digest, digest_len);
312 if (sig != NULL || !retry) {
313 OPENSSL_cleanse(&k, sizeof(EC_SCALAR));
314 return sig;
315 }
316
317 iters++;
318 if (iters > kMaxIterations) {
319 OPENSSL_cleanse(&k, sizeof(EC_SCALAR));
320 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_TOO_MANY_ITERATIONS);
321 return NULL;
322 }
323 }
324 }
325
326 // |ECDSA_sign| uses ASN1/CBB functionality, so it was previously placed in
327 // crypto/ecdsa_extra/ecdsa_asn1.c. It's now moved within the FIPS boundary for
328 // FIPS compliance.
329 int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
330 unsigned int *sig_len, const EC_KEY *eckey) {
331 if (eckey->eckey_method && eckey->eckey_method->sign) {
332 return eckey->eckey_method->sign(type, digest, (int)digest_len, sig, sig_len,
333 NULL, NULL,
334 (EC_KEY*) eckey /* cast away const */);
335 }
336
337 int ret = 0;
338 ECDSA_SIG *s = ECDSA_do_sign(digest, digest_len, eckey);
339 if (s == NULL) {
340 *sig_len = 0;
341 goto err;
342 }
343
344 CBB cbb;
345 CBB_init_fixed(&cbb, sig, ECDSA_size(eckey));
346 size_t len;
347 if (!ECDSA_SIG_marshal(&cbb, s) ||
348 !CBB_finish(&cbb, NULL, &len)) {
349 OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
350 *sig_len = 0;
351 goto err;
352 }
353 *sig_len = (unsigned)len;
354 ret = 1;
355
356 err:
357 ECDSA_SIG_free(s);
358 return ret;
359 }
360
361 // |ECDSA_verify| uses ASN1/CBB functionality, so it was previously placed in
362 // crypto/evp_extra/ecdsa_asn1.c. It's now moved within the FIPS boundary for
363 // FIPS compliance.
364 int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
365 const uint8_t *sig, size_t sig_len, const EC_KEY *eckey) {
366 ECDSA_SIG *s;
367 int ret = 0;
368 uint8_t *der = NULL;
369
370 // Decode the ECDSA signature.
371 s = ECDSA_SIG_from_bytes(sig, sig_len);
372 if (s == NULL) {
373 goto err;
374 }
375
376 // Defend against potential laxness in the DER parser.
377 size_t der_len;
378 if (!ECDSA_SIG_to_bytes(&der, &der_len, s) ||
379 der_len != sig_len || OPENSSL_memcmp(sig, der, sig_len) != 0) {
380 // This should never happen. crypto/bytestring is strictly DER.
381 OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
382 goto err;
383 }
384
385 ret = ECDSA_do_verify(digest, digest_len, s, eckey);
386
387 err:
388 OPENSSL_free(der);
389 ECDSA_SIG_free(s);
390 return ret;
391 }
392
393 ECDSA_SIG *ecdsa_digestsign_no_self_test(const EVP_MD *md, const uint8_t *input,
394 size_t in_len, const EC_KEY *eckey,
395 const uint8_t *nonce,
396 size_t nonce_len) {
397 uint8_t digest[EVP_MAX_MD_SIZE];
398 unsigned int digest_len = EVP_MAX_MD_SIZE;
399 if (!EVP_Digest(input, in_len, digest, &digest_len, md, NULL)) {
400 return 0;
401 }
402
403 return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey,
404 nonce, nonce_len);
405 }
406
407 int ecdsa_digestverify_no_self_test(const EVP_MD *md, const uint8_t *input,
408 size_t in_len, const ECDSA_SIG *sig,
409 const EC_KEY *eckey){
410 uint8_t digest[EVP_MAX_MD_SIZE];
411 unsigned int digest_len = EVP_MAX_MD_SIZE;
412 if (!EVP_Digest(input, in_len, digest, &digest_len, md, NULL)) {
413 return 0;
414 }
415
416 return ecdsa_do_verify_no_self_test(digest, digest_len, sig, eckey);
417 }