349ec419e07e107246be8450fc6114840a8723b8

1 /*

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

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

4 * Copyright (C) 2005-2008 Martin Willi

5 * Copyright (C) 2005 Jan Hutter

6 * Hochschule fuer Technik Rapperswil

7 *

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

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

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

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

12 *

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

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

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

16 * for more details.

17 *

18 * $Id$

19 */

21 #include <gmp.h>

25 #include <utils/randomizer.h>

26 #include <debug.h>

29 /**

30 * Modulus of Group 1 (MODP_768_BIT).

31 */

39 };

41 /**

42 * Modulus of Group 2 (MODP_1024_BIT).

43 */

53 };

55 /**

56 * Modulus of Group 5 (MODP_1536_BIT).

57 */

71 };

72 /**

73 * Modulus of Group 14 (MODP_2048_BIT).

74 */

92 };

94 /**

95 * Modulus of Group 15 (MODP_3072_BIT).

96 */

122 };

124 /**

125 * Modulus of Group 16 (MODP_4096_BIT).

126 */

160 };

162 /**

163 * Modulus of Group 17 (MODP_6144_BIT).

164 */

214 };

216 /**

217 * Modulus of Group 18 (MODP_8192_BIT).

218 */

284 };

288 /**

289 * Entry of the modulus list.

290 */

292 /**

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

294 */

295 diffie_hellman_group_t group;

297 /**

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

299 */

302 /*

303 * Length of modulus in bytes.

304 */

307 /*

308 * Generator value.

309 */

310 u_int16_t generator;

311 };

313 /**

314 * All supported modulus values.

315 */

325 };

329 /**

330 * Private data of an gmp_diffie_hellman_t object.

331 */

333 /**

334 * Public gmp_diffie_hellman_t interface.

335 */

338 /**

339 * Diffie Hellman group number.

340 */

341 u_int16_t group;

343 /*

344 * Generator value.

345 */

346 mpz_t g;

348 /**

349 * My private value.

350 */

351 mpz_t xa;

353 /**

354 * My public value.

355 */

356 mpz_t ya;

358 /**

359 * Other public value.

360 */

361 mpz_t yb;

363 /**

364 * Shared secret.

365 */

366 mpz_t zz;

368 /**

369 * Modulus.

370 */

371 mpz_t p;

373 /**

374 * Modulus length.

375 */

378 /**

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

380 */

382 };

384 /**

385 * Implementation of gmp_diffie_hellman_t.set_other_public_value.

386 */

388 {

389 mpz_t p_min_1;

396 /* check public value:

397 * 1. 0 or 1 is invalid as 0^a = 0 and 1^a = 1

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

401 {

402 #ifdef EXTENDED_DH_TEST

403 /* 3. test if y ^ q mod p = 1, where q = (p - 1)/2. */

412 {

415 }

416 else

417 {

419 }

421 #else

424 #endif

425 }

426 else

427 {

429 }

431 }

433 /**

434 * Implementation of gmp_diffie_hellman_t.get_other_public_value.

435 */

438 {

440 {

442 }

446 }

448 /**

449 * Implementation of gmp_diffie_hellman_t.get_my_public_value.

450 */

452 {

455 }

457 /**

458 * Implementation of gmp_diffie_hellman_t.get_shared_secret.

459 */

461 {

463 {

465 }

469 }

471 /**

472 * Implementation of gmp_diffie_hellman_t.get_dh_group.

473 */

475 {

477 }

479 /**

480 * Lookup the modulus in modulo table

481 */

483 {

488 {

490 {

491 chunk_t chunk;

499 }

500 }

502 }

504 /**

505 * Implementation of gmp_diffie_hellman_t.destroy.

506 */

508 {

516 }

518 /*

519 * Described in header.

520 */

522 {

525 chunk_t random;

526 status_t status;

528 /* public functions */

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

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

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

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

536 /* private variables */

547 /* find a modulus according to group */

549 {

552 }

558 {

561 }

568 }