ecs_ossl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442
  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. 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 (!BN_mod_inverse(k, k, order, ctx)) {
  169. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  170. goto err;
  171. }
  172. /* clear old values if necessary */
  173. if (*rp != NULL)
  174. BN_clear_free(*rp);
  175. if (*kinvp != NULL)
  176. BN_clear_free(*kinvp);
  177. /* save the pre-computed values */
  178. *rp = r;
  179. *kinvp = k;
  180. ret = 1;
  181. err:
  182. if (!ret) {
  183. if (k != NULL)
  184. BN_clear_free(k);
  185. if (r != NULL)
  186. BN_clear_free(r);
  187. }
  188. if (ctx_in == NULL)
  189. BN_CTX_free(ctx);
  190. if (order != NULL)
  191. BN_free(order);
  192. if (tmp_point != NULL)
  193. EC_POINT_free(tmp_point);
  194. if (X)
  195. BN_clear_free(X);
  196. return (ret);
  197. }
  198. static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
  199. const BIGNUM *in_kinv, const BIGNUM *in_r,
  200. EC_KEY *eckey)
  201. {
  202. int ok = 0, i;
  203. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *order = NULL;
  204. const BIGNUM *ckinv;
  205. BN_CTX *ctx = NULL;
  206. const EC_GROUP *group;
  207. ECDSA_SIG *ret;
  208. ECDSA_DATA *ecdsa;
  209. const BIGNUM *priv_key;
  210. ecdsa = ecdsa_check(eckey);
  211. group = EC_KEY_get0_group(eckey);
  212. priv_key = EC_KEY_get0_private_key(eckey);
  213. if (group == NULL || priv_key == NULL || ecdsa == NULL) {
  214. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
  215. return NULL;
  216. }
  217. ret = ECDSA_SIG_new();
  218. if (!ret) {
  219. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  220. return NULL;
  221. }
  222. s = ret->s;
  223. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  224. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL) {
  225. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  226. goto err;
  227. }
  228. if (!EC_GROUP_get_order(group, order, ctx)) {
  229. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
  230. goto err;
  231. }
  232. i = BN_num_bits(order);
  233. /*
  234. * Need to truncate digest if it is too long: first truncate whole bytes.
  235. */
  236. if (8 * dgst_len > i)
  237. dgst_len = (i + 7) / 8;
  238. if (!BN_bin2bn(dgst, dgst_len, m)) {
  239. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  240. goto err;
  241. }
  242. /* If still too long truncate remaining bits with a shift */
  243. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  244. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  245. goto err;
  246. }
  247. do {
  248. if (in_kinv == NULL || in_r == NULL) {
  249. if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r)) {
  250. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_ECDSA_LIB);
  251. goto err;
  252. }
  253. ckinv = kinv;
  254. } else {
  255. ckinv = in_kinv;
  256. if (BN_copy(ret->r, in_r) == NULL) {
  257. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  258. goto err;
  259. }
  260. }
  261. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx)) {
  262. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  263. goto err;
  264. }
  265. if (!BN_mod_add_quick(s, tmp, m, order)) {
  266. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  267. goto err;
  268. }
  269. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  270. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  271. goto err;
  272. }
  273. if (BN_is_zero(s)) {
  274. /*
  275. * if kinv and r have been supplied by the caller don't to
  276. * generate new kinv and r values
  277. */
  278. if (in_kinv != NULL && in_r != NULL) {
  279. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
  280. ECDSA_R_NEED_NEW_SETUP_VALUES);
  281. goto err;
  282. }
  283. } else
  284. /* s != 0 => we have a valid signature */
  285. break;
  286. }
  287. while (1);
  288. ok = 1;
  289. err:
  290. if (!ok) {
  291. ECDSA_SIG_free(ret);
  292. ret = NULL;
  293. }
  294. if (ctx)
  295. BN_CTX_free(ctx);
  296. if (m)
  297. BN_clear_free(m);
  298. if (tmp)
  299. BN_clear_free(tmp);
  300. if (order)
  301. BN_free(order);
  302. if (kinv)
  303. BN_clear_free(kinv);
  304. return ret;
  305. }
  306. static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
  307. const ECDSA_SIG *sig, EC_KEY *eckey)
  308. {
  309. int ret = -1, i;
  310. BN_CTX *ctx;
  311. BIGNUM *order, *u1, *u2, *m, *X;
  312. EC_POINT *point = NULL;
  313. const EC_GROUP *group;
  314. const EC_POINT *pub_key;
  315. /* check input values */
  316. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  317. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  318. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
  319. return -1;
  320. }
  321. ctx = BN_CTX_new();
  322. if (!ctx) {
  323. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  324. return -1;
  325. }
  326. BN_CTX_start(ctx);
  327. order = BN_CTX_get(ctx);
  328. u1 = BN_CTX_get(ctx);
  329. u2 = BN_CTX_get(ctx);
  330. m = BN_CTX_get(ctx);
  331. X = BN_CTX_get(ctx);
  332. if (!X) {
  333. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  334. goto err;
  335. }
  336. if (!EC_GROUP_get_order(group, order, ctx)) {
  337. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  338. goto err;
  339. }
  340. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  341. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  342. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  343. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
  344. ret = 0; /* signature is invalid */
  345. goto err;
  346. }
  347. /* calculate tmp1 = inv(S) mod order */
  348. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  349. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  350. goto err;
  351. }
  352. /* digest -> m */
  353. i = BN_num_bits(order);
  354. /*
  355. * Need to truncate digest if it is too long: first truncate whole bytes.
  356. */
  357. if (8 * dgst_len > i)
  358. dgst_len = (i + 7) / 8;
  359. if (!BN_bin2bn(dgst, dgst_len, m)) {
  360. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  361. goto err;
  362. }
  363. /* If still too long truncate remaining bits with a shift */
  364. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  365. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  366. goto err;
  367. }
  368. /* u1 = m * tmp mod order */
  369. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  370. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  371. goto err;
  372. }
  373. /* u2 = r * w mod q */
  374. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  375. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  376. goto err;
  377. }
  378. if ((point = EC_POINT_new(group)) == NULL) {
  379. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  380. goto err;
  381. }
  382. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  383. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  384. goto err;
  385. }
  386. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  387. NID_X9_62_prime_field) {
  388. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  389. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  390. goto err;
  391. }
  392. }
  393. #ifndef OPENSSL_NO_EC2M
  394. else { /* NID_X9_62_characteristic_two_field */
  395. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
  396. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  397. goto err;
  398. }
  399. }
  400. #endif
  401. if (!BN_nnmod(u1, X, order, ctx)) {
  402. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  403. goto err;
  404. }
  405. /* if the signature is correct u1 is equal to sig->r */
  406. ret = (BN_ucmp(u1, sig->r) == 0);
  407. err:
  408. BN_CTX_end(ctx);
  409. BN_CTX_free(ctx);
  410. if (point)
  411. EC_POINT_free(point);
  412. return ret;
  413. }