blob: 0d40a1fa7161d3bd5d59e76c67621a6ee835fac0 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (prime generation)
3 *
Manuel Pégourié-Gonnard6fb81872015-07-27 11:11:48 +02004 * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
Bence Szépkúti4e9f7122020-06-05 13:02:18 +02005 * 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é-Gonnard37ff1402015-09-04 14:21:07 +020012 *
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 Bakkerb96f1542010-07-18 20:36:00 +000024 *
Bence Szépkúti4e9f7122020-06-05 13:02:18 +020025 * **********
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 * **********
45 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000046 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000047 */
48
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020049#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000050#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020051#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020053#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000054
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000056#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000057#else
Rich Evans18b78c72015-02-11 14:06:19 +000058#include <stdio.h>
Simon Butcher4982e522016-08-24 20:20:20 +030059#include <stdlib.h>
Andres Amaya Garcia74c51e62018-04-29 19:34:09 +010060#define mbedtls_printf printf
61#define mbedtls_time_t time_t
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020062#define mbedtls_exit exit
Andres Amaya Garcia2b0599b2018-04-30 22:42:33 +010063#define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
Andres Amaya Garcia74c51e62018-04-29 19:34:09 +010064#define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
65#endif /* MBEDTLS_PLATFORM_C */
Rich Evansf90016a2015-01-19 14:26:37 +000066
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020067#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
68 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
69 !defined(MBEDTLS_GENPRIME)
Rich Evans85b05ec2015-02-12 11:37:29 +000070int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000071{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
73 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
74 "MBEDTLS_GENPRIME not defined.\n");
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +020075 mbedtls_exit( 0 );
Paul Bakker5690efc2011-05-26 13:16:06 +000076}
77#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020078
79#include "mbedtls/bignum.h"
80#include "mbedtls/entropy.h"
81#include "mbedtls/ctr_drbg.h"
82
83#include <stdio.h>
84#include <string.h>
85
86#define USAGE \
87 "\n usage: dh_genprime param=<>...\n" \
88 "\n acceprable parameters:\n" \
89 " bits=%%d default: 2048\n"
90
91#define DFL_BITS 2048
92
93/*
94 * Note: G = 4 is always a quadratic residue mod P,
95 * so it is a generator of order Q (with P = 2*Q+1).
96 */
97#define GENERATOR "4"
98
99int main( int argc, char **argv )
Paul Bakker5121ce52009-01-03 21:22:43 +0000100{
101 int ret = 1;
Andres Amaya Garcia74c51e62018-04-29 19:34:09 +0100102 int exit_code = MBEDTLS_EXIT_FAILURE;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200103 mbedtls_mpi G, P, Q;
104 mbedtls_entropy_context entropy;
105 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200106 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +0000107 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +0200108 int nbits = DFL_BITS;
109 int i;
110 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200113 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200114 mbedtls_entropy_init( &entropy );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200115
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +0200116 if( argc == 0 )
117 {
118 usage:
119 mbedtls_printf( USAGE );
makise-homura00ebcbf2020-08-22 23:56:46 +0300120 goto exit;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +0200121 }
122
123 for( i = 1; i < argc; i++ )
124 {
125 p = argv[i];
126 if( ( q = strchr( p, '=' ) ) == NULL )
127 goto usage;
128 *q++ = '\0';
129
130 if( strcmp( p, "bits" ) == 0 )
131 {
132 nbits = atoi( q );
133 if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS )
134 goto usage;
135 }
136 else
137 goto usage;
138 }
139
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200140 if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200141 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200143 goto exit;
144 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000145
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " ! Generating large primes may take minutes!\n" );
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +0100147
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200148 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000149 fflush( stdout );
150
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200151 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200152 (const unsigned char *) pers,
153 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000154 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200155 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000156 goto exit;
157 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 fflush( stdout );
161
162 /*
163 * This can take a long time...
164 */
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +0200165 if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 goto exit;
170 }
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 fflush( stdout );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 goto exit;
179 }
180
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200181 if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 goto exit;
185 }
186
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 if( ( ret = mbedtls_mpi_is_prime( &Q, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200189 mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 goto exit;
191 }
192
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200193 mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 fflush( stdout );
195
196 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
197 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200198 mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 goto exit;
200 }
201
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
203 ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000204 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200205 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Janos Follath98e28a72016-05-31 14:03:54 +0100206 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000207 goto exit;
208 }
209
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 fclose( fout );
212
Andres Amaya Garcia74c51e62018-04-29 19:34:09 +0100213 exit_code = MBEDTLS_EXIT_SUCCESS;
214
Paul Bakker5121ce52009-01-03 21:22:43 +0000215exit:
216
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
218 mbedtls_ctr_drbg_free( &ctr_drbg );
219 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Paul Bakkercce9d772011-11-18 14:26:47 +0000221#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200222 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 fflush( stdout ); getchar();
224#endif
225
Krzysztof Stachowiaka0865222019-04-24 14:24:46 +0200226 mbedtls_exit( exit_code );
Paul Bakker5121ce52009-01-03 21:22:43 +0000227}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
229 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */