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-2006 Martin Willi

12 * Copyright (C) 2005 Jan Hutter

13 * Hochschule fuer Technik Rapperswil

14 *

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

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

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

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

19 *

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

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

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

23 * for more details.

24 */

26 #include <gmp.h>

27 #include <stdio.h>

31 #include <utils/randomizer.h>

32 #include <debug.h>

49 /**

50 * Modulus of Group 1 (MODP_768_BIT).

51 */

59 };

61 /**

62 * Modulus of Group 2 (MODP_1024_BIT).

63 */

73 };

75 /**

76 * Modulus of Group 5 (MODP_1536_BIT).

77 */

91 };

92 /**

93 * Modulus of Group 14 (MODP_2048_BIT).

94 */

112 };

114 /**

115 * Modulus of Group 15 (MODP_3072_BIT).

116 */

142 };

144 /**

145 * Modulus of Group 16 (MODP_4096_BIT).

146 */

180 };

182 /**

183 * Modulus of Group 17 (MODP_6144_BIT).

184 */

234 };

236 /**

237 * Modulus of Group 18 (MODP_8192_BIT).

238 */

304 };

308 /**

309 * Entry of the modulus list.

310 */

312 /**

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

314 */

315 diffie_hellman_group_t group;

317 /**

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

319 */

322 /*

323 * Length of modulus in bytes.

324 */

327 /*

328 * Generator value.

329 */

330 u_int16_t generator;

331 };

333 /**

334 * All supported modulus values.

335 */

345 };

349 /**

350 * Private data of an diffie_hellman_t object.

351 *

352 */

354 /**

355 * Public diffie_hellman_t interface.

356 */

359 /**

360 * Diffie Hellman group number.

361 */

362 u_int16_t dh_group_number;

364 /**

365 * Modulus.

366 */

367 mpz_t modulus;

369 /**

370 * Modulus length.

371 */

374 /*

375 * Generator value.

376 */

377 u_int16_t generator;

379 /**

380 * My private value .

381 */

384 /**

385 * My public value.

386 */

387 mpz_t my_public;

389 /**

390 * Other public value.

391 */

392 mpz_t other_public;

394 /**

395 * Shared secret.

396 */

397 mpz_t secret;

399 /**

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

401 */

403 };

405 /**

406 * Compute the shared secret

407 */

409 {

412 }

414 /**

415 * Implementation of diffie_hellman_t.set_other_public_value.

416 */

418 {

421 /* check public value: */

422 /* 1. 0 or 1 wouldn't include your generated value */

423 /* 2. a public value larger or equal the modulus is invalid anyway */

426 {

429 }

431 }

433 /**

434 * Implementation of diffie_hellman_t.get_other_public_value.

435 */

438 {

440 {

442 }

446 }

449 /**

450 * Implementation of private_diffie_hellman_t.compute_public_value.

451 */

453 {

454 mpz_t generator;

460 }

462 /**

463 * Implementation of diffie_hellman_t.get_my_public_value.

464 */

466 {

469 }

471 /**

472 * Implementation of diffie_hellman_t.get_shared_secret.

473 */

475 {

477 {

479 }

483 }

485 /**

486 * Implementation of diffie_hellman_t.get_dh_group.

487 */

489 {

491 }

493 /**

494 * Lookup the modulus in modulo table

495 */

497 {

502 {

504 {

505 chunk_t chunk;

513 }

514 }

516 }

518 /**

519 * Implementation of diffie_hellman_t.destroy.

520 */

522 {

529 }

531 /*

532 * Described in header.

533 */

535 {

538 chunk_t random;

540 /* public functions */

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

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

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

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

548 /* private variables */

557 /* set this->modulus */

559 {

562 }

565 {

568 }

571 {

575 }

584 }