dhm.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712
  1. /*
  2. * Diffie-Hellman-Merkle key exchange
  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. * The following sources were referenced in the design of this implementation
  23. * of the Diffie-Hellman-Merkle algorithm:
  24. *
  25. * [1] Handbook of Applied Cryptography - 1997, Chapter 12
  26. * Menezes, van Oorschot and Vanstone
  27. *
  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_DHM_C)
  35. #include "mbedtls/dhm.h"
  36. #include "mbedtls/platform_util.h"
  37. #include <string.h>
  38. #if defined(MBEDTLS_PEM_PARSE_C)
  39. #include "mbedtls/pem.h"
  40. #endif
  41. #if defined(MBEDTLS_ASN1_PARSE_C)
  42. #include "mbedtls/asn1.h"
  43. #endif
  44. #if defined(MBEDTLS_PLATFORM_C)
  45. #include "mbedtls/platform.h"
  46. #else
  47. #include <stdlib.h>
  48. #include <stdio.h>
  49. #define mbedtls_printf printf
  50. #define mbedtls_calloc calloc
  51. #define mbedtls_free free
  52. #endif
  53. #if !defined(MBEDTLS_DHM_ALT)
  54. #define DHM_VALIDATE_RET( cond ) \
  55. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_DHM_BAD_INPUT_DATA )
  56. #define DHM_VALIDATE( cond ) \
  57. MBEDTLS_INTERNAL_VALIDATE( cond )
  58. /*
  59. * helper to validate the mbedtls_mpi size and import it
  60. */
  61. static int dhm_read_bignum( mbedtls_mpi *X,
  62. unsigned char **p,
  63. const unsigned char *end )
  64. {
  65. int ret, n;
  66. if( end - *p < 2 )
  67. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  68. n = ( (*p)[0] << 8 ) | (*p)[1];
  69. (*p) += 2;
  70. if( (int)( end - *p ) < n )
  71. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  72. if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
  73. return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret );
  74. (*p) += n;
  75. return( 0 );
  76. }
  77. /*
  78. * Verify sanity of parameter with regards to P
  79. *
  80. * Parameter should be: 2 <= public_param <= P - 2
  81. *
  82. * This means that we need to return an error if
  83. * public_param < 2 or public_param > P-2
  84. *
  85. * For more information on the attack, see:
  86. * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  87. * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  88. */
  89. static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P )
  90. {
  91. mbedtls_mpi L, U;
  92. int ret = 0;
  93. mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
  94. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
  95. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
  96. if( mbedtls_mpi_cmp_mpi( param, &L ) < 0 ||
  97. mbedtls_mpi_cmp_mpi( param, &U ) > 0 )
  98. {
  99. ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  100. }
  101. cleanup:
  102. mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
  103. return( ret );
  104. }
  105. void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
  106. {
  107. DHM_VALIDATE( ctx != NULL );
  108. memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
  109. }
  110. /*
  111. * Parse the ServerKeyExchange parameters
  112. */
  113. int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
  114. unsigned char **p,
  115. const unsigned char *end )
  116. {
  117. int ret;
  118. DHM_VALIDATE_RET( ctx != NULL );
  119. DHM_VALIDATE_RET( p != NULL && *p != NULL );
  120. DHM_VALIDATE_RET( end != NULL );
  121. if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
  122. ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
  123. ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
  124. return( ret );
  125. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  126. return( ret );
  127. ctx->len = mbedtls_mpi_size( &ctx->P );
  128. return( 0 );
  129. }
  130. /*
  131. * Setup and write the ServerKeyExchange parameters
  132. */
  133. int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
  134. unsigned char *output, size_t *olen,
  135. int (*f_rng)(void *, unsigned char *, size_t),
  136. void *p_rng )
  137. {
  138. int ret, count = 0;
  139. size_t n1, n2, n3;
  140. unsigned char *p;
  141. DHM_VALIDATE_RET( ctx != NULL );
  142. DHM_VALIDATE_RET( output != NULL );
  143. DHM_VALIDATE_RET( olen != NULL );
  144. DHM_VALIDATE_RET( f_rng != NULL );
  145. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  146. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  147. /*
  148. * Generate X as large as possible ( < P )
  149. */
  150. do
  151. {
  152. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  153. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  154. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  155. if( count++ > 10 )
  156. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
  157. }
  158. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  159. /*
  160. * Calculate GX = G^X mod P
  161. */
  162. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  163. &ctx->P , &ctx->RP ) );
  164. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  165. return( ret );
  166. /*
  167. * export P, G, GX
  168. */
  169. #define DHM_MPI_EXPORT( X, n ) \
  170. do { \
  171. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( ( X ), \
  172. p + 2, \
  173. ( n ) ) ); \
  174. *p++ = (unsigned char)( ( n ) >> 8 ); \
  175. *p++ = (unsigned char)( ( n ) ); \
  176. p += ( n ); \
  177. } while( 0 )
  178. n1 = mbedtls_mpi_size( &ctx->P );
  179. n2 = mbedtls_mpi_size( &ctx->G );
  180. n3 = mbedtls_mpi_size( &ctx->GX );
  181. p = output;
  182. DHM_MPI_EXPORT( &ctx->P , n1 );
  183. DHM_MPI_EXPORT( &ctx->G , n2 );
  184. DHM_MPI_EXPORT( &ctx->GX, n3 );
  185. *olen = p - output;
  186. ctx->len = n1;
  187. cleanup:
  188. if( ret != 0 )
  189. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
  190. return( 0 );
  191. }
  192. /*
  193. * Set prime modulus and generator
  194. */
  195. int mbedtls_dhm_set_group( mbedtls_dhm_context *ctx,
  196. const mbedtls_mpi *P,
  197. const mbedtls_mpi *G )
  198. {
  199. int ret;
  200. DHM_VALIDATE_RET( ctx != NULL );
  201. DHM_VALIDATE_RET( P != NULL );
  202. DHM_VALIDATE_RET( G != NULL );
  203. if( ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ||
  204. ( ret = mbedtls_mpi_copy( &ctx->G, G ) ) != 0 )
  205. {
  206. return( MBEDTLS_ERR_DHM_SET_GROUP_FAILED + ret );
  207. }
  208. ctx->len = mbedtls_mpi_size( &ctx->P );
  209. return( 0 );
  210. }
  211. /*
  212. * Import the peer's public value G^Y
  213. */
  214. int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
  215. const unsigned char *input, size_t ilen )
  216. {
  217. int ret;
  218. DHM_VALIDATE_RET( ctx != NULL );
  219. DHM_VALIDATE_RET( input != NULL );
  220. if( ilen < 1 || ilen > ctx->len )
  221. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  222. if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  223. return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
  224. return( 0 );
  225. }
  226. /*
  227. * Create own private value X and export G^X
  228. */
  229. int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
  230. unsigned char *output, size_t olen,
  231. int (*f_rng)(void *, unsigned char *, size_t),
  232. void *p_rng )
  233. {
  234. int ret, count = 0;
  235. DHM_VALIDATE_RET( ctx != NULL );
  236. DHM_VALIDATE_RET( output != NULL );
  237. DHM_VALIDATE_RET( f_rng != NULL );
  238. if( olen < 1 || olen > ctx->len )
  239. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  240. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  241. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  242. /*
  243. * generate X and calculate GX = G^X mod P
  244. */
  245. do
  246. {
  247. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  248. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  249. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  250. if( count++ > 10 )
  251. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
  252. }
  253. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  254. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  255. &ctx->P , &ctx->RP ) );
  256. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  257. return( ret );
  258. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
  259. cleanup:
  260. if( ret != 0 )
  261. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
  262. return( 0 );
  263. }
  264. /*
  265. * Use the blinding method and optimisation suggested in section 10 of:
  266. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  267. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  268. * Berlin Heidelberg, 1996. p. 104-113.
  269. */
  270. static int dhm_update_blinding( mbedtls_dhm_context *ctx,
  271. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  272. {
  273. int ret, count;
  274. /*
  275. * Don't use any blinding the first time a particular X is used,
  276. * but remember it to use blinding next time.
  277. */
  278. if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
  279. {
  280. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
  281. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
  282. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
  283. return( 0 );
  284. }
  285. /*
  286. * Ok, we need blinding. Can we re-use existing values?
  287. * If yes, just update them by squaring them.
  288. */
  289. if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
  290. {
  291. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  292. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
  293. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  294. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  295. return( 0 );
  296. }
  297. /*
  298. * We need to generate blinding values from scratch
  299. */
  300. /* Vi = random( 2, P-1 ) */
  301. count = 0;
  302. do
  303. {
  304. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng ) );
  305. while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
  306. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
  307. if( count++ > 10 )
  308. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  309. }
  310. while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
  311. /* Vf = Vi^-X mod P */
  312. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
  313. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
  314. cleanup:
  315. return( ret );
  316. }
  317. /*
  318. * Derive and export the shared secret (G^Y)^X mod P
  319. */
  320. int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  321. unsigned char *output, size_t output_size, size_t *olen,
  322. int (*f_rng)(void *, unsigned char *, size_t),
  323. void *p_rng )
  324. {
  325. int ret;
  326. mbedtls_mpi GYb;
  327. DHM_VALIDATE_RET( ctx != NULL );
  328. DHM_VALIDATE_RET( output != NULL );
  329. DHM_VALIDATE_RET( olen != NULL );
  330. if( output_size < ctx->len )
  331. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  332. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  333. return( ret );
  334. mbedtls_mpi_init( &GYb );
  335. /* Blind peer's value */
  336. if( f_rng != NULL )
  337. {
  338. MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
  339. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
  340. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
  341. }
  342. else
  343. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
  344. /* Do modular exponentiation */
  345. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
  346. &ctx->P, &ctx->RP ) );
  347. /* Unblind secret value */
  348. if( f_rng != NULL )
  349. {
  350. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
  351. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
  352. }
  353. *olen = mbedtls_mpi_size( &ctx->K );
  354. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
  355. cleanup:
  356. mbedtls_mpi_free( &GYb );
  357. if( ret != 0 )
  358. return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
  359. return( 0 );
  360. }
  361. /*
  362. * Free the components of a DHM key
  363. */
  364. void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
  365. {
  366. if( ctx == NULL )
  367. return;
  368. mbedtls_mpi_free( &ctx->pX );
  369. mbedtls_mpi_free( &ctx->Vf );
  370. mbedtls_mpi_free( &ctx->Vi );
  371. mbedtls_mpi_free( &ctx->RP );
  372. mbedtls_mpi_free( &ctx->K );
  373. mbedtls_mpi_free( &ctx->GY );
  374. mbedtls_mpi_free( &ctx->GX );
  375. mbedtls_mpi_free( &ctx->X );
  376. mbedtls_mpi_free( &ctx->G );
  377. mbedtls_mpi_free( &ctx->P );
  378. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
  379. }
  380. #if defined(MBEDTLS_ASN1_PARSE_C)
  381. /*
  382. * Parse DHM parameters
  383. */
  384. int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  385. size_t dhminlen )
  386. {
  387. int ret;
  388. size_t len;
  389. unsigned char *p, *end;
  390. #if defined(MBEDTLS_PEM_PARSE_C)
  391. mbedtls_pem_context pem;
  392. #endif /* MBEDTLS_PEM_PARSE_C */
  393. DHM_VALIDATE_RET( dhm != NULL );
  394. DHM_VALIDATE_RET( dhmin != NULL );
  395. #if defined(MBEDTLS_PEM_PARSE_C)
  396. mbedtls_pem_init( &pem );
  397. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  398. if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
  399. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  400. else
  401. ret = mbedtls_pem_read_buffer( &pem,
  402. "-----BEGIN DH PARAMETERS-----",
  403. "-----END DH PARAMETERS-----",
  404. dhmin, NULL, 0, &dhminlen );
  405. if( ret == 0 )
  406. {
  407. /*
  408. * Was PEM encoded
  409. */
  410. dhminlen = pem.buflen;
  411. }
  412. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  413. goto exit;
  414. p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
  415. #else
  416. p = (unsigned char *) dhmin;
  417. #endif /* MBEDTLS_PEM_PARSE_C */
  418. end = p + dhminlen;
  419. /*
  420. * DHParams ::= SEQUENCE {
  421. * prime INTEGER, -- P
  422. * generator INTEGER, -- g
  423. * privateValueLength INTEGER OPTIONAL
  424. * }
  425. */
  426. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  427. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  428. {
  429. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  430. goto exit;
  431. }
  432. end = p + len;
  433. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
  434. ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
  435. {
  436. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  437. goto exit;
  438. }
  439. if( p != end )
  440. {
  441. /* This might be the optional privateValueLength.
  442. * If so, we can cleanly discard it */
  443. mbedtls_mpi rec;
  444. mbedtls_mpi_init( &rec );
  445. ret = mbedtls_asn1_get_mpi( &p, end, &rec );
  446. mbedtls_mpi_free( &rec );
  447. if ( ret != 0 )
  448. {
  449. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  450. goto exit;
  451. }
  452. if ( p != end )
  453. {
  454. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
  455. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  456. goto exit;
  457. }
  458. }
  459. ret = 0;
  460. dhm->len = mbedtls_mpi_size( &dhm->P );
  461. exit:
  462. #if defined(MBEDTLS_PEM_PARSE_C)
  463. mbedtls_pem_free( &pem );
  464. #endif
  465. if( ret != 0 )
  466. mbedtls_dhm_free( dhm );
  467. return( ret );
  468. }
  469. #if defined(MBEDTLS_FS_IO)
  470. /*
  471. * Load all data from a file into a given buffer.
  472. *
  473. * The file is expected to contain either PEM or DER encoded data.
  474. * A terminating null byte is always appended. It is included in the announced
  475. * length only if the data looks like it is PEM encoded.
  476. */
  477. static int load_file( const char *path, unsigned char **buf, size_t *n )
  478. {
  479. FILE *f;
  480. long size;
  481. if( ( f = fopen( path, "rb" ) ) == NULL )
  482. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  483. fseek( f, 0, SEEK_END );
  484. if( ( size = ftell( f ) ) == -1 )
  485. {
  486. fclose( f );
  487. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  488. }
  489. fseek( f, 0, SEEK_SET );
  490. *n = (size_t) size;
  491. if( *n + 1 == 0 ||
  492. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  493. {
  494. fclose( f );
  495. return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
  496. }
  497. if( fread( *buf, 1, *n, f ) != *n )
  498. {
  499. fclose( f );
  500. mbedtls_platform_zeroize( *buf, *n + 1 );
  501. mbedtls_free( *buf );
  502. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  503. }
  504. fclose( f );
  505. (*buf)[*n] = '\0';
  506. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  507. ++*n;
  508. return( 0 );
  509. }
  510. /*
  511. * Load and parse DHM parameters
  512. */
  513. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
  514. {
  515. int ret;
  516. size_t n;
  517. unsigned char *buf;
  518. DHM_VALIDATE_RET( dhm != NULL );
  519. DHM_VALIDATE_RET( path != NULL );
  520. if( ( ret = load_file( path, &buf, &n ) ) != 0 )
  521. return( ret );
  522. ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
  523. mbedtls_platform_zeroize( buf, n );
  524. mbedtls_free( buf );
  525. return( ret );
  526. }
  527. #endif /* MBEDTLS_FS_IO */
  528. #endif /* MBEDTLS_ASN1_PARSE_C */
  529. #endif /* MBEDTLS_DHM_ALT */
  530. #if defined(MBEDTLS_SELF_TEST)
  531. #if defined(MBEDTLS_PEM_PARSE_C)
  532. static const char mbedtls_test_dhm_params[] =
  533. "-----BEGIN DH PARAMETERS-----\r\n"
  534. "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
  535. "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
  536. "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
  537. "-----END DH PARAMETERS-----\r\n";
  538. #else /* MBEDTLS_PEM_PARSE_C */
  539. static const char mbedtls_test_dhm_params[] = {
  540. 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9e, 0x35, 0xf4, 0x30, 0x44,
  541. 0x3a, 0x09, 0x90, 0x4f, 0x3a, 0x39, 0xa9, 0x79, 0x79, 0x7d, 0x07, 0x0d,
  542. 0xf5, 0x33, 0x78, 0xe7, 0x9c, 0x24, 0x38, 0xbe, 0xf4, 0xe7, 0x61, 0xf3,
  543. 0xc7, 0x14, 0x55, 0x33, 0x28, 0x58, 0x9b, 0x04, 0x1c, 0x80, 0x9b, 0xe1,
  544. 0xd6, 0xc6, 0xb5, 0xf1, 0xfc, 0x9f, 0x47, 0xd3, 0xa2, 0x54, 0x43, 0x18,
  545. 0x82, 0x53, 0xa9, 0x92, 0xa5, 0x68, 0x18, 0xb3, 0x7b, 0xa9, 0xde, 0x5a,
  546. 0x40, 0xd3, 0x62, 0xe5, 0x6e, 0xff, 0x0b, 0xe5, 0x41, 0x74, 0x74, 0xc1,
  547. 0x25, 0xc1, 0x99, 0x27, 0x2c, 0x8f, 0xe4, 0x1d, 0xea, 0x73, 0x3d, 0xf6,
  548. 0xf6, 0x62, 0xc9, 0x2a, 0xe7, 0x65, 0x56, 0xe7, 0x55, 0xd1, 0x0c, 0x64,
  549. 0xe6, 0xa5, 0x09, 0x68, 0xf6, 0x7f, 0xc6, 0xea, 0x73, 0xd0, 0xdc, 0xa8,
  550. 0x56, 0x9b, 0xe2, 0xba, 0x20, 0x4e, 0x23, 0x58, 0x0d, 0x8b, 0xca, 0x2f,
  551. 0x49, 0x75, 0xb3, 0x02, 0x01, 0x02 };
  552. #endif /* MBEDTLS_PEM_PARSE_C */
  553. static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
  554. /*
  555. * Checkup routine
  556. */
  557. int mbedtls_dhm_self_test( int verbose )
  558. {
  559. int ret;
  560. mbedtls_dhm_context dhm;
  561. mbedtls_dhm_init( &dhm );
  562. if( verbose != 0 )
  563. mbedtls_printf( " DHM parameter load: " );
  564. if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
  565. (const unsigned char *) mbedtls_test_dhm_params,
  566. mbedtls_test_dhm_params_len ) ) != 0 )
  567. {
  568. if( verbose != 0 )
  569. mbedtls_printf( "failed\n" );
  570. ret = 1;
  571. goto exit;
  572. }
  573. if( verbose != 0 )
  574. mbedtls_printf( "passed\n\n" );
  575. exit:
  576. mbedtls_dhm_free( &dhm );
  577. return( ret );
  578. }
  579. #endif /* MBEDTLS_SELF_TEST */
  580. #endif /* MBEDTLS_DHM_C */