dhm.c 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290
  1. /*
  2. * Diffie-Hellman-Merkle key exchange
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. /*
  26. * Reference:
  27. *
  28. * http://www.cacr.math.uwaterloo.ca/hac/ (chapter 12)
  29. */
  30. #include "config.h"
  31. #if defined(POLARSSL_DHM_C)
  32. #include "polarssl/dhm.h"
  33. /*
  34. * helper to validate the mpi size and import it
  35. */
  36. static int dhm_read_bignum( mpi *X,
  37. unsigned char **p,
  38. const unsigned char *end )
  39. {
  40. int ret, n;
  41. if( end - *p < 2 )
  42. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  43. n = ( (*p)[0] << 8 ) | (*p)[1];
  44. (*p) += 2;
  45. if( (int)( end - *p ) < n )
  46. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  47. if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
  48. return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
  49. (*p) += n;
  50. return( 0 );
  51. }
  52. /*
  53. * Verify sanity of public parameter with regards to P
  54. *
  55. * Public parameter should be: 2 <= public_param <= P - 2
  56. *
  57. * For more information on the attack, see:
  58. * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  59. * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  60. */
  61. static int dhm_check_range( const mpi *public_param, const mpi *P )
  62. {
  63. mpi L, U;
  64. int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
  65. mpi_init( &L ); mpi_init( &U );
  66. mpi_lset( &L, 2 );
  67. mpi_sub_int( &U, P, 2 );
  68. if( mpi_cmp_mpi( public_param, &L ) >= 0 &&
  69. mpi_cmp_mpi( public_param, &U ) <= 0 )
  70. {
  71. ret = 0;
  72. }
  73. mpi_free( &L ); mpi_free( &U );
  74. return( ret );
  75. }
  76. /*
  77. * Parse the ServerKeyExchange parameters
  78. */
  79. int dhm_read_params( dhm_context *ctx,
  80. unsigned char **p,
  81. const unsigned char *end )
  82. {
  83. int ret, n;
  84. memset( ctx, 0, sizeof( dhm_context ) );
  85. if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
  86. ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
  87. ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
  88. return( ret );
  89. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  90. return( ret );
  91. ctx->len = mpi_size( &ctx->P );
  92. if( end - *p < 2 )
  93. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  94. n = ( (*p)[0] << 8 ) | (*p)[1];
  95. (*p) += 2;
  96. if( end != *p + n )
  97. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  98. return( 0 );
  99. }
  100. /*
  101. * Setup and write the ServerKeyExchange parameters
  102. */
  103. int dhm_make_params( dhm_context *ctx, int x_size,
  104. unsigned char *output, size_t *olen,
  105. int (*f_rng)(void *), void *p_rng )
  106. {
  107. int ret, n;
  108. size_t n1, n2, n3;
  109. unsigned char *p;
  110. /*
  111. * Generate X as large as possible ( < P )
  112. */
  113. n = x_size / sizeof( t_uint ) + 1;
  114. mpi_fill_random( &ctx->X, n, f_rng, p_rng );
  115. while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  116. mpi_shift_r( &ctx->X, 1 );
  117. /*
  118. * Calculate GX = G^X mod P
  119. */
  120. MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  121. &ctx->P , &ctx->RP ) );
  122. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  123. return( ret );
  124. /*
  125. * export P, G, GX
  126. */
  127. #define DHM_MPI_EXPORT(X,n) \
  128. MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
  129. *p++ = (unsigned char)( n >> 8 ); \
  130. *p++ = (unsigned char)( n ); p += n;
  131. n1 = mpi_size( &ctx->P );
  132. n2 = mpi_size( &ctx->G );
  133. n3 = mpi_size( &ctx->GX );
  134. p = output;
  135. DHM_MPI_EXPORT( &ctx->P , n1 );
  136. DHM_MPI_EXPORT( &ctx->G , n2 );
  137. DHM_MPI_EXPORT( &ctx->GX, n3 );
  138. *olen = p - output;
  139. ctx->len = n1;
  140. cleanup:
  141. if( ret != 0 )
  142. return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
  143. return( 0 );
  144. }
  145. /*
  146. * Import the peer's public value G^Y
  147. */
  148. int dhm_read_public( dhm_context *ctx,
  149. const unsigned char *input, size_t ilen )
  150. {
  151. int ret;
  152. if( ctx == NULL || ilen < 1 || ilen > ctx->len )
  153. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  154. if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  155. return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
  156. return( 0 );
  157. }
  158. /*
  159. * Create own private value X and export G^X
  160. */
  161. int dhm_make_public( dhm_context *ctx, int x_size,
  162. unsigned char *output, size_t olen,
  163. int (*f_rng)(void *), void *p_rng )
  164. {
  165. int ret, n;
  166. if( ctx == NULL || olen < 1 || olen > ctx->len )
  167. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  168. /*
  169. * generate X and calculate GX = G^X mod P
  170. */
  171. n = x_size / sizeof( t_uint ) + 1;
  172. mpi_fill_random( &ctx->X, n, f_rng, p_rng );
  173. while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  174. mpi_shift_r( &ctx->X, 1 );
  175. MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  176. &ctx->P , &ctx->RP ) );
  177. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  178. return( ret );
  179. MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
  180. cleanup:
  181. if( ret != 0 )
  182. return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
  183. return( 0 );
  184. }
  185. /*
  186. * Derive and export the shared secret (G^Y)^X mod P
  187. */
  188. int dhm_calc_secret( dhm_context *ctx,
  189. unsigned char *output, size_t *olen )
  190. {
  191. int ret;
  192. if( ctx == NULL || *olen < ctx->len )
  193. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  194. MPI_CHK( mpi_exp_mod( &ctx->K, &ctx->GY, &ctx->X,
  195. &ctx->P, &ctx->RP ) );
  196. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  197. return( ret );
  198. *olen = mpi_size( &ctx->K );
  199. MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
  200. cleanup:
  201. if( ret != 0 )
  202. return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
  203. return( 0 );
  204. }
  205. /*
  206. * Free the components of a DHM key
  207. */
  208. void dhm_free( dhm_context *ctx )
  209. {
  210. mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
  211. mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
  212. mpi_free( &ctx->P );
  213. }
  214. #if defined(POLARSSL_SELF_TEST)
  215. /*
  216. * Checkup routine
  217. */
  218. int dhm_self_test( int verbose )
  219. {
  220. return( verbose++ );
  221. }
  222. #endif
  223. #endif