dhm.c 21 KB

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