ecdh.c 20 KB

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