ecs_ossl.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478
  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. * [email protected].
  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. * ([email protected]). This product includes software written by Tim
  55. * Hudson ([email protected]).
  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. int order_bits;
  94. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  95. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
  96. return 0;
  97. }
  98. if (ctx_in == NULL) {
  99. if ((ctx = BN_CTX_new()) == NULL) {
  100. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  101. return 0;
  102. }
  103. } else
  104. ctx = ctx_in;
  105. k = BN_new(); /* this value is later returned in *kinvp */
  106. r = BN_new(); /* this value is later returned in *rp */
  107. order = BN_new();
  108. X = BN_new();
  109. if (!k || !r || !order || !X) {
  110. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  111. goto err;
  112. }
  113. if ((tmp_point = EC_POINT_new(group)) == NULL) {
  114. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  115. goto err;
  116. }
  117. if (!EC_GROUP_get_order(group, order, ctx)) {
  118. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  119. goto err;
  120. }
  121. /* Preallocate space */
  122. order_bits = BN_num_bits(order);
  123. if (!BN_set_bit(k, order_bits)
  124. || !BN_set_bit(r, order_bits)
  125. || !BN_set_bit(X, order_bits))
  126. goto err;
  127. do {
  128. /* get random k */
  129. do
  130. if (!BN_rand_range(k, order)) {
  131. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,
  132. ECDSA_R_RANDOM_NUMBER_GENERATION_FAILED);
  133. goto err;
  134. }
  135. while (BN_is_zero(k)) ;
  136. /*
  137. * We do not want timing information to leak the length of k, so we
  138. * compute G*k using an equivalent scalar of fixed bit-length.
  139. *
  140. * We unconditionally perform both of these additions to prevent a
  141. * small timing information leakage. We then choose the sum that is
  142. * one bit longer than the order. This guarantees the code
  143. * path used in the constant time implementations elsewhere.
  144. *
  145. * TODO: revisit the BN_copy aiming for a memory access agnostic
  146. * conditional copy.
  147. */
  148. if (!BN_add(r, k, order)
  149. || !BN_add(X, r, order)
  150. || !BN_copy(k, BN_num_bits(r) > order_bits ? r : X))
  151. goto err;
  152. /* compute r the x-coordinate of generator * k */
  153. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  154. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  155. goto err;
  156. }
  157. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  158. NID_X9_62_prime_field) {
  159. if (!EC_POINT_get_affine_coordinates_GFp
  160. (group, tmp_point, X, NULL, ctx)) {
  161. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  162. goto err;
  163. }
  164. }
  165. #ifndef OPENSSL_NO_EC2M
  166. else { /* NID_X9_62_characteristic_two_field */
  167. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  168. tmp_point, X, NULL,
  169. ctx)) {
  170. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  171. goto err;
  172. }
  173. }
  174. #endif
  175. if (!BN_nnmod(r, X, order, ctx)) {
  176. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  177. goto err;
  178. }
  179. }
  180. while (BN_is_zero(r));
  181. /* compute the inverse of k */
  182. if (EC_GROUP_get_mont_data(group) != NULL) {
  183. /*
  184. * We want inverse in constant time, therefore we utilize the fact
  185. * order must be prime and use Fermats Little Theorem instead.
  186. */
  187. if (!BN_set_word(X, 2)) {
  188. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  189. goto err;
  190. }
  191. if (!BN_mod_sub(X, order, X, order, ctx)) {
  192. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  193. goto err;
  194. }
  195. BN_set_flags(X, BN_FLG_CONSTTIME);
  196. if (!BN_mod_exp_mont_consttime
  197. (k, k, X, order, ctx, EC_GROUP_get_mont_data(group))) {
  198. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  199. goto err;
  200. }
  201. } else {
  202. if (!BN_mod_inverse(k, k, order, ctx)) {
  203. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  204. goto err;
  205. }
  206. }
  207. /* clear old values if necessary */
  208. if (*rp != NULL)
  209. BN_clear_free(*rp);
  210. if (*kinvp != NULL)
  211. BN_clear_free(*kinvp);
  212. /* save the pre-computed values */
  213. *rp = r;
  214. *kinvp = k;
  215. ret = 1;
  216. err:
  217. if (!ret) {
  218. if (k != NULL)
  219. BN_clear_free(k);
  220. if (r != NULL)
  221. BN_clear_free(r);
  222. }
  223. if (ctx_in == NULL)
  224. BN_CTX_free(ctx);
  225. if (order != NULL)
  226. BN_free(order);
  227. if (tmp_point != NULL)
  228. EC_POINT_free(tmp_point);
  229. if (X)
  230. BN_clear_free(X);
  231. return (ret);
  232. }
  233. static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
  234. const BIGNUM *in_kinv, const BIGNUM *in_r,
  235. EC_KEY *eckey)
  236. {
  237. int ok = 0, i;
  238. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  239. const BIGNUM *ckinv;
  240. BN_CTX *ctx = NULL;
  241. const EC_GROUP *group;
  242. ECDSA_SIG *ret;
  243. ECDSA_DATA *ecdsa;
  244. const BIGNUM *priv_key;
  245. ecdsa = ecdsa_check(eckey);
  246. group = EC_KEY_get0_group(eckey);
  247. priv_key = EC_KEY_get0_private_key(eckey);
  248. if (group == NULL || priv_key == NULL || ecdsa == NULL) {
  249. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
  250. return NULL;
  251. }
  252. ret = ECDSA_SIG_new();
  253. if (!ret) {
  254. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  255. return NULL;
  256. }
  257. s = ret->s;
  258. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  259. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  260. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  261. goto err;
  262. }
  263. if (!EC_GROUP_get_order(group, order, ctx)) {
  264. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
  265. goto err;
  266. }
  267. i = BN_num_bits(order);
  268. /*
  269. * Need to truncate digest if it is too long: first truncate whole bytes.
  270. */
  271. if (8 * dgst_len > i)
  272. dgst_len = (i + 7) / 8;
  273. if (!BN_bin2bn(dgst, dgst_len, m)) {
  274. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  275. goto err;
  276. }
  277. /* If still too long truncate remaining bits with a shift */
  278. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  279. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  280. goto err;
  281. }
  282. do {
  283. if (in_kinv == NULL || in_r == NULL) {
  284. if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
  285. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_ECDSA_LIB);
  286. goto err;
  287. }
  288. ckinv = kinv;
  289. } else {
  290. ckinv = in_kinv;
  291. if (BN_copy(ret->r, in_r) == NULL) {
  292. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  293. goto err;
  294. }
  295. }
  296. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  297. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  298. goto err;
  299. }
  300. if (!BN_mod_add_quick(s, tmp, m, order)) {
  301. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  302. goto err;
  303. }
  304. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  305. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  306. goto err;
  307. }
  308. if (BN_is_zero(s)) {
  309. /*
  310. * if kinv and r have been supplied by the caller don't to
  311. * generate new kinv and r values
  312. */
  313. if (in_kinv != NULL && in_r != NULL) {
  314. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
  315. ECDSA_R_NEED_NEW_SETUP_VALUES);
  316. goto err;
  317. }
  318. } else
  319. /* s != 0 => we have a valid signature */
  320. break;
  321. }
  322. while (1);
  323. ok = 1;
  324. err:
  325. if (!ok) {
  326. ECDSA_SIG_free(ret);
  327. ret = NULL;
  328. }
  329. if (ctx)
  330. BN_CTX_free(ctx);
  331. if (m)
  332. BN_clear_free(m);
  333. if (tmp)
  334. BN_clear_free(tmp);
  335. if (order)
  336. BN_free(order);
  337. if (kinv)
  338. BN_clear_free(kinv);
  339. return ret;
  340. }
  341. static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
  342. const ECDSA_SIG *sig, EC_KEY *eckey)
  343. {
  344. int ret = -1, i;
  345. BN_CTX *ctx;
  346. BIGNUM *order, *u1, *u2, *m, *X;
  347. EC_POINT *point = NULL;
  348. const EC_GROUP *group;
  349. const EC_POINT *pub_key;
  350. /* check input values */
  351. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  352. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  353. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
  354. return -1;
  355. }
  356. ctx = BN_CTX_new();
  357. if (!ctx) {
  358. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  359. return -1;
  360. }
  361. BN_CTX_start(ctx);
  362. order = BN_CTX_get(ctx);
  363. u1 = BN_CTX_get(ctx);
  364. u2 = BN_CTX_get(ctx);
  365. m = BN_CTX_get(ctx);
  366. X = BN_CTX_get(ctx);
  367. if (!X) {
  368. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  369. goto err;
  370. }
  371. if (!EC_GROUP_get_order(group, order, ctx)) {
  372. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  373. goto err;
  374. }
  375. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  376. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  377. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  378. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
  379. ret = 0; /* signature is invalid */
  380. goto err;
  381. }
  382. /* calculate tmp1 = inv(S) mod order */
  383. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  384. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  385. goto err;
  386. }
  387. /* digest -> m */
  388. i = BN_num_bits(order);
  389. /*
  390. * Need to truncate digest if it is too long: first truncate whole bytes.
  391. */
  392. if (8 * dgst_len > i)
  393. dgst_len = (i + 7) / 8;
  394. if (!BN_bin2bn(dgst, dgst_len, m)) {
  395. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  396. goto err;
  397. }
  398. /* If still too long truncate remaining bits with a shift */
  399. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  400. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  401. goto err;
  402. }
  403. /* u1 = m * tmp mod order */
  404. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  405. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  406. goto err;
  407. }
  408. /* u2 = r * w mod q */
  409. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  410. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  411. goto err;
  412. }
  413. if ((point = EC_POINT_new(group)) == NULL) {
  414. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  415. goto err;
  416. }
  417. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  418. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  419. goto err;
  420. }
  421. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  422. NID_X9_62_prime_field) {
  423. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  424. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  425. goto err;
  426. }
  427. }
  428. #ifndef OPENSSL_NO_EC2M
  429. else { /* NID_X9_62_characteristic_two_field */
  430. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
  431. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  432. goto err;
  433. }
  434. }
  435. #endif
  436. if (!BN_nnmod(u1, X, order, ctx)) {
  437. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  438. goto err;
  439. }
  440. /* if the signature is correct u1 is equal to sig->r */
  441. ret = (BN_ucmp(u1, sig->r) == 0);
  442. err:
  443. BN_CTX_end(ctx);
  444. BN_CTX_free(ctx);
  445. if (point)
  446. EC_POINT_free(point);
  447. return ret;
  448. }