ecdsa.c 32 KB

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