blob: 84a94a18bd269ded50c2b5cad81461d95f874516 [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
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 Bakkerb96f1542010-07-18 20:36:00 +000018 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +000019 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakker5121ce52009-01-03 21:22:43 +000020 */
21
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000023#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020026#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000029#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000030#else
Rich Evans18b78c72015-02-11 14:06:19 +000031#include <stdio.h>
Simon Butcher4982e522016-08-24 20:20:20 +030032#include <stdlib.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
-~- redtangent ~-~9fa2e862016-05-26 10:07:49 +010034#define mbedtls_time_t time_t
Rich Evansf90016a2015-01-19 14:26:37 +000035#endif
36
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020037#if !defined(MBEDTLS_BIGNUM_C) || !defined(MBEDTLS_ENTROPY_C) || \
38 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
39 !defined(MBEDTLS_GENPRIME)
Rich Evans85b05ec2015-02-12 11:37:29 +000040int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000041{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020042 mbedtls_printf("MBEDTLS_BIGNUM_C and/or MBEDTLS_ENTROPY_C and/or "
43 "MBEDTLS_FS_IO and/or MBEDTLS_CTR_DRBG_C and/or "
44 "MBEDTLS_GENPRIME not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000045 return( 0 );
46}
47#else
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020048
49#include "mbedtls/bignum.h"
50#include "mbedtls/entropy.h"
51#include "mbedtls/ctr_drbg.h"
52
53#include <stdio.h>
54#include <string.h>
55
56#define USAGE \
57 "\n usage: dh_genprime param=<>...\n" \
58 "\n acceprable parameters:\n" \
59 " bits=%%d default: 2048\n"
60
61#define DFL_BITS 2048
62
63/*
64 * Note: G = 4 is always a quadratic residue mod P,
65 * so it is a generator of order Q (with P = 2*Q+1).
66 */
67#define GENERATOR "4"
68
69int main( int argc, char **argv )
Paul Bakker5121ce52009-01-03 21:22:43 +000070{
71 int ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020072 mbedtls_mpi G, P, Q;
73 mbedtls_entropy_context entropy;
74 mbedtls_ctr_drbg_context ctr_drbg;
Paul Bakkeref3f8c72013-06-24 13:01:08 +020075 const char *pers = "dh_genprime";
Paul Bakker5121ce52009-01-03 21:22:43 +000076 FILE *fout;
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020077 int nbits = DFL_BITS;
78 int i;
79 char *p, *q;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_mpi_init( &G ); mbedtls_mpi_init( &P ); mbedtls_mpi_init( &Q );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020082 mbedtls_ctr_drbg_init( &ctr_drbg );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020083 mbedtls_entropy_init( &entropy );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +020084
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +020085 if( argc == 0 )
86 {
87 usage:
88 mbedtls_printf( USAGE );
89 return( 1 );
90 }
91
92 for( i = 1; i < argc; i++ )
93 {
94 p = argv[i];
95 if( ( q = strchr( p, '=' ) ) == NULL )
96 goto usage;
97 *q++ = '\0';
98
99 if( strcmp( p, "bits" ) == 0 )
100 {
101 nbits = atoi( q );
102 if( nbits < 0 || nbits > MBEDTLS_MPI_MAX_BITS )
103 goto usage;
104 }
105 else
106 goto usage;
107 }
108
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200109 if( ( ret = mbedtls_mpi_read_string( &G, 10, GENERATOR ) ) != 0 )
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200110 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( " failed\n ! mbedtls_mpi_read_string returned %d\n", ret );
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200112 goto exit;
113 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 mbedtls_printf( " ! Generating large primes may take minutes!\n" );
Manuel Pégourié-Gonnard5e1e6112013-11-22 21:16:10 +0100116
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( "\n . Seeding the random number generator..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 fflush( stdout );
119
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200120 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200121 (const unsigned char *) pers,
122 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000123 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200124 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000125 goto exit;
126 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000127
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200128 mbedtls_printf( " ok\n . Generating the modulus, please wait..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000129 fflush( stdout );
130
131 /*
132 * This can take a long time...
133 */
Manuel Pégourié-Gonnard77c65622015-07-03 16:57:52 +0200134 if( ( ret = mbedtls_mpi_gen_prime( &P, nbits, 1,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200135 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000136 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200137 mbedtls_printf( " failed\n ! mbedtls_mpi_gen_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000138 goto exit;
139 }
140
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200141 mbedtls_printf( " ok\n . Verifying that Q = (P-1)/2 is prime..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000142 fflush( stdout );
143
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200144 if( ( ret = mbedtls_mpi_sub_int( &Q, &P, 1 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000145 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200146 mbedtls_printf( " failed\n ! mbedtls_mpi_sub_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000147 goto exit;
148 }
149
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200150 if( ( ret = mbedtls_mpi_div_int( &Q, NULL, &Q, 2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000151 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( " failed\n ! mbedtls_mpi_div_int returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 goto exit;
154 }
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 if( ( ret = mbedtls_mpi_is_prime( &Q, mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200158 mbedtls_printf( " failed\n ! mbedtls_mpi_is_prime returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000159 goto exit;
160 }
161
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200162 mbedtls_printf( " ok\n . Exporting the value in dh_prime.txt..." );
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 fflush( stdout );
164
165 if( ( fout = fopen( "dh_prime.txt", "wb+" ) ) == NULL )
166 {
167 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( " failed\n ! Could not create dh_prime.txt\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 goto exit;
170 }
171
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200172 if( ( ret = mbedtls_mpi_write_file( "P = ", &P, 16, fout ) != 0 ) ||
173 ( ret = mbedtls_mpi_write_file( "G = ", &G, 16, fout ) != 0 ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 mbedtls_printf( " failed\n ! mbedtls_mpi_write_file returned %d\n\n", ret );
Janos Follath98e28a72016-05-31 14:03:54 +0100176 fclose( fout );
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 goto exit;
178 }
179
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( " ok\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 fclose( fout );
182
183exit:
184
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_mpi_free( &G ); mbedtls_mpi_free( &P ); mbedtls_mpi_free( &Q );
186 mbedtls_ctr_drbg_free( &ctr_drbg );
187 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188
Paul Bakkercce9d772011-11-18 14:26:47 +0000189#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200190 mbedtls_printf( " Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000191 fflush( stdout ); getchar();
192#endif
193
194 return( ret );
195}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200196#endif /* MBEDTLS_BIGNUM_C && MBEDTLS_ENTROPY_C && MBEDTLS_FS_IO &&
197 MBEDTLS_CTR_DRBG_C && MBEDTLS_GENPRIME */