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 |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 5 | * 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 18 | */ |
| 19 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 20 | #include "mbedtls/build_info.h" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 21 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 22 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 23 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 24 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 25 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 26 | !defined(MBEDTLS_GENPRIME) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 27 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 28 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 29 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " |
| 30 | "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or " |
| 31 | "MBEDTLS_GENPRIME not defined.\n"); |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 32 | mbedtls_exit( 0 ); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 33 | } |
| 34 | #else |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 35 | |
| 36 | #include "mbedtls/bignum.h" |
| 37 | #include "mbedtls/entropy.h" |
| 38 | #include "mbedtls/ctr_drbg.h" |
| 39 | |
| 40 | #include <stdio.h> |
| 41 | #include <string.h> |
| 42 | |
| 43 | #define USAGE \ |
| 44 | "\n usage: dh_genprime param=<>...\n" \ |
Tom Cosgrove | 1797b05 | 2022-12-04 17:19:59 +0000 | [diff] [blame^] | 45 | "\n acceptable parameters:\n" \ |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 46 | " bits=%%d default: 2048\n" |
| 47 | |
| 48 | #define DFL_BITS 2048 |
| 49 | |
| 50 | /* |
| 51 | * Note: G = 4 is always a quadratic residue mod P, |
| 52 | * so it is a generator of order Q (with P = 2*Q+1). |
| 53 | */ |
| 54 | #define GENERATOR "4" |
| 55 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 56 | |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 57 | int main( int argc, char **argv ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 58 | { |
| 59 | int ret = 1; |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 60 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 61 | mbedtls_mpi G, P, Q; |
| 62 | mbedtls_entropy_context entropy; |
| 63 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 64 | const char *pers = "dh_genprime"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | FILE *fout; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 66 | int nbits = DFL_BITS; |
| 67 | int i; |
| 68 | char *p, *q; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 69 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | 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] | 71 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 72 | mbedtls_entropy_init( &entropy ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 73 | |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 74 | if( argc == 0 ) |
| 75 | { |
| 76 | usage: |
| 77 | mbedtls_printf( USAGE ); |
makise-homura | e014fec | 2020-08-22 23:56:46 +0300 | [diff] [blame] | 78 | goto exit; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | for( i = 1; i < argc; i++ ) |
| 82 | { |
| 83 | p = argv[i]; |
| 84 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 85 | goto usage; |
| 86 | *q++ = '\0'; |
| 87 | |
| 88 | if( strcmp( p, "bits" ) == 0 ) |
| 89 | { |
| 90 | nbits = atoi( q ); |
| 91 | if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS ) |
| 92 | goto usage; |
| 93 | } |
| 94 | else |
| 95 | goto usage; |
| 96 | } |
| 97 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 98 | if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 99 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 100 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 101 | goto exit; |
| 102 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 103 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | mbedtls_printf( " ! Generating large primes may take minutes!\n" ); |
Manuel Pégourié-Gonnard | 5e1e611 | 2013-11-22 21:16:10 +0100 | [diff] [blame] | 105 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 106 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 107 | fflush( stdout ); |
| 108 | |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 109 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 110 | (const unsigned char *) pers, |
| 111 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 112 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 113 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 114 | goto exit; |
| 115 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 117 | mbedtls_printf( " ok\n . Generating the modulus, please wait..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 118 | fflush( stdout ); |
| 119 | |
| 120 | /* |
| 121 | * This can take a long time... |
| 122 | */ |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 123 | if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 124 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 126 | 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] | 127 | goto exit; |
| 128 | } |
| 129 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 130 | mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 131 | fflush( stdout ); |
| 132 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 133 | if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 134 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 135 | 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] | 136 | goto exit; |
| 137 | } |
| 138 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 139 | if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | 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] | 142 | goto exit; |
| 143 | } |
| 144 | |
Janos Follath | a0b67c2 | 2018-09-18 14:48:23 +0100 | [diff] [blame] | 145 | 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] | 146 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | 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] | 148 | goto exit; |
| 149 | } |
| 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 152 | fflush( stdout ); |
| 153 | |
| 154 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 155 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 156 | mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 157 | goto exit; |
| 158 | } |
| 159 | |
ihsinme | d21ecd7 | 2022-11-08 14:30:45 +0300 | [diff] [blame] | 160 | if( ( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) ) != 0 ) || |
| 161 | ( ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) ) != 0 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 162 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 163 | 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] | 164 | fclose( fout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 165 | goto exit; |
| 166 | } |
| 167 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 168 | mbedtls_printf( " ok\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 169 | fclose( fout ); |
| 170 | |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame] | 171 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 172 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 173 | exit: |
| 174 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 175 | mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 176 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 177 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 178 | |
Krzysztof Stachowiak | 5e1b195 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 179 | mbedtls_exit( exit_code ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 180 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 181 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && |
| 182 | MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */ |