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

32 #include <utils/randomizer.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 file transform_substructure.h.

317 */

318 diffie_hellman_group_t group;

320 /**

321 * Pointer to first byte of modulus (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 * Sets the modulus for a specific diffie hellman group.

410 *

411 * @param this calling object

412 * @return

413 * SUCCESS if modulus could be found

414 * NOT_FOUND if modulus not supported

415 */

418 /**

419 * Makes sure my public value is computed.

420 *

421 * @param this calling object

422 */

425 /**

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

427 *

428 * @param this calling object

429 */

431 };

433 /**

434 * Implementation of private_diffie_hellman_t.set_modulus.

435 */

437 {

442 {

444 {

445 chunk_t modulus_chunk;

453 }

454 }

456 }

458 /**

459 * Implementation of diffie_hellman_t.set_other_public_value.

460 */

462 {

465 }

467 /**

468 * Implementation of diffie_hellman_t.get_other_public_value.

469 */

471 {

473 {

475 }

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

479 }

481 /**

482 * Implementation of private_diffie_hellman_t.compute_shared_secret.

483 */

485 {

486 /* initialize my public value */

488 /* calculate my public value */

492 }

494 /**

495 * Implementation of private_diffie_hellman_t.compute_public_value.

496 */

498 {

499 mpz_t generator;

500 /* initialize generator and set it*/

502 /* initialize my public value */

504 /* calculate my public value */

506 /* generator not used anymore */

508 }

510 /**

511 * Implementation of diffie_hellman_t.get_my_public_value.

512 */

514 {

517 }

519 /**

520 * Implementation of diffie_hellman_t.get_shared_secret.

521 */

523 {

525 {

527 }

531 }

533 /**

534 * Implementation of diffie_hellman_t.destroy.

535 */

537 {

544 {

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

547 }

549 }

551 /*

552 * Described in header.

553 */

555 {

558 /* public functions */

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

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

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

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

565 /* private functions */

570 /* private variables */

575 /* set this->modulus */

577 {

580 }

589 }