Subversion Repositories Kolibri OS

Rev

Blame | Last modification | View Log | Download | RSS feed

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