dhm.c 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661
  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. /*
  55. * helper to validate the mbedtls_mpi size and import it
  56. */
  57. static int dhm_read_bignum( mbedtls_mpi *X,
  58. unsigned char **p,
  59. const unsigned char *end )
  60. {
  61. int ret, n;
  62. if( end - *p < 2 )
  63. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  64. n = ( (*p)[0] << 8 ) | (*p)[1];
  65. (*p) += 2;
  66. if( (int)( end - *p ) < n )
  67. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  68. if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
  69. return( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED + ret );
  70. (*p) += n;
  71. return( 0 );
  72. }
  73. /*
  74. * Verify sanity of parameter with regards to P
  75. *
  76. * Parameter should be: 2 <= public_param <= P - 2
  77. *
  78. * This means that we need to return an error if
  79. * public_param < 2 or public_param > P-2
  80. *
  81. * For more information on the attack, see:
  82. * http://www.cl.cam.ac.uk/~rja14/Papers/psandqs.pdf
  83. * http://web.nvd.nist.gov/view/vuln/detail?vulnId=CVE-2005-2643
  84. */
  85. static int dhm_check_range( const mbedtls_mpi *param, const mbedtls_mpi *P )
  86. {
  87. mbedtls_mpi L, U;
  88. int ret = 0;
  89. mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
  90. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
  91. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
  92. if( mbedtls_mpi_cmp_mpi( param, &L ) < 0 ||
  93. mbedtls_mpi_cmp_mpi( param, &U ) > 0 )
  94. {
  95. ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  96. }
  97. cleanup:
  98. mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
  99. return( ret );
  100. }
  101. void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
  102. {
  103. memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
  104. }
  105. /*
  106. * Parse the ServerKeyExchange parameters
  107. */
  108. int mbedtls_dhm_read_params( mbedtls_dhm_context *ctx,
  109. unsigned char **p,
  110. const unsigned char *end )
  111. {
  112. int ret;
  113. if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
  114. ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
  115. ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
  116. return( ret );
  117. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  118. return( ret );
  119. ctx->len = mbedtls_mpi_size( &ctx->P );
  120. return( 0 );
  121. }
  122. /*
  123. * Setup and write the ServerKeyExchange parameters
  124. */
  125. int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
  126. unsigned char *output, size_t *olen,
  127. int (*f_rng)(void *, unsigned char *, size_t),
  128. void *p_rng )
  129. {
  130. int ret, count = 0;
  131. size_t n1, n2, n3;
  132. unsigned char *p;
  133. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  134. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  135. /*
  136. * Generate X as large as possible ( < P )
  137. */
  138. do
  139. {
  140. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  141. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  142. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  143. if( count++ > 10 )
  144. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
  145. }
  146. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  147. /*
  148. * Calculate GX = G^X mod P
  149. */
  150. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  151. &ctx->P , &ctx->RP ) );
  152. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  153. return( ret );
  154. /*
  155. * export P, G, GX
  156. */
  157. #define DHM_MPI_EXPORT( X, n ) \
  158. do { \
  159. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( ( X ), \
  160. p + 2, \
  161. ( n ) ) ); \
  162. *p++ = (unsigned char)( ( n ) >> 8 ); \
  163. *p++ = (unsigned char)( ( n ) ); \
  164. p += ( n ); \
  165. } while( 0 )
  166. n1 = mbedtls_mpi_size( &ctx->P );
  167. n2 = mbedtls_mpi_size( &ctx->G );
  168. n3 = mbedtls_mpi_size( &ctx->GX );
  169. p = output;
  170. DHM_MPI_EXPORT( &ctx->P , n1 );
  171. DHM_MPI_EXPORT( &ctx->G , n2 );
  172. DHM_MPI_EXPORT( &ctx->GX, n3 );
  173. *olen = p - output;
  174. ctx->len = n1;
  175. cleanup:
  176. if( ret != 0 )
  177. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
  178. return( 0 );
  179. }
  180. /*
  181. * Set prime modulus and generator
  182. */
  183. int mbedtls_dhm_set_group( mbedtls_dhm_context *ctx,
  184. const mbedtls_mpi *P,
  185. const mbedtls_mpi *G )
  186. {
  187. int ret;
  188. if( ctx == NULL || P == NULL || G == NULL )
  189. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  190. if( ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ||
  191. ( ret = mbedtls_mpi_copy( &ctx->G, G ) ) != 0 )
  192. {
  193. return( MBEDTLS_ERR_DHM_SET_GROUP_FAILED + ret );
  194. }
  195. ctx->len = mbedtls_mpi_size( &ctx->P );
  196. return( 0 );
  197. }
  198. /*
  199. * Import the peer's public value G^Y
  200. */
  201. int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
  202. const unsigned char *input, size_t ilen )
  203. {
  204. int ret;
  205. if( ctx == NULL || ilen < 1 || ilen > ctx->len )
  206. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  207. if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  208. return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
  209. return( 0 );
  210. }
  211. /*
  212. * Create own private value X and export G^X
  213. */
  214. int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
  215. unsigned char *output, size_t olen,
  216. int (*f_rng)(void *, unsigned char *, size_t),
  217. void *p_rng )
  218. {
  219. int ret, count = 0;
  220. if( ctx == NULL || olen < 1 || olen > ctx->len )
  221. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  222. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  223. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  224. /*
  225. * generate X and calculate GX = G^X mod P
  226. */
  227. do
  228. {
  229. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  230. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  231. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  232. if( count++ > 10 )
  233. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
  234. }
  235. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  236. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  237. &ctx->P , &ctx->RP ) );
  238. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  239. return( ret );
  240. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
  241. cleanup:
  242. if( ret != 0 )
  243. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED + ret );
  244. return( 0 );
  245. }
  246. /*
  247. * Use the blinding method and optimisation suggested in section 10 of:
  248. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  249. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  250. * Berlin Heidelberg, 1996. p. 104-113.
  251. */
  252. static int dhm_update_blinding( mbedtls_dhm_context *ctx,
  253. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  254. {
  255. int ret, count;
  256. /*
  257. * Don't use any blinding the first time a particular X is used,
  258. * but remember it to use blinding next time.
  259. */
  260. if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
  261. {
  262. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
  263. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
  264. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
  265. return( 0 );
  266. }
  267. /*
  268. * Ok, we need blinding. Can we re-use existing values?
  269. * If yes, just update them by squaring them.
  270. */
  271. if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
  272. {
  273. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  274. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
  275. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  276. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  277. return( 0 );
  278. }
  279. /*
  280. * We need to generate blinding values from scratch
  281. */
  282. /* Vi = random( 2, P-1 ) */
  283. count = 0;
  284. do
  285. {
  286. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng ) );
  287. while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
  288. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
  289. if( count++ > 10 )
  290. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  291. }
  292. while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
  293. /* Vf = Vi^-X mod P */
  294. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
  295. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
  296. cleanup:
  297. return( ret );
  298. }
  299. /*
  300. * Derive and export the shared secret (G^Y)^X mod P
  301. */
  302. int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  303. unsigned char *output, size_t output_size, size_t *olen,
  304. int (*f_rng)(void *, unsigned char *, size_t),
  305. void *p_rng )
  306. {
  307. int ret;
  308. mbedtls_mpi GYb;
  309. if( ctx == NULL || output_size < ctx->len )
  310. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  311. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  312. return( ret );
  313. mbedtls_mpi_init( &GYb );
  314. /* Blind peer's value */
  315. if( f_rng != NULL )
  316. {
  317. MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
  318. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
  319. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
  320. }
  321. else
  322. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
  323. /* Do modular exponentiation */
  324. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
  325. &ctx->P, &ctx->RP ) );
  326. /* Unblind secret value */
  327. if( f_rng != NULL )
  328. {
  329. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
  330. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
  331. }
  332. *olen = mbedtls_mpi_size( &ctx->K );
  333. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
  334. cleanup:
  335. mbedtls_mpi_free( &GYb );
  336. if( ret != 0 )
  337. return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
  338. return( 0 );
  339. }
  340. /*
  341. * Free the components of a DHM key
  342. */
  343. void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
  344. {
  345. mbedtls_mpi_free( &ctx->pX ); mbedtls_mpi_free( &ctx->Vf );
  346. mbedtls_mpi_free( &ctx->Vi ); mbedtls_mpi_free( &ctx->RP );
  347. mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY );
  348. mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X );
  349. mbedtls_mpi_free( &ctx->G ); mbedtls_mpi_free( &ctx->P );
  350. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
  351. }
  352. #if defined(MBEDTLS_ASN1_PARSE_C)
  353. /*
  354. * Parse DHM parameters
  355. */
  356. int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  357. size_t dhminlen )
  358. {
  359. int ret;
  360. size_t len;
  361. unsigned char *p, *end;
  362. #if defined(MBEDTLS_PEM_PARSE_C)
  363. mbedtls_pem_context pem;
  364. mbedtls_pem_init( &pem );
  365. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  366. if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
  367. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  368. else
  369. ret = mbedtls_pem_read_buffer( &pem,
  370. "-----BEGIN DH PARAMETERS-----",
  371. "-----END DH PARAMETERS-----",
  372. dhmin, NULL, 0, &dhminlen );
  373. if( ret == 0 )
  374. {
  375. /*
  376. * Was PEM encoded
  377. */
  378. dhminlen = pem.buflen;
  379. }
  380. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  381. goto exit;
  382. p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
  383. #else
  384. p = (unsigned char *) dhmin;
  385. #endif /* MBEDTLS_PEM_PARSE_C */
  386. end = p + dhminlen;
  387. /*
  388. * DHParams ::= SEQUENCE {
  389. * prime INTEGER, -- P
  390. * generator INTEGER, -- g
  391. * privateValueLength INTEGER OPTIONAL
  392. * }
  393. */
  394. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  395. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  396. {
  397. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  398. goto exit;
  399. }
  400. end = p + len;
  401. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
  402. ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
  403. {
  404. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  405. goto exit;
  406. }
  407. if( p != end )
  408. {
  409. /* This might be the optional privateValueLength.
  410. * If so, we can cleanly discard it */
  411. mbedtls_mpi rec;
  412. mbedtls_mpi_init( &rec );
  413. ret = mbedtls_asn1_get_mpi( &p, end, &rec );
  414. mbedtls_mpi_free( &rec );
  415. if ( ret != 0 )
  416. {
  417. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  418. goto exit;
  419. }
  420. if ( p != end )
  421. {
  422. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
  423. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  424. goto exit;
  425. }
  426. }
  427. ret = 0;
  428. dhm->len = mbedtls_mpi_size( &dhm->P );
  429. exit:
  430. #if defined(MBEDTLS_PEM_PARSE_C)
  431. mbedtls_pem_free( &pem );
  432. #endif
  433. if( ret != 0 )
  434. mbedtls_dhm_free( dhm );
  435. return( ret );
  436. }
  437. #if defined(MBEDTLS_FS_IO)
  438. /*
  439. * Load all data from a file into a given buffer.
  440. *
  441. * The file is expected to contain either PEM or DER encoded data.
  442. * A terminating null byte is always appended. It is included in the announced
  443. * length only if the data looks like it is PEM encoded.
  444. */
  445. static int load_file( const char *path, unsigned char **buf, size_t *n )
  446. {
  447. FILE *f;
  448. long size;
  449. if( ( f = fopen( path, "rb" ) ) == NULL )
  450. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  451. fseek( f, 0, SEEK_END );
  452. if( ( size = ftell( f ) ) == -1 )
  453. {
  454. fclose( f );
  455. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  456. }
  457. fseek( f, 0, SEEK_SET );
  458. *n = (size_t) size;
  459. if( *n + 1 == 0 ||
  460. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  461. {
  462. fclose( f );
  463. return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
  464. }
  465. if( fread( *buf, 1, *n, f ) != *n )
  466. {
  467. fclose( f );
  468. mbedtls_platform_zeroize( *buf, *n + 1 );
  469. mbedtls_free( *buf );
  470. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  471. }
  472. fclose( f );
  473. (*buf)[*n] = '\0';
  474. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  475. ++*n;
  476. return( 0 );
  477. }
  478. /*
  479. * Load and parse DHM parameters
  480. */
  481. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
  482. {
  483. int ret;
  484. size_t n;
  485. unsigned char *buf;
  486. if( ( ret = load_file( path, &buf, &n ) ) != 0 )
  487. return( ret );
  488. ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
  489. mbedtls_platform_zeroize( buf, n );
  490. mbedtls_free( buf );
  491. return( ret );
  492. }
  493. #endif /* MBEDTLS_FS_IO */
  494. #endif /* MBEDTLS_ASN1_PARSE_C */
  495. #endif /* MBEDTLS_DHM_ALT */
  496. #if defined(MBEDTLS_SELF_TEST)
  497. static const char mbedtls_test_dhm_params[] =
  498. "-----BEGIN DH PARAMETERS-----\r\n"
  499. "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
  500. "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
  501. "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
  502. "-----END DH PARAMETERS-----\r\n";
  503. static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
  504. /*
  505. * Checkup routine
  506. */
  507. int mbedtls_dhm_self_test( int verbose )
  508. {
  509. int ret;
  510. mbedtls_dhm_context dhm;
  511. mbedtls_dhm_init( &dhm );
  512. if( verbose != 0 )
  513. mbedtls_printf( " DHM parameter load: " );
  514. if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
  515. (const unsigned char *) mbedtls_test_dhm_params,
  516. mbedtls_test_dhm_params_len ) ) != 0 )
  517. {
  518. if( verbose != 0 )
  519. mbedtls_printf( "failed\n" );
  520. ret = 1;
  521. goto exit;
  522. }
  523. if( verbose != 0 )
  524. mbedtls_printf( "passed\n\n" );
  525. exit:
  526. mbedtls_dhm_free( &dhm );
  527. return( ret );
  528. }
  529. #endif /* MBEDTLS_SELF_TEST */
  530. #endif /* MBEDTLS_DHM_C */