ecdsa_ossl.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521
  1. /*
  2. * Copyright 2002-2018 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the OpenSSL license (the "License"). You may not use
  5. * this file except in compliance with the License. You can obtain a copy
  6. * in the file LICENSE in the source distribution or at
  7. * https://www.openssl.org/source/license.html
  8. */
  9. #include <string.h>
  10. #include <openssl/err.h>
  11. #include <openssl/obj_mac.h>
  12. #include <openssl/bn.h>
  13. #include <openssl/rand.h>
  14. #include <openssl/ec.h>
  15. #include "ec_lcl.h"
  16. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  17. unsigned char *sig, unsigned int *siglen,
  18. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
  19. {
  20. ECDSA_SIG *s;
  21. s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
  22. if (s == NULL) {
  23. *siglen = 0;
  24. return 0;
  25. }
  26. *siglen = i2d_ECDSA_SIG(s, &sig);
  27. ECDSA_SIG_free(s);
  28. return 1;
  29. }
  30. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
  31. BIGNUM **kinvp, BIGNUM **rp,
  32. const unsigned char *dgst, int dlen)
  33. {
  34. BN_CTX *ctx = NULL;
  35. BIGNUM *k = NULL, *r = NULL, *X = NULL;
  36. const BIGNUM *order;
  37. EC_POINT *tmp_point = NULL;
  38. const EC_GROUP *group;
  39. int ret = 0;
  40. int order_bits;
  41. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  42. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_PASSED_NULL_PARAMETER);
  43. return 0;
  44. }
  45. if (!EC_KEY_can_sign(eckey)) {
  46. ECerr(EC_F_ECDSA_SIGN_SETUP, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  47. return 0;
  48. }
  49. if (ctx_in == NULL) {
  50. if ((ctx = BN_CTX_new()) == NULL) {
  51. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  52. return 0;
  53. }
  54. } else
  55. ctx = ctx_in;
  56. k = BN_new(); /* this value is later returned in *kinvp */
  57. r = BN_new(); /* this value is later returned in *rp */
  58. X = BN_new();
  59. if (k == NULL || r == NULL || X == NULL) {
  60. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_MALLOC_FAILURE);
  61. goto err;
  62. }
  63. if ((tmp_point = EC_POINT_new(group)) == NULL) {
  64. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  65. goto err;
  66. }
  67. order = EC_GROUP_get0_order(group);
  68. if (order == NULL) {
  69. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  70. goto err;
  71. }
  72. /* Preallocate space */
  73. order_bits = BN_num_bits(order);
  74. if (!BN_set_bit(k, order_bits)
  75. || !BN_set_bit(r, order_bits)
  76. || !BN_set_bit(X, order_bits))
  77. goto err;
  78. do {
  79. /* get random k */
  80. do
  81. if (dgst != NULL) {
  82. if (!BN_generate_dsa_nonce
  83. (k, order, EC_KEY_get0_private_key(eckey), dgst, dlen,
  84. ctx)) {
  85. ECerr(EC_F_ECDSA_SIGN_SETUP,
  86. EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  87. goto err;
  88. }
  89. } else {
  90. if (!BN_priv_rand_range(k, order)) {
  91. ECerr(EC_F_ECDSA_SIGN_SETUP,
  92. EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  93. goto err;
  94. }
  95. }
  96. while (BN_is_zero(k));
  97. /* compute r the x-coordinate of generator * k */
  98. if (!EC_POINT_mul(group, tmp_point, k, NULL, NULL, ctx)) {
  99. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  100. goto err;
  101. }
  102. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  103. NID_X9_62_prime_field) {
  104. if (!EC_POINT_get_affine_coordinates_GFp
  105. (group, tmp_point, X, NULL, ctx)) {
  106. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  107. goto err;
  108. }
  109. }
  110. #ifndef OPENSSL_NO_EC2M
  111. else { /* NID_X9_62_characteristic_two_field */
  112. if (!EC_POINT_get_affine_coordinates_GF2m(group,
  113. tmp_point, X, NULL,
  114. ctx)) {
  115. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_EC_LIB);
  116. goto err;
  117. }
  118. }
  119. #endif
  120. if (!BN_nnmod(r, X, order, ctx)) {
  121. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  122. goto err;
  123. }
  124. }
  125. while (BN_is_zero(r));
  126. /* Check if optimized inverse is implemented */
  127. if (EC_GROUP_do_inverse_ord(group, k, k, ctx) == 0) {
  128. /* compute the inverse of k */
  129. if (group->mont_data != NULL) {
  130. /*
  131. * We want inverse in constant time, therefore we utilize the fact
  132. * order must be prime and use Fermats Little Theorem instead.
  133. */
  134. if (!BN_set_word(X, 2)) {
  135. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  136. goto err;
  137. }
  138. if (!BN_mod_sub(X, order, X, order, ctx)) {
  139. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  140. goto err;
  141. }
  142. BN_set_flags(X, BN_FLG_CONSTTIME);
  143. if (!BN_mod_exp_mont_consttime(k, k, X, order, ctx,
  144. group->mont_data)) {
  145. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  146. goto err;
  147. }
  148. } else {
  149. if (!BN_mod_inverse(k, k, order, ctx)) {
  150. ECerr(EC_F_ECDSA_SIGN_SETUP, ERR_R_BN_LIB);
  151. goto err;
  152. }
  153. }
  154. }
  155. /* clear old values if necessary */
  156. BN_clear_free(*rp);
  157. BN_clear_free(*kinvp);
  158. /* save the pre-computed values */
  159. *rp = r;
  160. *kinvp = k;
  161. ret = 1;
  162. err:
  163. if (!ret) {
  164. BN_clear_free(k);
  165. BN_clear_free(r);
  166. }
  167. if (ctx != ctx_in)
  168. BN_CTX_free(ctx);
  169. EC_POINT_free(tmp_point);
  170. BN_clear_free(X);
  171. return ret;
  172. }
  173. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  174. BIGNUM **rp)
  175. {
  176. return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
  177. }
  178. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  179. const BIGNUM *in_kinv, const BIGNUM *in_r,
  180. EC_KEY *eckey)
  181. {
  182. int ok = 0, i;
  183. BIGNUM *kinv = NULL, *s, *m = NULL, *tmp = NULL, *blind = NULL;
  184. BIGNUM *blindm = NULL;
  185. const BIGNUM *order, *ckinv;
  186. BN_CTX *ctx = NULL;
  187. const EC_GROUP *group;
  188. ECDSA_SIG *ret;
  189. const BIGNUM *priv_key;
  190. group = EC_KEY_get0_group(eckey);
  191. priv_key = EC_KEY_get0_private_key(eckey);
  192. if (group == NULL || priv_key == NULL) {
  193. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_PASSED_NULL_PARAMETER);
  194. return NULL;
  195. }
  196. if (!EC_KEY_can_sign(eckey)) {
  197. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  198. return NULL;
  199. }
  200. ret = ECDSA_SIG_new();
  201. if (ret == NULL) {
  202. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  203. return NULL;
  204. }
  205. ret->r = BN_new();
  206. ret->s = BN_new();
  207. if (ret->r == NULL || ret->s == NULL) {
  208. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  209. goto err;
  210. }
  211. s = ret->s;
  212. ctx = BN_CTX_secure_new();
  213. if (ctx == NULL) {
  214. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  215. goto err;
  216. }
  217. BN_CTX_start(ctx);
  218. tmp = BN_CTX_get(ctx);
  219. m = BN_CTX_get(ctx);
  220. blind = BN_CTX_get(ctx);
  221. blindm = BN_CTX_get(ctx);
  222. if (blindm == NULL) {
  223. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  224. goto err;
  225. }
  226. order = EC_GROUP_get0_order(group);
  227. if (order == NULL) {
  228. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_EC_LIB);
  229. goto err;
  230. }
  231. i = BN_num_bits(order);
  232. /*
  233. * Need to truncate digest if it is too long: first truncate whole bytes.
  234. */
  235. if (8 * dgst_len > i)
  236. dgst_len = (i + 7) / 8;
  237. if (!BN_bin2bn(dgst, dgst_len, m)) {
  238. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  239. goto err;
  240. }
  241. /* If still too long truncate remaining bits with a shift */
  242. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  243. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  244. goto err;
  245. }
  246. do {
  247. if (in_kinv == NULL || in_r == NULL) {
  248. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
  249. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_ECDSA_LIB);
  250. goto err;
  251. }
  252. ckinv = kinv;
  253. } else {
  254. ckinv = in_kinv;
  255. if (BN_copy(ret->r, in_r) == NULL) {
  256. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_MALLOC_FAILURE);
  257. goto err;
  258. }
  259. }
  260. /*
  261. * The normal signature calculation is:
  262. *
  263. * s := k^-1 * (m + r * priv_key) mod order
  264. *
  265. * We will blind this to protect against side channel attacks
  266. *
  267. * s := k^-1 * blind^-1 * (blind * m + blind * r * priv_key) mod order
  268. */
  269. /* Generate a blinding value */
  270. do {
  271. if (!BN_priv_rand(blind, BN_num_bits(order) - 1,
  272. BN_RAND_TOP_ANY, BN_RAND_BOTTOM_ANY))
  273. goto err;
  274. } while (BN_is_zero(blind));
  275. BN_set_flags(blind, BN_FLG_CONSTTIME);
  276. BN_set_flags(blindm, BN_FLG_CONSTTIME);
  277. BN_set_flags(tmp, BN_FLG_CONSTTIME);
  278. /* tmp := blind * priv_key * r mod order */
  279. if (!BN_mod_mul(tmp, blind, priv_key, order, ctx)) {
  280. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  281. goto err;
  282. }
  283. if (!BN_mod_mul(tmp, tmp, ret->r, order, ctx)) {
  284. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  285. goto err;
  286. }
  287. /* blindm := blind * m mod order */
  288. if (!BN_mod_mul(blindm, blind, m, order, ctx)) {
  289. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  290. goto err;
  291. }
  292. /* s : = (blind * priv_key * r) + (blind * m) mod order */
  293. if (!BN_mod_add_quick(s, tmp, blindm, order)) {
  294. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  295. goto err;
  296. }
  297. /* s:= s * blind^-1 mod order */
  298. if (BN_mod_inverse(blind, blind, order, ctx) == NULL) {
  299. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  300. goto err;
  301. }
  302. if (!BN_mod_mul(s, s, blind, order, ctx)) {
  303. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  304. goto err;
  305. }
  306. /* s := s * k^-1 mod order */
  307. if (!BN_mod_mul(s, s, ckinv, order, ctx)) {
  308. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, ERR_R_BN_LIB);
  309. goto err;
  310. }
  311. if (BN_is_zero(s)) {
  312. /*
  313. * if kinv and r have been supplied by the caller, don't
  314. * generate new kinv and r values
  315. */
  316. if (in_kinv != NULL && in_r != NULL) {
  317. ECerr(EC_F_OSSL_ECDSA_SIGN_SIG, EC_R_NEED_NEW_SETUP_VALUES);
  318. goto err;
  319. }
  320. } else
  321. /* s != 0 => we have a valid signature */
  322. break;
  323. }
  324. while (1);
  325. ok = 1;
  326. err:
  327. if (!ok) {
  328. ECDSA_SIG_free(ret);
  329. ret = NULL;
  330. }
  331. BN_CTX_end(ctx);
  332. BN_CTX_free(ctx);
  333. BN_clear_free(kinv);
  334. return ret;
  335. }
  336. /*-
  337. * returns
  338. * 1: correct signature
  339. * 0: incorrect signature
  340. * -1: error
  341. */
  342. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  343. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
  344. {
  345. ECDSA_SIG *s;
  346. const unsigned char *p = sigbuf;
  347. unsigned char *der = NULL;
  348. int derlen = -1;
  349. int ret = -1;
  350. s = ECDSA_SIG_new();
  351. if (s == NULL)
  352. return ret;
  353. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
  354. goto err;
  355. /* Ensure signature uses DER and doesn't have trailing garbage */
  356. derlen = i2d_ECDSA_SIG(s, &der);
  357. if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
  358. goto err;
  359. ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
  360. err:
  361. OPENSSL_clear_free(der, derlen);
  362. ECDSA_SIG_free(s);
  363. return ret;
  364. }
  365. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  366. const ECDSA_SIG *sig, EC_KEY *eckey)
  367. {
  368. int ret = -1, i;
  369. BN_CTX *ctx;
  370. const BIGNUM *order;
  371. BIGNUM *u1, *u2, *m, *X;
  372. EC_POINT *point = NULL;
  373. const EC_GROUP *group;
  374. const EC_POINT *pub_key;
  375. /* check input values */
  376. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  377. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  378. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_MISSING_PARAMETERS);
  379. return -1;
  380. }
  381. if (!EC_KEY_can_sign(eckey)) {
  382. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  383. return -1;
  384. }
  385. ctx = BN_CTX_new();
  386. if (ctx == NULL) {
  387. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
  388. return -1;
  389. }
  390. BN_CTX_start(ctx);
  391. u1 = BN_CTX_get(ctx);
  392. u2 = BN_CTX_get(ctx);
  393. m = BN_CTX_get(ctx);
  394. X = BN_CTX_get(ctx);
  395. if (X == NULL) {
  396. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  397. goto err;
  398. }
  399. order = EC_GROUP_get0_order(group);
  400. if (order == NULL) {
  401. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
  402. goto err;
  403. }
  404. if (BN_is_zero(sig->r) || BN_is_negative(sig->r) ||
  405. BN_ucmp(sig->r, order) >= 0 || BN_is_zero(sig->s) ||
  406. BN_is_negative(sig->s) || BN_ucmp(sig->s, order) >= 0) {
  407. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, EC_R_BAD_SIGNATURE);
  408. ret = 0; /* signature is invalid */
  409. goto err;
  410. }
  411. /* calculate tmp1 = inv(S) mod order */
  412. /* Check if optimized inverse is implemented */
  413. if (EC_GROUP_do_inverse_ord(group, u2, sig->s, ctx) == 0) {
  414. if (!BN_mod_inverse(u2, sig->s, order, ctx)) {
  415. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  416. goto err;
  417. }
  418. }
  419. /* digest -> m */
  420. i = BN_num_bits(order);
  421. /*
  422. * Need to truncate digest if it is too long: first truncate whole bytes.
  423. */
  424. if (8 * dgst_len > i)
  425. dgst_len = (i + 7) / 8;
  426. if (!BN_bin2bn(dgst, dgst_len, m)) {
  427. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  428. goto err;
  429. }
  430. /* If still too long truncate remaining bits with a shift */
  431. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  432. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  433. goto err;
  434. }
  435. /* u1 = m * tmp mod order */
  436. if (!BN_mod_mul(u1, m, u2, order, ctx)) {
  437. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  438. goto err;
  439. }
  440. /* u2 = r * w mod q */
  441. if (!BN_mod_mul(u2, sig->r, u2, order, ctx)) {
  442. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  443. goto err;
  444. }
  445. if ((point = EC_POINT_new(group)) == NULL) {
  446. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_MALLOC_FAILURE);
  447. goto err;
  448. }
  449. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  450. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
  451. goto err;
  452. }
  453. if (EC_METHOD_get_field_type(EC_GROUP_method_of(group)) ==
  454. NID_X9_62_prime_field) {
  455. if (!EC_POINT_get_affine_coordinates_GFp(group, point, X, NULL, ctx)) {
  456. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
  457. goto err;
  458. }
  459. }
  460. #ifndef OPENSSL_NO_EC2M
  461. else { /* NID_X9_62_characteristic_two_field */
  462. if (!EC_POINT_get_affine_coordinates_GF2m(group, point, X, NULL, ctx)) {
  463. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_EC_LIB);
  464. goto err;
  465. }
  466. }
  467. #endif
  468. if (!BN_nnmod(u1, X, order, ctx)) {
  469. ECerr(EC_F_OSSL_ECDSA_VERIFY_SIG, ERR_R_BN_LIB);
  470. goto err;
  471. }
  472. /* if the signature is correct u1 is equal to sig->r */
  473. ret = (BN_ucmp(u1, sig->r) == 0);
  474. err:
  475. BN_CTX_end(ctx);
  476. BN_CTX_free(ctx);
  477. EC_POINT_free(point);
  478. return ret;
  479. }