ecdsa.c 15 KB

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