ecdsa.c
Go to the documentation of this file.
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 "../../internal.h"
65 #include "../bn/internal.h"
66 #include "../ec/internal.h"
67 #include "internal.h"
68 
69 
70 // digest_to_scalar interprets |digest_len| bytes from |digest| as a scalar for
71 // ECDSA.
73  const uint8_t *digest, size_t digest_len) {
74  const BIGNUM *order = &group->order;
75  size_t num_bits = BN_num_bits(order);
76  // Need to truncate digest if it is too long: first truncate whole bytes.
77  size_t num_bytes = (num_bits + 7) / 8;
78  if (digest_len > num_bytes) {
79  digest_len = num_bytes;
80  }
81  OPENSSL_memset(out, 0, sizeof(EC_SCALAR));
82  for (size_t i = 0; i < digest_len; i++) {
83  out->bytes[i] = digest[digest_len - 1 - i];
84  }
85 
86  // If it is still too long, truncate remaining bits with a shift.
87  if (8 * digest_len > num_bits) {
88  bn_rshift_words(out->words, out->words, 8 - (num_bits & 0x7), order->width);
89  }
90 
91  // |out| now has the same bit width as |order|, but this only bounds by
92  // 2*|order|. Subtract the order if out of range.
93  //
94  // Montgomery multiplication accepts the looser bounds, so this isn't strictly
95  // necessary, but it is a cleaner abstraction and has no performance impact.
96  BN_ULONG tmp[EC_MAX_WORDS];
97  bn_reduce_once_in_place(out->words, 0 /* no carry */, order->d, tmp,
98  order->width);
99 }
100 
102  ECDSA_SIG *sig = OPENSSL_malloc(sizeof(ECDSA_SIG));
103  if (sig == NULL) {
104  return NULL;
105  }
106  sig->r = BN_new();
107  sig->s = BN_new();
108  if (sig->r == NULL || sig->s == NULL) {
109  ECDSA_SIG_free(sig);
110  return NULL;
111  }
112  return sig;
113 }
114 
116  if (sig == NULL) {
117  return;
118  }
119 
120  BN_free(sig->r);
121  BN_free(sig->s);
122  OPENSSL_free(sig);
123 }
124 
125 const BIGNUM *ECDSA_SIG_get0_r(const ECDSA_SIG *sig) {
126  return sig->r;
127 }
128 
129 const BIGNUM *ECDSA_SIG_get0_s(const ECDSA_SIG *sig) {
130  return sig->s;
131 }
132 
133 void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r,
134  const BIGNUM **out_s) {
135  if (out_r != NULL) {
136  *out_r = sig->r;
137  }
138  if (out_s != NULL) {
139  *out_s = sig->s;
140  }
141 }
142 
144  if (r == NULL || s == NULL) {
145  return 0;
146  }
147  BN_free(sig->r);
148  BN_free(sig->s);
149  sig->r = r;
150  sig->s = s;
151  return 1;
152 }
153 
154 int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
155  const ECDSA_SIG *sig, const EC_KEY *eckey) {
156  const EC_GROUP *group = EC_KEY_get0_group(eckey);
157  const EC_POINT *pub_key = EC_KEY_get0_public_key(eckey);
158  if (group == NULL || pub_key == NULL || sig == NULL) {
160  return 0;
161  }
162 
163  EC_SCALAR r, s, u1, u2, s_inv_mont, m;
164  if (BN_is_zero(sig->r) ||
165  !ec_bignum_to_scalar(group, &r, sig->r) ||
166  BN_is_zero(sig->s) ||
167  !ec_bignum_to_scalar(group, &s, sig->s)) {
169  return 0;
170  }
171 
172  // s_inv_mont = s^-1 in the Montgomery domain.
173  if (!ec_scalar_to_montgomery_inv_vartime(group, &s_inv_mont, &s)) {
175  return 0;
176  }
177 
178  // u1 = m * s^-1 mod order
179  // u2 = r * s^-1 mod order
180  //
181  // |s_inv_mont| is in Montgomery form while |m| and |r| are not, so |u1| and
182  // |u2| will be taken out of Montgomery form, as desired.
183  digest_to_scalar(group, &m, digest, digest_len);
184  ec_scalar_mul_montgomery(group, &u1, &m, &s_inv_mont);
185  ec_scalar_mul_montgomery(group, &u2, &r, &s_inv_mont);
186 
188  if (!ec_point_mul_scalar_public(group, &point, &u1, &pub_key->raw, &u2)) {
190  return 0;
191  }
192 
193  if (!ec_cmp_x_coordinate(group, &point, &r)) {
195  return 0;
196  }
197 
198  return 1;
199 }
200 
201 static ECDSA_SIG *ecdsa_sign_impl(const EC_GROUP *group, int *out_retry,
202  const EC_SCALAR *priv_key, const EC_SCALAR *k,
203  const uint8_t *digest, size_t digest_len) {
204  *out_retry = 0;
205 
206  // Check that the size of the group order is FIPS compliant (FIPS 186-4
207  // B.5.2).
208  const BIGNUM *order = EC_GROUP_get0_order(group);
209  if (BN_num_bits(order) < 160) {
211  return NULL;
212  }
213 
214  // Compute r, the x-coordinate of k * generator.
215  EC_RAW_POINT tmp_point;
216  EC_SCALAR r;
217  if (!ec_point_mul_scalar_base(group, &tmp_point, k) ||
218  !ec_get_x_coordinate_as_scalar(group, &r, &tmp_point)) {
219  return NULL;
220  }
221 
222  if (ec_scalar_is_zero(group, &r)) {
223  *out_retry = 1;
224  return NULL;
225  }
226 
227  // s = priv_key * r. Note if only one parameter is in the Montgomery domain,
228  // |ec_scalar_mod_mul_montgomery| will compute the answer in the normal
229  // domain.
230  EC_SCALAR s;
232  ec_scalar_mul_montgomery(group, &s, priv_key, &s);
233 
234  // s = m + priv_key * r.
235  EC_SCALAR tmp;
236  digest_to_scalar(group, &tmp, digest, digest_len);
237  ec_scalar_add(group, &s, &s, &tmp);
238 
239  // s = k^-1 * (m + priv_key * r). First, we compute k^-1 in the Montgomery
240  // domain. This is |ec_scalar_to_montgomery| followed by
241  // |ec_scalar_inv0_montgomery|, but |ec_scalar_inv0_montgomery| followed by
242  // |ec_scalar_from_montgomery| is equivalent and slightly more efficient.
243  // Then, as above, only one parameter is in the Montgomery domain, so the
244  // result is in the normal domain. Finally, note k is non-zero (or computing r
245  // would fail), so the inverse must exist.
246  ec_scalar_inv0_montgomery(group, &tmp, k); // tmp = k^-1 R^2
247  ec_scalar_from_montgomery(group, &tmp, &tmp); // tmp = k^-1 R
249  if (ec_scalar_is_zero(group, &s)) {
250  *out_retry = 1;
251  return NULL;
252  }
253 
255  if (ret == NULL || //
256  !bn_set_words(ret->r, r.words, order->width) ||
257  !bn_set_words(ret->s, s.words, order->width)) {
259  return NULL;
260  }
261  return ret;
262 }
263 
265  size_t digest_len,
266  const EC_KEY *eckey,
267  const uint8_t *nonce,
268  size_t nonce_len) {
269  if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
271  return NULL;
272  }
273 
274  const EC_GROUP *group = EC_KEY_get0_group(eckey);
275  if (group == NULL || eckey->priv_key == NULL) {
277  return NULL;
278  }
279  const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
280 
281  EC_SCALAR k;
282  if (!ec_scalar_from_bytes(group, &k, nonce, nonce_len)) {
283  return NULL;
284  }
285  int retry_ignored;
286  return ecdsa_sign_impl(group, &retry_ignored, priv_key, &k, digest,
287  digest_len);
288 }
289 
290 // This function is only exported for testing and is not called in production
291 // code.
293  const uint8_t *digest, size_t digest_len, const EC_KEY *eckey,
294  const uint8_t *nonce, size_t nonce_len) {
295  return ecdsa_sign_with_nonce_for_known_answer_test(digest, digest_len, eckey,
296  nonce, nonce_len);
297 }
298 
299 ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
300  const EC_KEY *eckey) {
301  if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
303  return NULL;
304  }
305 
306  const EC_GROUP *group = EC_KEY_get0_group(eckey);
307  if (group == NULL || eckey->priv_key == NULL) {
309  return NULL;
310  }
311  const BIGNUM *order = EC_GROUP_get0_order(group);
312  const EC_SCALAR *priv_key = &eckey->priv_key->scalar;
313 
314  // Pass a SHA512 hash of the private key and digest as additional data
315  // into the RBG. This is a hardening measure against entropy failure.
317  "additional_data is too large for SHA-512");
318  SHA512_CTX sha;
319  uint8_t additional_data[SHA512_DIGEST_LENGTH];
320  SHA512_Init(&sha);
321  SHA512_Update(&sha, priv_key->words, order->width * sizeof(BN_ULONG));
322  SHA512_Update(&sha, digest, digest_len);
323  SHA512_Final(additional_data, &sha);
324 
325  for (;;) {
326  EC_SCALAR k;
327  if (!ec_random_nonzero_scalar(group, &k, additional_data)) {
328  return NULL;
329  }
330 
331  int retry;
332  ECDSA_SIG *sig =
333  ecdsa_sign_impl(group, &retry, priv_key, &k, digest, digest_len);
334  if (sig != NULL || !retry) {
335  return sig;
336  }
337  }
338 }
EC_GROUP_get0_order
#define EC_GROUP_get0_order
Definition: boringssl_prefix_symbols.h:1323
ec_get_x_coordinate_as_scalar
#define ec_get_x_coordinate_as_scalar
Definition: boringssl_prefix_symbols.h:3106
bn.h
ec_scalar_from_montgomery
#define ec_scalar_from_montgomery
Definition: boringssl_prefix_symbols.h:3129
ec_bignum_to_scalar
#define ec_bignum_to_scalar
Definition: boringssl_prefix_symbols.h:3093
ec_point_mul_scalar_base
#define ec_point_mul_scalar_base
Definition: boringssl_prefix_symbols.h:3116
ecdsa_sig_st::r
BIGNUM * r
Definition: ecdsa.h:106
gen_build_yaml.out
dictionary out
Definition: src/benchmark/gen_build_yaml.py:24
ec_scalar_mul_montgomery
#define ec_scalar_mul_montgomery
Definition: boringssl_prefix_symbols.h:3132
ec_scalar_to_montgomery_inv_vartime
#define ec_scalar_to_montgomery_inv_vartime
Definition: boringssl_prefix_symbols.h:3139
ecdsa.h
OPENSSL_PUT_ERROR
#define OPENSSL_PUT_ERROR(library, reason)
Definition: err.h:423
bignum_st::width
int width
Definition: bn.h:975
ec_point_mul_scalar_public
#define ec_point_mul_scalar_public
Definition: boringssl_prefix_symbols.h:3119
string.h
ECDSA_SIG_get0_s
const BIGNUM * ECDSA_SIG_get0_s(const ECDSA_SIG *sig)
Definition: ecdsa.c:129
ecdsa_sig_st::s
BIGNUM * s
Definition: ecdsa.h:107
ECDSA_SIG_free
void ECDSA_SIG_free(ECDSA_SIG *sig)
Definition: ecdsa.c:115
SHA512_Update
#define SHA512_Update
Definition: boringssl_prefix_symbols.h:2172
BN_free
#define BN_free
Definition: boringssl_prefix_symbols.h:923
ec_cmp_x_coordinate
#define ec_cmp_x_coordinate
Definition: boringssl_prefix_symbols.h:3094
ERR_R_EC_LIB
#define ERR_R_EC_LIB
Definition: err.h:343
uint8_t
unsigned char uint8_t
Definition: stdint-msvc2008.h:78
ecdsa_sign_with_nonce_for_known_answer_test
ECDSA_SIG * ecdsa_sign_with_nonce_for_known_answer_test(const uint8_t *digest, size_t digest_len, const EC_KEY *eckey, const uint8_t *nonce, size_t nonce_len)
Definition: ecdsa.c:264
EC_KEY_get0_group
#define EC_KEY_get0_group
Definition: boringssl_prefix_symbols.h:1344
setup.k
k
Definition: third_party/bloaty/third_party/capstone/bindings/python/setup.py:42
OPENSSL_memset
static void * OPENSSL_memset(void *dst, int c, size_t n)
Definition: third_party/boringssl-with-bazel/src/crypto/internal.h:835
EC_RAW_POINT
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:260
ecdsa_sign_impl
static ECDSA_SIG * ecdsa_sign_impl(const EC_GROUP *group, int *out_retry, const EC_SCALAR *priv_key, const EC_SCALAR *k, const uint8_t *digest, size_t digest_len)
Definition: ecdsa.c:201
SHA512_Init
#define SHA512_Init
Definition: boringssl_prefix_symbols.h:2170
retry
void retry(grpc_end2end_test_config config)
Definition: retry.cc:319
ECDSA_SIG_get0_r
const BIGNUM * ECDSA_SIG_get0_r(const ECDSA_SIG *sig)
Definition: ecdsa.c:125
OPENSSL_malloc
#define OPENSSL_malloc
Definition: boringssl_prefix_symbols.h:1885
bn_rshift_words
#define bn_rshift_words
Definition: boringssl_prefix_symbols.h:2902
ecdsa_method_st::sign
int(* sign)(const uint8_t *digest, size_t digest_len, uint8_t *sig, unsigned int *sig_len, EC_KEY *eckey)
Definition: ec_key.h:285
ERR_R_PASSED_NULL_PARAMETER
#define ERR_R_PASSED_NULL_PARAMETER
Definition: err.h:373
ECDSA_sign_with_nonce_and_leak_private_key_for_testing
ECDSA_SIG * ECDSA_sign_with_nonce_and_leak_private_key_for_testing(const uint8_t *digest, size_t digest_len, const EC_KEY *eckey, const uint8_t *nonce, size_t nonce_len)
Definition: ecdsa.c:292
ec_scalar_to_montgomery
#define ec_scalar_to_montgomery
Definition: boringssl_prefix_symbols.h:3138
ECDSA_R_NOT_IMPLEMENTED
#define ECDSA_R_NOT_IMPLEMENTED
Definition: ecdsa.h:232
SHA512_Final
#define SHA512_Final
Definition: boringssl_prefix_symbols.h:2169
ec_key_st::priv_key
EC_WRAPPED_SCALAR * priv_key
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:730
ec_key_st::ecdsa_meth
ECDSA_METHOD * ecdsa_meth
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:737
sha.h
EC_WRAPPED_SCALAR::scalar
EC_SCALAR scalar
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:720
ECDSA_SIG_set0
int ECDSA_SIG_set0(ECDSA_SIG *sig, BIGNUM *r, BIGNUM *s)
Definition: ecdsa.c:143
err.h
ERR_R_INTERNAL_ERROR
#define ERR_R_INTERNAL_ERROR
Definition: err.h:374
ECDSA_SIG_get0
void ECDSA_SIG_get0(const ECDSA_SIG *sig, const BIGNUM **out_r, const BIGNUM **out_s)
Definition: ecdsa.c:133
EC_KEY_get0_public_key
#define EC_KEY_get0_public_key
Definition: boringssl_prefix_symbols.h:1346
SHA512_DIGEST_LENGTH
#define SHA512_DIGEST_LENGTH
Definition: sha.h:230
BN_is_zero
#define BN_is_zero
Definition: boringssl_prefix_symbols.h:940
ec_random_nonzero_scalar
#define ec_random_nonzero_scalar
Definition: boringssl_prefix_symbols.h:3125
ECDSA_do_verify
int ECDSA_do_verify(const uint8_t *digest, size_t digest_len, const ECDSA_SIG *sig, const EC_KEY *eckey)
Definition: ecdsa.c:154
ec_key_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:723
ec_scalar_inv0_montgomery
#define ec_scalar_inv0_montgomery
Definition: boringssl_prefix_symbols.h:3130
BN_num_bits
#define BN_num_bits
Definition: boringssl_prefix_symbols.h:974
point
Definition: bloaty/third_party/zlib/examples/zran.c:67
EC_R_INVALID_GROUP_ORDER
#define EC_R_INVALID_GROUP_ORDER
Definition: ec.h:419
upload.group
group
Definition: bloaty/third_party/googletest/googlemock/scripts/upload.py:397
ec_scalar_from_bytes
#define ec_scalar_from_bytes
Definition: boringssl_prefix_symbols.h:3128
ECDSA_SIG_new
ECDSA_SIG * ECDSA_SIG_new(void)
Definition: ecdsa.c:101
digest_to_scalar
static void digest_to_scalar(const EC_GROUP *group, EC_SCALAR *out, const uint8_t *digest, size_t digest_len)
Definition: ecdsa.c:72
bn_set_words
#define bn_set_words
Definition: boringssl_prefix_symbols.h:2907
bignum_st
Definition: bn.h:957
ec_point_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:615
internal.h
ret
UniquePtr< SSL_SESSION > ret
Definition: ssl_x509.cc:1029
ec_group_st
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:573
fix_build_deps.r
r
Definition: fix_build_deps.py:491
bignum_st::d
BN_ULONG * d
Definition: bn.h:960
ec_scalar_add
#define ec_scalar_add
Definition: boringssl_prefix_symbols.h:3126
ECDSA_do_sign
ECDSA_SIG * ECDSA_do_sign(const uint8_t *digest, size_t digest_len, const EC_KEY *eckey)
Definition: ecdsa.c:299
ec_scalar_is_zero
#define ec_scalar_is_zero
Definition: boringssl_prefix_symbols.h:3131
ECDSA_R_MISSING_PARAMETERS
#define ECDSA_R_MISSING_PARAMETERS
Definition: ecdsa.h:230
ec_point_st::raw
EC_RAW_POINT raw
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:623
EC_SCALAR
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:103
mem.h
EC_MAX_WORDS
#define EC_MAX_WORDS
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:92
type_check.h
autogen_x86imm.tmp
tmp
Definition: autogen_x86imm.py:12
EC_SCALAR::words
BN_ULONG words[EC_MAX_WORDS]
Definition: third_party/boringssl-with-bazel/src/crypto/fipsmodule/ec/internal.h:106
regress.m
m
Definition: regress/regress.py:25
OPENSSL_free
#define OPENSSL_free
Definition: boringssl_prefix_symbols.h:1869
ECDSA_R_BAD_SIGNATURE
#define ECDSA_R_BAD_SIGNATURE
Definition: ecdsa.h:229
ecdsa_sig_st
Definition: ecdsa.h:105
BN_new
#define BN_new
Definition: boringssl_prefix_symbols.h:971
sha512_state_st
Definition: sha.h:256
OPENSSL_STATIC_ASSERT
#define OPENSSL_STATIC_ASSERT(cond, msg)
Definition: type_check.h:75
i
uint64_t i
Definition: abseil-cpp/absl/container/btree_benchmark.cc:230
bn_reduce_once_in_place
#define bn_reduce_once_in_place
Definition: boringssl_prefix_symbols.h:2898


grpc
Author(s):
autogenerated on Thu Mar 13 2025 02:59:14