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/allocator.h>

31 #include <utils/randomizer.h>

32 #include <utils/gmp_helper.h>

35 /**

36 * String mappings for diffie_hellman_group_t.

37 */

38 mapping_t diffie_hellman_group_m[] = {

49 };

52 /**

53 * Modulus of Group 1 (MODP_768_BIT).

54 */

62 };

64 /**

65 * Modulus of Group 2 (MODP_1024_BIT).

66 */

76 };

78 /**

79 * Modulus of Group 5 (MODP_1536_BIT).

80 */

94 };

95 /**

96 * Modulus of Group 14 (MODP_2048_BIT).

97 */

115 };

117 /**

118 * Modulus of Group 15 (MODP_3072_BIT).

119 */

145 };

147 /**

148 * Modulus of Group 16 (MODP_4096_BIT).

149 */

183 };

185 /**

186 * Modulus of Group 17 (MODP_6144_BIT).

187 */

237 };

239 /**

240 * Modulus of Group 18 (MODP_8192_BIT).

241 */

307 };

311 /**

312 * Entry of the modulus list.

313 */

315 /**

316 * Group number as it is defined in transform_substructure.h.

317 */

318 diffie_hellman_group_t group;

320 /**

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

322 */

325 /*

326 * Length of modulus in bytes.

327 */

330 /*

331 * Generator value.

332 */

333 u_int16_t generator;

334 };

337 /**

338 * All supported modulus values.

339 */

349 };

353 /**

354 * Private data of an diffie_hellman_t object.

355 *

356 */

358 /**

359 * Public diffie_hellman_t interface.

360 */

363 /**

364 * Diffie Hellman group number.

365 */

366 u_int16_t dh_group_number;

368 /**

369 * Modulus.

370 */

371 mpz_t modulus;

373 /**

374 * Modulus length.

375 */

378 /*

379 * Generator value.

380 */

381 u_int16_t generator;

383 /**

384 * My prime .

385 */

386 mpz_t my_prime;

388 /**

389 * My public value.

390 */

391 mpz_t my_public_value;

393 /**

394 * Other public value.

395 */

396 mpz_t other_public_value;

398 /**

399 * Shared secret.

400 */

401 mpz_t shared_secret;

403 /**

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

405 */

408 /**

409 * helper class for gmp functions.

410 */

413 /**

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

415 *

416 * @param this calling object

417 * @return

418 * SUCCESS if modulus could be found

419 * NOT_FOUND if modulus not supported

420 */

423 /**

424 * Makes sure my public value is computed.

425 *

426 * @param this calling object

427 */

430 /**

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

432 *

433 * @param this calling object

434 */

436 };

438 /**

439 * Implementation of private_diffie_hellman_t.set_modulus.

440 */

442 {

447 {

449 {

450 chunk_t modulus_chunk;

458 }

459 }

461 }

463 /**

464 * Implementation of diffie_hellman_t.set_other_public_value.

465 */

467 {

470 }

472 /**

473 * Implementation of diffie_hellman_t.get_other_public_value.

474 */

476 {

478 {

480 }

481 this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->other_public_value), public_value,this->modulus_length);

483 }

485 /**

486 * Implementation of private_diffie_hellman_t.compute_shared_secret.

487 */

489 {

490 /* initialize my public value */

492 /* calculate my public value */

496 }

499 /**

500 * Implementation of private_diffie_hellman_t.compute_public_value.

501 */

503 {

504 mpz_t generator;

505 /* initialize generator and set it*/

507 /* initialize my public value */

509 /* calculate my public value */

511 /* generator not used anymore */

513 }

515 /**

516 * Implementation of diffie_hellman_t.get_my_public_value.

517 */

519 {

520 this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->my_public_value), public_value,this->modulus_length);

521 }

523 /**

524 * Implementation of diffie_hellman_t.get_shared_secret.

525 */

527 {

529 {

531 }

532 this->gmp_helper->mpz_to_chunk(this->gmp_helper,&(this->shared_secret), secret,this->modulus_length);

534 }

536 /**

537 * Implementation of diffie_hellman_t.destroy.

538 */

540 {

547 {

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

551 }

553 }

556 /*

557 * Described in header.

558 */

560 {

563 /* public functions */

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

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

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

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

570 /* private functions */

575 /* private variables */

580 /* set this->modulus */

582 {

586 }

595 }