ecdsa_ossl.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481
  1. /*
  2. * Copyright 2002-2023 The OpenSSL Project Authors. All Rights Reserved.
  3. *
  4. * Licensed under the Apache License 2.0 (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. /*
  10. * ECDSA low level APIs are deprecated for public use, but still ok for
  11. * internal use.
  12. */
  13. #include "internal/deprecated.h"
  14. #include <string.h>
  15. #include <openssl/err.h>
  16. #include <openssl/obj_mac.h>
  17. #include <openssl/rand.h>
  18. #include "crypto/bn.h"
  19. #include "ec_local.h"
  20. #define MIN_ECDSA_SIGN_ORDERBITS 64
  21. /*
  22. * It is highly unlikely that a retry will happen,
  23. * Multiple retries would indicate that something is wrong
  24. * with the group parameters (which would normally only happen
  25. * with a bad custom group).
  26. */
  27. #define MAX_ECDSA_SIGN_RETRIES 8
  28. int ossl_ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  29. BIGNUM **rp)
  30. {
  31. if (eckey->group->meth->ecdsa_sign_setup == NULL) {
  32. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
  33. return 0;
  34. }
  35. return eckey->group->meth->ecdsa_sign_setup(eckey, ctx_in, kinvp, rp);
  36. }
  37. ECDSA_SIG *ossl_ecdsa_sign_sig(const unsigned char *dgst, int dgst_len,
  38. const BIGNUM *in_kinv, const BIGNUM *in_r,
  39. EC_KEY *eckey)
  40. {
  41. if (eckey->group->meth->ecdsa_sign_sig == NULL) {
  42. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
  43. return NULL;
  44. }
  45. return eckey->group->meth->ecdsa_sign_sig(dgst, dgst_len,
  46. in_kinv, in_r, eckey);
  47. }
  48. int ossl_ecdsa_verify_sig(const unsigned char *dgst, int dgst_len,
  49. const ECDSA_SIG *sig, EC_KEY *eckey)
  50. {
  51. if (eckey->group->meth->ecdsa_verify_sig == NULL) {
  52. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_ECDSA);
  53. return 0;
  54. }
  55. return eckey->group->meth->ecdsa_verify_sig(dgst, dgst_len, sig, eckey);
  56. }
  57. int ossl_ecdsa_sign(int type, const unsigned char *dgst, int dlen,
  58. unsigned char *sig, unsigned int *siglen,
  59. const BIGNUM *kinv, const BIGNUM *r, EC_KEY *eckey)
  60. {
  61. ECDSA_SIG *s;
  62. s = ECDSA_do_sign_ex(dgst, dlen, kinv, r, eckey);
  63. if (s == NULL) {
  64. *siglen = 0;
  65. return 0;
  66. }
  67. *siglen = i2d_ECDSA_SIG(s, sig != NULL ? &sig : NULL);
  68. ECDSA_SIG_free(s);
  69. return 1;
  70. }
  71. static int ecdsa_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in,
  72. BIGNUM **kinvp, BIGNUM **rp,
  73. const unsigned char *dgst, int dlen)
  74. {
  75. BN_CTX *ctx = NULL;
  76. BIGNUM *k = NULL, *r = NULL, *X = NULL;
  77. const BIGNUM *order;
  78. EC_POINT *tmp_point = NULL;
  79. const EC_GROUP *group;
  80. int ret = 0;
  81. int order_bits;
  82. const BIGNUM *priv_key;
  83. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL) {
  84. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  85. return 0;
  86. }
  87. if ((priv_key = EC_KEY_get0_private_key(eckey)) == NULL) {
  88. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  89. return 0;
  90. }
  91. if (!EC_KEY_can_sign(eckey)) {
  92. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  93. return 0;
  94. }
  95. if ((ctx = ctx_in) == NULL) {
  96. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL) {
  97. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  98. return 0;
  99. }
  100. }
  101. k = BN_secure_new(); /* this value is later returned in *kinvp */
  102. r = BN_new(); /* this value is later returned in *rp */
  103. X = BN_new();
  104. if (k == NULL || r == NULL || X == NULL) {
  105. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  106. goto err;
  107. }
  108. if ((tmp_point = EC_POINT_new(group)) == NULL) {
  109. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  110. goto err;
  111. }
  112. order = EC_GROUP_get0_order(group);
  113. /* Preallocate space */
  114. order_bits = BN_num_bits(order);
  115. /* Check the number of bits here so that an infinite loop is not possible */
  116. if (order_bits < MIN_ECDSA_SIGN_ORDERBITS
  117. || !BN_set_bit(k, order_bits)
  118. || !BN_set_bit(r, order_bits)
  119. || !BN_set_bit(X, order_bits))
  120. goto err;
  121. do {
  122. /* get random k */
  123. do {
  124. if (dgst != NULL) {
  125. if (!BN_generate_dsa_nonce(k, order, priv_key,
  126. dgst, dlen, ctx)) {
  127. ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  128. goto err;
  129. }
  130. } else {
  131. if (!BN_priv_rand_range_ex(k, order, 0, ctx)) {
  132. ERR_raise(ERR_LIB_EC, EC_R_RANDOM_NUMBER_GENERATION_FAILED);
  133. goto err;
  134. }
  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. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  140. goto err;
  141. }
  142. if (!EC_POINT_get_affine_coordinates(group, tmp_point, X, NULL, ctx)) {
  143. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  144. goto err;
  145. }
  146. if (!BN_nnmod(r, X, order, ctx)) {
  147. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  148. goto err;
  149. }
  150. } while (BN_is_zero(r));
  151. /* compute the inverse of k */
  152. if (!ossl_ec_group_do_inverse_ord(group, k, k, ctx)) {
  153. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  154. goto err;
  155. }
  156. /* clear old values if necessary */
  157. BN_clear_free(*rp);
  158. BN_clear_free(*kinvp);
  159. /* save the pre-computed values */
  160. *rp = r;
  161. *kinvp = k;
  162. ret = 1;
  163. err:
  164. if (!ret) {
  165. BN_clear_free(k);
  166. BN_clear_free(r);
  167. }
  168. if (ctx != ctx_in)
  169. BN_CTX_free(ctx);
  170. EC_POINT_free(tmp_point);
  171. BN_clear_free(X);
  172. return ret;
  173. }
  174. int ossl_ecdsa_simple_sign_setup(EC_KEY *eckey, BN_CTX *ctx_in, BIGNUM **kinvp,
  175. BIGNUM **rp)
  176. {
  177. return ecdsa_sign_setup(eckey, ctx_in, kinvp, rp, NULL, 0);
  178. }
  179. ECDSA_SIG *ossl_ecdsa_simple_sign_sig(const unsigned char *dgst, int dgst_len,
  180. const BIGNUM *in_kinv, const BIGNUM *in_r,
  181. EC_KEY *eckey)
  182. {
  183. int ok = 0, i;
  184. int retries = 0;
  185. BIGNUM *kinv = NULL, *s, *m = NULL;
  186. const BIGNUM *order, *ckinv;
  187. BN_CTX *ctx = NULL;
  188. const EC_GROUP *group;
  189. ECDSA_SIG *ret;
  190. const BIGNUM *priv_key;
  191. group = EC_KEY_get0_group(eckey);
  192. priv_key = EC_KEY_get0_private_key(eckey);
  193. if (group == NULL) {
  194. ERR_raise(ERR_LIB_EC, ERR_R_PASSED_NULL_PARAMETER);
  195. return NULL;
  196. }
  197. if (priv_key == NULL) {
  198. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PRIVATE_KEY);
  199. return NULL;
  200. }
  201. if (!EC_KEY_can_sign(eckey)) {
  202. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  203. return NULL;
  204. }
  205. ret = ECDSA_SIG_new();
  206. if (ret == NULL) {
  207. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  208. return NULL;
  209. }
  210. ret->r = BN_new();
  211. ret->s = BN_new();
  212. if (ret->r == NULL || ret->s == NULL) {
  213. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  214. goto err;
  215. }
  216. s = ret->s;
  217. if ((ctx = BN_CTX_new_ex(eckey->libctx)) == NULL
  218. || (m = BN_new()) == NULL) {
  219. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  220. goto err;
  221. }
  222. order = EC_GROUP_get0_order(group);
  223. i = BN_num_bits(order);
  224. /*
  225. * Need to truncate digest if it is too long: first truncate whole bytes.
  226. */
  227. if (8 * dgst_len > i)
  228. dgst_len = (i + 7) / 8;
  229. if (!BN_bin2bn(dgst, dgst_len, m)) {
  230. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  231. goto err;
  232. }
  233. /* If still too long, truncate remaining bits with a shift */
  234. if ((8 * dgst_len > i) && !BN_rshift(m, m, 8 - (i & 0x7))) {
  235. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  236. goto err;
  237. }
  238. do {
  239. if (in_kinv == NULL || in_r == NULL) {
  240. if (!ecdsa_sign_setup(eckey, ctx, &kinv, &ret->r, dgst, dgst_len)) {
  241. ERR_raise(ERR_LIB_EC, ERR_R_ECDSA_LIB);
  242. goto err;
  243. }
  244. ckinv = kinv;
  245. } else {
  246. ckinv = in_kinv;
  247. if (BN_copy(ret->r, in_r) == NULL) {
  248. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  249. goto err;
  250. }
  251. }
  252. /*
  253. * With only one multiplicant being in Montgomery domain
  254. * multiplication yields real result without post-conversion.
  255. * Also note that all operations but last are performed with
  256. * zero-padded vectors. Last operation, BN_mod_mul_montgomery
  257. * below, returns user-visible value with removed zero padding.
  258. */
  259. if (!bn_to_mont_fixed_top(s, ret->r, group->mont_data, ctx)
  260. || !bn_mul_mont_fixed_top(s, s, priv_key, group->mont_data, ctx)) {
  261. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  262. goto err;
  263. }
  264. if (!bn_mod_add_fixed_top(s, s, m, order)) {
  265. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  266. goto err;
  267. }
  268. /*
  269. * |s| can still be larger than modulus, because |m| can be. In
  270. * such case we count on Montgomery reduction to tie it up.
  271. */
  272. if (!bn_to_mont_fixed_top(s, s, group->mont_data, ctx)
  273. || !BN_mod_mul_montgomery(s, s, ckinv, group->mont_data, ctx)) {
  274. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  275. goto err;
  276. }
  277. if (BN_is_zero(s)) {
  278. /*
  279. * if kinv and r have been supplied by the caller, don't
  280. * generate new kinv and r values
  281. */
  282. if (in_kinv != NULL && in_r != NULL) {
  283. ERR_raise(ERR_LIB_EC, EC_R_NEED_NEW_SETUP_VALUES);
  284. goto err;
  285. }
  286. /* Avoid infinite loops cause by invalid group parameters */
  287. if (retries++ > MAX_ECDSA_SIGN_RETRIES) {
  288. ERR_raise(ERR_LIB_EC, EC_R_TOO_MANY_RETRIES);
  289. goto err;
  290. }
  291. } else {
  292. /* s != 0 => we have a valid signature */
  293. break;
  294. }
  295. } while (1);
  296. ok = 1;
  297. err:
  298. if (!ok) {
  299. ECDSA_SIG_free(ret);
  300. ret = NULL;
  301. }
  302. BN_CTX_free(ctx);
  303. BN_clear_free(m);
  304. BN_clear_free(kinv);
  305. return ret;
  306. }
  307. /*-
  308. * returns
  309. * 1: correct signature
  310. * 0: incorrect signature
  311. * -1: error
  312. */
  313. int ossl_ecdsa_verify(int type, const unsigned char *dgst, int dgst_len,
  314. const unsigned char *sigbuf, int sig_len, EC_KEY *eckey)
  315. {
  316. ECDSA_SIG *s;
  317. const unsigned char *p = sigbuf;
  318. unsigned char *der = NULL;
  319. int derlen = -1;
  320. int ret = -1;
  321. s = ECDSA_SIG_new();
  322. if (s == NULL)
  323. return ret;
  324. if (d2i_ECDSA_SIG(&s, &p, sig_len) == NULL)
  325. goto err;
  326. /* Ensure signature uses DER and doesn't have trailing garbage */
  327. derlen = i2d_ECDSA_SIG(s, &der);
  328. if (derlen != sig_len || memcmp(sigbuf, der, derlen) != 0)
  329. goto err;
  330. ret = ECDSA_do_verify(dgst, dgst_len, s, eckey);
  331. err:
  332. OPENSSL_free(der);
  333. ECDSA_SIG_free(s);
  334. return ret;
  335. }
  336. int ossl_ecdsa_simple_verify_sig(const unsigned char *dgst, int dgst_len,
  337. const ECDSA_SIG *sig, EC_KEY *eckey)
  338. {
  339. int ret = -1, i;
  340. BN_CTX *ctx;
  341. const BIGNUM *order;
  342. BIGNUM *u1, *u2, *m, *X;
  343. EC_POINT *point = NULL;
  344. const EC_GROUP *group;
  345. const EC_POINT *pub_key;
  346. /* check input values */
  347. if (eckey == NULL || (group = EC_KEY_get0_group(eckey)) == NULL ||
  348. (pub_key = EC_KEY_get0_public_key(eckey)) == NULL || sig == NULL) {
  349. ERR_raise(ERR_LIB_EC, EC_R_MISSING_PARAMETERS);
  350. return -1;
  351. }
  352. if (!EC_KEY_can_sign(eckey)) {
  353. ERR_raise(ERR_LIB_EC, EC_R_CURVE_DOES_NOT_SUPPORT_SIGNING);
  354. return -1;
  355. }
  356. ctx = BN_CTX_new_ex(eckey->libctx);
  357. if (ctx == NULL) {
  358. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  359. return -1;
  360. }
  361. BN_CTX_start(ctx);
  362. u1 = BN_CTX_get(ctx);
  363. u2 = BN_CTX_get(ctx);
  364. m = BN_CTX_get(ctx);
  365. X = BN_CTX_get(ctx);
  366. if (X == NULL) {
  367. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  368. goto err;
  369. }
  370. order = EC_GROUP_get0_order(group);
  371. if (order == NULL) {
  372. ERR_raise(ERR_LIB_EC, 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. ERR_raise(ERR_LIB_EC, EC_R_BAD_SIGNATURE);
  379. ret = 0; /* signature is invalid */
  380. goto err;
  381. }
  382. /* calculate tmp1 = inv(S) mod order */
  383. if (!ossl_ec_group_do_inverse_ord(group, u2, sig->s, ctx)) {
  384. ERR_raise(ERR_LIB_EC, 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. ERR_raise(ERR_LIB_EC, 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. ERR_raise(ERR_LIB_EC, 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. ERR_raise(ERR_LIB_EC, 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. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  411. goto err;
  412. }
  413. if ((point = EC_POINT_new(group)) == NULL) {
  414. ERR_raise(ERR_LIB_EC, ERR_R_MALLOC_FAILURE);
  415. goto err;
  416. }
  417. if (!EC_POINT_mul(group, point, u1, pub_key, u2, ctx)) {
  418. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  419. goto err;
  420. }
  421. if (!EC_POINT_get_affine_coordinates(group, point, X, NULL, ctx)) {
  422. ERR_raise(ERR_LIB_EC, ERR_R_EC_LIB);
  423. goto err;
  424. }
  425. if (!BN_nnmod(u1, X, order, ctx)) {
  426. ERR_raise(ERR_LIB_EC, ERR_R_BN_LIB);
  427. goto err;
  428. }
  429. /* if the signature is correct u1 is equal to sig->r */
  430. ret = (BN_ucmp(u1, sig->r) == 0);
  431. err:
  432. BN_CTX_end(ctx);
  433. BN_CTX_free(ctx);
  434. EC_POINT_free(point);
  435. return ret;
  436. }