ecdh.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697
  1. /*
  2. * Elliptic curve Diffie-Hellman
  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 http://www.secg.org/index.php?action=secg,docs_secg
  23. * RFC 4492
  24. */
  25. #include "common.h"
  26. #if defined(MBEDTLS_ECDH_C)
  27. #include "mbedtls/ecdh.h"
  28. #include "mbedtls/platform_util.h"
  29. #include "mbedtls/error.h"
  30. #include <string.h>
  31. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  32. typedef mbedtls_ecdh_context mbedtls_ecdh_context_mbed;
  33. #endif
  34. static mbedtls_ecp_group_id mbedtls_ecdh_grp_id(
  35. const mbedtls_ecdh_context *ctx)
  36. {
  37. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  38. return ctx->grp.id;
  39. #else
  40. return ctx->grp_id;
  41. #endif
  42. }
  43. int mbedtls_ecdh_can_do(mbedtls_ecp_group_id gid)
  44. {
  45. /* At this time, all groups support ECDH. */
  46. (void) gid;
  47. return 1;
  48. }
  49. #if !defined(MBEDTLS_ECDH_GEN_PUBLIC_ALT)
  50. /*
  51. * Generate public key (restartable version)
  52. *
  53. * Note: this internal function relies on its caller preserving the value of
  54. * the output parameter 'd' across continuation calls. This would not be
  55. * acceptable for a public function but is OK here as we control call sites.
  56. */
  57. static int ecdh_gen_public_restartable(mbedtls_ecp_group *grp,
  58. mbedtls_mpi *d, mbedtls_ecp_point *Q,
  59. int (*f_rng)(void *, unsigned char *, size_t),
  60. void *p_rng,
  61. mbedtls_ecp_restart_ctx *rs_ctx)
  62. {
  63. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  64. int restarting = 0;
  65. #if defined(MBEDTLS_ECP_RESTARTABLE)
  66. restarting = (rs_ctx != NULL && rs_ctx->rsm != NULL);
  67. #endif
  68. /* If multiplication is in progress, we already generated a privkey */
  69. if (!restarting) {
  70. MBEDTLS_MPI_CHK(mbedtls_ecp_gen_privkey(grp, d, f_rng, p_rng));
  71. }
  72. MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, Q, d, &grp->G,
  73. f_rng, p_rng, rs_ctx));
  74. cleanup:
  75. return ret;
  76. }
  77. /*
  78. * Generate public key
  79. */
  80. int mbedtls_ecdh_gen_public(mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  81. int (*f_rng)(void *, unsigned char *, size_t),
  82. void *p_rng)
  83. {
  84. return ecdh_gen_public_restartable(grp, d, Q, f_rng, p_rng, NULL);
  85. }
  86. #endif /* !MBEDTLS_ECDH_GEN_PUBLIC_ALT */
  87. #if !defined(MBEDTLS_ECDH_COMPUTE_SHARED_ALT)
  88. /*
  89. * Compute shared secret (SEC1 3.3.1)
  90. */
  91. static int ecdh_compute_shared_restartable(mbedtls_ecp_group *grp,
  92. mbedtls_mpi *z,
  93. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  94. int (*f_rng)(void *, unsigned char *, size_t),
  95. void *p_rng,
  96. mbedtls_ecp_restart_ctx *rs_ctx)
  97. {
  98. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  99. mbedtls_ecp_point P;
  100. mbedtls_ecp_point_init(&P);
  101. MBEDTLS_MPI_CHK(mbedtls_ecp_mul_restartable(grp, &P, d, Q,
  102. f_rng, p_rng, rs_ctx));
  103. if (mbedtls_ecp_is_zero(&P)) {
  104. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  105. goto cleanup;
  106. }
  107. MBEDTLS_MPI_CHK(mbedtls_mpi_copy(z, &P.X));
  108. cleanup:
  109. mbedtls_ecp_point_free(&P);
  110. return ret;
  111. }
  112. /*
  113. * Compute shared secret (SEC1 3.3.1)
  114. */
  115. int mbedtls_ecdh_compute_shared(mbedtls_ecp_group *grp, mbedtls_mpi *z,
  116. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  117. int (*f_rng)(void *, unsigned char *, size_t),
  118. void *p_rng)
  119. {
  120. return ecdh_compute_shared_restartable(grp, z, Q, d,
  121. f_rng, p_rng, NULL);
  122. }
  123. #endif /* !MBEDTLS_ECDH_COMPUTE_SHARED_ALT */
  124. static void ecdh_init_internal(mbedtls_ecdh_context_mbed *ctx)
  125. {
  126. mbedtls_ecp_group_init(&ctx->grp);
  127. mbedtls_mpi_init(&ctx->d);
  128. mbedtls_ecp_point_init(&ctx->Q);
  129. mbedtls_ecp_point_init(&ctx->Qp);
  130. mbedtls_mpi_init(&ctx->z);
  131. #if defined(MBEDTLS_ECP_RESTARTABLE)
  132. mbedtls_ecp_restart_init(&ctx->rs);
  133. #endif
  134. }
  135. /*
  136. * Initialize context
  137. */
  138. void mbedtls_ecdh_init(mbedtls_ecdh_context *ctx)
  139. {
  140. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  141. ecdh_init_internal(ctx);
  142. mbedtls_ecp_point_init(&ctx->Vi);
  143. mbedtls_ecp_point_init(&ctx->Vf);
  144. mbedtls_mpi_init(&ctx->_d);
  145. #else
  146. memset(ctx, 0, sizeof(mbedtls_ecdh_context));
  147. ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
  148. #endif
  149. ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
  150. #if defined(MBEDTLS_ECP_RESTARTABLE)
  151. ctx->restart_enabled = 0;
  152. #endif
  153. }
  154. static int ecdh_setup_internal(mbedtls_ecdh_context_mbed *ctx,
  155. mbedtls_ecp_group_id grp_id)
  156. {
  157. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  158. ret = mbedtls_ecp_group_load(&ctx->grp, grp_id);
  159. if (ret != 0) {
  160. return MBEDTLS_ERR_ECP_FEATURE_UNAVAILABLE;
  161. }
  162. return 0;
  163. }
  164. /*
  165. * Setup context
  166. */
  167. int mbedtls_ecdh_setup(mbedtls_ecdh_context *ctx, mbedtls_ecp_group_id grp_id)
  168. {
  169. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  170. return ecdh_setup_internal(ctx, grp_id);
  171. #else
  172. switch (grp_id) {
  173. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  174. case MBEDTLS_ECP_DP_CURVE25519:
  175. ctx->point_format = MBEDTLS_ECP_PF_COMPRESSED;
  176. ctx->var = MBEDTLS_ECDH_VARIANT_EVEREST;
  177. ctx->grp_id = grp_id;
  178. return mbedtls_everest_setup(&ctx->ctx.everest_ecdh, grp_id);
  179. #endif
  180. default:
  181. ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
  182. ctx->var = MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0;
  183. ctx->grp_id = grp_id;
  184. ecdh_init_internal(&ctx->ctx.mbed_ecdh);
  185. return ecdh_setup_internal(&ctx->ctx.mbed_ecdh, grp_id);
  186. }
  187. #endif
  188. }
  189. static void ecdh_free_internal(mbedtls_ecdh_context_mbed *ctx)
  190. {
  191. mbedtls_ecp_group_free(&ctx->grp);
  192. mbedtls_mpi_free(&ctx->d);
  193. mbedtls_ecp_point_free(&ctx->Q);
  194. mbedtls_ecp_point_free(&ctx->Qp);
  195. mbedtls_mpi_free(&ctx->z);
  196. #if defined(MBEDTLS_ECP_RESTARTABLE)
  197. mbedtls_ecp_restart_free(&ctx->rs);
  198. #endif
  199. }
  200. #if defined(MBEDTLS_ECP_RESTARTABLE)
  201. /*
  202. * Enable restartable operations for context
  203. */
  204. void mbedtls_ecdh_enable_restart(mbedtls_ecdh_context *ctx)
  205. {
  206. ctx->restart_enabled = 1;
  207. }
  208. #endif
  209. /*
  210. * Free context
  211. */
  212. void mbedtls_ecdh_free(mbedtls_ecdh_context *ctx)
  213. {
  214. if (ctx == NULL) {
  215. return;
  216. }
  217. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  218. mbedtls_ecp_point_free(&ctx->Vi);
  219. mbedtls_ecp_point_free(&ctx->Vf);
  220. mbedtls_mpi_free(&ctx->_d);
  221. ecdh_free_internal(ctx);
  222. #else
  223. switch (ctx->var) {
  224. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  225. case MBEDTLS_ECDH_VARIANT_EVEREST:
  226. mbedtls_everest_free(&ctx->ctx.everest_ecdh);
  227. break;
  228. #endif
  229. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  230. ecdh_free_internal(&ctx->ctx.mbed_ecdh);
  231. break;
  232. default:
  233. break;
  234. }
  235. ctx->point_format = MBEDTLS_ECP_PF_UNCOMPRESSED;
  236. ctx->var = MBEDTLS_ECDH_VARIANT_NONE;
  237. ctx->grp_id = MBEDTLS_ECP_DP_NONE;
  238. #endif
  239. }
  240. static int ecdh_make_params_internal(mbedtls_ecdh_context_mbed *ctx,
  241. size_t *olen, int point_format,
  242. unsigned char *buf, size_t blen,
  243. int (*f_rng)(void *,
  244. unsigned char *,
  245. size_t),
  246. void *p_rng,
  247. int restart_enabled)
  248. {
  249. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  250. size_t grp_len, pt_len;
  251. #if defined(MBEDTLS_ECP_RESTARTABLE)
  252. mbedtls_ecp_restart_ctx *rs_ctx = NULL;
  253. #endif
  254. if (ctx->grp.pbits == 0) {
  255. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  256. }
  257. #if defined(MBEDTLS_ECP_RESTARTABLE)
  258. if (restart_enabled) {
  259. rs_ctx = &ctx->rs;
  260. }
  261. #else
  262. (void) restart_enabled;
  263. #endif
  264. #if defined(MBEDTLS_ECP_RESTARTABLE)
  265. if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q,
  266. f_rng, p_rng, rs_ctx)) != 0) {
  267. return ret;
  268. }
  269. #else
  270. if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q,
  271. f_rng, p_rng)) != 0) {
  272. return ret;
  273. }
  274. #endif /* MBEDTLS_ECP_RESTARTABLE */
  275. if ((ret = mbedtls_ecp_tls_write_group(&ctx->grp, &grp_len, buf,
  276. blen)) != 0) {
  277. return ret;
  278. }
  279. buf += grp_len;
  280. blen -= grp_len;
  281. if ((ret = mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format,
  282. &pt_len, buf, blen)) != 0) {
  283. return ret;
  284. }
  285. *olen = grp_len + pt_len;
  286. return 0;
  287. }
  288. /*
  289. * Setup and write the ServerKeyExchange parameters (RFC 4492)
  290. * struct {
  291. * ECParameters curve_params;
  292. * ECPoint public;
  293. * } ServerECDHParams;
  294. */
  295. int mbedtls_ecdh_make_params(mbedtls_ecdh_context *ctx, size_t *olen,
  296. unsigned char *buf, size_t blen,
  297. int (*f_rng)(void *, unsigned char *, size_t),
  298. void *p_rng)
  299. {
  300. int restart_enabled = 0;
  301. #if defined(MBEDTLS_ECP_RESTARTABLE)
  302. restart_enabled = ctx->restart_enabled;
  303. #else
  304. (void) restart_enabled;
  305. #endif
  306. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  307. return ecdh_make_params_internal(ctx, olen, ctx->point_format, buf, blen,
  308. f_rng, p_rng, restart_enabled);
  309. #else
  310. switch (ctx->var) {
  311. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  312. case MBEDTLS_ECDH_VARIANT_EVEREST:
  313. return mbedtls_everest_make_params(&ctx->ctx.everest_ecdh, olen,
  314. buf, blen, f_rng, p_rng);
  315. #endif
  316. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  317. return ecdh_make_params_internal(&ctx->ctx.mbed_ecdh, olen,
  318. ctx->point_format, buf, blen,
  319. f_rng, p_rng,
  320. restart_enabled);
  321. default:
  322. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  323. }
  324. #endif
  325. }
  326. static int ecdh_read_params_internal(mbedtls_ecdh_context_mbed *ctx,
  327. const unsigned char **buf,
  328. const unsigned char *end)
  329. {
  330. return mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, buf,
  331. end - *buf);
  332. }
  333. /*
  334. * Read the ServerKeyExchange parameters (RFC 4492)
  335. * struct {
  336. * ECParameters curve_params;
  337. * ECPoint public;
  338. * } ServerECDHParams;
  339. */
  340. int mbedtls_ecdh_read_params(mbedtls_ecdh_context *ctx,
  341. const unsigned char **buf,
  342. const unsigned char *end)
  343. {
  344. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  345. mbedtls_ecp_group_id grp_id;
  346. if ((ret = mbedtls_ecp_tls_read_group_id(&grp_id, buf, end - *buf))
  347. != 0) {
  348. return ret;
  349. }
  350. if ((ret = mbedtls_ecdh_setup(ctx, grp_id)) != 0) {
  351. return ret;
  352. }
  353. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  354. return ecdh_read_params_internal(ctx, buf, end);
  355. #else
  356. switch (ctx->var) {
  357. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  358. case MBEDTLS_ECDH_VARIANT_EVEREST:
  359. return mbedtls_everest_read_params(&ctx->ctx.everest_ecdh,
  360. buf, end);
  361. #endif
  362. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  363. return ecdh_read_params_internal(&ctx->ctx.mbed_ecdh,
  364. buf, end);
  365. default:
  366. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  367. }
  368. #endif
  369. }
  370. static int ecdh_get_params_internal(mbedtls_ecdh_context_mbed *ctx,
  371. const mbedtls_ecp_keypair *key,
  372. mbedtls_ecdh_side side)
  373. {
  374. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  375. /* If it's not our key, just import the public part as Qp */
  376. if (side == MBEDTLS_ECDH_THEIRS) {
  377. return mbedtls_ecp_copy(&ctx->Qp, &key->Q);
  378. }
  379. /* Our key: import public (as Q) and private parts */
  380. if (side != MBEDTLS_ECDH_OURS) {
  381. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  382. }
  383. if ((ret = mbedtls_ecp_copy(&ctx->Q, &key->Q)) != 0 ||
  384. (ret = mbedtls_mpi_copy(&ctx->d, &key->d)) != 0) {
  385. return ret;
  386. }
  387. return 0;
  388. }
  389. /*
  390. * Get parameters from a keypair
  391. */
  392. int mbedtls_ecdh_get_params(mbedtls_ecdh_context *ctx,
  393. const mbedtls_ecp_keypair *key,
  394. mbedtls_ecdh_side side)
  395. {
  396. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  397. if (side != MBEDTLS_ECDH_OURS && side != MBEDTLS_ECDH_THEIRS) {
  398. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  399. }
  400. if (mbedtls_ecdh_grp_id(ctx) == MBEDTLS_ECP_DP_NONE) {
  401. /* This is the first call to get_params(). Set up the context
  402. * for use with the group. */
  403. if ((ret = mbedtls_ecdh_setup(ctx, key->grp.id)) != 0) {
  404. return ret;
  405. }
  406. } else {
  407. /* This is not the first call to get_params(). Check that the
  408. * current key's group is the same as the context's, which was set
  409. * from the first key's group. */
  410. if (mbedtls_ecdh_grp_id(ctx) != key->grp.id) {
  411. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  412. }
  413. }
  414. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  415. return ecdh_get_params_internal(ctx, key, side);
  416. #else
  417. switch (ctx->var) {
  418. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  419. case MBEDTLS_ECDH_VARIANT_EVEREST:
  420. {
  421. mbedtls_everest_ecdh_side s = side == MBEDTLS_ECDH_OURS ?
  422. MBEDTLS_EVEREST_ECDH_OURS :
  423. MBEDTLS_EVEREST_ECDH_THEIRS;
  424. return mbedtls_everest_get_params(&ctx->ctx.everest_ecdh,
  425. key, s);
  426. }
  427. #endif
  428. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  429. return ecdh_get_params_internal(&ctx->ctx.mbed_ecdh,
  430. key, side);
  431. default:
  432. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  433. }
  434. #endif
  435. }
  436. static int ecdh_make_public_internal(mbedtls_ecdh_context_mbed *ctx,
  437. size_t *olen, int point_format,
  438. unsigned char *buf, size_t blen,
  439. int (*f_rng)(void *,
  440. unsigned char *,
  441. size_t),
  442. void *p_rng,
  443. int restart_enabled)
  444. {
  445. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  446. #if defined(MBEDTLS_ECP_RESTARTABLE)
  447. mbedtls_ecp_restart_ctx *rs_ctx = NULL;
  448. #endif
  449. if (ctx->grp.pbits == 0) {
  450. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  451. }
  452. #if defined(MBEDTLS_ECP_RESTARTABLE)
  453. if (restart_enabled) {
  454. rs_ctx = &ctx->rs;
  455. }
  456. #else
  457. (void) restart_enabled;
  458. #endif
  459. #if defined(MBEDTLS_ECP_RESTARTABLE)
  460. if ((ret = ecdh_gen_public_restartable(&ctx->grp, &ctx->d, &ctx->Q,
  461. f_rng, p_rng, rs_ctx)) != 0) {
  462. return ret;
  463. }
  464. #else
  465. if ((ret = mbedtls_ecdh_gen_public(&ctx->grp, &ctx->d, &ctx->Q,
  466. f_rng, p_rng)) != 0) {
  467. return ret;
  468. }
  469. #endif /* MBEDTLS_ECP_RESTARTABLE */
  470. return mbedtls_ecp_tls_write_point(&ctx->grp, &ctx->Q, point_format, olen,
  471. buf, blen);
  472. }
  473. /*
  474. * Setup and export the client public value
  475. */
  476. int mbedtls_ecdh_make_public(mbedtls_ecdh_context *ctx, size_t *olen,
  477. unsigned char *buf, size_t blen,
  478. int (*f_rng)(void *, unsigned char *, size_t),
  479. void *p_rng)
  480. {
  481. int restart_enabled = 0;
  482. #if defined(MBEDTLS_ECP_RESTARTABLE)
  483. restart_enabled = ctx->restart_enabled;
  484. #endif
  485. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  486. return ecdh_make_public_internal(ctx, olen, ctx->point_format, buf, blen,
  487. f_rng, p_rng, restart_enabled);
  488. #else
  489. switch (ctx->var) {
  490. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  491. case MBEDTLS_ECDH_VARIANT_EVEREST:
  492. return mbedtls_everest_make_public(&ctx->ctx.everest_ecdh, olen,
  493. buf, blen, f_rng, p_rng);
  494. #endif
  495. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  496. return ecdh_make_public_internal(&ctx->ctx.mbed_ecdh, olen,
  497. ctx->point_format, buf, blen,
  498. f_rng, p_rng,
  499. restart_enabled);
  500. default:
  501. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  502. }
  503. #endif
  504. }
  505. static int ecdh_read_public_internal(mbedtls_ecdh_context_mbed *ctx,
  506. const unsigned char *buf, size_t blen)
  507. {
  508. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  509. const unsigned char *p = buf;
  510. if ((ret = mbedtls_ecp_tls_read_point(&ctx->grp, &ctx->Qp, &p,
  511. blen)) != 0) {
  512. return ret;
  513. }
  514. if ((size_t) (p - buf) != blen) {
  515. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  516. }
  517. return 0;
  518. }
  519. /*
  520. * Parse and import the client's public value
  521. */
  522. int mbedtls_ecdh_read_public(mbedtls_ecdh_context *ctx,
  523. const unsigned char *buf, size_t blen)
  524. {
  525. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  526. return ecdh_read_public_internal(ctx, buf, blen);
  527. #else
  528. switch (ctx->var) {
  529. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  530. case MBEDTLS_ECDH_VARIANT_EVEREST:
  531. return mbedtls_everest_read_public(&ctx->ctx.everest_ecdh,
  532. buf, blen);
  533. #endif
  534. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  535. return ecdh_read_public_internal(&ctx->ctx.mbed_ecdh,
  536. buf, blen);
  537. default:
  538. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  539. }
  540. #endif
  541. }
  542. static int ecdh_calc_secret_internal(mbedtls_ecdh_context_mbed *ctx,
  543. size_t *olen, unsigned char *buf,
  544. size_t blen,
  545. int (*f_rng)(void *,
  546. unsigned char *,
  547. size_t),
  548. void *p_rng,
  549. int restart_enabled)
  550. {
  551. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  552. #if defined(MBEDTLS_ECP_RESTARTABLE)
  553. mbedtls_ecp_restart_ctx *rs_ctx = NULL;
  554. #endif
  555. if (ctx == NULL || ctx->grp.pbits == 0) {
  556. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  557. }
  558. #if defined(MBEDTLS_ECP_RESTARTABLE)
  559. if (restart_enabled) {
  560. rs_ctx = &ctx->rs;
  561. }
  562. #else
  563. (void) restart_enabled;
  564. #endif
  565. #if defined(MBEDTLS_ECP_RESTARTABLE)
  566. if ((ret = ecdh_compute_shared_restartable(&ctx->grp, &ctx->z, &ctx->Qp,
  567. &ctx->d, f_rng, p_rng,
  568. rs_ctx)) != 0) {
  569. return ret;
  570. }
  571. #else
  572. if ((ret = mbedtls_ecdh_compute_shared(&ctx->grp, &ctx->z, &ctx->Qp,
  573. &ctx->d, f_rng, p_rng)) != 0) {
  574. return ret;
  575. }
  576. #endif /* MBEDTLS_ECP_RESTARTABLE */
  577. if (mbedtls_mpi_size(&ctx->z) > blen) {
  578. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  579. }
  580. *olen = ctx->grp.pbits / 8 + ((ctx->grp.pbits % 8) != 0);
  581. if (mbedtls_ecp_get_type(&ctx->grp) == MBEDTLS_ECP_TYPE_MONTGOMERY) {
  582. return mbedtls_mpi_write_binary_le(&ctx->z, buf, *olen);
  583. }
  584. return mbedtls_mpi_write_binary(&ctx->z, buf, *olen);
  585. }
  586. /*
  587. * Derive and export the shared secret
  588. */
  589. int mbedtls_ecdh_calc_secret(mbedtls_ecdh_context *ctx, size_t *olen,
  590. unsigned char *buf, size_t blen,
  591. int (*f_rng)(void *, unsigned char *, size_t),
  592. void *p_rng)
  593. {
  594. int restart_enabled = 0;
  595. #if defined(MBEDTLS_ECP_RESTARTABLE)
  596. restart_enabled = ctx->restart_enabled;
  597. #endif
  598. #if defined(MBEDTLS_ECDH_LEGACY_CONTEXT)
  599. return ecdh_calc_secret_internal(ctx, olen, buf, blen, f_rng, p_rng,
  600. restart_enabled);
  601. #else
  602. switch (ctx->var) {
  603. #if defined(MBEDTLS_ECDH_VARIANT_EVEREST_ENABLED)
  604. case MBEDTLS_ECDH_VARIANT_EVEREST:
  605. return mbedtls_everest_calc_secret(&ctx->ctx.everest_ecdh, olen,
  606. buf, blen, f_rng, p_rng);
  607. #endif
  608. case MBEDTLS_ECDH_VARIANT_MBEDTLS_2_0:
  609. return ecdh_calc_secret_internal(&ctx->ctx.mbed_ecdh, olen, buf,
  610. blen, f_rng, p_rng,
  611. restart_enabled);
  612. default:
  613. return MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  614. }
  615. #endif
  616. }
  617. #endif /* MBEDTLS_ECDH_C */