dhm.c 20 KB

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