dhm.c 18 KB

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