ecs_ossl.c 16 KB

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