dhm.c 16 KB

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