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.
 
 
 
 
 
 

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