ecdh.c 21 KB

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