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 | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 5 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 6 | * |
| 7 | * This file is provided under the Apache License 2.0, or the |
| 8 | * GNU General Public License v2.0 or later. |
| 9 | * |
| 10 | * ********** |
| 11 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 12 | * |
| 13 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 14 | * not use this file except in compliance with the License. |
| 15 | * You may obtain a copy of the License at |
| 16 | * |
| 17 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 18 | * |
| 19 | * Unless required by applicable law or agreed to in writing, software |
| 20 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 21 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 22 | * See the License for the specific language governing permissions and |
| 23 | * limitations under the License. |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 24 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 25 | * ********** |
| 26 | * |
| 27 | * ********** |
| 28 | * GNU General Public License v2.0 or later: |
| 29 | * |
| 30 | * This program is free software; you can redistribute it and/or modify |
| 31 | * it under the terms of the GNU General Public License as published by |
| 32 | * the Free Software Foundation; either version 2 of the License, or |
| 33 | * (at your option) any later version. |
| 34 | * |
| 35 | * This program is distributed in the hope that it will be useful, |
| 36 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 37 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 38 | * GNU General Public License for more details. |
| 39 | * |
| 40 | * You should have received a copy of the GNU General Public License along |
| 41 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 42 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 43 | * |
| 44 | * ********** |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 45 | */ |
| 46 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 47 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 48 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 49 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 50 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 51 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 55 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 56 | #include <stdio.h> |
Simon Butcher | 4982e52 | 2016-08-24 20:20:20 +0300 | [diff] [blame] | 57 | #include <stdlib.h> |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 58 | #define mbedtls_printf printf |
| 59 | #define mbedtls_time_t time_t |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 60 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 61 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 62 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 63 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 64 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 65 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 66 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 67 | !defined(MBEDTLS_GENPRIME) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 68 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 69 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " |
| 71 | "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or " |
| 72 | "MBEDTLS_GENPRIME not defined.\n"); |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 73 | mbedtls_exit( 0 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 74 | } |
| 75 | #else |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 76 | |
| 77 | #include "mbedtls/bignum.h" |
| 78 | #include "mbedtls/entropy.h" |
| 79 | #include "mbedtls/ctr_drbg.h" |
| 80 | |
| 81 | #include <stdio.h> |
| 82 | #include <string.h> |
| 83 | |
| 84 | #define USAGE \ |
| 85 | "\n usage: dh_genprime param=<>...\n" \ |
| 86 | "\n acceprable parameters:\n" \ |
| 87 | " bits=%%d default: 2048\n" |
| 88 | |
| 89 | #define DFL_BITS 2048 |
| 90 | |
| 91 | /* |
| 92 | * Note: G = 4 is always a quadratic residue mod P, |
| 93 | * so it is a generator of order Q (with P = 2*Q+1). |
| 94 | */ |
| 95 | #define GENERATOR "4" |
| 96 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 97 | |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 98 | int main( int argc, char **argv ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 99 | { |
| 100 | int ret = 1; |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 101 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 102 | mbedtls_mpi G, P, Q; |
| 103 | mbedtls_entropy_context entropy; |
| 104 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 105 | const char *pers = "dh_genprime"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 106 | FILE *fout; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 107 | int nbits = DFL_BITS; |
| 108 | int i; |
| 109 | char *p, *q; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q ); |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 112 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 113 | mbedtls_entropy_init( &entropy ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 114 | |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 115 | if( argc == 0 ) |
| 116 | { |
| 117 | usage: |
| 118 | mbedtls_printf( USAGE ); |
makise-homura | 03b4ef6 | 2020-08-22 23:56:46 +0300 | [diff] [blame] | 119 | goto exit; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | for( i = 1; i < argc; i++ ) |
| 123 | { |
| 124 | p = argv[i]; |
| 125 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 126 | goto usage; |
| 127 | *q++ = '\0'; |
| 128 | |
| 129 | if( strcmp( p, "bits" ) == 0 ) |
| 130 | { |
| 131 | nbits = atoi( q ); |
| 132 | if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS ) |
| 133 | goto usage; |
| 134 | } |
| 135 | else |
| 136 | goto usage; |
| 137 | } |
| 138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 142 | goto exit; |
| 143 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 144 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 145 | mbedtls_printf( " ! Generating large primes may take minutes!\n" ); |
Manuel Pégourié-Gonnard | 5e1e611 | 2013-11-22 21:16:10 +0100 | [diff] [blame] | 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | fflush( stdout ); |
| 149 | |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 150 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 151 | (const unsigned char *) pers, |
| 152 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 153 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 154 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 155 | goto exit; |
| 156 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 158 | mbedtls_printf( " ok\n . Generating the modulus, please wait..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 159 | fflush( stdout ); |
| 160 | |
| 161 | /* |
| 162 | * This can take a long time... |
| 163 | */ |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 164 | if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 167 | 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] | 168 | goto exit; |
| 169 | } |
| 170 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 171 | mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 172 | fflush( stdout ); |
| 173 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 175 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 176 | 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] | 177 | goto exit; |
| 178 | } |
| 179 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 180 | if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 181 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | 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] | 183 | goto exit; |
| 184 | } |
| 185 | |
Janos Follath | a0b67c2 | 2018-09-18 14:48:23 +0100 | [diff] [blame] | 186 | if( ( ret = mbedtls_mpi_is_prime_ext( &Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 188 | 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] | 189 | goto exit; |
| 190 | } |
| 191 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 192 | mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | fflush( stdout ); |
| 194 | |
| 195 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 196 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 197 | mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 198 | goto exit; |
| 199 | } |
| 200 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || |
| 202 | ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 203 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 204 | mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret ); |
Janos Follath | 98e28a7 | 2016-05-31 14:03:54 +0100 | [diff] [blame] | 205 | fclose( fout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | goto exit; |
| 207 | } |
| 208 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 209 | mbedtls_printf( " ok\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 210 | fclose( fout ); |
| 211 | |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 212 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 213 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 214 | exit: |
| 215 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 216 | mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 217 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 218 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 220 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 221 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 222 | fflush( stdout ); getchar(); |
| 223 | #endif |
| 224 | |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 225 | mbedtls_exit( exit_code ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 226 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 227 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && |
| 228 | MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */ |