dhm.c 21 KB

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