blob: a4f3f0a97e695e826d9130cc3fdf2b6a778d5ed8 [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 Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
17 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
18 !defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010019int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000020{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020021 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010022 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
23 "MBEDTLS_GENPRIME not defined.\n");
24 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000025}
26#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020027
28#include "mbedtls/bignum.h"
29#include "mbedtls/entropy.h"
30#include "mbedtls/ctr_drbg.h"
31
32#include <stdio.h>
33#include <string.h>
34
35#define USAGE \
36 "\n usage: dh_genprime param=<>...\n" \
Tom Cosgrove49f99bc2022-12-04 16:44:21 +000037 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020038 " bits=%%d default: 2048\n"
39
40#define DFL_BITS 2048
41
42/*
43 * Note: G = 4 is always a quadratic residue mod P,
44 * so it is a generator of order Q (with P = 2*Q+1).
45 */
46#define GENERATOR "4"
47
Simon Butcher63cb97e2018-12-06 17:43:31 +000048
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010049int main(int argc, char **argv)
Paul Bakker5121ce52009-01-03 21:22:43 +000050{
51 int ret = 1;
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +010052 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020053 mbedtls_mpi G, P, Q;
54 mbedtls_entropy_context entropy;
55 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020056 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000057 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020058 int nbits = DFL_BITS;
59 int i;
60 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062 mbedtls_mpi_init(&G); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
63 mbedtls_ctr_drbg_init(&ctr_drbg);
64 mbedtls_entropy_init(&entropy);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020065
Aditya Deshpande0504ac22023-01-30 15:58:50 +000066 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010067usage:
68 mbedtls_printf(USAGE);
makise-homurae014fec2020-08-22 23:56:46 +030069 goto exit;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020070 }
71
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020073 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 if ((q = strchr(p, '=')) == NULL) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020075 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010076 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020077 *q++ = '\0';
78
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079 if (strcmp(p, "bits") == 0) {
80 nbits = atoi(q);
81 if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020082 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010083 }
84 } else {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020085 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020087 }
88
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) {
90 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string returned %d\n", ret);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020091 goto exit;
92 }
Paul Bakker5121ce52009-01-03 21:22:43 +000093
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010094 mbedtls_printf(" ! Generating large primes may take minutes!\n");
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +010095
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010096 mbedtls_printf("\n . Seeding the random number generator...");
97 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000098
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
100 (const unsigned char *) pers,
101 strlen(pers))) != 0) {
102 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103 goto exit;
104 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_printf(" ok\n . Generating the modulus, please wait...");
107 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 /*
110 * This can take a long time...
111 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if ((ret = mbedtls_mpi_gen_prime(&P, nbits, 1,
113 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
114 mbedtls_printf(" failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 goto exit;
116 }
117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
119 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100121 if ((ret = mbedtls_mpi_sub_int(&Q, &P, 1)) != 0) {
122 mbedtls_printf(" failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000123 goto exit;
124 }
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 if ((ret = mbedtls_mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
127 mbedtls_printf(" failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 goto exit;
129 }
130
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100131 if ((ret = mbedtls_mpi_is_prime_ext(&Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
132 mbedtls_printf(" failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 goto exit;
134 }
135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 mbedtls_printf(" ok\n . Exporting the value in dh_prime.txt...");
137 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
140 mbedtls_printf(" failed\n ! Could not create dh_prime.txt\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 goto exit;
142 }
143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (((ret = mbedtls_mpi_write_file("P = ", &P, 16, fout)) != 0) ||
145 ((ret = mbedtls_mpi_write_file("G = ", &G, 16, fout)) != 0)) {
146 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
147 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 goto exit;
149 }
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 mbedtls_printf(" ok\n\n");
152 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +0100154 exit_code = MBEDTLS_EXIT_SUCCESS;
155
Paul Bakker5121ce52009-01-03 21:22:43 +0000156exit:
157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 mbedtls_mpi_free(&G); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
159 mbedtls_ctr_drbg_free(&ctr_drbg);
160 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000161
Paul Bakkercce9d772011-11-18 14:26:47 +0000162#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 mbedtls_printf(" Press Enter to exit this program.\n");
164 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000165#endif
166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000168}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200169#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
170 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */