ecs_ossl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465
  1. /* crypto/ecdsa/ecs_ossl.c */
  2. /*
  3. * Written by Nils Larsch for the OpenSSL project
  4. */
  5. /* ====================================================================
  6. * Copyright (c) 1998-2004 The OpenSSL Project. All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions
  10. * are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. *
  15. * 2. Redistributions in binary form must reproduce the above copyright
  16. * notice, this list of conditions and the following disclaimer in
  17. * the documentation and/or other materials provided with the
  18. * distribution.
  19. *
  20. * 3. All advertising materials mentioning features or use of this
  21. * software must display the following acknowledgment:
  22. * "This product includes software developed by the OpenSSL Project
  23. * for use in the OpenSSL Toolkit. (http://www.OpenSSL.org/)"
  24. *
  25. * 4. The names "OpenSSL Toolkit" and "OpenSSL Project" must not be used to
  26. * endorse or promote products derived from this software without
  27. * prior written permission. For written permission, please contact
  28. * openssl-core@OpenSSL.org.
  29. *
  30. * 5. Products derived from this software may not be called "OpenSSL"
  31. * nor may "OpenSSL" appear in their names without prior written
  32. * permission of the OpenSSL Project.
  33. *
  34. * 6. Redistributions of any form whatsoever must retain the following
  35. * acknowledgment:
  36. * "This product includes software developed by the OpenSSL Project
  37. * for use in the OpenSSL Toolkit (http://www.OpenSSL.org/)"
  38. *
  39. * THIS SOFTWARE IS PROVIDED BY THE OpenSSL PROJECT ``AS IS'' AND ANY
  40. * EXPRESSED OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  41. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
  42. * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE OpenSSL PROJECT OR
  43. * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  44. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  45. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  46. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  47. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  48. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  49. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  50. * OF THE POSSIBILITY OF SUCH DAMAGE.
  51. * ====================================================================
  52. *
  53. * This product includes cryptographic software written by Eric Young
  54. * (eay@cryptsoft.com). This product includes software written by Tim
  55. * Hudson (tjh@cryptsoft.com).
  56. *
  57. */
  58. #include "ecs_locl.h"
  59. #include <openssl/err.h>
  60. #include <openssl/obj_mac.h>
  61. #include <openssl/bn.h>
  62. static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dlen,
  63. const BIGNUM *, const BIGNUM *,
  64. EC_KEY *eckey);
  65. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  66. BIGNUM **rp);
  67. static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
  68. const ECDSA_SIG *sig, EC_KEY *eckey);
  69. static ECDSA_METHOD openssl_ecdsa_meth = {
  70. "OpenSSL ECDSA method",
  71. ecdsa_do_sign,
  72. ecdsa_sign_setup,
  73. ecdsa_do_verify,
  74. #if 0
  75. NULL, /* init */
  76. NULL, /* finish */
  77. #endif
  78. 0, /* flags */
  79. NULL /* app_data */
  80. };
  81. const ECDSA_METHOD *ECDSA_OpenSSL(void)
  82. {
  83. return &openssl_ecdsa_meth;
  84. }
  85. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  86. BIGNUM **rp)
  87. {
  88. BN_CTX *ctx = NULL;
  89. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  90. EC_POINT *tmp_point = NULL;
  91. const EC_GROUP *group;
  92. int ret = 0;
  93. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  94. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
  95. return 0;
  96. }
  97. if (ctx_in == NULL) {
  98. if ((ctx = BN_CTX_new()) == NULL) {
  99. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  100. return 0;
  101. }
  102. } else
  103. ctx = ctx_in;
  104. k = BN_new(); /* this value is later returned in *kinvp */
  105. r = BN_new(); /* this value is later returned in *rp */
  106. order = BN_new();
  107. X = BN_new();
  108. if (!k || !r || !order || !X) {
  109. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  110. goto err;
  111. }
  112. if ((tmp_point = EC_POINT_new(group)) == NULL) {
  113. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  114. goto err;
  115. }
  116. if (!EC_GROUP_get_order(group, order, ctx)) {
  117. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  118. goto err;
  119. }
  120. do {
  121. /* get random k */
  122. do
  123. if (!BN_rand_range(k, order)) {
  124. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
  125. ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  126. goto err;
  127. }
  128. while (BN_is_zero(k)) ;
  129. /*
  130. * We do not want timing information to leak the length of k, so we
  131. * compute G*k using an equivalent scalar of fixed bit-length.
  132. */
  133. if (!BN_add(k, k, order))
  134. goto err;
  135. if (BN_num_bits(k) <= BN_num_bits(order))
  136. if (!BN_add(k, k, order))
  137. goto err;
  138. /* compute r the x-coordinate of generator * k */
  139. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  140. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  141. goto err;
  142. }
  143. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  144. NID_X9_62_prime_field) {
  145. if (!EC_POINT_get_affine_coordinates_GFp
  146. (group, tmp_point, X, NULL, ctx)) {
  147. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  148. goto err;
  149. }
  150. }
  151. #ifndef OPENSSL_NO_EC2M
  152. else { /* NID_X9_62_characteristic_two_field */
  153. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  154. tmp_point, X, NULL,
  155. ctx)) {
  156. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  157. goto err;
  158. }
  159. }
  160. #endif
  161. if (!BN_nnmod(r, X, order, ctx)) {
  162. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  163. goto err;
  164. }
  165. }
  166. while (BN_is_zero(r));
  167. /* compute the inverse of k */
  168. if (EC_GROUP_get_mont_data(group) != NULL) {
  169. /*
  170. * We want inverse in constant time, therefore we utilize the fact
  171. * order must be prime and use Fermats Little Theorem instead.
  172. */
  173. if (!BN_set_word(X, 2)) {
  174. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  175. goto err;
  176. }
  177. if (!BN_mod_sub(X, order, X, order, ctx)) {
  178. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  179. goto err;
  180. }
  181. BN_set_flags(X, BN_FLG_CONSTTIME);
  182. if (!BN_mod_exp_mont_consttime
  183. (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
  184. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  185. goto err;
  186. }
  187. } else {
  188. if (!BN_mod_inverse(k, k, order, ctx)) {
  189. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  190. goto err;
  191. }
  192. }
  193. /* clear old values if necessary */
  194. if (*rp != NULL)
  195. BN_clear_free(*rp);
  196. if (*kinvp != NULL)
  197. BN_clear_free(*kinvp);
  198. /* save the pre-computed values */
  199. *rp = r;
  200. *kinvp = k;
  201. ret = 1;
  202. err:
  203. if (!ret) {
  204. if (k != NULL)
  205. BN_clear_free(k);
  206. if (r != NULL)
  207. BN_clear_free(r);
  208. }
  209. if (ctx_in == NULL)
  210. BN_CTX_free(ctx);
  211. if (order != NULL)
  212. BN_free(order);
  213. if (tmp_point != NULL)
  214. EC_POINT_free(tmp_point);
  215. if (X)
  216. BN_clear_free(X);
  217. return (ret);
  218. }
  219. static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
  220. const BIGNUM *in_kinv, const BIGNUM *in_r,
  221. EC_KEY *eckey)
  222. {
  223. int ok = 0, i;
  224. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  225. const BIGNUM *ckinv;
  226. BN_CTX *ctx = NULL;
  227. const EC_GROUP *group;
  228. ECDSA_SIG *ret;
  229. ECDSA_DATA *ecdsa;
  230. const BIGNUM *priv_key;
  231. ecdsa = ecdsa_check(eckey);
  232. group = EC_KEY_get0_group(eckey);
  233. priv_key = EC_KEY_get0_private_key(eckey);
  234. if (group == NULL || priv_key == NULL || ecdsa == NULL) {
  235. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
  236. return NULL;
  237. }
  238. ret = ECDSA_SIG_new();
  239. if (!ret) {
  240. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  241. return NULL;
  242. }
  243. s = ret->s;
  244. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  245. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  246. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  247. goto err;
  248. }
  249. if (!EC_GROUP_get_order(group, order, ctx)) {
  250. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
  251. goto err;
  252. }
  253. i = BN_num_bits(order);
  254. /*
  255. * Need to truncate digest if it is too long: first truncate whole bytes.
  256. */
  257. if (8 * dgst_len > i)
  258. dgst_len = (i + 7) / 8;
  259. if (!BN_bin2bn(dgst, dgst_len, m)) {
  260. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  261. goto err;
  262. }
  263. /* If still too long truncate remaining bits with a shift */
  264. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  265. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  266. goto err;
  267. }
  268. do {
  269. if (in_kinv == NULL || in_r == NULL) {
  270. if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
  271. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_ECDSA_LIB);
  272. goto err;
  273. }
  274. ckinv = kinv;
  275. } else {
  276. ckinv = in_kinv;
  277. if (BN_copy(ret->r, in_r) == NULL) {
  278. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  279. goto err;
  280. }
  281. }
  282. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  283. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  284. goto err;
  285. }
  286. if (!BN_mod_add_quick(s, tmp, m, order)) {
  287. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  288. goto err;
  289. }
  290. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  291. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  292. goto err;
  293. }
  294. if (BN_is_zero(s)) {
  295. /*
  296. * if kinv and r have been supplied by the caller don't to
  297. * generate new kinv and r values
  298. */
  299. if (in_kinv != NULL && in_r != NULL) {
  300. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
  301. ECDSA_R_NEED_NEW_SETUP_VALUES);
  302. goto err;
  303. }
  304. } else
  305. /* s != 0 => we have a valid signature */
  306. break;
  307. }
  308. while (1);
  309. ok = 1;
  310. err:
  311. if (!ok) {
  312. ECDSA_SIG_free(ret);
  313. ret = NULL;
  314. }
  315. if (ctx)
  316. BN_CTX_free(ctx);
  317. if (m)
  318. BN_clear_free(m);
  319. if (tmp)
  320. BN_clear_free(tmp);
  321. if (order)
  322. BN_free(order);
  323. if (kinv)
  324. BN_clear_free(kinv);
  325. return ret;
  326. }
  327. static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
  328. const ECDSA_SIG *sig, EC_KEY *eckey)
  329. {
  330. int ret = -1, i;
  331. BN_CTX *ctx;
  332. BIGNUM *order, *u1, *u2, *m, *X;
  333. EC_POINT *point = NULL;
  334. const EC_GROUP *group;
  335. const EC_POINT *pub_key;
  336. /* check input values */
  337. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  338. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  339. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
  340. return -1;
  341. }
  342. ctx = BN_CTX_new();
  343. if (!ctx) {
  344. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  345. return -1;
  346. }
  347. BN_CTX_start(ctx);
  348. order = BN_CTX_get(ctx);
  349. u1 = BN_CTX_get(ctx);
  350. u2 = BN_CTX_get(ctx);
  351. m = BN_CTX_get(ctx);
  352. X = BN_CTX_get(ctx);
  353. if (!X) {
  354. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  355. goto err;
  356. }
  357. if (!EC_GROUP_get_order(group, order, ctx)) {
  358. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  359. goto err;
  360. }
  361. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  362. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  363. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  364. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
  365. ret = 0; /* signature is invalid */
  366. goto err;
  367. }
  368. /* calculate tmp1 = inv(S) mod order */
  369. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  370. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  371. goto err;
  372. }
  373. /* digest -> m */
  374. i = BN_num_bits(order);
  375. /*
  376. * Need to truncate digest if it is too long: first truncate whole bytes.
  377. */
  378. if (8 * dgst_len > i)
  379. dgst_len = (i + 7) / 8;
  380. if (!BN_bin2bn(dgst, dgst_len, m)) {
  381. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  382. goto err;
  383. }
  384. /* If still too long truncate remaining bits with a shift */
  385. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  386. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  387. goto err;
  388. }
  389. /* u1 = m * tmp mod order */
  390. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  391. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  392. goto err;
  393. }
  394. /* u2 = r * w mod q */
  395. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  396. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  397. goto err;
  398. }
  399. if ((point = EC_POINT_new(group)) == NULL) {
  400. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  401. goto err;
  402. }
  403. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  404. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  405. goto err;
  406. }
  407. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  408. NID_X9_62_prime_field) {
  409. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  410. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  411. goto err;
  412. }
  413. }
  414. #ifndef OPENSSL_NO_EC2M
  415. else { /* NID_X9_62_characteristic_two_field */
  416. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
  417. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  418. goto err;
  419. }
  420. }
  421. #endif
  422. if (!BN_nnmod(u1, X, order, ctx)) {
  423. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  424. goto err;
  425. }
  426. /* if the signature is correct u1 is equal to sig->r */
  427. ret = (BN_ucmp(u1, sig->r) == 0);
  428. err:
  429. BN_CTX_end(ctx);
  430. BN_CTX_free(ctx);
  431. if (point)
  432. EC_POINT_free(point);
  433. return ret;
  434. }