ecdsa.c 27 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868
  1. /*
  2. * Elliptic curve DSA
  3. *
  4. * Copyright The Mbed TLS Contributors
  5. * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
  6. */
  7. /*
  8. * References:
  9. *
  10. * SEC1 https://www.secg.org/sec1-v2.pdf
  11. */
  12. #include "common.h"
  13. #if defined(MBEDTLS_ECDSA_C)
  14. #include "mbedtls/ecdsa.h"
  15. #include "mbedtls/asn1write.h"
  16. #include <string.h>
  17. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  18. #include "mbedtls/hmac_drbg.h"
  19. #endif
  20. #include "mbedtls/platform.h"
  21. #include "mbedtls/platform_util.h"
  22. #include "mbedtls/error.h"
  23. #if defined(MBEDTLS_ECP_RESTARTABLE)
  24. /*
  25. * Sub-context for ecdsa_verify()
  26. */
  27. struct mbedtls_ecdsa_restart_ver {
  28. mbedtls_mpi u1, u2; /* intermediate values */
  29. enum { /* what to do next? */
  30. ecdsa_ver_init = 0, /* getting started */
  31. ecdsa_ver_muladd, /* muladd step */
  32. } state;
  33. };
  34. /*
  35. * Init verify restart sub-context
  36. */
  37. static void ecdsa_restart_ver_init(mbedtls_ecdsa_restart_ver_ctx *ctx)
  38. {
  39. mbedtls_mpi_init(&ctx->u1);
  40. mbedtls_mpi_init(&ctx->u2);
  41. ctx->state = ecdsa_ver_init;
  42. }
  43. /*
  44. * Free the components of a verify restart sub-context
  45. */
  46. static void ecdsa_restart_ver_free(mbedtls_ecdsa_restart_ver_ctx *ctx)
  47. {
  48. if (ctx == NULL) {
  49. return;
  50. }
  51. mbedtls_mpi_free(&ctx->u1);
  52. mbedtls_mpi_free(&ctx->u2);
  53. ecdsa_restart_ver_init(ctx);
  54. }
  55. /*
  56. * Sub-context for ecdsa_sign()
  57. */
  58. struct mbedtls_ecdsa_restart_sig {
  59. int sign_tries;
  60. int key_tries;
  61. mbedtls_mpi k; /* per-signature random */
  62. mbedtls_mpi r; /* r value */
  63. enum { /* what to do next? */
  64. ecdsa_sig_init = 0, /* getting started */
  65. ecdsa_sig_mul, /* doing ecp_mul() */
  66. ecdsa_sig_modn, /* mod N computations */
  67. } state;
  68. };
  69. /*
  70. * Init verify sign sub-context
  71. */
  72. static void ecdsa_restart_sig_init(mbedtls_ecdsa_restart_sig_ctx *ctx)
  73. {
  74. ctx->sign_tries = 0;
  75. ctx->key_tries = 0;
  76. mbedtls_mpi_init(&ctx->k);
  77. mbedtls_mpi_init(&ctx->r);
  78. ctx->state = ecdsa_sig_init;
  79. }
  80. /*
  81. * Free the components of a sign restart sub-context
  82. */
  83. static void ecdsa_restart_sig_free(mbedtls_ecdsa_restart_sig_ctx *ctx)
  84. {
  85. if (ctx == NULL) {
  86. return;
  87. }
  88. mbedtls_mpi_free(&ctx->k);
  89. mbedtls_mpi_free(&ctx->r);
  90. }
  91. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  92. /*
  93. * Sub-context for ecdsa_sign_det()
  94. */
  95. struct mbedtls_ecdsa_restart_det {
  96. mbedtls_hmac_drbg_context rng_ctx; /* DRBG state */
  97. enum { /* what to do next? */
  98. ecdsa_det_init = 0, /* getting started */
  99. ecdsa_det_sign, /* make signature */
  100. } state;
  101. };
  102. /*
  103. * Init verify sign_det sub-context
  104. */
  105. static void ecdsa_restart_det_init(mbedtls_ecdsa_restart_det_ctx *ctx)
  106. {
  107. mbedtls_hmac_drbg_init(&ctx->rng_ctx);
  108. ctx->state = ecdsa_det_init;
  109. }
  110. /*
  111. * Free the components of a sign_det restart sub-context
  112. */
  113. static void ecdsa_restart_det_free(mbedtls_ecdsa_restart_det_ctx *ctx)
  114. {
  115. if (ctx == NULL) {
  116. return;
  117. }
  118. mbedtls_hmac_drbg_free(&ctx->rng_ctx);
  119. ecdsa_restart_det_init(ctx);
  120. }
  121. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  122. #define ECDSA_RS_ECP (rs_ctx == NULL ? NULL : &rs_ctx->ecp)
  123. /* Utility macro for checking and updating ops budget */
  124. #define ECDSA_BUDGET(ops) \
  125. MBEDTLS_MPI_CHK(mbedtls_ecp_check_budget(grp, ECDSA_RS_ECP, ops));
  126. /* Call this when entering a function that needs its own sub-context */
  127. #define ECDSA_RS_ENTER(SUB) do { \
  128. /* reset ops count for this call if top-level */ \
  129. if (rs_ctx != NULL && rs_ctx->ecp.depth++ == 0) \
  130. rs_ctx->ecp.ops_done = 0; \
  131. \
  132. /* set up our own sub-context if needed */ \
  133. if (mbedtls_ecp_restart_is_enabled() && \
  134. rs_ctx != NULL && rs_ctx->SUB == NULL) \
  135. { \
  136. rs_ctx->SUB = mbedtls_calloc(1, sizeof(*rs_ctx->SUB)); \
  137. if (rs_ctx->SUB == NULL) \
  138. return MBEDTLS_ERR_ECP_ALLOC_FAILED; \
  139. \
  140. ecdsa_restart_## SUB ##_init(rs_ctx->SUB); \
  141. } \
  142. } while (0)
  143. /* Call this when leaving a function that needs its own sub-context */
  144. #define ECDSA_RS_LEAVE(SUB) do { \
  145. /* clear our sub-context when not in progress (done or error) */ \
  146. if (rs_ctx != NULL && rs_ctx->SUB != NULL && \
  147. ret != MBEDTLS_ERR_ECP_IN_PROGRESS) \
  148. { \
  149. ecdsa_restart_## SUB ##_free(rs_ctx->SUB); \
  150. mbedtls_free(rs_ctx->SUB); \
  151. rs_ctx->SUB = NULL; \
  152. } \
  153. \
  154. if (rs_ctx != NULL) \
  155. rs_ctx->ecp.depth--; \
  156. } while (0)
  157. #else /* MBEDTLS_ECP_RESTARTABLE */
  158. #define ECDSA_RS_ECP NULL
  159. #define ECDSA_BUDGET(ops) /* no-op; for compatibility */
  160. #define ECDSA_RS_ENTER(SUB) (void) rs_ctx
  161. #define ECDSA_RS_LEAVE(SUB) (void) rs_ctx
  162. #endif /* MBEDTLS_ECP_RESTARTABLE */
  163. #if defined(MBEDTLS_ECDSA_DETERMINISTIC) || \
  164. !defined(MBEDTLS_ECDSA_SIGN_ALT) || \
  165. !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  166. /*
  167. * Derive a suitable integer for group grp from a buffer of length len
  168. * SEC1 4.1.3 step 5 aka SEC1 4.1.4 step 3
  169. */
  170. static int derive_mpi(const mbedtls_ecp_group *grp, mbedtls_mpi *x,
  171. const unsigned char *buf, size_t blen)
  172. {
  173. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  174. size_t n_size = (grp->nbits + 7) / 8;
  175. size_t use_size = blen > n_size ? n_size : blen;
  176. MBEDTLS_MPI_CHK(mbedtls_mpi_read_binary(x, buf, use_size));
  177. if (use_size * 8 > grp->nbits) {
  178. MBEDTLS_MPI_CHK(mbedtls_mpi_shift_r(x, use_size * 8 - grp->nbits));
  179. }
  180. /* While at it, reduce modulo N */
  181. if (mbedtls_mpi_cmp_mpi(x, &grp->N) >= 0) {
  182. MBEDTLS_MPI_CHK(mbedtls_mpi_sub_mpi(x, x, &grp->N));
  183. }
  184. cleanup:
  185. return ret;
  186. }
  187. #endif /* ECDSA_DETERMINISTIC || !ECDSA_SIGN_ALT || !ECDSA_VERIFY_ALT */
  188. int mbedtls_ecdsa_can_do(mbedtls_ecp_group_id gid)
  189. {
  190. switch (gid) {
  191. #ifdef MBEDTLS_ECP_DP_CURVE25519_ENABLED
  192. case MBEDTLS_ECP_DP_CURVE25519: return 0;
  193. #endif
  194. #ifdef MBEDTLS_ECP_DP_CURVE448_ENABLED
  195. case MBEDTLS_ECP_DP_CURVE448: return 0;
  196. #endif
  197. default: return 1;
  198. }
  199. }
  200. #if !defined(MBEDTLS_ECDSA_SIGN_ALT)
  201. /*
  202. * Compute ECDSA signature of a hashed message (SEC1 4.1.3)
  203. * Obviously, compared to SEC1 4.1.3, we skip step 4 (hash message)
  204. */
  205. int mbedtls_ecdsa_sign_restartable(mbedtls_ecp_group *grp,
  206. mbedtls_mpi *r, mbedtls_mpi *s,
  207. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  208. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng,
  209. int (*f_rng_blind)(void *, unsigned char *, size_t),
  210. void *p_rng_blind,
  211. mbedtls_ecdsa_restart_ctx *rs_ctx)
  212. {
  213. int ret, key_tries, sign_tries;
  214. int *p_sign_tries = &sign_tries, *p_key_tries = &key_tries;
  215. mbedtls_ecp_point R;
  216. mbedtls_mpi k, e, t;
  217. mbedtls_mpi *pk = &k, *pr = r;
  218. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  219. if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
  220. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  221. }
  222. /* Make sure d is in range 1..n-1 */
  223. if (mbedtls_mpi_cmp_int(d, 1) < 0 || mbedtls_mpi_cmp_mpi(d, &grp->N) >= 0) {
  224. return MBEDTLS_ERR_ECP_INVALID_KEY;
  225. }
  226. mbedtls_ecp_point_init(&R);
  227. mbedtls_mpi_init(&k); mbedtls_mpi_init(&e); mbedtls_mpi_init(&t);
  228. ECDSA_RS_ENTER(sig);
  229. #if defined(MBEDTLS_ECP_RESTARTABLE)
  230. if (rs_ctx != NULL && rs_ctx->sig != NULL) {
  231. /* redirect to our context */
  232. p_sign_tries = &rs_ctx->sig->sign_tries;
  233. p_key_tries = &rs_ctx->sig->key_tries;
  234. pk = &rs_ctx->sig->k;
  235. pr = &rs_ctx->sig->r;
  236. /* jump to current step */
  237. if (rs_ctx->sig->state == ecdsa_sig_mul) {
  238. goto mul;
  239. }
  240. if (rs_ctx->sig->state == ecdsa_sig_modn) {
  241. goto modn;
  242. }
  243. }
  244. #endif /* MBEDTLS_ECP_RESTARTABLE */
  245. *p_sign_tries = 0;
  246. do {
  247. if ((*p_sign_tries)++ > 10) {
  248. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  249. goto cleanup;
  250. }
  251. /*
  252. * Steps 1-3: generate a suitable ephemeral keypair
  253. * and set r = xR mod n
  254. */
  255. *p_key_tries = 0;
  256. do {
  257. if ((*p_key_tries)++ > 10) {
  258. ret = MBEDTLS_ERR_ECP_RANDOM_FAILED;
  259. goto cleanup;
  260. }
  261. MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, pk, f_rng, p_rng));
  262. #if defined(MBEDTLS_ECP_RESTARTABLE)
  263. if (rs_ctx != NULL && rs_ctx->sig != NULL) {
  264. rs_ctx->sig->state = ecdsa_sig_mul;
  265. }
  266. mul:
  267. #endif
  268. MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &R, pk, &grp->G,
  269. f_rng_blind,
  270. p_rng_blind,
  271. ECDSA_RS_ECP));
  272. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pr, &R.X, &grp->N));
  273. } while (mbedtls_mpi_cmp_int(pr, 0) == 0);
  274. #if defined(MBEDTLS_ECP_RESTARTABLE)
  275. if (rs_ctx != NULL && rs_ctx->sig != NULL) {
  276. rs_ctx->sig->state = ecdsa_sig_modn;
  277. }
  278. modn:
  279. #endif
  280. /*
  281. * Accounting for everything up to the end of the loop
  282. * (step 6, but checking now avoids saving e and t)
  283. */
  284. ECDSA_BUDGET(MBEDTLS_ECP_OPS_INV + 4);
  285. /*
  286. * Step 5: derive MPI from hashed message
  287. */
  288. MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
  289. /*
  290. * Generate a random value to blind inv_mod in next step,
  291. * avoiding a potential timing leak.
  292. */
  293. MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, &t, f_rng_blind,
  294. p_rng_blind));
  295. /*
  296. * Step 6: compute s = (e + r * d) / k = t (e + rd) / (kt) mod n
  297. */
  298. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, pr, d));
  299. MBEDTLS_MPI_CHK(mbedtls_mpi_add_mpi(&e, &e, s));
  300. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(&e, &e, &t));
  301. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pk, pk, &t));
  302. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pk, pk, &grp->N));
  303. MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(s, pk, &grp->N));
  304. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(s, s, &e));
  305. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(s, s, &grp->N));
  306. } while (mbedtls_mpi_cmp_int(s, 0) == 0);
  307. #if defined(MBEDTLS_ECP_RESTARTABLE)
  308. if (rs_ctx != NULL && rs_ctx->sig != NULL) {
  309. MBEDTLS_MPI_CHK(mbedtls_mpi_copy(r, pr));
  310. }
  311. #endif
  312. cleanup:
  313. mbedtls_ecp_point_free(&R);
  314. mbedtls_mpi_free(&k); mbedtls_mpi_free(&e); mbedtls_mpi_free(&t);
  315. ECDSA_RS_LEAVE(sig);
  316. return ret;
  317. }
  318. /*
  319. * Compute ECDSA signature of a hashed message
  320. */
  321. int mbedtls_ecdsa_sign(mbedtls_ecp_group *grp, mbedtls_mpi *r, mbedtls_mpi *s,
  322. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  323. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  324. {
  325. /* Use the same RNG for both blinding and ephemeral key generation */
  326. return mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
  327. f_rng, p_rng, f_rng, p_rng, NULL);
  328. }
  329. #endif /* !MBEDTLS_ECDSA_SIGN_ALT */
  330. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  331. /*
  332. * Deterministic signature wrapper
  333. *
  334. * note: The f_rng_blind parameter must not be NULL.
  335. *
  336. */
  337. int mbedtls_ecdsa_sign_det_restartable(mbedtls_ecp_group *grp,
  338. mbedtls_mpi *r, mbedtls_mpi *s,
  339. const mbedtls_mpi *d, const unsigned char *buf, size_t blen,
  340. mbedtls_md_type_t md_alg,
  341. int (*f_rng_blind)(void *, unsigned char *, size_t),
  342. void *p_rng_blind,
  343. mbedtls_ecdsa_restart_ctx *rs_ctx)
  344. {
  345. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  346. mbedtls_hmac_drbg_context rng_ctx;
  347. mbedtls_hmac_drbg_context *p_rng = &rng_ctx;
  348. unsigned char data[2 * MBEDTLS_ECP_MAX_BYTES];
  349. size_t grp_len = (grp->nbits + 7) / 8;
  350. const mbedtls_md_info_t *md_info;
  351. mbedtls_mpi h;
  352. if ((md_info = mbedtls_md_info_from_type(md_alg)) == NULL) {
  353. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  354. }
  355. mbedtls_mpi_init(&h);
  356. mbedtls_hmac_drbg_init(&rng_ctx);
  357. ECDSA_RS_ENTER(det);
  358. #if defined(MBEDTLS_ECP_RESTARTABLE)
  359. if (rs_ctx != NULL && rs_ctx->det != NULL) {
  360. /* redirect to our context */
  361. p_rng = &rs_ctx->det->rng_ctx;
  362. /* jump to current step */
  363. if (rs_ctx->det->state == ecdsa_det_sign) {
  364. goto sign;
  365. }
  366. }
  367. #endif /* MBEDTLS_ECP_RESTARTABLE */
  368. /* Use private key and message hash (reduced) to initialize HMAC_DRBG */
  369. MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(d, data, grp_len));
  370. MBEDTLS_MPI_CHK(derive_mpi(grp, &h, buf, blen));
  371. MBEDTLS_MPI_CHK(mbedtls_mpi_write_binary(&h, data + grp_len, grp_len));
  372. MBEDTLS_MPI_CHK(mbedtls_hmac_drbg_seed_buf(p_rng, md_info, data, 2 * grp_len));
  373. #if defined(MBEDTLS_ECP_RESTARTABLE)
  374. if (rs_ctx != NULL && rs_ctx->det != NULL) {
  375. rs_ctx->det->state = ecdsa_det_sign;
  376. }
  377. sign:
  378. #endif
  379. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  380. (void) f_rng_blind;
  381. (void) p_rng_blind;
  382. ret = mbedtls_ecdsa_sign(grp, r, s, d, buf, blen,
  383. mbedtls_hmac_drbg_random, p_rng);
  384. #else
  385. ret = mbedtls_ecdsa_sign_restartable(grp, r, s, d, buf, blen,
  386. mbedtls_hmac_drbg_random, p_rng,
  387. f_rng_blind, p_rng_blind, rs_ctx);
  388. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  389. cleanup:
  390. mbedtls_hmac_drbg_free(&rng_ctx);
  391. mbedtls_mpi_free(&h);
  392. ECDSA_RS_LEAVE(det);
  393. return ret;
  394. }
  395. /*
  396. * Deterministic signature wrapper
  397. */
  398. int mbedtls_ecdsa_sign_det_ext(mbedtls_ecp_group *grp, mbedtls_mpi *r,
  399. mbedtls_mpi *s, const mbedtls_mpi *d,
  400. const unsigned char *buf, size_t blen,
  401. mbedtls_md_type_t md_alg,
  402. int (*f_rng_blind)(void *, unsigned char *,
  403. size_t),
  404. void *p_rng_blind)
  405. {
  406. return mbedtls_ecdsa_sign_det_restartable(grp, r, s, d, buf, blen, md_alg,
  407. f_rng_blind, p_rng_blind, NULL);
  408. }
  409. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  410. #if !defined(MBEDTLS_ECDSA_VERIFY_ALT)
  411. /*
  412. * Verify ECDSA signature of hashed message (SEC1 4.1.4)
  413. * Obviously, compared to SEC1 4.1.3, we skip step 2 (hash message)
  414. */
  415. int mbedtls_ecdsa_verify_restartable(mbedtls_ecp_group *grp,
  416. const unsigned char *buf, size_t blen,
  417. const mbedtls_ecp_point *Q,
  418. const mbedtls_mpi *r,
  419. const mbedtls_mpi *s,
  420. mbedtls_ecdsa_restart_ctx *rs_ctx)
  421. {
  422. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  423. mbedtls_mpi e, s_inv, u1, u2;
  424. mbedtls_ecp_point R;
  425. mbedtls_mpi *pu1 = &u1, *pu2 = &u2;
  426. mbedtls_ecp_point_init(&R);
  427. mbedtls_mpi_init(&e); mbedtls_mpi_init(&s_inv);
  428. mbedtls_mpi_init(&u1); mbedtls_mpi_init(&u2);
  429. /* Fail cleanly on curves such as Curve25519 that can't be used for ECDSA */
  430. if (!mbedtls_ecdsa_can_do(grp->id) || grp->N.p == NULL) {
  431. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  432. }
  433. ECDSA_RS_ENTER(ver);
  434. #if defined(MBEDTLS_ECP_RESTARTABLE)
  435. if (rs_ctx != NULL && rs_ctx->ver != NULL) {
  436. /* redirect to our context */
  437. pu1 = &rs_ctx->ver->u1;
  438. pu2 = &rs_ctx->ver->u2;
  439. /* jump to current step */
  440. if (rs_ctx->ver->state == ecdsa_ver_muladd) {
  441. goto muladd;
  442. }
  443. }
  444. #endif /* MBEDTLS_ECP_RESTARTABLE */
  445. /*
  446. * Step 1: make sure r and s are in range 1..n-1
  447. */
  448. if (mbedtls_mpi_cmp_int(r, 1) < 0 || mbedtls_mpi_cmp_mpi(r, &grp->N) >= 0 ||
  449. mbedtls_mpi_cmp_int(s, 1) < 0 || mbedtls_mpi_cmp_mpi(s, &grp->N) >= 0) {
  450. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  451. goto cleanup;
  452. }
  453. /*
  454. * Step 3: derive MPI from hashed message
  455. */
  456. MBEDTLS_MPI_CHK(derive_mpi(grp, &e, buf, blen));
  457. /*
  458. * Step 4: u1 = e / s mod n, u2 = r / s mod n
  459. */
  460. ECDSA_BUDGET(MBEDTLS_ECP_OPS_CHK + MBEDTLS_ECP_OPS_INV + 2);
  461. MBEDTLS_MPI_CHK(mbedtls_mpi_inv_mod(&s_inv, s, &grp->N));
  462. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu1, &e, &s_inv));
  463. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu1, pu1, &grp->N));
  464. MBEDTLS_MPI_CHK(mbedtls_mpi_mul_mpi(pu2, r, &s_inv));
  465. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(pu2, pu2, &grp->N));
  466. #if defined(MBEDTLS_ECP_RESTARTABLE)
  467. if (rs_ctx != NULL && rs_ctx->ver != NULL) {
  468. rs_ctx->ver->state = ecdsa_ver_muladd;
  469. }
  470. muladd:
  471. #endif
  472. /*
  473. * Step 5: R = u1 G + u2 Q
  474. */
  475. MBEDTLS_MPI_CHK(mbedtls_ecp_muladd_restartable(grp,
  476. &R, pu1, &grp->G, pu2, Q, ECDSA_RS_ECP));
  477. if (mbedtls_ecp_is_zero(&R)) {
  478. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  479. goto cleanup;
  480. }
  481. /*
  482. * Step 6: convert xR to an integer (no-op)
  483. * Step 7: reduce xR mod n (gives v)
  484. */
  485. MBEDTLS_MPI_CHK(mbedtls_mpi_mod_mpi(&R.X, &R.X, &grp->N));
  486. /*
  487. * Step 8: check if v (that is, R.X) is equal to r
  488. */
  489. if (mbedtls_mpi_cmp_mpi(&R.X, r) != 0) {
  490. ret = MBEDTLS_ERR_ECP_VERIFY_FAILED;
  491. goto cleanup;
  492. }
  493. cleanup:
  494. mbedtls_ecp_point_free(&R);
  495. mbedtls_mpi_free(&e); mbedtls_mpi_free(&s_inv);
  496. mbedtls_mpi_free(&u1); mbedtls_mpi_free(&u2);
  497. ECDSA_RS_LEAVE(ver);
  498. return ret;
  499. }
  500. /*
  501. * Verify ECDSA signature of hashed message
  502. */
  503. int mbedtls_ecdsa_verify(mbedtls_ecp_group *grp,
  504. const unsigned char *buf, size_t blen,
  505. const mbedtls_ecp_point *Q,
  506. const mbedtls_mpi *r,
  507. const mbedtls_mpi *s)
  508. {
  509. return mbedtls_ecdsa_verify_restartable(grp, buf, blen, Q, r, s, NULL);
  510. }
  511. #endif /* !MBEDTLS_ECDSA_VERIFY_ALT */
  512. /*
  513. * Convert a signature (given by context) to ASN.1
  514. */
  515. static int ecdsa_signature_to_asn1(const mbedtls_mpi *r, const mbedtls_mpi *s,
  516. unsigned char *sig, size_t sig_size,
  517. size_t *slen)
  518. {
  519. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  520. unsigned char buf[MBEDTLS_ECDSA_MAX_LEN] = { 0 };
  521. unsigned char *p = buf + sizeof(buf);
  522. size_t len = 0;
  523. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, s));
  524. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_mpi(&p, buf, r));
  525. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_len(&p, buf, len));
  526. MBEDTLS_ASN1_CHK_ADD(len, mbedtls_asn1_write_tag(&p, buf,
  527. MBEDTLS_ASN1_CONSTRUCTED |
  528. MBEDTLS_ASN1_SEQUENCE));
  529. if (len > sig_size) {
  530. return MBEDTLS_ERR_ECP_BUFFER_TOO_SMALL;
  531. }
  532. memcpy(sig, p, len);
  533. *slen = len;
  534. return 0;
  535. }
  536. /*
  537. * Compute and write signature
  538. */
  539. int mbedtls_ecdsa_write_signature_restartable(mbedtls_ecdsa_context *ctx,
  540. mbedtls_md_type_t md_alg,
  541. const unsigned char *hash, size_t hlen,
  542. unsigned char *sig, size_t sig_size, size_t *slen,
  543. int (*f_rng)(void *, unsigned char *, size_t),
  544. void *p_rng,
  545. mbedtls_ecdsa_restart_ctx *rs_ctx)
  546. {
  547. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  548. mbedtls_mpi r, s;
  549. if (f_rng == NULL) {
  550. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  551. }
  552. mbedtls_mpi_init(&r);
  553. mbedtls_mpi_init(&s);
  554. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  555. MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_det_restartable(&ctx->grp, &r, &s, &ctx->d,
  556. hash, hlen, md_alg, f_rng,
  557. p_rng, rs_ctx));
  558. #else
  559. (void) md_alg;
  560. #if defined(MBEDTLS_ECDSA_SIGN_ALT)
  561. (void) rs_ctx;
  562. MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign(&ctx->grp, &r, &s, &ctx->d,
  563. hash, hlen, f_rng, p_rng));
  564. #else
  565. /* Use the same RNG for both blinding and ephemeral key generation */
  566. MBEDTLS_MPI_CHK(mbedtls_ecdsa_sign_restartable(&ctx->grp, &r, &s, &ctx->d,
  567. hash, hlen, f_rng, p_rng, f_rng,
  568. p_rng, rs_ctx));
  569. #endif /* MBEDTLS_ECDSA_SIGN_ALT */
  570. #endif /* MBEDTLS_ECDSA_DETERMINISTIC */
  571. MBEDTLS_MPI_CHK(ecdsa_signature_to_asn1(&r, &s, sig, sig_size, slen));
  572. cleanup:
  573. mbedtls_mpi_free(&r);
  574. mbedtls_mpi_free(&s);
  575. return ret;
  576. }
  577. /*
  578. * Compute and write signature
  579. */
  580. int mbedtls_ecdsa_write_signature(mbedtls_ecdsa_context *ctx,
  581. mbedtls_md_type_t md_alg,
  582. const unsigned char *hash, size_t hlen,
  583. unsigned char *sig, size_t sig_size, size_t *slen,
  584. int (*f_rng)(void *, unsigned char *, size_t),
  585. void *p_rng)
  586. {
  587. return mbedtls_ecdsa_write_signature_restartable(
  588. ctx, md_alg, hash, hlen, sig, sig_size, slen,
  589. f_rng, p_rng, NULL);
  590. }
  591. /*
  592. * Read and check signature
  593. */
  594. int mbedtls_ecdsa_read_signature(mbedtls_ecdsa_context *ctx,
  595. const unsigned char *hash, size_t hlen,
  596. const unsigned char *sig, size_t slen)
  597. {
  598. return mbedtls_ecdsa_read_signature_restartable(
  599. ctx, hash, hlen, sig, slen, NULL);
  600. }
  601. /*
  602. * Restartable read and check signature
  603. */
  604. int mbedtls_ecdsa_read_signature_restartable(mbedtls_ecdsa_context *ctx,
  605. const unsigned char *hash, size_t hlen,
  606. const unsigned char *sig, size_t slen,
  607. mbedtls_ecdsa_restart_ctx *rs_ctx)
  608. {
  609. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  610. unsigned char *p = (unsigned char *) sig;
  611. const unsigned char *end = sig + slen;
  612. size_t len;
  613. mbedtls_mpi r, s;
  614. mbedtls_mpi_init(&r);
  615. mbedtls_mpi_init(&s);
  616. if ((ret = mbedtls_asn1_get_tag(&p, end, &len,
  617. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE)) != 0) {
  618. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  619. goto cleanup;
  620. }
  621. if (p + len != end) {
  622. ret = MBEDTLS_ERROR_ADD(MBEDTLS_ERR_ECP_BAD_INPUT_DATA,
  623. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH);
  624. goto cleanup;
  625. }
  626. if ((ret = mbedtls_asn1_get_mpi(&p, end, &r)) != 0 ||
  627. (ret = mbedtls_asn1_get_mpi(&p, end, &s)) != 0) {
  628. ret += MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  629. goto cleanup;
  630. }
  631. #if defined(MBEDTLS_ECDSA_VERIFY_ALT)
  632. (void) rs_ctx;
  633. if ((ret = mbedtls_ecdsa_verify(&ctx->grp, hash, hlen,
  634. &ctx->Q, &r, &s)) != 0) {
  635. goto cleanup;
  636. }
  637. #else
  638. if ((ret = mbedtls_ecdsa_verify_restartable(&ctx->grp, hash, hlen,
  639. &ctx->Q, &r, &s, rs_ctx)) != 0) {
  640. goto cleanup;
  641. }
  642. #endif /* MBEDTLS_ECDSA_VERIFY_ALT */
  643. /* At this point we know that the buffer starts with a valid signature.
  644. * Return 0 if the buffer just contains the signature, and a specific
  645. * error code if the valid signature is followed by more data. */
  646. if (p != end) {
  647. ret = MBEDTLS_ERR_ECP_SIG_LEN_MISMATCH;
  648. }
  649. cleanup:
  650. mbedtls_mpi_free(&r);
  651. mbedtls_mpi_free(&s);
  652. return ret;
  653. }
  654. #if !defined(MBEDTLS_ECDSA_GENKEY_ALT)
  655. /*
  656. * Generate key pair
  657. */
  658. int mbedtls_ecdsa_genkey(mbedtls_ecdsa_context *ctx, mbedtls_ecp_group_id gid,
  659. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng)
  660. {
  661. int ret = 0;
  662. ret = mbedtls_ecp_group_load(&ctx->grp, gid);
  663. if (ret != 0) {
  664. return ret;
  665. }
  666. return mbedtls_ecp_gen_keypair(&ctx->grp, &ctx->d,
  667. &ctx->Q, f_rng, p_rng);
  668. }
  669. #endif /* !MBEDTLS_ECDSA_GENKEY_ALT */
  670. /*
  671. * Set context from an mbedtls_ecp_keypair
  672. */
  673. int mbedtls_ecdsa_from_keypair(mbedtls_ecdsa_context *ctx, const mbedtls_ecp_keypair *key)
  674. {
  675. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  676. if ((ret = mbedtls_ecp_group_copy(&ctx->grp, &key->grp)) != 0 ||
  677. (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0 ||
  678. (ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0) {
  679. mbedtls_ecdsa_free(ctx);
  680. }
  681. return ret;
  682. }
  683. /*
  684. * Initialize context
  685. */
  686. void mbedtls_ecdsa_init(mbedtls_ecdsa_context *ctx)
  687. {
  688. mbedtls_ecp_keypair_init(ctx);
  689. }
  690. /*
  691. * Free context
  692. */
  693. void mbedtls_ecdsa_free(mbedtls_ecdsa_context *ctx)
  694. {
  695. if (ctx == NULL) {
  696. return;
  697. }
  698. mbedtls_ecp_keypair_free(ctx);
  699. }
  700. #if defined(MBEDTLS_ECP_RESTARTABLE)
  701. /*
  702. * Initialize a restart context
  703. */
  704. void mbedtls_ecdsa_restart_init(mbedtls_ecdsa_restart_ctx *ctx)
  705. {
  706. mbedtls_ecp_restart_init(&ctx->ecp);
  707. ctx->ver = NULL;
  708. ctx->sig = NULL;
  709. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  710. ctx->det = NULL;
  711. #endif
  712. }
  713. /*
  714. * Free the components of a restart context
  715. */
  716. void mbedtls_ecdsa_restart_free(mbedtls_ecdsa_restart_ctx *ctx)
  717. {
  718. if (ctx == NULL) {
  719. return;
  720. }
  721. mbedtls_ecp_restart_free(&ctx->ecp);
  722. ecdsa_restart_ver_free(ctx->ver);
  723. mbedtls_free(ctx->ver);
  724. ctx->ver = NULL;
  725. ecdsa_restart_sig_free(ctx->sig);
  726. mbedtls_free(ctx->sig);
  727. ctx->sig = NULL;
  728. #if defined(MBEDTLS_ECDSA_DETERMINISTIC)
  729. ecdsa_restart_det_free(ctx->det);
  730. mbedtls_free(ctx->det);
  731. ctx->det = NULL;
  732. #endif
  733. }
  734. #endif /* MBEDTLS_ECP_RESTARTABLE */
  735. #endif /* MBEDTLS_ECDSA_C */