ecdh.c 23 KB

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