dhm.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599
  1. /*
  2. * Diffie-Hellman-Merkle key exchange
  3. *
  4. * Copyright (C) 2006-2014, 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. #if !defined(POLARSSL_CONFIG_FILE)
  31. #include "polarssl/config.h"
  32. #else
  33. #include POLARSSL_CONFIG_FILE
  34. #endif
  35. #if defined(POLARSSL_DHM_C)
  36. #include "polarssl/dhm.h"
  37. #if defined(POLARSSL_PEM_PARSE_C)
  38. #include "polarssl/pem.h"
  39. #endif
  40. #if defined(POLARSSL_ASN1_PARSE_C)
  41. #include "polarssl/asn1.h"
  42. #endif
  43. #if defined(POLARSSL_PLATFORM_C)
  44. #include "polarssl/platform.h"
  45. #else
  46. #include <stdlib.h>
  47. #define polarssl_printf printf
  48. #define polarssl_malloc malloc
  49. #define polarssl_free free
  50. #endif
  51. /* Implementation that should never be optimized out by the compiler */
  52. static void polarssl_zeroize( void *v, size_t n ) {
  53. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  54. }
  55. /*
  56. * helper to validate the mpi size and import it
  57. */
  58. static int dhm_read_bignum( mpi *X,
  59. unsigned char **p,
  60. const unsigned char *end )
  61. {
  62. int ret, n;
  63. if( end - *p < 2 )
  64. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  65. n = ( (*p)[0] << 8 ) | (*p)[1];
  66. (*p) += 2;
  67. if( (int)( end - *p ) < n )
  68. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  69. if( ( ret = mpi_read_binary( X, *p, n ) ) != 0 )
  70. return( POLARSSL_ERR_DHM_READ_PARAMS_FAILED + ret );
  71. (*p) += n;
  72. return( 0 );
  73. }
  74. /*
  75. * Verify sanity of parameter with regards to P
  76. *
  77. * Parameter should be: 2 <= public_param <= P - 2
  78. *
  79. * For more information on the attack, see:
  80. * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  81. * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  82. */
  83. static int dhm_check_range( const mpi *param, const mpi *P )
  84. {
  85. mpi L, U;
  86. int ret = POLARSSL_ERR_DHM_BAD_INPUT_DATA;
  87. mpi_init( &L ); mpi_init( &U );
  88. MPI_CHK( mpi_lset( &L, 2 ) );
  89. MPI_CHK( mpi_sub_int( &U, P, 2 ) );
  90. if( mpi_cmp_mpi( param, &L ) >= 0 &&
  91. mpi_cmp_mpi( param, &U ) <= 0 )
  92. {
  93. ret = 0;
  94. }
  95. cleanup:
  96. mpi_free( &L ); mpi_free( &U );
  97. return( ret );
  98. }
  99. void dhm_init( dhm_context *ctx )
  100. {
  101. memset( ctx, 0, sizeof( dhm_context ) );
  102. }
  103. /*
  104. * Parse the ServerKeyExchange parameters
  105. */
  106. int dhm_read_params( dhm_context *ctx,
  107. unsigned char **p,
  108. const unsigned char *end )
  109. {
  110. int ret;
  111. if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
  112. ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
  113. ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
  114. return( ret );
  115. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  116. return( ret );
  117. ctx->len = mpi_size( &ctx->P );
  118. return( 0 );
  119. }
  120. /*
  121. * Setup and write the ServerKeyExchange parameters
  122. */
  123. int dhm_make_params( dhm_context *ctx, int x_size,
  124. unsigned char *output, size_t *olen,
  125. int (*f_rng)(void *, unsigned char *, size_t),
  126. void *p_rng )
  127. {
  128. int ret, count = 0;
  129. size_t n1, n2, n3;
  130. unsigned char *p;
  131. if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
  132. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  133. /*
  134. * Generate X as large as possible ( < P )
  135. */
  136. do
  137. {
  138. mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
  139. while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  140. MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
  141. if( count++ > 10 )
  142. return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED );
  143. }
  144. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  145. /*
  146. * Calculate GX = G^X mod P
  147. */
  148. MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  149. &ctx->P , &ctx->RP ) );
  150. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  151. return( ret );
  152. /*
  153. * export P, G, GX
  154. */
  155. #define DHM_MPI_EXPORT(X,n) \
  156. MPI_CHK( mpi_write_binary( X, p + 2, n ) ); \
  157. *p++ = (unsigned char)( n >> 8 ); \
  158. *p++ = (unsigned char)( n ); p += n;
  159. n1 = mpi_size( &ctx->P );
  160. n2 = mpi_size( &ctx->G );
  161. n3 = mpi_size( &ctx->GX );
  162. p = output;
  163. DHM_MPI_EXPORT( &ctx->P , n1 );
  164. DHM_MPI_EXPORT( &ctx->G , n2 );
  165. DHM_MPI_EXPORT( &ctx->GX, n3 );
  166. *olen = p - output;
  167. ctx->len = n1;
  168. cleanup:
  169. if( ret != 0 )
  170. return( POLARSSL_ERR_DHM_MAKE_PARAMS_FAILED + ret );
  171. return( 0 );
  172. }
  173. /*
  174. * Import the peer's public value G^Y
  175. */
  176. int dhm_read_public( dhm_context *ctx,
  177. const unsigned char *input, size_t ilen )
  178. {
  179. int ret;
  180. if( ctx == NULL || ilen < 1 || ilen > ctx->len )
  181. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  182. if( ( ret = mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  183. return( POLARSSL_ERR_DHM_READ_PUBLIC_FAILED + ret );
  184. return( 0 );
  185. }
  186. /*
  187. * Create own private value X and export G^X
  188. */
  189. int dhm_make_public( dhm_context *ctx, int x_size,
  190. unsigned char *output, size_t olen,
  191. int (*f_rng)(void *, unsigned char *, size_t),
  192. void *p_rng )
  193. {
  194. int ret, count = 0;
  195. if( ctx == NULL || olen < 1 || olen > ctx->len )
  196. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  197. if( mpi_cmp_int( &ctx->P, 0 ) == 0 )
  198. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  199. /*
  200. * generate X and calculate GX = G^X mod P
  201. */
  202. do
  203. {
  204. mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
  205. while( mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  206. MPI_CHK( mpi_shift_r( &ctx->X, 1 ) );
  207. if( count++ > 10 )
  208. return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED );
  209. }
  210. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  211. MPI_CHK( mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  212. &ctx->P , &ctx->RP ) );
  213. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  214. return( ret );
  215. MPI_CHK( mpi_write_binary( &ctx->GX, output, olen ) );
  216. cleanup:
  217. if( ret != 0 )
  218. return( POLARSSL_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
  219. return( 0 );
  220. }
  221. /*
  222. * Use the blinding method and optimisation suggested in section 10 of:
  223. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  224. * DSS, and other systems. In : Advances in Cryptology—CRYPTO’96. Springer
  225. * Berlin Heidelberg, 1996. p. 104-113.
  226. */
  227. static int dhm_update_blinding( dhm_context *ctx,
  228. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  229. {
  230. int ret, count;
  231. /*
  232. * Don't use any blinding the first time a particular X is used,
  233. * but remember it to use blinding next time.
  234. */
  235. if( mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
  236. {
  237. MPI_CHK( mpi_copy( &ctx->pX, &ctx->X ) );
  238. MPI_CHK( mpi_lset( &ctx->Vi, 1 ) );
  239. MPI_CHK( mpi_lset( &ctx->Vf, 1 ) );
  240. return( 0 );
  241. }
  242. /*
  243. * Ok, we need blinding. Can we re-use existing values?
  244. * If yes, just update them by squaring them.
  245. */
  246. if( mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
  247. {
  248. MPI_CHK( mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  249. MPI_CHK( mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
  250. MPI_CHK( mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  251. MPI_CHK( mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  252. return( 0 );
  253. }
  254. /*
  255. * We need to generate blinding values from scratch
  256. */
  257. /* Vi = random( 2, P-1 ) */
  258. count = 0;
  259. do
  260. {
  261. mpi_fill_random( &ctx->Vi, mpi_size( &ctx->P ), f_rng, p_rng );
  262. while( mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
  263. MPI_CHK( mpi_shift_r( &ctx->Vi, 1 ) );
  264. if( count++ > 10 )
  265. return( POLARSSL_ERR_MPI_NOT_ACCEPTABLE );
  266. }
  267. while( mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
  268. /* Vf = Vi^-X mod P */
  269. MPI_CHK( mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
  270. MPI_CHK( mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
  271. cleanup:
  272. return( ret );
  273. }
  274. /*
  275. * Derive and export the shared secret (G^Y)^X mod P
  276. */
  277. int dhm_calc_secret( dhm_context *ctx,
  278. unsigned char *output, size_t *olen,
  279. int (*f_rng)(void *, unsigned char *, size_t),
  280. void *p_rng )
  281. {
  282. int ret;
  283. mpi GYb;
  284. if( ctx == NULL || *olen < ctx->len )
  285. return( POLARSSL_ERR_DHM_BAD_INPUT_DATA );
  286. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  287. return( ret );
  288. mpi_init( &GYb );
  289. /* Blind peer's value */
  290. if( f_rng != NULL )
  291. {
  292. MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
  293. MPI_CHK( mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
  294. MPI_CHK( mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
  295. }
  296. else
  297. MPI_CHK( mpi_copy( &GYb, &ctx->GY ) );
  298. /* Do modular exponentiation */
  299. MPI_CHK( mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
  300. &ctx->P, &ctx->RP ) );
  301. /* Unblind secret value */
  302. if( f_rng != NULL )
  303. {
  304. MPI_CHK( mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
  305. MPI_CHK( mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
  306. }
  307. *olen = mpi_size( &ctx->K );
  308. MPI_CHK( mpi_write_binary( &ctx->K, output, *olen ) );
  309. cleanup:
  310. mpi_free( &GYb );
  311. if( ret != 0 )
  312. return( POLARSSL_ERR_DHM_CALC_SECRET_FAILED + ret );
  313. return( 0 );
  314. }
  315. /*
  316. * Free the components of a DHM key
  317. */
  318. void dhm_free( dhm_context *ctx )
  319. {
  320. mpi_free( &ctx->pX); mpi_free( &ctx->Vf ); mpi_free( &ctx->Vi );
  321. mpi_free( &ctx->RP ); mpi_free( &ctx->K ); mpi_free( &ctx->GY );
  322. mpi_free( &ctx->GX ); mpi_free( &ctx->X ); mpi_free( &ctx->G );
  323. mpi_free( &ctx->P );
  324. polarssl_zeroize( ctx, sizeof( dhm_context ) );
  325. }
  326. #if defined(POLARSSL_ASN1_PARSE_C)
  327. /*
  328. * Parse DHM parameters
  329. */
  330. int dhm_parse_dhm( dhm_context *dhm, const unsigned char *dhmin,
  331. size_t dhminlen )
  332. {
  333. int ret;
  334. size_t len;
  335. unsigned char *p, *end;
  336. #if defined(POLARSSL_PEM_PARSE_C)
  337. pem_context pem;
  338. pem_init( &pem );
  339. ret = pem_read_buffer( &pem,
  340. "-----BEGIN DH PARAMETERS-----",
  341. "-----END DH PARAMETERS-----",
  342. dhmin, NULL, 0, &dhminlen );
  343. if( ret == 0 )
  344. {
  345. /*
  346. * Was PEM encoded
  347. */
  348. dhminlen = pem.buflen;
  349. }
  350. else if( ret != POLARSSL_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  351. goto exit;
  352. p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
  353. #else
  354. p = (unsigned char *) dhmin;
  355. #endif /* POLARSSL_PEM_PARSE_C */
  356. end = p + dhminlen;
  357. /*
  358. * DHParams ::= SEQUENCE {
  359. * prime INTEGER, -- P
  360. * generator INTEGER, -- g
  361. * }
  362. */
  363. if( ( ret = asn1_get_tag( &p, end, &len,
  364. ASN1_CONSTRUCTED | ASN1_SEQUENCE ) ) != 0 )
  365. {
  366. ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
  367. goto exit;
  368. }
  369. end = p + len;
  370. if( ( ret = asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
  371. ( ret = asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
  372. {
  373. ret = POLARSSL_ERR_DHM_INVALID_FORMAT + ret;
  374. goto exit;
  375. }
  376. if( p != end )
  377. {
  378. ret = POLARSSL_ERR_DHM_INVALID_FORMAT +
  379. POLARSSL_ERR_ASN1_LENGTH_MISMATCH;
  380. goto exit;
  381. }
  382. ret = 0;
  383. dhm->len = mpi_size( &dhm->P );
  384. exit:
  385. #if defined(POLARSSL_PEM_PARSE_C)
  386. pem_free( &pem );
  387. #endif
  388. if( ret != 0 )
  389. dhm_free( dhm );
  390. return( ret );
  391. }
  392. #if defined(POLARSSL_FS_IO)
  393. /*
  394. * Load all data from a file into a given buffer.
  395. */
  396. static int load_file( const char *path, unsigned char **buf, size_t *n )
  397. {
  398. FILE *f;
  399. long size;
  400. if( ( f = fopen( path, "rb" ) ) == NULL )
  401. return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
  402. fseek( f, 0, SEEK_END );
  403. if( ( size = ftell( f ) ) == -1 )
  404. {
  405. fclose( f );
  406. return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
  407. }
  408. fseek( f, 0, SEEK_SET );
  409. *n = (size_t) size;
  410. if( *n + 1 == 0 ||
  411. ( *buf = (unsigned char *) polarssl_malloc( *n + 1 ) ) == NULL )
  412. {
  413. fclose( f );
  414. return( POLARSSL_ERR_DHM_MALLOC_FAILED );
  415. }
  416. if( fread( *buf, 1, *n, f ) != *n )
  417. {
  418. fclose( f );
  419. polarssl_free( *buf );
  420. return( POLARSSL_ERR_DHM_FILE_IO_ERROR );
  421. }
  422. fclose( f );
  423. (*buf)[*n] = '\0';
  424. return( 0 );
  425. }
  426. /*
  427. * Load and parse DHM parameters
  428. */
  429. int dhm_parse_dhmfile( dhm_context *dhm, const char *path )
  430. {
  431. int ret;
  432. size_t n;
  433. unsigned char *buf;
  434. if( ( ret = load_file( path, &buf, &n ) ) != 0 )
  435. return( ret );
  436. ret = dhm_parse_dhm( dhm, buf, n );
  437. polarssl_zeroize( buf, n + 1 );
  438. polarssl_free( buf );
  439. return( ret );
  440. }
  441. #endif /* POLARSSL_FS_IO */
  442. #endif /* POLARSSL_ASN1_PARSE_C */
  443. #if defined(POLARSSL_SELF_TEST)
  444. #include "polarssl/certs.h"
  445. /*
  446. * Checkup routine
  447. */
  448. int dhm_self_test( int verbose )
  449. {
  450. #if defined(POLARSSL_CERTS_C)
  451. int ret;
  452. dhm_context dhm;
  453. dhm_init( &dhm );
  454. if( verbose != 0 )
  455. polarssl_printf( " DHM parameter load: " );
  456. if( ( ret = dhm_parse_dhm( &dhm, (const unsigned char *) test_dhm_params,
  457. strlen( test_dhm_params ) ) ) != 0 )
  458. {
  459. if( verbose != 0 )
  460. polarssl_printf( "failed\n" );
  461. ret = 1;
  462. goto exit;
  463. }
  464. if( verbose != 0 )
  465. polarssl_printf( "passed\n\n" );
  466. exit:
  467. dhm_free( &dhm );
  468. return( ret );
  469. #else
  470. if( verbose != 0 )
  471. polarssl_printf( " DHM parameter load: skipped\n" );
  472. return( 0 );
  473. #endif /* POLARSSL_CERTS_C */
  474. }
  475. #endif /* POLARSSL_SELF_TEST */
  476. #endif /* POLARSSL_DHM_C */