ecdh.c 20 KB

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