Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (prime generation) |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +0000 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 6 | */ |
| 7 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 9 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 10 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 12 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 14 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 16 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 17 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 18 | !defined(MBEDTLS_GENPRIME) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 19 | int main(void) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 20 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 21 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 22 | "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or " |
| 23 | "MBEDTLS_GENPRIME not defined.\n"); |
| 24 | mbedtls_exit(0); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 25 | } |
| 26 | #else |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 27 | |
| 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 Cosgrove | 49f99bc | 2022-12-04 16:44:21 +0000 | [diff] [blame] | 37 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 38 | " 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 Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 48 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 49 | int main(int argc, char **argv) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | { |
| 51 | int ret = 1; |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 52 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | mbedtls_mpi G, P, Q; |
| 54 | mbedtls_entropy_context entropy; |
| 55 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 56 | const char *pers = "dh_genprime"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | FILE *fout; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 58 | int nbits = DFL_BITS; |
| 59 | int i; |
| 60 | char *p, *q; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 61 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 62 | 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 Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 65 | |
Aditya Deshpande | 0504ac2 | 2023-01-30 15:58:50 +0000 | [diff] [blame] | 66 | if (argc < 2) { |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 67 | usage: |
| 68 | mbedtls_printf(USAGE); |
makise-homura | e014fec | 2020-08-22 23:56:46 +0300 | [diff] [blame] | 69 | goto exit; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 70 | } |
| 71 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | for (i = 1; i < argc; i++) { |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 73 | p = argv[i]; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 74 | if ((q = strchr(p, '=')) == NULL) { |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 75 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 76 | } |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 77 | *q++ = '\0'; |
| 78 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 79 | if (strcmp(p, "bits") == 0) { |
| 80 | nbits = atoi(q); |
| 81 | if (nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS) { |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 82 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 83 | } |
| 84 | } else { |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 85 | goto usage; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 86 | } |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 87 | } |
| 88 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 89 | if ((ret = mbedtls_mpi_read_string(&G, 10, GENERATOR)) != 0) { |
| 90 | mbedtls_printf(" failed\n ! mbedtls_mpi_read_string returned %d\n", ret); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 91 | goto exit; |
| 92 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 93 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 94 | mbedtls_printf(" ! Generating large primes may take minutes!\n"); |
Manuel Pégourié-Gonnard | 5e1e611 | 2013-11-22 21:16:10 +0100 | [diff] [blame] | 95 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 96 | mbedtls_printf("\n . Seeding the random number generator..."); |
| 97 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 98 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 99 | 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 Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 103 | goto exit; |
| 104 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | mbedtls_printf(" ok\n . Generating the modulus, please wait..."); |
| 107 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 108 | |
| 109 | /* |
| 110 | * This can take a long time... |
| 111 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 112 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
| 117 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 118 | mbedtls_printf(" ok\n . Verifying that Q = (P-1)/2 is prime..."); |
| 119 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 120 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 121 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 123 | goto exit; |
| 124 | } |
| 125 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 126 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 128 | goto exit; |
| 129 | } |
| 130 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 131 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 133 | goto exit; |
| 134 | } |
| 135 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 136 | mbedtls_printf(" ok\n . Exporting the value in dh_prime.txt..."); |
| 137 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 138 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 139 | if ((fout = fopen("dh_prime.txt", "wb+")) == NULL) { |
| 140 | mbedtls_printf(" failed\n ! Could not create dh_prime.txt\n\n"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | goto exit; |
| 142 | } |
| 143 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 144 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | goto exit; |
| 149 | } |
| 150 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 151 | mbedtls_printf(" ok\n\n"); |
| 152 | fclose(fout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 153 | |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 154 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 155 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | exit: |
| 157 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 158 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 161 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 162 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 163 | mbedtls_printf(" Press Enter to exit this program.\n"); |
| 164 | fflush(stdout); getchar(); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | #endif |
| 166 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 167 | mbedtls_exit(exit_code); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 169 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && |
| 170 | MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */ |