You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

505 lines
14 KiB

  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 <string.h>
  54. #include <openssl/bn.h>
  55. #include <openssl/err.h>
  56. #include <openssl/mem.h>
  57. #include "../ec/internal.h"
  58. int ECDSA_sign(int type, const uint8_t *digest, size_t digest_len, uint8_t *sig,
  59. unsigned int *sig_len, EC_KEY *eckey) {
  60. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  61. return eckey->ecdsa_meth->sign(digest, digest_len, sig, sig_len, eckey);
  62. }
  63. return ECDSA_sign_ex(type, digest, digest_len, sig, sig_len, NULL, NULL,
  64. eckey);
  65. }
  66. int ECDSA_verify(int type, const uint8_t *digest, size_t digest_len,
  67. const uint8_t *sig, size_t sig_len, EC_KEY *eckey) {
  68. ECDSA_SIG *s;
  69. int ret = 0;
  70. uint8_t *der = NULL;
  71. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  72. return eckey->ecdsa_meth->verify(digest, digest_len, sig, sig_len, eckey);
  73. }
  74. s = ECDSA_SIG_new();
  75. const uint8_t *sigp = sig;
  76. if (s == NULL || d2i_ECDSA_SIG(&s, &sigp, sig_len) == NULL ||
  77. sigp != sig + sig_len) {
  78. goto err;
  79. }
  80. /* Ensure that the signature uses DER and doesn't have trailing garbage. */
  81. const int der_len = i2d_ECDSA_SIG(s, &der);
  82. if (der_len < 0 || (size_t) der_len != sig_len || memcmp(sig, der, sig_len)) {
  83. goto err;
  84. }
  85. ret = ECDSA_do_verify(digest, digest_len, s, eckey);
  86. err:
  87. if (der != NULL) {
  88. OPENSSL_free(der);
  89. }
  90. if (s != NULL) {
  91. ECDSA_SIG_free(s);
  92. }
  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, digest_to_bn, 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, digest_to_bn, 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. 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, EC_KEY *eckey) {
  125. int ret = 0;
  126. BN_CTX *ctx;
  127. BIGNUM *order, *u1, *u2, *m, *X;
  128. EC_POINT *point = NULL;
  129. const EC_GROUP *group;
  130. const EC_POINT *pub_key;
  131. if (eckey->ecdsa_meth && eckey->ecdsa_meth->verify) {
  132. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_NOT_IMPLEMENTED);
  133. return 0;
  134. }
  135. /* check input values */
  136. if ((group = EC_KEY_get0_group(eckey)) == NULL ||
  137. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL ||
  138. sig == NULL) {
  139. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_MISSING_PARAMETERS);
  140. return 0;
  141. }
  142. ctx = BN_CTX_new();
  143. if (!ctx) {
  144. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_MALLOC_FAILURE);
  145. return 0;
  146. }
  147. BN_CTX_start(ctx);
  148. order = BN_CTX_get(ctx);
  149. u1 = BN_CTX_get(ctx);
  150. u2 = BN_CTX_get(ctx);
  151. m = BN_CTX_get(ctx);
  152. X = BN_CTX_get(ctx);
  153. if (!X) {
  154. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  155. goto err;
  156. }
  157. if (!EC_GROUP_get_order(group, order, ctx)) {
  158. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  159. goto err;
  160. }
  161. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  162. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  163. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  164. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ECDSA_R_BAD_SIGNATURE);
  165. ret = 0; /* signature is invalid */
  166. goto err;
  167. }
  168. /* calculate tmp1 = inv(S) mod order */
  169. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  170. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  171. goto err;
  172. }
  173. if (!digest_to_bn(m, digest, digest_len, order)) {
  174. goto err;
  175. }
  176. /* u1 = m * tmp mod order */
  177. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  178. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  179. goto err;
  180. }
  181. /* u2 = r * w mod q */
  182. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  183. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  184. goto err;
  185. }
  186. point = EC_POINT_new(group);
  187. if (point == NULL) {
  188. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_MALLOC_FAILURE);
  189. goto err;
  190. }
  191. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  192. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  193. goto err;
  194. }
  195. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  196. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_EC_LIB);
  197. goto err;
  198. }
  199. if (!BN_nnmod(u1, X, order, ctx)) {
  200. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_verify, ERR_R_BN_LIB);
  201. goto err;
  202. }
  203. /* if the signature is correct u1 is equal to sig->r */
  204. ret = (BN_ucmp(u1, sig->r) == 0);
  205. err:
  206. BN_CTX_end(ctx);
  207. BN_CTX_free(ctx);
  208. if (point) {
  209. EC_POINT_free(point);
  210. }
  211. return ret;
  212. }
  213. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  214. BIGNUM **rp, const uint8_t *digest,
  215. size_t digest_len) {
  216. BN_CTX *ctx = NULL;
  217. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  218. EC_POINT *tmp_point = NULL;
  219. const EC_GROUP *group;
  220. int ret = 0;
  221. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  222. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_PASSED_NULL_PARAMETER);
  223. return 0;
  224. }
  225. if (ctx_in == NULL) {
  226. if ((ctx = BN_CTX_new()) == NULL) {
  227. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_MALLOC_FAILURE);
  228. return 0;
  229. }
  230. } else {
  231. ctx = ctx_in;
  232. }
  233. k = BN_new(); /* this value is later returned in *kinvp */
  234. r = BN_new(); /* this value is later returned in *rp */
  235. order = BN_new();
  236. X = BN_new();
  237. if (!k || !r || !order || !X) {
  238. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_MALLOC_FAILURE);
  239. goto err;
  240. }
  241. tmp_point = EC_POINT_new(group);
  242. if (tmp_point == NULL) {
  243. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  244. goto err;
  245. }
  246. if (!EC_GROUP_get_order(group, order, ctx)) {
  247. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  248. goto err;
  249. }
  250. do {
  251. /* If possible, we'll include the private key and message digest in the k
  252. * generation. The |digest| argument is only empty if |ECDSA_sign_setup| is
  253. * being used. */
  254. do {
  255. int ok;
  256. if (digest_len > 0) {
  257. ok = BN_generate_dsa_nonce(k, order, EC_KEY_get0_private_key(eckey),
  258. digest, digest_len, ctx);
  259. } else {
  260. ok = BN_rand_range(k, order);
  261. }
  262. if (!ok) {
  263. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup,
  264. ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  265. goto err;
  266. }
  267. } while (BN_is_zero(k));
  268. /* We do not want timing information to leak the length of k,
  269. * so we compute G*k using an equivalent scalar of fixed
  270. * bit-length. */
  271. if (!BN_add(k, k, order)) {
  272. goto err;
  273. }
  274. if (BN_num_bits(k) <= BN_num_bits(order)) {
  275. if (!BN_add(k, k, order)) {
  276. goto err;
  277. }
  278. }
  279. /* compute r the x-coordinate of generator * k */
  280. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  281. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  282. goto err;
  283. }
  284. if (!EC_POINT_get_affine_coordinates_GFp(group, tmp_point, X, NULL, ctx)) {
  285. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_EC_LIB);
  286. goto err;
  287. }
  288. if (!BN_nnmod(r, X, order, ctx)) {
  289. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_BN_LIB);
  290. goto err;
  291. }
  292. } while (BN_is_zero(r));
  293. /* compute the inverse of k */
  294. if (!BN_mod_inverse(k, k, order, ctx)) {
  295. OPENSSL_PUT_ERROR(ECDSA, ecdsa_sign_setup, ERR_R_BN_LIB);
  296. goto err;
  297. }
  298. /* clear old values if necessary */
  299. if (*rp != NULL) {
  300. BN_clear_free(*rp);
  301. }
  302. if (*kinvp != NULL) {
  303. BN_clear_free(*kinvp);
  304. }
  305. /* save the pre-computed values */
  306. *rp = r;
  307. *kinvp = k;
  308. ret = 1;
  309. err:
  310. if (!ret) {
  311. if (k != NULL) {
  312. BN_clear_free(k);
  313. }
  314. if (r != NULL) {
  315. BN_clear_free(r);
  316. }
  317. }
  318. if (ctx_in == NULL) {
  319. BN_CTX_free(ctx);
  320. }
  321. if (order != NULL) {
  322. BN_free(order);
  323. }
  324. if (tmp_point != NULL) {
  325. EC_POINT_free(tmp_point);
  326. }
  327. if (X) {
  328. BN_clear_free(X);
  329. }
  330. return ret;
  331. }
  332. int ECDSA_sign_setup(EC_KEY *eckey, BN_CTX *ctx, BIGNUM **kinv, BIGNUM **rp) {
  333. return ecdsa_sign_setup(eckey, ctx, kinv, rp, NULL, 0);
  334. }
  335. ECDSA_SIG *ECDSA_do_sign_ex(const uint8_t *digest, size_t digest_len,
  336. const BIGNUM *in_kinv, const BIGNUM *in_r,
  337. EC_KEY *eckey) {
  338. int ok = 0;
  339. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  340. const BIGNUM *ckinv;
  341. BN_CTX *ctx = NULL;
  342. const EC_GROUP *group;
  343. ECDSA_SIG *ret;
  344. const BIGNUM *priv_key;
  345. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  346. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ECDSA_R_NOT_IMPLEMENTED);
  347. return NULL;
  348. }
  349. group = EC_KEY_get0_group(eckey);
  350. priv_key = EC_KEY_get0_private_key(eckey);
  351. if (group == NULL || priv_key == NULL) {
  352. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_PASSED_NULL_PARAMETER);
  353. return NULL;
  354. }
  355. ret = ECDSA_SIG_new();
  356. if (!ret) {
  357. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  358. return NULL;
  359. }
  360. s = ret->s;
  361. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  362. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  363. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  364. goto err;
  365. }
  366. if (!EC_GROUP_get_order(group, order, ctx)) {
  367. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_EC_LIB);
  368. goto err;
  369. }
  370. if (!digest_to_bn(m, digest, digest_len, order)) {
  371. goto err;
  372. }
  373. for (;;) {
  374. if (in_kinv == NULL || in_r == NULL) {
  375. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, digest, digest_len)) {
  376. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_ECDSA_LIB);
  377. goto err;
  378. }
  379. ckinv = kinv;
  380. } else {
  381. ckinv = in_kinv;
  382. if (BN_copy(ret->r, in_r) == NULL) {
  383. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_MALLOC_FAILURE);
  384. goto err;
  385. }
  386. }
  387. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  388. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  389. goto err;
  390. }
  391. if (!BN_mod_add_quick(s, tmp, m, order)) {
  392. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  393. goto err;
  394. }
  395. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  396. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ERR_R_BN_LIB);
  397. goto err;
  398. }
  399. if (BN_is_zero(s)) {
  400. /* if kinv and r have been supplied by the caller
  401. * don't to generate new kinv and r values */
  402. if (in_kinv != NULL && in_r != NULL) {
  403. OPENSSL_PUT_ERROR(ECDSA, ECDSA_do_sign_ex, ECDSA_R_NEED_NEW_SETUP_VALUES);
  404. goto err;
  405. }
  406. } else {
  407. /* s != 0 => we have a valid signature */
  408. break;
  409. }
  410. }
  411. ok = 1;
  412. err:
  413. if (!ok) {
  414. ECDSA_SIG_free(ret);
  415. ret = NULL;
  416. }
  417. if (ctx) {
  418. BN_CTX_free(ctx);
  419. }
  420. if (m) {
  421. BN_clear_free(m);
  422. }
  423. if (tmp) {
  424. BN_clear_free(tmp);
  425. }
  426. if (order) {
  427. BN_free(order);
  428. }
  429. if (kinv) {
  430. BN_clear_free(kinv);
  431. }
  432. return ret;
  433. }
  434. int ECDSA_sign_ex(int type, const uint8_t *digest, size_t digest_len,
  435. uint8_t *sig, unsigned int *sig_len, const BIGNUM *kinv,
  436. const BIGNUM *r, EC_KEY *eckey) {
  437. ECDSA_SIG *s = NULL;
  438. if (eckey->ecdsa_meth && eckey->ecdsa_meth->sign) {
  439. OPENSSL_PUT_ERROR(ECDSA, ECDSA_sign_ex, ECDSA_R_NOT_IMPLEMENTED);
  440. *sig_len = 0;
  441. return 0;
  442. }
  443. s = ECDSA_do_sign_ex(digest, digest_len, kinv, r, eckey);
  444. if (s == NULL) {
  445. *sig_len = 0;
  446. return 0;
  447. }
  448. *sig_len = i2d_ECDSA_SIG(s, &sig);
  449. ECDSA_SIG_free(s);
  450. return 1;
  451. }