ecdsa.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479
  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/bytestring.h>
  57. #include <openssl/err.h>
  58. #include <openssl/mem.h>
  59. #include "../bn/internal.h"
  60. #include "../ec/internal.h"
  61. #include "../internal.h"
  62. int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
  63. unsigned int *sig_len, const EC_KEY *eckey) {
  64. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  65. return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len,
  66. (EC_KEY*) eckey /* cast away const */);
  67. }
  68. return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
  69. eckey);
  70. }
  71. int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
  72. const uint8_t *sig, size_t sig_len, const EC_KEY *eckey) {
  73. ECDSA_SIG *s;
  74. int ret = 0;
  75. uint8_t *der = NULL;
  76. /* Decode the ECDSA signature. */
  77. s = ECDSA_SIG_from_bytes(sig, sig_len);
  78. if (s == NULL) {
  79. goto err;
  80. }
  81. /* Defend against potential laxness in the DER parser. */
  82. size_t der_len;
  83. if (!ECDSA_SIG_to_bytes(&der, &der_len, s) ||
  84. der_len != sig_len || OPENSSL_memcmp(sig, der, sig_len) != 0) {
  85. /* This should never happen. crypto/bytestring is strictly DER. */
  86. OPENSSL_PUT_ERROR(ECDSA, ERR_R_INTERNAL_ERROR);
  87. goto err;
  88. }
  89. ret = ECDSA_do_verify(digest, digest_len, s, eckey);
  90. err:
  91. OPENSSL_free(der);
  92. ECDSA_SIG_free(s);
  93. return ret;
  94. }
  95. /* digest_to_bn interprets |digest_len| bytes from |digest| as a big-endian
  96. * number and sets |out| to that value. It then truncates |out| so that it's,
  97. * at most, as long as |order|. It returns one on success and zero otherwise. */
  98. static int digest_to_bn(BIGNUM *out, const uint8_t *digest, size_t digest_len,
  99. const BIGNUM *order) {
  100. size_t num_bits;
  101. num_bits = BN_num_bits(order);
  102. /* Need to truncate digest if it is too long: first truncate whole
  103. * bytes. */
  104. if (8 * digest_len > num_bits) {
  105. digest_len = (num_bits + 7) / 8;
  106. }
  107. if (!BN_bin2bn(digest, digest_len, out)) {
  108. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  109. return 0;
  110. }
  111. /* If still too long truncate remaining bits with a shift */
  112. if ((8 * digest_len > num_bits) &&
  113. !BN_rshift(out, out, 8 - (num_bits & 0x7))) {
  114. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  115. return 0;
  116. }
  117. return 1;
  118. }
  119. ECDSA_SIG *ECDSA_do_sign(const uint8_t *digest, size_t digest_len,
  120. const EC_KEY *key) {
  121. return ECDSA_do_sign_ex(digest, digest_len, NULL, NULL, key);
  122. }
  123. int ECDSA_do_verify(const uint8_t *digest, size_t digest_len,
  124. const ECDSA_SIG *sig, const EC_KEY *eckey) {
  125. int ret = 0;
  126. BN_CTX *ctx;
  127. BIGNUM *u1, *u2, *m, *X;
  128. EC_POINT *point = NULL;
  129. const EC_GROUP *group;
  130. const EC_POINT *pub_key;
  131. /* check input values */
  132. if ((group = EC_KEY_get0_group(eckey)) == NULL ||
  133. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
  134. sig == NULL) {
  135. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_MISSING_PARAMETERS);
  136. return 0;
  137. }
  138. ctx = BN_CTX_new();
  139. if (!ctx) {
  140. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  141. return 0;
  142. }
  143. BN_CTX_start(ctx);
  144. u1 = BN_CTX_get(ctx);
  145. u2 = BN_CTX_get(ctx);
  146. m = BN_CTX_get(ctx);
  147. X = BN_CTX_get(ctx);
  148. if (u1 == NULL || u2 == NULL || m == NULL || X == NULL) {
  149. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  150. goto err;
  151. }
  152. const BIGNUM *order = EC_GROUP_get0_order(group);
  153. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  154. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  155. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  156. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_BAD_SIGNATURE);
  157. ret = 0; /* signature is invalid */
  158. goto err;
  159. }
  160. /* calculate tmp1 = inv(S) mod order */
  161. int no_inverse;
  162. if (!BN_mod_inverse_odd(u2, &no_inverse, sig->s, order, ctx)) {
  163. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  164. goto err;
  165. }
  166. if (!digest_to_bn(m, digest, digest_len, order)) {
  167. goto err;
  168. }
  169. /* u1 = m * tmp mod order */
  170. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  171. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  172. goto err;
  173. }
  174. /* u2 = r * w mod q */
  175. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  176. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  177. goto err;
  178. }
  179. point = EC_POINT_new(group);
  180. if (point == NULL) {
  181. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  182. goto err;
  183. }
  184. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  185. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  186. goto err;
  187. }
  188. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  189. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  190. goto err;
  191. }
  192. if (!BN_nnmod(u1, X, order, ctx)) {
  193. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  194. goto err;
  195. }
  196. /* if the signature is correct u1 is equal to sig->r */
  197. ret = (BN_ucmp(u1, sig->r) == 0);
  198. err:
  199. BN_CTX_end(ctx);
  200. BN_CTX_free(ctx);
  201. EC_POINT_free(point);
  202. return ret;
  203. }
  204. static int ecdsa_sign_setup(const EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  205. BIGNUM **rp, const uint8_t *digest,
  206. size_t digest_len) {
  207. BN_CTX *ctx = NULL;
  208. BIGNUM *k = NULL, *r = NULL, *tmp = NULL;
  209. EC_POINT *tmp_point = NULL;
  210. const EC_GROUP *group;
  211. int ret = 0;
  212. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  213. OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
  214. return 0;
  215. }
  216. if (ctx_in == NULL) {
  217. if ((ctx = BN_CTX_new()) == NULL) {
  218. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  219. return 0;
  220. }
  221. } else {
  222. ctx = ctx_in;
  223. }
  224. k = BN_new(); /* this value is later returned in *kinvp */
  225. r = BN_new(); /* this value is later returned in *rp */
  226. tmp = BN_new();
  227. if (k == NULL || r == NULL || tmp == NULL) {
  228. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  229. goto err;
  230. }
  231. tmp_point = EC_POINT_new(group);
  232. if (tmp_point == NULL) {
  233. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  234. goto err;
  235. }
  236. const BIGNUM *order = EC_GROUP_get0_order(group);
  237. do {
  238. /* If possible, we'll include the private key and message digest in the k
  239. * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
  240. * being used. */
  241. if (digest_len > 0) {
  242. do {
  243. if (!BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
  244. digest, digest_len, ctx)) {
  245. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  246. goto err;
  247. }
  248. } while (BN_is_zero(k));
  249. } else if (!BN_rand_range_ex(k, 1, order)) {
  250. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  251. goto err;
  252. }
  253. /* We do not want timing information to leak the length of k,
  254. * so we compute G*k using an equivalent scalar of fixed
  255. * bit-length. */
  256. if (!BN_add(k, k, order)) {
  257. goto err;
  258. }
  259. if (BN_num_bits(k) <= BN_num_bits(order)) {
  260. if (!BN_add(k, k, order)) {
  261. goto err;
  262. }
  263. }
  264. /* compute r the x-coordinate of generator * k */
  265. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  266. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  267. goto err;
  268. }
  269. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, tmp, NULL,
  270. ctx)) {
  271. OPENSSL_PUT_ERROR(ECDSA, ERR_R_EC_LIB);
  272. goto err;
  273. }
  274. if (!BN_nnmod(r, tmp, order, ctx)) {
  275. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  276. goto err;
  277. }
  278. } while (BN_is_zero(r));
  279. /* Compute the inverse of k. The order is a prime, so use Fermat's Little
  280. * Theorem. Note |ec_group_get_mont_data| may return NULL but
  281. * |bn_mod_inverse_prime| allows this. */
  282. if (!bn_mod_inverse_prime(k, k, order, ctx, ec_group_get_mont_data(group))) {
  283. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  284. goto err;
  285. }
  286. /* clear old values if necessary */
  287. BN_clear_free(*rp);
  288. BN_clear_free(*kinvp);
  289. /* save the pre-computed values */
  290. *rp = r;
  291. *kinvp = k;
  292. ret = 1;
  293. err:
  294. if (!ret) {
  295. BN_clear_free(k);
  296. BN_clear_free(r);
  297. }
  298. if (ctx_in == NULL) {
  299. BN_CTX_free(ctx);
  300. }
  301. EC_POINT_free(tmp_point);
  302. BN_clear_free(tmp);
  303. return ret;
  304. }
  305. int ECDSA_sign_setup(const EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv,
  306. BIGNUM **rp) {
  307. return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
  308. }
  309. ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
  310. const BIGNUM *in_kinv, const BIGNUM *in_r,
  311. const EC_KEY *eckey) {
  312. int ok = 0;
  313. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL;
  314. const BIGNUM *ckinv;
  315. BN_CTX *ctx = NULL;
  316. const EC_GROUP *group;
  317. ECDSA_SIG *ret;
  318. const BIGNUM *priv_key;
  319. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  320. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
  321. return NULL;
  322. }
  323. group = EC_KEY_get0_group(eckey);
  324. priv_key = EC_KEY_get0_private_key(eckey);
  325. if (group == NULL || priv_key == NULL) {
  326. OPENSSL_PUT_ERROR(ECDSA, ERR_R_PASSED_NULL_PARAMETER);
  327. return NULL;
  328. }
  329. ret = ECDSA_SIG_new();
  330. if (!ret) {
  331. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  332. return NULL;
  333. }
  334. s = ret->s;
  335. if ((ctx = BN_CTX_new()) == NULL ||
  336. (tmp = BN_new()) == NULL ||
  337. (m = BN_new()) == NULL) {
  338. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  339. goto err;
  340. }
  341. const BIGNUM *order = EC_GROUP_get0_order(group);
  342. if (!digest_to_bn(m, digest, digest_len, order)) {
  343. goto err;
  344. }
  345. for (;;) {
  346. if (in_kinv == NULL || in_r == NULL) {
  347. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
  348. OPENSSL_PUT_ERROR(ECDSA, ERR_R_ECDSA_LIB);
  349. goto err;
  350. }
  351. ckinv = kinv;
  352. } else {
  353. ckinv = in_kinv;
  354. if (BN_copy(ret->r, in_r) == NULL) {
  355. OPENSSL_PUT_ERROR(ECDSA, ERR_R_MALLOC_FAILURE);
  356. goto err;
  357. }
  358. }
  359. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  360. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  361. goto err;
  362. }
  363. if (!BN_mod_add_quick(s, tmp, m, order)) {
  364. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  365. goto err;
  366. }
  367. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  368. OPENSSL_PUT_ERROR(ECDSA, ERR_R_BN_LIB);
  369. goto err;
  370. }
  371. if (BN_is_zero(s)) {
  372. /* if kinv and r have been supplied by the caller
  373. * don't to generate new kinv and r values */
  374. if (in_kinv != NULL && in_r != NULL) {
  375. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NEED_NEW_SETUP_VALUES);
  376. goto err;
  377. }
  378. } else {
  379. /* s != 0 => we have a valid signature */
  380. break;
  381. }
  382. }
  383. ok = 1;
  384. err:
  385. if (!ok) {
  386. ECDSA_SIG_free(ret);
  387. ret = NULL;
  388. }
  389. BN_CTX_free(ctx);
  390. BN_clear_free(m);
  391. BN_clear_free(tmp);
  392. BN_clear_free(kinv);
  393. return ret;
  394. }
  395. int ECDSA_sign_ex(int type, const uint8_t *digest, size_t digest_len,
  396. uint8_t *sig, unsigned int *sig_len, const BIGNUM *kinv,
  397. const BIGNUM *r, const EC_KEY *eckey) {
  398. int ret = 0;
  399. ECDSA_SIG *s = NULL;
  400. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  401. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_NOT_IMPLEMENTED);
  402. *sig_len = 0;
  403. goto err;
  404. }
  405. s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
  406. if (s == NULL) {
  407. *sig_len = 0;
  408. goto err;
  409. }
  410. CBB cbb;
  411. CBB_zero(&cbb);
  412. size_t len;
  413. if (!CBB_init_fixed(&cbb, sig, ECDSA_size(eckey)) ||
  414. !ECDSA_SIG_marshal(&cbb, s) ||
  415. !CBB_finish(&cbb, NULL, &len)) {
  416. OPENSSL_PUT_ERROR(ECDSA, ECDSA_R_ENCODE_ERROR);
  417. CBB_cleanup(&cbb);
  418. *sig_len = 0;
  419. goto err;
  420. }
  421. *sig_len = (unsigned)len;
  422. ret = 1;
  423. err:
  424. ECDSA_SIG_free(s);
  425. return ret;
  426. }