dhm.c 20 KB

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