Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (prime generation) |
| 3 | * |
Manuel Pégourié-Gonnard | 6fb8187 | 2015-07-27 11:11:48 +0200 | [diff] [blame] | 4 | * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved |
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 | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 18 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 19 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 20 | */ |
| 21 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 22 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 23 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 24 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 25 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 26 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 27 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 28 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 29 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 30 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 31 | #include <stdio.h> |
Simon Butcher | 4982e52 | 2016-08-24 20:20:20 +0300 | [diff] [blame] | 32 | #include <stdlib.h> |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame^] | 33 | #define mbedtls_printf printf |
| 34 | #define mbedtls_time_t time_t |
| 35 | #define MBEDTLS_EXTI_SUCCESS EXIT_SUCCESS |
| 36 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 37 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 38 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 39 | #if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \ |
| 40 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 41 | !defined(MBEDTLS_GENPRIME) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 42 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 43 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or " |
| 45 | "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or " |
| 46 | "MBEDTLS_GENPRIME not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 47 | return( 0 ); |
| 48 | } |
| 49 | #else |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 50 | |
| 51 | #include "mbedtls/bignum.h" |
| 52 | #include "mbedtls/entropy.h" |
| 53 | #include "mbedtls/ctr_drbg.h" |
| 54 | |
| 55 | #include <stdio.h> |
| 56 | #include <string.h> |
| 57 | |
| 58 | #define USAGE \ |
| 59 | "\n usage: dh_genprime param=<>...\n" \ |
| 60 | "\n acceprable parameters:\n" \ |
| 61 | " bits=%%d default: 2048\n" |
| 62 | |
| 63 | #define DFL_BITS 2048 |
| 64 | |
| 65 | /* |
| 66 | * Note: G = 4 is always a quadratic residue mod P, |
| 67 | * so it is a generator of order Q (with P = 2*Q+1). |
| 68 | */ |
| 69 | #define GENERATOR "4" |
| 70 | |
| 71 | int main( int argc, char **argv ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 72 | { |
| 73 | int ret = 1; |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame^] | 74 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | mbedtls_mpi G, P, Q; |
| 76 | mbedtls_entropy_context entropy; |
| 77 | mbedtls_ctr_drbg_context ctr_drbg; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 78 | const char *pers = "dh_genprime"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 79 | FILE *fout; |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 80 | int nbits = DFL_BITS; |
| 81 | int i; |
| 82 | char *p, *q; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 84 | 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] | 85 | mbedtls_ctr_drbg_init( &ctr_drbg ); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 86 | mbedtls_entropy_init( &entropy ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 87 | |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 88 | if( argc == 0 ) |
| 89 | { |
| 90 | usage: |
| 91 | mbedtls_printf( USAGE ); |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame^] | 92 | return( exit_code ); |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | for( i = 1; i < argc; i++ ) |
| 96 | { |
| 97 | p = argv[i]; |
| 98 | if( ( q = strchr( p, '=' ) ) == NULL ) |
| 99 | goto usage; |
| 100 | *q++ = '\0'; |
| 101 | |
| 102 | if( strcmp( p, "bits" ) == 0 ) |
| 103 | { |
| 104 | nbits = atoi( q ); |
| 105 | if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS ) |
| 106 | goto usage; |
| 107 | } |
| 108 | else |
| 109 | goto usage; |
| 110 | } |
| 111 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 112 | if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 113 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 114 | mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 115 | goto exit; |
| 116 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 117 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 118 | mbedtls_printf( " ! Generating large primes may take minutes!\n" ); |
Manuel Pégourié-Gonnard | 5e1e611 | 2013-11-22 21:16:10 +0100 | [diff] [blame] | 119 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 120 | mbedtls_printf( "\n . Seeding the random number generator..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 121 | fflush( stdout ); |
| 122 | |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 123 | if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy, |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 124 | (const unsigned char *) pers, |
| 125 | strlen( pers ) ) ) != 0 ) |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 126 | { |
Manuel Pégourié-Gonnard | ec160c0 | 2015-04-28 22:52:30 +0200 | [diff] [blame] | 127 | mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret ); |
Paul Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 128 | goto exit; |
| 129 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 130 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 131 | mbedtls_printf( " ok\n . Generating the modulus, please wait..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 132 | fflush( stdout ); |
| 133 | |
| 134 | /* |
| 135 | * This can take a long time... |
| 136 | */ |
Manuel Pégourié-Gonnard | 77c6562 | 2015-07-03 16:57:52 +0200 | [diff] [blame] | 137 | if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 138 | mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 139 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 140 | 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] | 141 | goto exit; |
| 142 | } |
| 143 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 144 | mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 145 | fflush( stdout ); |
| 146 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 147 | if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 148 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 149 | 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] | 150 | goto exit; |
| 151 | } |
| 152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 154 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 155 | 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] | 156 | goto exit; |
| 157 | } |
| 158 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 159 | if( ( ret = mbedtls_mpi_is_prime( &Q, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 160 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 161 | 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] | 162 | goto exit; |
| 163 | } |
| 164 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 165 | mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 166 | fflush( stdout ); |
| 167 | |
| 168 | if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL ) |
| 169 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 170 | mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 171 | goto exit; |
| 172 | } |
| 173 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 174 | if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) || |
| 175 | ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 176 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 177 | 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] | 178 | fclose( fout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 179 | goto exit; |
| 180 | } |
| 181 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 182 | mbedtls_printf( " ok\n\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 183 | fclose( fout ); |
| 184 | |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame^] | 185 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 186 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 187 | exit: |
| 188 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q ); |
| 190 | mbedtls_ctr_drbg_free( &ctr_drbg ); |
| 191 | mbedtls_entropy_free( &entropy ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 192 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 193 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 194 | mbedtls_printf( " Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 195 | fflush( stdout ); getchar(); |
| 196 | #endif |
| 197 | |
Andres Amaya Garcia | d6bfeff | 2018-04-29 19:34:09 +0100 | [diff] [blame^] | 198 | return( exit_code ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 199 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 200 | #endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO && |
| 201 | MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */ |