ecdh.c 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267
  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. * References:
  25. *
  26. * SEC1 http://www.secg.org/index.php?action=secg,docs_secg
  27. * RFC 4492
  28. */
  29. #if !defined(MBEDTLS_CONFIG_FILE)
  30. #include "mbedtls/config.h"
  31. #else
  32. #include MBEDTLS_CONFIG_FILE
  33. #endif
  34. #if defined(MBEDTLS_ECDH_C)
  35. #include "mbedtls/ecdh.h"
  36. #include <string.h>
  37. /*
  38. * Generate public key: simple wrapper around mbedtls_ecp_gen_keypair
  39. */
  40. int mbedtls_ecdh_gen_public( mbedtls_ecp_group *grp, mbedtls_mpi *d, mbedtls_ecp_point *Q,
  41. int (*f_rng)(void *, unsigned char *, size_t),
  42. void *p_rng )
  43. {
  44. return mbedtls_ecp_gen_keypair( grp, d, Q, f_rng, p_rng );
  45. }
  46. /*
  47. * Compute shared secret (SEC1 3.3.1)
  48. */
  49. int mbedtls_ecdh_compute_shared( mbedtls_ecp_group *grp, mbedtls_mpi *z,
  50. const mbedtls_ecp_point *Q, const mbedtls_mpi *d,
  51. int (*f_rng)(void *, unsigned char *, size_t),
  52. void *p_rng )
  53. {
  54. int ret;
  55. mbedtls_ecp_point P;
  56. mbedtls_ecp_point_init( &P );
  57. /*
  58. * Make sure Q is a valid pubkey before using it
  59. */
  60. MBEDTLS_MPI_CHK( mbedtls_ecp_check_pubkey( grp, Q ) );
  61. MBEDTLS_MPI_CHK( mbedtls_ecp_mul( grp, &P, d, Q, f_rng, p_rng ) );
  62. if( mbedtls_ecp_is_zero( &P ) )
  63. {
  64. ret = MBEDTLS_ERR_ECP_BAD_INPUT_DATA;
  65. goto cleanup;
  66. }
  67. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( z, &P.X ) );
  68. cleanup:
  69. mbedtls_ecp_point_free( &P );
  70. return( ret );
  71. }
  72. /*
  73. * Initialize context
  74. */
  75. void mbedtls_ecdh_init( mbedtls_ecdh_context *ctx )
  76. {
  77. memset( ctx, 0, sizeof( mbedtls_ecdh_context ) );
  78. }
  79. /*
  80. * Free context
  81. */
  82. void mbedtls_ecdh_free( mbedtls_ecdh_context *ctx )
  83. {
  84. if( ctx == NULL )
  85. return;
  86. mbedtls_ecp_group_free( &ctx->grp );
  87. mbedtls_ecp_point_free( &ctx->Q );
  88. mbedtls_ecp_point_free( &ctx->Qp );
  89. mbedtls_ecp_point_free( &ctx->Vi );
  90. mbedtls_ecp_point_free( &ctx->Vf );
  91. mbedtls_mpi_free( &ctx->d );
  92. mbedtls_mpi_free( &ctx->z );
  93. mbedtls_mpi_free( &ctx->_d );
  94. }
  95. /*
  96. * Setup and write the ServerKeyExhange parameters (RFC 4492)
  97. * struct {
  98. * ECParameters curve_params;
  99. * ECPoint public;
  100. * } ServerECDHParams;
  101. */
  102. int mbedtls_ecdh_make_params( mbedtls_ecdh_context *ctx, size_t *olen,
  103. unsigned char *buf, size_t blen,
  104. int (*f_rng)(void *, unsigned char *, size_t),
  105. void *p_rng )
  106. {
  107. int ret;
  108. size_t grp_len, pt_len;
  109. if( ctx == NULL || ctx->grp.pbits == 0 )
  110. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  111. if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
  112. != 0 )
  113. return( ret );
  114. if( ( ret = mbedtls_ecp_tls_write_group( &ctx->grp, &grp_len, buf, blen ) )
  115. != 0 )
  116. return( ret );
  117. buf += grp_len;
  118. blen -= grp_len;
  119. if( ( ret = mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
  120. &pt_len, buf, blen ) ) != 0 )
  121. return( ret );
  122. *olen = grp_len + pt_len;
  123. return( 0 );
  124. }
  125. /*
  126. * Read the ServerKeyExhange parameters (RFC 4492)
  127. * struct {
  128. * ECParameters curve_params;
  129. * ECPoint public;
  130. * } ServerECDHParams;
  131. */
  132. int mbedtls_ecdh_read_params( mbedtls_ecdh_context *ctx,
  133. const unsigned char **buf, const unsigned char *end )
  134. {
  135. int ret;
  136. if( ( ret = mbedtls_ecp_tls_read_group( &ctx->grp, buf, end - *buf ) ) != 0 )
  137. return( ret );
  138. if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, buf, end - *buf ) )
  139. != 0 )
  140. return( ret );
  141. return( 0 );
  142. }
  143. /*
  144. * Get parameters from a keypair
  145. */
  146. int mbedtls_ecdh_get_params( mbedtls_ecdh_context *ctx, const mbedtls_ecp_keypair *key,
  147. mbedtls_ecdh_side side )
  148. {
  149. int ret;
  150. if( ( ret = mbedtls_ecp_group_copy( &ctx->grp, &key->grp ) ) != 0 )
  151. return( ret );
  152. /* If it's not our key, just import the public part as Qp */
  153. if( side == MBEDTLS_ECDH_THEIRS )
  154. return( mbedtls_ecp_copy( &ctx->Qp, &key->Q ) );
  155. /* Our key: import public (as Q) and private parts */
  156. if( side != MBEDTLS_ECDH_OURS )
  157. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  158. if( ( ret = mbedtls_ecp_copy( &ctx->Q, &key->Q ) ) != 0 ||
  159. ( ret = mbedtls_mpi_copy( &ctx->d, &key->d ) ) != 0 )
  160. return( ret );
  161. return( 0 );
  162. }
  163. /*
  164. * Setup and export the client public value
  165. */
  166. int mbedtls_ecdh_make_public( mbedtls_ecdh_context *ctx, size_t *olen,
  167. unsigned char *buf, size_t blen,
  168. int (*f_rng)(void *, unsigned char *, size_t),
  169. void *p_rng )
  170. {
  171. int ret;
  172. if( ctx == NULL || ctx->grp.pbits == 0 )
  173. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  174. if( ( ret = mbedtls_ecdh_gen_public( &ctx->grp, &ctx->d, &ctx->Q, f_rng, p_rng ) )
  175. != 0 )
  176. return( ret );
  177. return mbedtls_ecp_tls_write_point( &ctx->grp, &ctx->Q, ctx->point_format,
  178. olen, buf, blen );
  179. }
  180. /*
  181. * Parse and import the client's public value
  182. */
  183. int mbedtls_ecdh_read_public( mbedtls_ecdh_context *ctx,
  184. const unsigned char *buf, size_t blen )
  185. {
  186. int ret;
  187. const unsigned char *p = buf;
  188. if( ctx == NULL )
  189. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  190. if( ( ret = mbedtls_ecp_tls_read_point( &ctx->grp, &ctx->Qp, &p, blen ) ) != 0 )
  191. return( ret );
  192. if( (size_t)( p - buf ) != blen )
  193. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  194. return( 0 );
  195. }
  196. /*
  197. * Derive and export the shared secret
  198. */
  199. int mbedtls_ecdh_calc_secret( mbedtls_ecdh_context *ctx, size_t *olen,
  200. unsigned char *buf, size_t blen,
  201. int (*f_rng)(void *, unsigned char *, size_t),
  202. void *p_rng )
  203. {
  204. int ret;
  205. if( ctx == NULL )
  206. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  207. if( ( ret = mbedtls_ecdh_compute_shared( &ctx->grp, &ctx->z, &ctx->Qp, &ctx->d,
  208. f_rng, p_rng ) ) != 0 )
  209. {
  210. return( ret );
  211. }
  212. if( mbedtls_mpi_size( &ctx->z ) > blen )
  213. return( MBEDTLS_ERR_ECP_BAD_INPUT_DATA );
  214. *olen = ctx->grp.pbits / 8 + ( ( ctx->grp.pbits % 8 ) != 0 );
  215. return mbedtls_mpi_write_binary( &ctx->z, buf, *olen );
  216. }
  217. #endif /* MBEDTLS_ECDH_C */