ecs_ossl.c 11 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. * 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 *, EC_KEY *eckey);
  64. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  65. BIGNUM **rp);
  66. static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
  67. const ECDSA_SIG *sig, EC_KEY *eckey);
  68. static ECDSA_METHOD openssl_ecdsa_meth = {
  69. "OpenSSL ECDSA method",
  70. ecdsa_do_sign,
  71. ecdsa_sign_setup,
  72. ecdsa_do_verify,
  73. #if 0
  74. NULL, /* init */
  75. NULL, /* finish */
  76. #endif
  77. 0, /* flags */
  78. NULL /* app_data */
  79. };
  80. const ECDSA_METHOD *ECDSA_OpenSSL(void)
  81. {
  82. return &openssl_ecdsa_meth;
  83. }
  84. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  85. BIGNUM **rp)
  86. {
  87. BN_CTX *ctx = NULL;
  88. BIGNUM *k = NULL, *r = NULL, *order = NULL, *X = NULL;
  89. EC_POINT *tmp_point=NULL;
  90. const EC_GROUP *group;
  91. int ret = 0;
  92. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL)
  93. {
  94. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
  95. return 0;
  96. }
  97. if (ctx_in == NULL)
  98. {
  99. if ((ctx = BN_CTX_new()) == NULL)
  100. {
  101. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_MALLOC_FAILURE);
  102. return 0;
  103. }
  104. }
  105. else
  106. ctx = ctx_in;
  107. k = BN_new(); /* this value is later returned in *kinvp */
  108. r = BN_new(); /* this value is later returned in *rp */
  109. order = BN_new();
  110. X = BN_new();
  111. if (!k || !r || !order || !X)
  112. {
  113. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  114. goto err;
  115. }
  116. if ((tmp_point = EC_POINT_new(group)) == NULL)
  117. {
  118. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  119. goto err;
  120. }
  121. if (!EC_GROUP_get_order(group, order, ctx))
  122. {
  123. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  124. goto err;
  125. }
  126. do
  127. {
  128. /* get random k */
  129. do
  130. if (!BN_rand_range(k, order))
  131. {
  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. /* compute r the x-coordinate of generator * k */
  138. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx))
  139. {
  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)) == NID_X9_62_prime_field)
  144. {
  145. if (!EC_POINT_get_affine_coordinates_GFp(group,
  146. tmp_point, X, NULL, ctx))
  147. {
  148. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
  149. goto err;
  150. }
  151. }
  152. else /* NID_X9_62_characteristic_two_field */
  153. {
  154. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  155. tmp_point, X, NULL, ctx))
  156. {
  157. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP,ERR_R_EC_LIB);
  158. goto err;
  159. }
  160. }
  161. if (!BN_nnmod(r, X, order, ctx))
  162. {
  163. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  164. goto err;
  165. }
  166. }
  167. while (BN_is_zero(r));
  168. /* compute the inverse of k */
  169. if (!BN_mod_inverse(k, k, order, ctx))
  170. {
  171. ECDSAerr(ECDSA_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  172. goto err;
  173. }
  174. /* clear old values if necessary */
  175. if (*rp != NULL)
  176. BN_clear_free(*rp);
  177. if (*kinvp != NULL)
  178. BN_clear_free(*kinvp);
  179. /* save the pre-computed values */
  180. *rp = r;
  181. *kinvp = k;
  182. ret = 1;
  183. err:
  184. if (!ret)
  185. {
  186. if (k != NULL) BN_clear_free(k);
  187. if (r != NULL) BN_clear_free(r);
  188. }
  189. if (ctx_in == NULL)
  190. BN_CTX_free(ctx);
  191. if (order != NULL)
  192. BN_free(order);
  193. if (tmp_point != NULL)
  194. EC_POINT_free(tmp_point);
  195. if (X)
  196. BN_clear_free(X);
  197. return(ret);
  198. }
  199. static ECDSA_SIG *ecdsa_do_sign(const unsigned char *dgst, int dgst_len,
  200. const BIGNUM *in_kinv, const BIGNUM *in_r, EC_KEY *eckey)
  201. {
  202. int ok = 0;
  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. {
  215. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_PASSED_NULL_PARAMETER);
  216. return NULL;
  217. }
  218. ret = ECDSA_SIG_new();
  219. if (!ret)
  220. {
  221. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  222. return NULL;
  223. }
  224. s = ret->s;
  225. if ((ctx = BN_CTX_new()) == NULL || (order = BN_new()) == NULL ||
  226. (tmp = BN_new()) == NULL || (m = BN_new()) == NULL)
  227. {
  228. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  229. goto err;
  230. }
  231. if (!EC_GROUP_get_order(group, order, ctx))
  232. {
  233. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_EC_LIB);
  234. goto err;
  235. }
  236. if (dgst_len > BN_num_bytes(order))
  237. {
  238. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,
  239. ECDSA_R_DATA_TOO_LARGE_FOR_KEY_SIZE);
  240. goto err;
  241. }
  242. if (!BN_bin2bn(dgst, dgst_len, m))
  243. {
  244. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  245. goto err;
  246. }
  247. do
  248. {
  249. if (in_kinv == NULL || in_r == NULL)
  250. {
  251. if (!ECDSA_sign_setup(eckey, ctx, &kinv, &ret->r))
  252. {
  253. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN,ERR_R_ECDSA_LIB);
  254. goto err;
  255. }
  256. ckinv = kinv;
  257. }
  258. else
  259. {
  260. ckinv = in_kinv;
  261. if (BN_copy(ret->r, in_r) == NULL)
  262. {
  263. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_MALLOC_FAILURE);
  264. goto err;
  265. }
  266. }
  267. if (!BN_mod_mul(tmp, priv_key, ret->r, order, ctx))
  268. {
  269. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  270. goto err;
  271. }
  272. if (!BN_mod_add_quick(s, tmp, m, order))
  273. {
  274. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  275. goto err;
  276. }
  277. if (!BN_mod_mul(s, s, ckinv, order, ctx))
  278. {
  279. ECDSAerr(ECDSA_F_ECDSA_DO_SIGN, ERR_R_BN_LIB);
  280. goto err;
  281. }
  282. }
  283. while (BN_is_zero(s));
  284. ok = 1;
  285. err:
  286. if (!ok)
  287. {
  288. ECDSA_SIG_free(ret);
  289. ret = NULL;
  290. }
  291. if (ctx)
  292. BN_CTX_free(ctx);
  293. if (m)
  294. BN_clear_free(m);
  295. if (tmp)
  296. BN_clear_free(tmp);
  297. if (order)
  298. BN_free(order);
  299. if (kinv)
  300. BN_clear_free(kinv);
  301. return ret;
  302. }
  303. static int ecdsa_do_verify(const unsigned char *dgst, int dgst_len,
  304. const ECDSA_SIG *sig, EC_KEY *eckey)
  305. {
  306. int ret = -1;
  307. BN_CTX *ctx;
  308. BIGNUM *order, *u1, *u2, *m, *X;
  309. EC_POINT *point = NULL;
  310. const EC_GROUP *group;
  311. const EC_POINT *pub_key;
  312. /* check input values */
  313. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  314. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL)
  315. {
  316. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_MISSING_PARAMETERS);
  317. return -1;
  318. }
  319. ctx = BN_CTX_new();
  320. if (!ctx)
  321. {
  322. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  323. return -1;
  324. }
  325. BN_CTX_start(ctx);
  326. order = BN_CTX_get(ctx);
  327. u1 = BN_CTX_get(ctx);
  328. u2 = BN_CTX_get(ctx);
  329. m = BN_CTX_get(ctx);
  330. X = BN_CTX_get(ctx);
  331. if (!X)
  332. {
  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. {
  338. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  339. goto err;
  340. }
  341. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  342. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  343. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0)
  344. {
  345. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ECDSA_R_BAD_SIGNATURE);
  346. ret = 0; /* signature is invalid */
  347. goto err;
  348. }
  349. /* calculate tmp1 = inv(S) mod order */
  350. if (!BN_mod_inverse(u2, sig->s, order, ctx))
  351. {
  352. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  353. goto err;
  354. }
  355. /* digest -> m */
  356. if (!BN_bin2bn(dgst, dgst_len, m))
  357. {
  358. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  359. goto err;
  360. }
  361. /* u1 = m * tmp mod order */
  362. if (!BN_mod_mul(u1, m, u2, order, ctx))
  363. {
  364. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  365. goto err;
  366. }
  367. /* u2 = r * w mod q */
  368. if (!BN_mod_mul(u2, sig->r, u2, order, ctx))
  369. {
  370. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  371. goto err;
  372. }
  373. if ((point = EC_POINT_new(group)) == NULL)
  374. {
  375. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_MALLOC_FAILURE);
  376. goto err;
  377. }
  378. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx))
  379. {
  380. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  381. goto err;
  382. }
  383. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) == NID_X9_62_prime_field)
  384. {
  385. if (!EC_POINT_get_affine_coordinates_GFp(group,
  386. point, X, NULL, ctx))
  387. {
  388. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  389. goto err;
  390. }
  391. }
  392. else /* NID_X9_62_characteristic_two_field */
  393. {
  394. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  395. point, X, NULL, ctx))
  396. {
  397. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_EC_LIB);
  398. goto err;
  399. }
  400. }
  401. if (!BN_nnmod(u1, X, order, ctx))
  402. {
  403. ECDSAerr(ECDSA_F_ECDSA_DO_VERIFY, ERR_R_BN_LIB);
  404. goto err;
  405. }
  406. /* if the signature is correct u1 is equal to sig->r */
  407. ret = (BN_ucmp(u1, sig->r) == 0);
  408. err:
  409. BN_CTX_end(ctx);
  410. BN_CTX_free(ctx);
  411. if (point)
  412. EC_POINT_free(point);
  413. return ret;
  414. }