1 /**

2 * @file diffie_hellman.c

3 *

4 * @brief Implementation of diffie_hellman_t.

5 *

6 */

8 /*

9 * Copyright (C) 1998-2002 D. Hugh Redelmeier.

10 * Copyright (C) 1999, 2000, 2001 Henry Spencer.

11 * Copyright (C) 2005 Jan Hutter, Martin Willi

12 * Hochschule fuer Technik Rapperswil

13 *

14 * This program is free software; you can redistribute it and/or modify it

15 * under the terms of the GNU General Public License as published by the

16 * Free Software Foundation; either version 2 of the License, or (at your

17 * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.

18 *

19 * This program is distributed in the hope that it will be useful, but

20 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY

21 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License

22 * for more details.

23 */

25 #include <gmp.h>

26 #include <stdio.h>

30 #include <daemon.h>

31 #include <utils/randomizer.h>

34 /**

35 * String mappings for diffie_hellman_group_t.

36 */

37 mapping_t diffie_hellman_group_m[] = {

48 };

51 /**

52 * Modulus of Group 1 (MODP_768_BIT).

53 */

61 };

63 /**

64 * Modulus of Group 2 (MODP_1024_BIT).

65 */

75 };

77 /**

78 * Modulus of Group 5 (MODP_1536_BIT).

79 */

93 };

94 /**

95 * Modulus of Group 14 (MODP_2048_BIT).

96 */

114 };

116 /**

117 * Modulus of Group 15 (MODP_3072_BIT).

118 */

144 };

146 /**

147 * Modulus of Group 16 (MODP_4096_BIT).

148 */

182 };

184 /**

185 * Modulus of Group 17 (MODP_6144_BIT).

186 */

236 };

238 /**

239 * Modulus of Group 18 (MODP_8192_BIT).

240 */

306 };

310 /**

311 * Entry of the modulus list.

312 */

314 /**

315 * Group number as it is defined in file transform_substructure.h.

316 */

317 diffie_hellman_group_t group;

319 /**

320 * Pointer to first byte of modulus (network order).

321 */

324 /*

325 * Length of modulus in bytes.

326 */

329 /*

330 * Generator value.

331 */

332 u_int16_t generator;

333 };

336 /**

337 * All supported modulus values.

338 */

348 };

352 /**

353 * Private data of an diffie_hellman_t object.

354 *

355 */

357 /**

358 * Public diffie_hellman_t interface.

359 */

362 /**

363 * Diffie Hellman group number.

364 */

365 u_int16_t dh_group_number;

367 /**

368 * Modulus.

369 */

370 mpz_t modulus;

372 /**

373 * Modulus length.

374 */

377 /*

378 * Generator value.

379 */

380 u_int16_t generator;

382 /**

383 * My private value .

384 */

385 mpz_t my_private_value;

387 /**

388 * My public value.

389 */

390 mpz_t my_public_value;

392 /**

393 * Other public value.

394 */

395 mpz_t other_public_value;

397 /**

398 * Shared secret.

399 */

400 mpz_t shared_secret;

402 /**

403 * True if shared secret is computed and stored in my_public_value.

404 */

407 /**

408 * Sets the modulus for a specific diffie hellman group.

409 *

410 * @param this calling object

411 * @return

412 * SUCCESS if modulus could be found

413 * NOT_FOUND if modulus not supported

414 */

417 /**

418 * Makes sure my public value is computed.

419 *

420 * @param this calling object

421 */

424 /**

425 * Computes shared secret (other public value must be available).

426 *

427 * @param this calling object

428 */

430 };

432 /**

433 * Implementation of private_diffie_hellman_t.set_modulus.

434 */

436 {

441 {

443 {

444 chunk_t modulus_chunk;

452 }

453 }

455 }

457 /**

458 * Implementation of diffie_hellman_t.set_other_public_value.

459 */

461 {

464 }

466 /**

467 * Implementation of diffie_hellman_t.get_other_public_value.

468 */

470 {

472 {

474 }

476 public_value->ptr = mpz_export(NULL, NULL, 1, public_value->len, 1, 0, this->other_public_value);

478 }

480 /**

481 * Implementation of private_diffie_hellman_t.compute_shared_secret.

482 */

484 {

485 /* initialize my public value */

487 /* calculate my public value */

491 }

493 /**

494 * Implementation of private_diffie_hellman_t.compute_public_value.

495 */

497 {

498 mpz_t generator;

499 /* initialize generator and set it*/

501 /* initialize my public value */

503 /* calculate my public value */

505 /* generator not used anymore */

507 }

509 /**

510 * Implementation of diffie_hellman_t.get_my_public_value.

511 */

513 {

516 }

518 /**

519 * Implementation of diffie_hellman_t.get_shared_secret.

520 */

522 {

524 {

526 }

530 }

532 /**

533 * Implementation of diffie_hellman_t.get_dh_group.

534 */

536 {

538 }

540 /**

541 * Implementation of diffie_hellman_t.destroy.

542 */

544 {

551 {

552 /* other public value gets initialized together with shared secret */

554 }

556 }

558 /*

559 * Described in header.

560 */

562 {

565 chunk_t random_bytes;

567 /* public functions */

568 this->public.get_shared_secret = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_shared_secret;

569 this->public.set_other_public_value = (void (*)(diffie_hellman_t *, chunk_t )) set_other_public_value;

570 this->public.get_other_public_value = (status_t (*)(diffie_hellman_t *, chunk_t *)) get_other_public_value;

571 this->public.get_my_public_value = (void (*)(diffie_hellman_t *, chunk_t *)) get_my_public_value;

575 /* private functions */

580 /* private variables */

586 /* set this->modulus */

588 {

591 }

594 {

597 }

598 if (randomizer->allocate_pseudo_random_bytes(randomizer, this->modulus_length, &random_bytes) != SUCCESS)

599 {

603 }

615 }