ecdsa.c 16 KB

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