blob: 6872e61e3393f6a286628d5c1180922978da40c7 [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
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000011
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020012#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
13 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
14 !defined(MBEDTLS_GENPRIME)
Gilles Peskine449bd832023-01-11 14:50:10 +010015int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000016{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010018 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
19 "MBEDTLS_GENPRIME not defined.\n");
20 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000021}
22#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020023
24#include "mbedtls/bignum.h"
25#include "mbedtls/entropy.h"
26#include "mbedtls/ctr_drbg.h"
27
28#include <stdio.h>
29#include <string.h>
30
31#define USAGE \
32 "\n usage: dh_genprime param=<>...\n" \
Tom Cosgrove1797b052022-12-04 17:19:59 +000033 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020034 " bits=%%d default: 2048\n"
35
36#define DFL_BITS 2048
37
38/*
39 * Note: G = 4 is always a quadratic residue mod P,
40 * so it is a generator of order Q (with P = 2*Q+1).
41 */
42#define GENERATOR "4"
43
Simon Butcher63cb97e2018-12-06 17:43:31 +000044
Gilles Peskine449bd832023-01-11 14:50:10 +010045int main(int argc, char **argv)
Paul Bakker5121ce52009-01-03 21:22:43 +000046{
47 int ret = 1;
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +010048 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049 mbedtls_mpi G, P, Q;
50 mbedtls_entropy_context entropy;
51 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020052 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000053 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020054 int nbits = DFL_BITS;
55 int i;
56 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Gilles Peskine449bd832023-01-11 14:50:10 +010058 mbedtls_mpi_init(&G); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
59 mbedtls_ctr_drbg_init(&ctr_drbg);
60 mbedtls_entropy_init(&entropy);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020061
Aditya Deshpande644a5c02023-01-30 15:58:50 +000062 if (argc < 2) {
Gilles Peskine449bd832023-01-11 14:50:10 +010063usage:
64 mbedtls_printf(USAGE);
makise-homurae014fec2020-08-22 23:56:46 +030065 goto exit;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020066 }
67
Gilles Peskine449bd832023-01-11 14:50:10 +010068 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020069 p = argv[i];
Gilles Peskine449bd832023-01-11 14:50:10 +010070 if ((q = strchr(p, '=')) == NULL) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020071 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010072 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020073 *q++ = '\0';
74
Gilles Peskine449bd832023-01-11 14:50:10 +010075 if (strcmp(p, "bits") == 0) {
76 nbits = atoi(q);
77 if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020078 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010079 }
80 } else {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020081 goto usage;
Gilles Peskine449bd832023-01-11 14:50:10 +010082 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020083 }
84
Gilles Peskine449bd832023-01-11 14:50:10 +010085 if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) {
86 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string returned %d\n", ret);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020087 goto exit;
88 }
Paul Bakker5121ce52009-01-03 21:22:43 +000089
Gilles Peskine449bd832023-01-11 14:50:10 +010090 mbedtls_printf(" ! Generating large primes may take minutes!\n");
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +010091
Gilles Peskine449bd832023-01-11 14:50:10 +010092 mbedtls_printf("\n . Seeding the random number generator...");
93 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000094
Gilles Peskine449bd832023-01-11 14:50:10 +010095 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
96 (const unsigned char *) pers,
97 strlen(pers))) != 0) {
98 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000099 goto exit;
100 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Gilles Peskine449bd832023-01-11 14:50:10 +0100102 mbedtls_printf(" ok\n . Generating the modulus, please wait...");
103 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000104
105 /*
106 * This can take a long time...
107 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if ((ret = mbedtls_mpi_gen_prime(&P, nbits, 1,
109 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
110 mbedtls_printf(" failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111 goto exit;
112 }
113
Gilles Peskine449bd832023-01-11 14:50:10 +0100114 mbedtls_printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
115 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Gilles Peskine449bd832023-01-11 14:50:10 +0100117 if ((ret = mbedtls_mpi_sub_int(&Q, &P, 1)) != 0) {
118 mbedtls_printf(" failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 goto exit;
120 }
121
Gilles Peskine449bd832023-01-11 14:50:10 +0100122 if ((ret = mbedtls_mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
123 mbedtls_printf(" failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000124 goto exit;
125 }
126
Gilles Peskine449bd832023-01-11 14:50:10 +0100127 if ((ret = mbedtls_mpi_is_prime_ext(&Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
128 mbedtls_printf(" failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 goto exit;
130 }
131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 mbedtls_printf(" ok\n . Exporting the value in dh_prime.txt...");
133 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000134
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
136 mbedtls_printf(" failed\n ! Could not create dh_prime.txt\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000137 goto exit;
138 }
139
Gilles Peskine449bd832023-01-11 14:50:10 +0100140 if (((ret = mbedtls_mpi_write_file("P = ", &P, 16, fout)) != 0) ||
141 ((ret = mbedtls_mpi_write_file("G = ", &G, 16, fout)) != 0)) {
142 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
143 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 goto exit;
145 }
146
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_printf(" ok\n\n");
148 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +0100150 exit_code = MBEDTLS_EXIT_SUCCESS;
151
Paul Bakker5121ce52009-01-03 21:22:43 +0000152exit:
153
Gilles Peskine449bd832023-01-11 14:50:10 +0100154 mbedtls_mpi_free(&G); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
155 mbedtls_ctr_drbg_free(&ctr_drbg);
156 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000157
Gilles Peskine449bd832023-01-11 14:50:10 +0100158 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000159}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200160#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
161 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */