ecdsa.c 32 KB

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