ecdsa_ossl.c 13 KB

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