dhm.c 16 KB

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