ecdh.c 7.0 KB

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