dhm.c 20 KB

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