dhm.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625
  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. #if defined(MBEDTLS_PEM_PARSE_C)
  37. #include "mbedtls/pem.h"
  38. #endif
  39. #if defined(MBEDTLS_ASN1_PARSE_C)
  40. #include "mbedtls/asn1.h"
  41. #endif
  42. #if defined(MBEDTLS_PLATFORM_C)
  43. #include "mbedtls/platform.h"
  44. #else
  45. #include <stdlib.h>
  46. #include <stdio.h>
  47. #define mbedtls_printf printf
  48. #define mbedtls_calloc calloc
  49. #define mbedtls_free free
  50. #endif
  51. /* Implementation that should never be optimized out by the compiler */
  52. static void mbedtls_zeroize( void *v, size_t n ) {
  53. volatile unsigned char *p = v; while( n-- ) *p++ = 0;
  54. }
  55. /*
  56. * helper to validate the mbedtls_mpi size and import it
  57. */
  58. static int dhm_read_bignum( mbedtls_mpi *X,
  59. unsigned char **p,
  60. const unsigned char *end )
  61. {
  62. int ret, n;
  63. if( end - *p < 2 )
  64. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  65. n = ( (*p)[0] << 8 ) | (*p)[1];
  66. (*p) += 2;
  67. if( (int)( end - *p ) < n )
  68. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  69. if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
  70. return( MBEDTLS_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 mbedtls_mpi *param, const mbedtls_mpi *P )
  84. {
  85. mbedtls_mpi L, U;
  86. int ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  87. mbedtls_mpi_init( &L ); mbedtls_mpi_init( &U );
  88. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &L, 2 ) );
  89. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
  90. if( mbedtls_mpi_cmp_mpi( param, &L ) >= 0 &&
  91. mbedtls_mpi_cmp_mpi( param, &U ) <= 0 )
  92. {
  93. ret = 0;
  94. }
  95. cleanup:
  96. mbedtls_mpi_free( &L ); mbedtls_mpi_free( &U );
  97. return( ret );
  98. }
  99. void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
  100. {
  101. memset( ctx, 0, sizeof( mbedtls_dhm_context ) );
  102. }
  103. /*
  104. * Parse the ServerKeyExchange parameters
  105. */
  106. int mbedtls_dhm_read_params( mbedtls_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 = mbedtls_mpi_size( &ctx->P );
  118. return( 0 );
  119. }
  120. /*
  121. * Setup and write the ServerKeyExchange parameters
  122. */
  123. int mbedtls_dhm_make_params( mbedtls_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( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  132. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  133. /*
  134. * Generate X as large as possible ( < P )
  135. */
  136. do
  137. {
  138. mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
  139. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  140. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  141. if( count++ > 10 )
  142. return( MBEDTLS_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. MBEDTLS_MPI_CHK( mbedtls_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. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( X, p + 2, n ) ); \
  157. *p++ = (unsigned char)( n >> 8 ); \
  158. *p++ = (unsigned char)( n ); p += n;
  159. n1 = mbedtls_mpi_size( &ctx->P );
  160. n2 = mbedtls_mpi_size( &ctx->G );
  161. n3 = mbedtls_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( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED + ret );
  171. return( 0 );
  172. }
  173. /*
  174. * Import the peer's public value G^Y
  175. */
  176. int mbedtls_dhm_read_public( mbedtls_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( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  182. if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  183. return( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED + ret );
  184. return( 0 );
  185. }
  186. /*
  187. * Create own private value X and export G^X
  188. */
  189. int mbedtls_dhm_make_public( mbedtls_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( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  197. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  198. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  199. /*
  200. * generate X and calculate GX = G^X mod P
  201. */
  202. do
  203. {
  204. mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng );
  205. while( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->P ) >= 0 )
  206. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->X, 1 ) );
  207. if( count++ > 10 )
  208. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
  209. }
  210. while( dhm_check_range( &ctx->X, &ctx->P ) != 0 );
  211. MBEDTLS_MPI_CHK( mbedtls_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. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
  216. cleanup:
  217. if( ret != 0 )
  218. return( MBEDTLS_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( mbedtls_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( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
  236. {
  237. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
  238. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
  239. MBEDTLS_MPI_CHK( mbedtls_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( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
  247. {
  248. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  249. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
  250. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  251. MBEDTLS_MPI_CHK( mbedtls_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. mbedtls_mpi_fill_random( &ctx->Vi, mbedtls_mpi_size( &ctx->P ), f_rng, p_rng );
  262. while( mbedtls_mpi_cmp_mpi( &ctx->Vi, &ctx->P ) >= 0 )
  263. MBEDTLS_MPI_CHK( mbedtls_mpi_shift_r( &ctx->Vi, 1 ) );
  264. if( count++ > 10 )
  265. return( MBEDTLS_ERR_MPI_NOT_ACCEPTABLE );
  266. }
  267. while( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) <= 0 );
  268. /* Vf = Vi^-X mod P */
  269. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vi, &ctx->P ) );
  270. MBEDTLS_MPI_CHK( mbedtls_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 mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  278. unsigned char *output, size_t output_size, size_t *olen,
  279. int (*f_rng)(void *, unsigned char *, size_t),
  280. void *p_rng )
  281. {
  282. int ret;
  283. mbedtls_mpi GYb;
  284. if( ctx == NULL || output_size < ctx->len )
  285. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  286. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  287. return( ret );
  288. mbedtls_mpi_init( &GYb );
  289. /* Blind peer's value */
  290. if( f_rng != NULL )
  291. {
  292. MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
  293. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
  294. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
  295. }
  296. else
  297. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
  298. /* Do modular exponentiation */
  299. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
  300. &ctx->P, &ctx->RP ) );
  301. /* Unblind secret value */
  302. if( f_rng != NULL )
  303. {
  304. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
  305. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
  306. }
  307. *olen = mbedtls_mpi_size( &ctx->K );
  308. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
  309. cleanup:
  310. mbedtls_mpi_free( &GYb );
  311. if( ret != 0 )
  312. return( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED + ret );
  313. return( 0 );
  314. }
  315. /*
  316. * Free the components of a DHM key
  317. */
  318. void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
  319. {
  320. mbedtls_mpi_free( &ctx->pX); mbedtls_mpi_free( &ctx->Vf ); mbedtls_mpi_free( &ctx->Vi );
  321. mbedtls_mpi_free( &ctx->RP ); mbedtls_mpi_free( &ctx->K ); mbedtls_mpi_free( &ctx->GY );
  322. mbedtls_mpi_free( &ctx->GX ); mbedtls_mpi_free( &ctx->X ); mbedtls_mpi_free( &ctx->G );
  323. mbedtls_mpi_free( &ctx->P );
  324. mbedtls_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
  325. }
  326. #if defined(MBEDTLS_ASN1_PARSE_C)
  327. /*
  328. * Parse DHM parameters
  329. */
  330. int mbedtls_dhm_parse_dhm( mbedtls_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(MBEDTLS_PEM_PARSE_C)
  337. mbedtls_pem_context pem;
  338. mbedtls_pem_init( &pem );
  339. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  340. if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
  341. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  342. else
  343. ret = mbedtls_pem_read_buffer( &pem,
  344. "-----BEGIN DH PARAMETERS-----",
  345. "-----END DH PARAMETERS-----",
  346. dhmin, NULL, 0, &dhminlen );
  347. if( ret == 0 )
  348. {
  349. /*
  350. * Was PEM encoded
  351. */
  352. dhminlen = pem.buflen;
  353. }
  354. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  355. goto exit;
  356. p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
  357. #else
  358. p = (unsigned char *) dhmin;
  359. #endif /* MBEDTLS_PEM_PARSE_C */
  360. end = p + dhminlen;
  361. /*
  362. * DHParams ::= SEQUENCE {
  363. * prime INTEGER, -- P
  364. * generator INTEGER, -- g
  365. * privateValueLength INTEGER OPTIONAL
  366. * }
  367. */
  368. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  369. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  370. {
  371. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  372. goto exit;
  373. }
  374. end = p + len;
  375. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
  376. ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
  377. {
  378. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  379. goto exit;
  380. }
  381. if( p != end )
  382. {
  383. /* This might be the optional privateValueLength.
  384. * If so, we can cleanly discard it */
  385. mbedtls_mpi rec;
  386. mbedtls_mpi_init( &rec );
  387. ret = mbedtls_asn1_get_mpi( &p, end, &rec );
  388. mbedtls_mpi_free( &rec );
  389. if ( ret != 0 )
  390. {
  391. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT + ret;
  392. goto exit;
  393. }
  394. if ( p != end )
  395. {
  396. ret = MBEDTLS_ERR_DHM_INVALID_FORMAT +
  397. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH;
  398. goto exit;
  399. }
  400. }
  401. ret = 0;
  402. dhm->len = mbedtls_mpi_size( &dhm->P );
  403. exit:
  404. #if defined(MBEDTLS_PEM_PARSE_C)
  405. mbedtls_pem_free( &pem );
  406. #endif
  407. if( ret != 0 )
  408. mbedtls_dhm_free( dhm );
  409. return( ret );
  410. }
  411. #if defined(MBEDTLS_FS_IO)
  412. /*
  413. * Load all data from a file into a given buffer.
  414. *
  415. * The file is expected to contain either PEM or DER encoded data.
  416. * A terminating null byte is always appended. It is included in the announced
  417. * length only if the data looks like it is PEM encoded.
  418. */
  419. static int load_file( const char *path, unsigned char **buf, size_t *n )
  420. {
  421. FILE *f;
  422. long size;
  423. if( ( f = fopen( path, "rb" ) ) == NULL )
  424. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  425. fseek( f, 0, SEEK_END );
  426. if( ( size = ftell( f ) ) == -1 )
  427. {
  428. fclose( f );
  429. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  430. }
  431. fseek( f, 0, SEEK_SET );
  432. *n = (size_t) size;
  433. if( *n + 1 == 0 ||
  434. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  435. {
  436. fclose( f );
  437. return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
  438. }
  439. if( fread( *buf, 1, *n, f ) != *n )
  440. {
  441. fclose( f );
  442. mbedtls_free( *buf );
  443. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  444. }
  445. fclose( f );
  446. (*buf)[*n] = '\0';
  447. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  448. ++*n;
  449. return( 0 );
  450. }
  451. /*
  452. * Load and parse DHM parameters
  453. */
  454. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
  455. {
  456. int ret;
  457. size_t n;
  458. unsigned char *buf;
  459. if( ( ret = load_file( path, &buf, &n ) ) != 0 )
  460. return( ret );
  461. ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
  462. mbedtls_zeroize( buf, n );
  463. mbedtls_free( buf );
  464. return( ret );
  465. }
  466. #endif /* MBEDTLS_FS_IO */
  467. #endif /* MBEDTLS_ASN1_PARSE_C */
  468. #if defined(MBEDTLS_SELF_TEST)
  469. static const char mbedtls_test_dhm_params[] =
  470. "-----BEGIN DH PARAMETERS-----\r\n"
  471. "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
  472. "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
  473. "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
  474. "-----END DH PARAMETERS-----\r\n";
  475. static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
  476. /*
  477. * Checkup routine
  478. */
  479. int mbedtls_dhm_self_test( int verbose )
  480. {
  481. int ret;
  482. mbedtls_dhm_context dhm;
  483. mbedtls_dhm_init( &dhm );
  484. if( verbose != 0 )
  485. mbedtls_printf( " DHM parameter load: " );
  486. if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
  487. (const unsigned char *) mbedtls_test_dhm_params,
  488. mbedtls_test_dhm_params_len ) ) != 0 )
  489. {
  490. if( verbose != 0 )
  491. mbedtls_printf( "failed\n" );
  492. ret = 1;
  493. goto exit;
  494. }
  495. if( verbose != 0 )
  496. mbedtls_printf( "passed\n\n" );
  497. exit:
  498. mbedtls_dhm_free( &dhm );
  499. return( ret );
  500. }
  501. #endif /* MBEDTLS_SELF_TEST */
  502. #endif /* MBEDTLS_DHM_C */