blob: 3e81d135f56ac3529e24f89c9507e51c6055db19 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000023
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020024#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
25 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
26 !defined(MBEDTLS_GENPRIME)
Rich Evans85b05ec2015-02-12 11:37:29 +000027int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000028{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029 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 Stachowiak5e1b1952019-04-24 14:24:46 +020032 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000033}
34#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020035
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 Cosgrove1797b052022-12-04 17:19:59 +000045 "\n acceptable parameters:\n" \
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020046 " 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 Butcher63cb97e2018-12-06 17:43:31 +000056
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020057int main( int argc, char **argv )
Paul Bakker5121ce52009-01-03 21:22:43 +000058{
59 int ret = 1;
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +010060 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_mpi G, P, Q;
62 mbedtls_entropy_context entropy;
63 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020064 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000065 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020066 int nbits = DFL_BITS;
67 int i;
68 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020070 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020071 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_entropy_init( &entropy );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020073
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020074 if( argc == 0 )
75 {
76 usage:
77 mbedtls_printf( USAGE );
makise-homurae014fec2020-08-22 23:56:46 +030078 goto exit;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020079 }
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é-Gonnard2cf5a7c2015-04-08 12:49:31 +020098 if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020099 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200101 goto exit;
102 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000103
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200104 mbedtls_printf( " ! Generating large primes may take minutes!\n" );
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +0100105
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200106 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 fflush( stdout );
108
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200109 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200110 (const unsigned char *) pers,
111 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000112 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200113 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000114 goto exit;
115 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 fflush( stdout );
119
120 /*
121 * This can take a long time...
122 */
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +0200123 if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000125 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 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
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200130 mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000131 fflush( stdout );
132
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200133 if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000134 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 goto exit;
137 }
138
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200139 if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 goto exit;
143 }
144
Janos Follatha0b67c22018-09-18 14:48:23 +0100145 if( ( ret = mbedtls_mpi_is_prime_ext( &Q, 50, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200147 mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 goto exit;
149 }
150
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 fflush( stdout );
153
154 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
155 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 goto exit;
158 }
159
ihsinmed21ecd72022-11-08 14:30:45 +0300160 if( ( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) ) != 0 ) ||
161 ( ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) ) != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000162 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200163 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Janos Follath98e28a72016-05-31 14:03:54 +0100164 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 goto exit;
166 }
167
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 fclose( fout );
170
Andres Amaya Garciad6bfeff2018-04-29 19:34:09 +0100171 exit_code = MBEDTLS_EXIT_SUCCESS;
172
Paul Bakker5121ce52009-01-03 21:22:43 +0000173exit:
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 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 Bakker5121ce52009-01-03 21:22:43 +0000178
Krzysztof Stachowiak5e1b1952019-04-24 14:24:46 +0200179 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 */