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 <utils/randomizer.h>

33 /**

34 * String mappings for diffie_hellman_group_t.

35 */

36 mapping_t diffie_hellman_group_m[] = {

47 };

50 /**

51 * Modulus of Group 1 (MODP_768_BIT).

52 */

60 };

62 /**

63 * Modulus of Group 2 (MODP_1024_BIT).

64 */

74 };

76 /**

77 * Modulus of Group 5 (MODP_1536_BIT).

78 */

92 };

93 /**

94 * Modulus of Group 14 (MODP_2048_BIT).

95 */

113 };

115 /**

116 * Modulus of Group 15 (MODP_3072_BIT).

117 */

143 };

145 /**

146 * Modulus of Group 16 (MODP_4096_BIT).

147 */

181 };

183 /**

184 * Modulus of Group 17 (MODP_6144_BIT).

185 */

235 };

237 /**

238 * Modulus of Group 18 (MODP_8192_BIT).

239 */

305 };

309 /**

310 * Entry of the modulus list.

311 */

313 /**

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

315 */

316 diffie_hellman_group_t group;

318 /**

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

320 */

323 /*

324 * Length of modulus in bytes.

325 */

328 /*

329 * Generator value.

330 */

331 u_int16_t generator;

332 };

335 /**

336 * All supported modulus values.

337 */

347 };

351 /**

352 * Private data of an diffie_hellman_t object.

353 *

354 */

356 /**

357 * Public diffie_hellman_t interface.

358 */

361 /**

362 * Diffie Hellman group number.

363 */

364 u_int16_t dh_group_number;

366 /**

367 * Modulus.

368 */

369 mpz_t modulus;

371 /**

372 * Modulus length.

373 */

376 /*

377 * Generator value.

378 */

379 u_int16_t generator;

381 /**

382 * My private value .

383 */

384 mpz_t my_private_value;

386 /**

387 * My public value.

388 */

389 mpz_t my_public_value;

391 /**

392 * Other public value.

393 */

394 mpz_t other_public_value;

396 /**

397 * Shared secret.

398 */

399 mpz_t shared_secret;

401 /**

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

403 */

406 /**

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

408 *

409 * @param this calling object

410 * @return

411 * SUCCESS if modulus could be found

412 * NOT_FOUND if modulus not supported

413 */

416 /**

417 * Makes sure my public value is computed.

418 *

419 * @param this calling object

420 */

423 /**

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

425 *

426 * @param this calling object

427 */

429 };

431 /**

432 * Implementation of private_diffie_hellman_t.set_modulus.

433 */

435 {

440 {

442 {

443 chunk_t modulus_chunk;

451 }

452 }

454 }

456 /**

457 * Implementation of diffie_hellman_t.set_other_public_value.

458 */

460 {

463 }

465 /**

466 * Implementation of diffie_hellman_t.get_other_public_value.

467 */

469 {

471 {

473 }

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

477 }

479 /**

480 * Implementation of private_diffie_hellman_t.compute_shared_secret.

481 */

483 {

484 /* initialize my public value */

486 /* calculate my public value */

490 }

492 /**

493 * Implementation of private_diffie_hellman_t.compute_public_value.

494 */

496 {

497 mpz_t generator;

498 /* initialize generator and set it*/

500 /* initialize my public value */

502 /* calculate my public value */

504 /* generator not used anymore */

506 }

508 /**

509 * Implementation of diffie_hellman_t.get_my_public_value.

510 */

512 {

515 }

517 /**

518 * Implementation of diffie_hellman_t.get_shared_secret.

519 */

521 {

523 {

525 }

529 }

531 /**

532 * Implementation of diffie_hellman_t.get_dh_group.

533 */

535 {

537 }

539 /**

540 * Implementation of diffie_hellman_t.destroy.

541 */

543 {

550 {

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

553 }

555 }

557 /*

558 * Described in header.

559 */

561 {

564 chunk_t random_bytes;

566 /* public functions */

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

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

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

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

574 /* private functions */

579 /* private variables */

585 /* set this->modulus */

587 {

590 }

593 {

596 }

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

598 {

602 }

614 }