blob: 9037ce3c1c275ee4d37913d27ad454b057475614 [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02005 * SPDX-License-Identifier: Apache-2.0
6 *
7 * Licensed under the Apache License, Version 2.0 (the "License"); you may
8 * not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
10 *
11 * http://www.apache.org/licenses/LICENSE-2.0
12 *
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
15 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
Paul Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
29 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
30 !defined(MBEDTLS_GENPRIME)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010031int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000032{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010034 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
35 "MBEDTLS_GENPRIME not defined.\n");
36 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000037}
38#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020039
40#include "mbedtls/bignum.h"
41#include "mbedtls/entropy.h"
42#include "mbedtls/ctr_drbg.h"
43
44#include <stdio.h>
45#include <string.h>
46
47#define USAGE \
48 "\n usage: dh_genprime param=<>...\n" \
Tom Cosgrove49f99bc2022-12-04 16:44:21 +000049 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020050 " bits=%%d default: 2048\n"
51
52#define DFL_BITS 2048
53
54/*
55 * Note: G = 4 is always a quadratic residue mod P,
56 * so it is a generator of order Q (with P = 2*Q+1).
57 */
58#define GENERATOR "4"
59
Simon Butcher63cb97e2018-12-06 17:43:31 +000060
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010061int main(int argc, char **argv)
Paul Bakker5121ce52009-01-03 21:22:43 +000062{
63 int ret = 1;
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +010064 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020065 mbedtls_mpi G, P, Q;
66 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020068 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000069 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020070 int nbits = DFL_BITS;
71 int i;
72 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074 mbedtls_mpi_init(&G); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
75 mbedtls_ctr_drbg_init(&ctr_drbg);
76 mbedtls_entropy_init(&entropy);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020077
Aditya Deshpande0504ac22023-01-30 15:58:50 +000078 if (argc < 2) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010079usage:
80 mbedtls_printf(USAGE);
makise-homurae014fec2020-08-22 23:56:46 +030081 goto exit;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020082 }
83
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 for (i = 1; i < argc; i++) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020085 p = argv[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 if ((q = strchr(p, '=')) == NULL) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020087 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010088 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020089 *q++ = '\0';
90
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010091 if (strcmp(p, "bits") == 0) {
92 nbits = atoi(q);
93 if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020094 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 }
96 } else {
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020097 goto usage;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 }
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020099 }
100
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) {
102 mbedtls_printf(" failed\n ! mbedtls_mpi_read_string returned %d\n", ret);
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200103 goto exit;
104 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_printf(" ! Generating large primes may take minutes!\n");
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +0100107
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 mbedtls_printf("\n . Seeding the random number generator...");
109 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000110
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100111 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
112 (const unsigned char *) pers,
113 strlen(pers))) != 0) {
114 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000115 goto exit;
116 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_printf(" ok\n . Generating the modulus, please wait...");
119 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000120
121 /*
122 * This can take a long time...
123 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if ((ret = mbedtls_mpi_gen_prime(&P, nbits, 1,
125 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
126 mbedtls_printf(" failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 goto exit;
128 }
129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mbedtls_printf(" ok\n . Verifying that Q = (P-1)/2 is prime...");
131 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 if ((ret = mbedtls_mpi_sub_int(&Q, &P, 1)) != 0) {
134 mbedtls_printf(" failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 if ((ret = mbedtls_mpi_div_int(&Q, NULL, &Q, 2)) != 0) {
139 mbedtls_printf(" failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 goto exit;
141 }
142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 if ((ret = mbedtls_mpi_is_prime_ext(&Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
144 mbedtls_printf(" failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 goto exit;
146 }
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 mbedtls_printf(" ok\n . Exporting the value in dh_prime.txt...");
149 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) {
152 mbedtls_printf(" failed\n ! Could not create dh_prime.txt\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 goto exit;
154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if (((ret = mbedtls_mpi_write_file("P = ", &P, 16, fout)) != 0) ||
157 ((ret = mbedtls_mpi_write_file("G = ", &G, 16, fout)) != 0)) {
158 mbedtls_printf(" failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret);
159 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 goto exit;
161 }
162
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100163 mbedtls_printf(" ok\n\n");
164 fclose(fout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000165
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +0100166 exit_code = MBEDTLS_EXIT_SUCCESS;
167
Paul Bakker5121ce52009-01-03 21:22:43 +0000168exit:
169
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100170 mbedtls_mpi_free(&G); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
171 mbedtls_ctr_drbg_free(&ctr_drbg);
172 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000173
Paul Bakkercce9d772011-11-18 14:26:47 +0000174#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 mbedtls_printf(" Press Enter to exit this program.\n");
176 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000177#endif
178
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000180}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
182 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */