blob: ebaf9265f35c8bcd1652a4dc2520f396bbbb65e1 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Felix Conway998760a2025-03-24 11:37:33 +00008#define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS
9
Bence Szépkútic662b362021-05-27 11:25:03 +020010#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000011
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000012#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
15 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
16 !defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010017int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000018{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020019 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010020 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
21 "MBEDTLS_GENPRIME not defined.\n");
22 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000023}
24#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020025
26#include "mbedtls/bignum.h"
27#include "mbedtls/entropy.h"
28#include "mbedtls/ctr_drbg.h"
29
30#include <stdio.h>
31#include <string.h>
32
33#define USAGE \
34 "\n usage: dh_genprime param=<>...\n" \
Tom Cosgrove1797b052022-12-04 17:19:59 +000035 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020036 " bits=%%d default: 2048\n"
37
38#define DFL_BITS 2048
39
40/*
41 * Note: G = 4 is always a quadratic residue mod P,
42 * so it is a generator of order Q (with P = 2*Q+1).
43 */
44#define GENERATOR "4"
45
Simon Butcher63cb97e2018-12-06 17:43:31 +000046
Gilles Peskine449bd832023-01-11 14:50:10 +010047int main(int argc, char **argv)
Paul Bakker5121ce52009-01-03 21:22:43 +000048{
49 int ret = 1;
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +010050 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020051 mbedtls_mpi G, P, Q;
52 mbedtls_entropy_context entropy;
53 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020054 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000055 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020056 int nbits = DFL_BITS;
57 int i;
58 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000059
Gilles Peskine449bd832023-01-11 14:50:10 +010060 mbedtls_mpi_init(&G); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
61 mbedtls_ctr_drbg_init(&ctr_drbg);
62 mbedtls_entropy_init(&entropy);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020063
Aditya Deshpande644a5c02023-01-30 15:58:50 +000064 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +010065usage:
66 mbedtls_printf(USAGE);
makise-homurae014fec2020-08-22 23:56:46 +030067 goto exit;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020068 }
69
Gilles Peskine449bd832023-01-11 14:50:10 +010070 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020071 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010072 if ((q = strchr(p, '=')) == NULL) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020073 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010074 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020075 *q++ = '\0';
76
Gilles Peskine449bd832023-01-11 14:50:10 +010077 if (strcmp(p, "bits") == 0) {
78 nbits = atoi(q);
79 if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020080 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010081 }
82 } else {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020083 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010084 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020085 }
86
Gilles Peskine449bd832023-01-11 14:50:10 +010087 if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) {
88 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string returned %d\n", ret);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020089 goto exit;
90 }
Paul Bakker5121ce52009-01-03 21:22:43 +000091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_printf(" ! Generating large primes may take minutes!\n");
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +010093
Gilles Peskine449bd832023-01-11 14:50:10 +010094 mbedtls_printf("\n . Seeding the random number generator...");
95 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000096
Gilles Peskine449bd832023-01-11 14:50:10 +010097 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
98 (const unsigned char *) pers,
99 strlen(pers))) != 0) {
100 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000101 goto exit;
102 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Gilles Peskine449bd832023-01-11 14:50:10 +0100104 mbedtls_printf(" ok\n . Generating the modulus, please wait...");
105 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000106
107 /*
108 * This can take a long time...
109 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100110 if ((ret = mbedtls_mpi_gen_prime(&P, nbits, 1,
111 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
112 mbedtls_printf(" failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 goto exit;
114 }
115
Gilles Peskine449bd832023-01-11 14:50:10 +0100116 mbedtls_printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
117 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 if ((ret = mbedtls_mpi_sub_int(&Q, &P, 1)) != 0) {
120 mbedtls_printf(" failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000121 goto exit;
122 }
123
Gilles Peskine449bd832023-01-11 14:50:10 +0100124 if ((ret = mbedtls_mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
125 mbedtls_printf(" failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 goto exit;
127 }
128
Gilles Peskine449bd832023-01-11 14:50:10 +0100129 if ((ret = mbedtls_mpi_is_prime_ext(&Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
130 mbedtls_printf(" failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 goto exit;
132 }
133
Gilles Peskine449bd832023-01-11 14:50:10 +0100134 mbedtls_printf(" ok\n . Exporting the value in dh_prime.txt...");
135 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000136
Gilles Peskine449bd832023-01-11 14:50:10 +0100137 if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
138 mbedtls_printf(" failed\n ! Could not create dh_prime.txt\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 goto exit;
140 }
141
Gilles Peskine449bd832023-01-11 14:50:10 +0100142 if (((ret = mbedtls_mpi_write_file("P = ", &P, 16, fout)) != 0) ||
143 ((ret = mbedtls_mpi_write_file("G = ", &G, 16, fout)) != 0)) {
144 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
145 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
Gilles Peskine449bd832023-01-11 14:50:10 +0100149 mbedtls_printf(" ok\n\n");
150 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000151
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +0100152 exit_code = MBEDTLS_EXIT_SUCCESS;
153
Paul Bakker5121ce52009-01-03 21:22:43 +0000154exit:
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_mpi_free(&G); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
157 mbedtls_ctr_drbg_free(&ctr_drbg);
158 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000159
Gilles Peskine449bd832023-01-11 14:50:10 +0100160 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000161}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
163 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */