dhm.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720
  1. /*
  2. * Diffie-Hellman-Merkle key exchange
  3. *
  4. * Copyright The Mbed TLS Contributors
  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. /*
  20. * The following sources were referenced in the design of this implementation
  21. * of the Diffie-Hellman-Merkle algorithm:
  22. *
  23. * [1] Handbook of Applied Cryptography - 1997, Chapter 12
  24. * Menezes, van Oorschot and Vanstone
  25. *
  26. */
  27. #include "common.h"
  28. #if defined(MBEDTLS_DHM_C)
  29. #include "mbedtls/dhm.h"
  30. #include "mbedtls/platform_util.h"
  31. #include "mbedtls/error.h"
  32. #include <string.h>
  33. #if defined(MBEDTLS_PEM_PARSE_C)
  34. #include "mbedtls/pem.h"
  35. #endif
  36. #if defined(MBEDTLS_ASN1_PARSE_C)
  37. #include "mbedtls/asn1.h"
  38. #endif
  39. #if defined(MBEDTLS_PLATFORM_C)
  40. #include "mbedtls/platform.h"
  41. #else
  42. #include <stdlib.h>
  43. #include <stdio.h>
  44. #define mbedtls_printf printf
  45. #define mbedtls_calloc calloc
  46. #define mbedtls_free free
  47. #endif
  48. #if !defined(MBEDTLS_DHM_ALT)
  49. #define DHM_VALIDATE_RET( cond ) \
  50. MBEDTLS_INTERNAL_VALIDATE_RET( cond, MBEDTLS_ERR_DHM_BAD_INPUT_DATA )
  51. #define DHM_VALIDATE( cond ) \
  52. MBEDTLS_INTERNAL_VALIDATE( cond )
  53. /*
  54. * helper to validate the mbedtls_mpi size and import it
  55. */
  56. static int dhm_read_bignum( mbedtls_mpi *X,
  57. unsigned char **p,
  58. const unsigned char *end )
  59. {
  60. int ret, n;
  61. if( end - *p < 2 )
  62. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  63. n = ( (*p)[0] << 8 ) | (*p)[1];
  64. (*p) += 2;
  65. if( (int)( end - *p ) < n )
  66. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  67. if( ( ret = mbedtls_mpi_read_binary( X, *p, n ) ) != 0 )
  68. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_READ_PARAMS_FAILED, ret ) );
  69. (*p) += n;
  70. return( 0 );
  71. }
  72. /*
  73. * Verify sanity of parameter with regards to P
  74. *
  75. * Parameter should be: 2 <= public_param <= P - 2
  76. *
  77. * This means that we need to return an error if
  78. * public_param < 2 or 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 U;
  87. int ret = 0;
  88. mbedtls_mpi_init( &U );
  89. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( &U, P, 2 ) );
  90. if( mbedtls_mpi_cmp_int( param, 2 ) < 0 ||
  91. mbedtls_mpi_cmp_mpi( param, &U ) > 0 )
  92. {
  93. ret = MBEDTLS_ERR_DHM_BAD_INPUT_DATA;
  94. }
  95. cleanup:
  96. mbedtls_mpi_free( &U );
  97. return( ret );
  98. }
  99. void mbedtls_dhm_init( mbedtls_dhm_context *ctx )
  100. {
  101. DHM_VALIDATE( ctx != NULL );
  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 = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  112. DHM_VALIDATE_RET( ctx != NULL );
  113. DHM_VALIDATE_RET( p != NULL && *p != NULL );
  114. DHM_VALIDATE_RET( end != NULL );
  115. if( ( ret = dhm_read_bignum( &ctx->P, p, end ) ) != 0 ||
  116. ( ret = dhm_read_bignum( &ctx->G, p, end ) ) != 0 ||
  117. ( ret = dhm_read_bignum( &ctx->GY, p, end ) ) != 0 )
  118. return( ret );
  119. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  120. return( ret );
  121. ctx->len = mbedtls_mpi_size( &ctx->P );
  122. return( 0 );
  123. }
  124. /*
  125. * Pick a random R in the range [2, M-2] for blinding or key generation.
  126. */
  127. static int dhm_random_below( mbedtls_mpi *R, const mbedtls_mpi *M,
  128. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  129. {
  130. int ret;
  131. MBEDTLS_MPI_CHK( mbedtls_mpi_random( R, 3, M, f_rng, p_rng ) );
  132. MBEDTLS_MPI_CHK( mbedtls_mpi_sub_int( R, R, 1 ) );
  133. cleanup:
  134. return( ret );
  135. }
  136. static int dhm_make_common( mbedtls_dhm_context *ctx, int x_size,
  137. int (*f_rng)(void *, unsigned char *, size_t),
  138. void *p_rng )
  139. {
  140. int ret = 0;
  141. if( mbedtls_mpi_cmp_int( &ctx->P, 0 ) == 0 )
  142. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  143. if( x_size < 0 )
  144. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  145. if( (unsigned) x_size < mbedtls_mpi_size( &ctx->P ) )
  146. {
  147. MBEDTLS_MPI_CHK( mbedtls_mpi_fill_random( &ctx->X, x_size, f_rng, p_rng ) );
  148. }
  149. else
  150. {
  151. /* Generate X as large as possible ( <= P - 2 ) */
  152. ret = dhm_random_below( &ctx->X, &ctx->P, f_rng, p_rng );
  153. if( ret == MBEDTLS_ERR_MPI_NOT_ACCEPTABLE )
  154. return( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED );
  155. if( ret != 0 )
  156. return( ret );
  157. }
  158. /*
  159. * Calculate GX = G^X mod P
  160. */
  161. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->GX, &ctx->G, &ctx->X,
  162. &ctx->P , &ctx->RP ) );
  163. if( ( ret = dhm_check_range( &ctx->GX, &ctx->P ) ) != 0 )
  164. return( ret );
  165. cleanup:
  166. return( ret );
  167. }
  168. /*
  169. * Setup and write the ServerKeyExchange parameters
  170. */
  171. int mbedtls_dhm_make_params( mbedtls_dhm_context *ctx, int x_size,
  172. unsigned char *output, size_t *olen,
  173. int (*f_rng)(void *, unsigned char *, size_t),
  174. void *p_rng )
  175. {
  176. int ret;
  177. size_t n1, n2, n3;
  178. unsigned char *p;
  179. DHM_VALIDATE_RET( ctx != NULL );
  180. DHM_VALIDATE_RET( output != NULL );
  181. DHM_VALIDATE_RET( olen != NULL );
  182. DHM_VALIDATE_RET( f_rng != NULL );
  183. ret = dhm_make_common( ctx, x_size, f_rng, p_rng );
  184. if( ret != 0 )
  185. goto cleanup;
  186. /*
  187. * Export P, G, GX. RFC 5246 §4.4 states that "leading zero octets are
  188. * not required". We omit leading zeros for compactness.
  189. */
  190. #define DHM_MPI_EXPORT( X, n ) \
  191. do { \
  192. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( ( X ), \
  193. p + 2, \
  194. ( n ) ) ); \
  195. *p++ = MBEDTLS_BYTE_1( n ); \
  196. *p++ = MBEDTLS_BYTE_0( n ); \
  197. p += ( n ); \
  198. } while( 0 )
  199. n1 = mbedtls_mpi_size( &ctx->P );
  200. n2 = mbedtls_mpi_size( &ctx->G );
  201. n3 = mbedtls_mpi_size( &ctx->GX );
  202. p = output;
  203. DHM_MPI_EXPORT( &ctx->P , n1 );
  204. DHM_MPI_EXPORT( &ctx->G , n2 );
  205. DHM_MPI_EXPORT( &ctx->GX, n3 );
  206. *olen = p - output;
  207. ctx->len = n1;
  208. cleanup:
  209. if( ret != 0 && ret > -128 )
  210. ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED, ret );
  211. return( ret );
  212. }
  213. /*
  214. * Set prime modulus and generator
  215. */
  216. int mbedtls_dhm_set_group( mbedtls_dhm_context *ctx,
  217. const mbedtls_mpi *P,
  218. const mbedtls_mpi *G )
  219. {
  220. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  221. DHM_VALIDATE_RET( ctx != NULL );
  222. DHM_VALIDATE_RET( P != NULL );
  223. DHM_VALIDATE_RET( G != NULL );
  224. if( ( ret = mbedtls_mpi_copy( &ctx->P, P ) ) != 0 ||
  225. ( ret = mbedtls_mpi_copy( &ctx->G, G ) ) != 0 )
  226. {
  227. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_SET_GROUP_FAILED, ret ) );
  228. }
  229. ctx->len = mbedtls_mpi_size( &ctx->P );
  230. return( 0 );
  231. }
  232. /*
  233. * Import the peer's public value G^Y
  234. */
  235. int mbedtls_dhm_read_public( mbedtls_dhm_context *ctx,
  236. const unsigned char *input, size_t ilen )
  237. {
  238. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  239. DHM_VALIDATE_RET( ctx != NULL );
  240. DHM_VALIDATE_RET( input != NULL );
  241. if( ilen < 1 || ilen > ctx->len )
  242. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  243. if( ( ret = mbedtls_mpi_read_binary( &ctx->GY, input, ilen ) ) != 0 )
  244. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_READ_PUBLIC_FAILED, ret ) );
  245. return( 0 );
  246. }
  247. /*
  248. * Create own private value X and export G^X
  249. */
  250. int mbedtls_dhm_make_public( mbedtls_dhm_context *ctx, int x_size,
  251. unsigned char *output, size_t olen,
  252. int (*f_rng)(void *, unsigned char *, size_t),
  253. void *p_rng )
  254. {
  255. int ret;
  256. DHM_VALIDATE_RET( ctx != NULL );
  257. DHM_VALIDATE_RET( output != NULL );
  258. DHM_VALIDATE_RET( f_rng != NULL );
  259. if( olen < 1 || olen > ctx->len )
  260. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  261. ret = dhm_make_common( ctx, x_size, f_rng, p_rng );
  262. if( ret == MBEDTLS_ERR_DHM_MAKE_PARAMS_FAILED )
  263. return( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED );
  264. if( ret != 0 )
  265. goto cleanup;
  266. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->GX, output, olen ) );
  267. cleanup:
  268. if( ret != 0 && ret > -128 )
  269. ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_MAKE_PUBLIC_FAILED, ret );
  270. return( ret );
  271. }
  272. /*
  273. * Use the blinding method and optimisation suggested in section 10 of:
  274. * KOCHER, Paul C. Timing attacks on implementations of Diffie-Hellman, RSA,
  275. * DSS, and other systems. In : Advances in Cryptology-CRYPTO'96. Springer
  276. * Berlin Heidelberg, 1996. p. 104-113.
  277. */
  278. static int dhm_update_blinding( mbedtls_dhm_context *ctx,
  279. int (*f_rng)(void *, unsigned char *, size_t), void *p_rng )
  280. {
  281. int ret;
  282. mbedtls_mpi R;
  283. mbedtls_mpi_init( &R );
  284. /*
  285. * Don't use any blinding the first time a particular X is used,
  286. * but remember it to use blinding next time.
  287. */
  288. if( mbedtls_mpi_cmp_mpi( &ctx->X, &ctx->pX ) != 0 )
  289. {
  290. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &ctx->pX, &ctx->X ) );
  291. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vi, 1 ) );
  292. MBEDTLS_MPI_CHK( mbedtls_mpi_lset( &ctx->Vf, 1 ) );
  293. return( 0 );
  294. }
  295. /*
  296. * Ok, we need blinding. Can we re-use existing values?
  297. * If yes, just update them by squaring them.
  298. */
  299. if( mbedtls_mpi_cmp_int( &ctx->Vi, 1 ) != 0 )
  300. {
  301. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vi, &ctx->Vi, &ctx->Vi ) );
  302. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vi, &ctx->Vi, &ctx->P ) );
  303. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &ctx->Vf ) );
  304. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  305. return( 0 );
  306. }
  307. /*
  308. * We need to generate blinding values from scratch
  309. */
  310. /* Vi = random( 2, P-2 ) */
  311. MBEDTLS_MPI_CHK( dhm_random_below( &ctx->Vi, &ctx->P, f_rng, p_rng ) );
  312. /* Vf = Vi^-X mod P
  313. * First compute Vi^-1 = R * (R Vi)^-1, (avoiding leaks from inv_mod),
  314. * then elevate to the Xth power. */
  315. MBEDTLS_MPI_CHK( dhm_random_below( &R, &ctx->P, f_rng, p_rng ) );
  316. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vi, &R ) );
  317. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  318. MBEDTLS_MPI_CHK( mbedtls_mpi_inv_mod( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  319. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->Vf, &ctx->Vf, &R ) );
  320. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->Vf, &ctx->Vf, &ctx->P ) );
  321. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->Vf, &ctx->Vf, &ctx->X, &ctx->P, &ctx->RP ) );
  322. cleanup:
  323. mbedtls_mpi_free( &R );
  324. return( ret );
  325. }
  326. /*
  327. * Derive and export the shared secret (G^Y)^X mod P
  328. */
  329. int mbedtls_dhm_calc_secret( mbedtls_dhm_context *ctx,
  330. unsigned char *output, size_t output_size, size_t *olen,
  331. int (*f_rng)(void *, unsigned char *, size_t),
  332. void *p_rng )
  333. {
  334. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  335. mbedtls_mpi GYb;
  336. DHM_VALIDATE_RET( ctx != NULL );
  337. DHM_VALIDATE_RET( output != NULL );
  338. DHM_VALIDATE_RET( olen != NULL );
  339. if( output_size < ctx->len )
  340. return( MBEDTLS_ERR_DHM_BAD_INPUT_DATA );
  341. if( ( ret = dhm_check_range( &ctx->GY, &ctx->P ) ) != 0 )
  342. return( ret );
  343. mbedtls_mpi_init( &GYb );
  344. /* Blind peer's value */
  345. if( f_rng != NULL )
  346. {
  347. MBEDTLS_MPI_CHK( dhm_update_blinding( ctx, f_rng, p_rng ) );
  348. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &GYb, &ctx->GY, &ctx->Vi ) );
  349. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &GYb, &GYb, &ctx->P ) );
  350. }
  351. else
  352. MBEDTLS_MPI_CHK( mbedtls_mpi_copy( &GYb, &ctx->GY ) );
  353. /* Do modular exponentiation */
  354. MBEDTLS_MPI_CHK( mbedtls_mpi_exp_mod( &ctx->K, &GYb, &ctx->X,
  355. &ctx->P, &ctx->RP ) );
  356. /* Unblind secret value */
  357. if( f_rng != NULL )
  358. {
  359. MBEDTLS_MPI_CHK( mbedtls_mpi_mul_mpi( &ctx->K, &ctx->K, &ctx->Vf ) );
  360. MBEDTLS_MPI_CHK( mbedtls_mpi_mod_mpi( &ctx->K, &ctx->K, &ctx->P ) );
  361. }
  362. /* Output the secret without any leading zero byte. This is mandatory
  363. * for TLS per RFC 5246 §8.1.2. */
  364. *olen = mbedtls_mpi_size( &ctx->K );
  365. MBEDTLS_MPI_CHK( mbedtls_mpi_write_binary( &ctx->K, output, *olen ) );
  366. cleanup:
  367. mbedtls_mpi_free( &GYb );
  368. if( ret != 0 )
  369. return( MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_CALC_SECRET_FAILED, ret ) );
  370. return( 0 );
  371. }
  372. /*
  373. * Free the components of a DHM key
  374. */
  375. void mbedtls_dhm_free( mbedtls_dhm_context *ctx )
  376. {
  377. if( ctx == NULL )
  378. return;
  379. mbedtls_mpi_free( &ctx->pX );
  380. mbedtls_mpi_free( &ctx->Vf );
  381. mbedtls_mpi_free( &ctx->Vi );
  382. mbedtls_mpi_free( &ctx->RP );
  383. mbedtls_mpi_free( &ctx->K );
  384. mbedtls_mpi_free( &ctx->GY );
  385. mbedtls_mpi_free( &ctx->GX );
  386. mbedtls_mpi_free( &ctx->X );
  387. mbedtls_mpi_free( &ctx->G );
  388. mbedtls_mpi_free( &ctx->P );
  389. mbedtls_platform_zeroize( ctx, sizeof( mbedtls_dhm_context ) );
  390. }
  391. #if defined(MBEDTLS_ASN1_PARSE_C)
  392. /*
  393. * Parse DHM parameters
  394. */
  395. int mbedtls_dhm_parse_dhm( mbedtls_dhm_context *dhm, const unsigned char *dhmin,
  396. size_t dhminlen )
  397. {
  398. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  399. size_t len;
  400. unsigned char *p, *end;
  401. #if defined(MBEDTLS_PEM_PARSE_C)
  402. mbedtls_pem_context pem;
  403. #endif /* MBEDTLS_PEM_PARSE_C */
  404. DHM_VALIDATE_RET( dhm != NULL );
  405. DHM_VALIDATE_RET( dhmin != NULL );
  406. #if defined(MBEDTLS_PEM_PARSE_C)
  407. mbedtls_pem_init( &pem );
  408. /* Avoid calling mbedtls_pem_read_buffer() on non-null-terminated string */
  409. if( dhminlen == 0 || dhmin[dhminlen - 1] != '\0' )
  410. ret = MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT;
  411. else
  412. ret = mbedtls_pem_read_buffer( &pem,
  413. "-----BEGIN DH PARAMETERS-----",
  414. "-----END DH PARAMETERS-----",
  415. dhmin, NULL, 0, &dhminlen );
  416. if( ret == 0 )
  417. {
  418. /*
  419. * Was PEM encoded
  420. */
  421. dhminlen = pem.buflen;
  422. }
  423. else if( ret != MBEDTLS_ERR_PEM_NO_HEADER_FOOTER_PRESENT )
  424. goto exit;
  425. p = ( ret == 0 ) ? pem.buf : (unsigned char *) dhmin;
  426. #else
  427. p = (unsigned char *) dhmin;
  428. #endif /* MBEDTLS_PEM_PARSE_C */
  429. end = p + dhminlen;
  430. /*
  431. * DHParams ::= SEQUENCE {
  432. * prime INTEGER, -- P
  433. * generator INTEGER, -- g
  434. * privateValueLength INTEGER OPTIONAL
  435. * }
  436. */
  437. if( ( ret = mbedtls_asn1_get_tag( &p, end, &len,
  438. MBEDTLS_ASN1_CONSTRUCTED | MBEDTLS_ASN1_SEQUENCE ) ) != 0 )
  439. {
  440. ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_INVALID_FORMAT, ret );
  441. goto exit;
  442. }
  443. end = p + len;
  444. if( ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->P ) ) != 0 ||
  445. ( ret = mbedtls_asn1_get_mpi( &p, end, &dhm->G ) ) != 0 )
  446. {
  447. ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_INVALID_FORMAT, ret );
  448. goto exit;
  449. }
  450. if( p != end )
  451. {
  452. /* This might be the optional privateValueLength.
  453. * If so, we can cleanly discard it */
  454. mbedtls_mpi rec;
  455. mbedtls_mpi_init( &rec );
  456. ret = mbedtls_asn1_get_mpi( &p, end, &rec );
  457. mbedtls_mpi_free( &rec );
  458. if ( ret != 0 )
  459. {
  460. ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_INVALID_FORMAT, ret );
  461. goto exit;
  462. }
  463. if ( p != end )
  464. {
  465. ret = MBEDTLS_ERROR_ADD( MBEDTLS_ERR_DHM_INVALID_FORMAT,
  466. MBEDTLS_ERR_ASN1_LENGTH_MISMATCH );
  467. goto exit;
  468. }
  469. }
  470. ret = 0;
  471. dhm->len = mbedtls_mpi_size( &dhm->P );
  472. exit:
  473. #if defined(MBEDTLS_PEM_PARSE_C)
  474. mbedtls_pem_free( &pem );
  475. #endif
  476. if( ret != 0 )
  477. mbedtls_dhm_free( dhm );
  478. return( ret );
  479. }
  480. #if defined(MBEDTLS_FS_IO)
  481. /*
  482. * Load all data from a file into a given buffer.
  483. *
  484. * The file is expected to contain either PEM or DER encoded data.
  485. * A terminating null byte is always appended. It is included in the announced
  486. * length only if the data looks like it is PEM encoded.
  487. */
  488. static int load_file( const char *path, unsigned char **buf, size_t *n )
  489. {
  490. FILE *f;
  491. long size;
  492. if( ( f = fopen( path, "rb" ) ) == NULL )
  493. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  494. fseek( f, 0, SEEK_END );
  495. if( ( size = ftell( f ) ) == -1 )
  496. {
  497. fclose( f );
  498. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  499. }
  500. fseek( f, 0, SEEK_SET );
  501. *n = (size_t) size;
  502. if( *n + 1 == 0 ||
  503. ( *buf = mbedtls_calloc( 1, *n + 1 ) ) == NULL )
  504. {
  505. fclose( f );
  506. return( MBEDTLS_ERR_DHM_ALLOC_FAILED );
  507. }
  508. if( fread( *buf, 1, *n, f ) != *n )
  509. {
  510. fclose( f );
  511. mbedtls_platform_zeroize( *buf, *n + 1 );
  512. mbedtls_free( *buf );
  513. return( MBEDTLS_ERR_DHM_FILE_IO_ERROR );
  514. }
  515. fclose( f );
  516. (*buf)[*n] = '\0';
  517. if( strstr( (const char *) *buf, "-----BEGIN " ) != NULL )
  518. ++*n;
  519. return( 0 );
  520. }
  521. /*
  522. * Load and parse DHM parameters
  523. */
  524. int mbedtls_dhm_parse_dhmfile( mbedtls_dhm_context *dhm, const char *path )
  525. {
  526. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  527. size_t n;
  528. unsigned char *buf;
  529. DHM_VALIDATE_RET( dhm != NULL );
  530. DHM_VALIDATE_RET( path != NULL );
  531. if( ( ret = load_file( path, &buf, &n ) ) != 0 )
  532. return( ret );
  533. ret = mbedtls_dhm_parse_dhm( dhm, buf, n );
  534. mbedtls_platform_zeroize( buf, n );
  535. mbedtls_free( buf );
  536. return( ret );
  537. }
  538. #endif /* MBEDTLS_FS_IO */
  539. #endif /* MBEDTLS_ASN1_PARSE_C */
  540. #endif /* MBEDTLS_DHM_ALT */
  541. #if defined(MBEDTLS_SELF_TEST)
  542. #if defined(MBEDTLS_PEM_PARSE_C)
  543. static const char mbedtls_test_dhm_params[] =
  544. "-----BEGIN DH PARAMETERS-----\r\n"
  545. "MIGHAoGBAJ419DBEOgmQTzo5qXl5fQcN9TN455wkOL7052HzxxRVMyhYmwQcgJvh\r\n"
  546. "1sa18fyfR9OiVEMYglOpkqVoGLN7qd5aQNNi5W7/C+VBdHTBJcGZJyyP5B3qcz32\r\n"
  547. "9mLJKudlVudV0Qxk5qUJaPZ/xupz0NyoVpviuiBOI1gNi8ovSXWzAgEC\r\n"
  548. "-----END DH PARAMETERS-----\r\n";
  549. #else /* MBEDTLS_PEM_PARSE_C */
  550. static const char mbedtls_test_dhm_params[] = {
  551. 0x30, 0x81, 0x87, 0x02, 0x81, 0x81, 0x00, 0x9e, 0x35, 0xf4, 0x30, 0x44,
  552. 0x3a, 0x09, 0x90, 0x4f, 0x3a, 0x39, 0xa9, 0x79, 0x79, 0x7d, 0x07, 0x0d,
  553. 0xf5, 0x33, 0x78, 0xe7, 0x9c, 0x24, 0x38, 0xbe, 0xf4, 0xe7, 0x61, 0xf3,
  554. 0xc7, 0x14, 0x55, 0x33, 0x28, 0x58, 0x9b, 0x04, 0x1c, 0x80, 0x9b, 0xe1,
  555. 0xd6, 0xc6, 0xb5, 0xf1, 0xfc, 0x9f, 0x47, 0xd3, 0xa2, 0x54, 0x43, 0x18,
  556. 0x82, 0x53, 0xa9, 0x92, 0xa5, 0x68, 0x18, 0xb3, 0x7b, 0xa9, 0xde, 0x5a,
  557. 0x40, 0xd3, 0x62, 0xe5, 0x6e, 0xff, 0x0b, 0xe5, 0x41, 0x74, 0x74, 0xc1,
  558. 0x25, 0xc1, 0x99, 0x27, 0x2c, 0x8f, 0xe4, 0x1d, 0xea, 0x73, 0x3d, 0xf6,
  559. 0xf6, 0x62, 0xc9, 0x2a, 0xe7, 0x65, 0x56, 0xe7, 0x55, 0xd1, 0x0c, 0x64,
  560. 0xe6, 0xa5, 0x09, 0x68, 0xf6, 0x7f, 0xc6, 0xea, 0x73, 0xd0, 0xdc, 0xa8,
  561. 0x56, 0x9b, 0xe2, 0xba, 0x20, 0x4e, 0x23, 0x58, 0x0d, 0x8b, 0xca, 0x2f,
  562. 0x49, 0x75, 0xb3, 0x02, 0x01, 0x02 };
  563. #endif /* MBEDTLS_PEM_PARSE_C */
  564. static const size_t mbedtls_test_dhm_params_len = sizeof( mbedtls_test_dhm_params );
  565. /*
  566. * Checkup routine
  567. */
  568. int mbedtls_dhm_self_test( int verbose )
  569. {
  570. int ret = MBEDTLS_ERR_ERROR_CORRUPTION_DETECTED;
  571. mbedtls_dhm_context dhm;
  572. mbedtls_dhm_init( &dhm );
  573. if( verbose != 0 )
  574. mbedtls_printf( " DHM parameter load: " );
  575. if( ( ret = mbedtls_dhm_parse_dhm( &dhm,
  576. (const unsigned char *) mbedtls_test_dhm_params,
  577. mbedtls_test_dhm_params_len ) ) != 0 )
  578. {
  579. if( verbose != 0 )
  580. mbedtls_printf( "failed\n" );
  581. ret = 1;
  582. goto exit;
  583. }
  584. if( verbose != 0 )
  585. mbedtls_printf( "passed\n\n" );
  586. exit:
  587. mbedtls_dhm_free( &dhm );
  588. return( ret );
  589. }
  590. #endif /* MBEDTLS_SELF_TEST */
  591. #endif /* MBEDTLS_DHM_C */