blob: 71fa73704bfa1adaad61134fa87de719c4148449 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
Manuel Pégourié-Gonnarda658a402015-01-23 09:45:19 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnardfe446432015-03-06 13:17:10 +00006 * This file is part of mbed TLS (https://tls.mbed.org)
Paul Bakkere0ccd0a2009-01-04 16:27:10 +00007 *
Paul Bakker5121ce52009-01-03 21:22:43 +00008 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
12 *
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
17 *
18 * You should have received a copy of the GNU General Public License along
19 * with this program; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
21 */
22
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020025#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020027#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#if defined(MBEDTLS_PLATFORM_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000030#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000031#else
Rich Evans18b78c72015-02-11 14:06:19 +000032#include <stdio.h>
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020033#define mbedtls_printf printf
Rich Evansf90016a2015-01-19 14:26:37 +000034#endif
35
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020036#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
37 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
38 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
39 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000040#include "mbedtls/net.h"
41#include "mbedtls/aes.h"
42#include "mbedtls/dhm.h"
43#include "mbedtls/rsa.h"
44#include "mbedtls/sha1.h"
45#include "mbedtls/entropy.h"
46#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000047
Rich Evans18b78c72015-02-11 14:06:19 +000048#include <stdio.h>
49#include <string.h>
50#endif
51
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020052#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000053#define PLAINTEXT "==Hello there!=="
54
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
56 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
57 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
58 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Rich Evans85b05ec2015-02-12 11:37:29 +000059int main( void )
Paul Bakker5690efc2011-05-26 13:16:06 +000060{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020061 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
62 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
63 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
64 "MBEDTLS_CTR_DRBG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000065 return( 0 );
66}
67#else
Rich Evans85b05ec2015-02-12 11:37:29 +000068int main( void )
Paul Bakker5121ce52009-01-03 21:22:43 +000069{
70 FILE *f;
71
Paul Bakker23986e52011-04-24 08:57:21 +000072 int ret;
73 size_t n, buflen;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020074 mbedtls_net_context listen_fd, client_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000075
Paul Bakker520ea912012-10-24 14:17:01 +000076 unsigned char buf[2048];
Paul Bakker5121ce52009-01-03 21:22:43 +000077 unsigned char hash[20];
78 unsigned char buf2[2];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020081 mbedtls_entropy_context entropy;
82 mbedtls_ctr_drbg_context ctr_drbg;
83 mbedtls_rsa_context rsa;
84 mbedtls_dhm_context dhm;
85 mbedtls_aes_context aes;
Paul Bakker5121ce52009-01-03 21:22:43 +000086
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020087 mbedtls_net_init( &listen_fd );
88 mbedtls_net_init( &client_fd );
89 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020090 mbedtls_dhm_init( &dhm );
91 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020092 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 /*
95 * 1. Setup the RNG
96 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020097 mbedtls_printf( "\n . Seeding the random number generator" );
Paul Bakker5121ce52009-01-03 21:22:43 +000098 fflush( stdout );
99
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200100 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200101 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200102 (const unsigned char *) pers,
103 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000104 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200105 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000106 goto exit;
107 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000108
109 /*
110 * 2a. Read the server's private RSA key
111 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 mbedtls_printf( "\n . Reading private key from rsa_priv.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000113 fflush( stdout );
114
115 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
116 {
117 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 mbedtls_printf( " failed\n ! Could not open rsa_priv.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 " ! Please run rsa_genkey first\n\n" );
120 goto exit;
121 }
122
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200123 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000124
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200125 if( ( ret = mbedtls_mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
126 ( ret = mbedtls_mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
127 ( ret = mbedtls_mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
128 ( ret = mbedtls_mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
129 ( ret = mbedtls_mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
130 ( ret = mbedtls_mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
131 ( ret = mbedtls_mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
132 ( ret = mbedtls_mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000133 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200134 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000135 goto exit;
136 }
137
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200138 rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
Manuel Pégourié-Gonnard6c5abfa2015-02-13 14:12:07 +0000139
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 fclose( f );
141
142 /*
143 * 2b. Get the DHM modulus and generator
144 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( "\n . Reading DH parameters from dh_prime.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 fflush( stdout );
147
148 if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
149 {
150 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200151 mbedtls_printf( " failed\n ! Could not open dh_prime.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +0000152 " ! Please run dh_genprime first\n\n" );
153 goto exit;
154 }
155
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200156 if( mbedtls_mpi_read_file( &dhm.P, 16, f ) != 0 ||
157 mbedtls_mpi_read_file( &dhm.G, 16, f ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( " failed\n ! Invalid DH parameter file\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 goto exit;
161 }
162
163 fclose( f );
164
165 /*
166 * 3. Wait for a client to connect
167 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200168 mbedtls_printf( "\n . Waiting for a remote connection" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000169 fflush( stdout );
170
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200171 if( ( ret = mbedtls_net_bind( &listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000172 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200173 mbedtls_printf( " failed\n ! mbedtls_net_bind returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 goto exit;
175 }
176
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200177 if( ( ret = mbedtls_net_accept( &listen_fd, &client_fd,
Manuel Pégourié-Gonnard0b104b02015-05-14 21:52:40 +0200178 NULL, 0, NULL ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200180 mbedtls_printf( " failed\n ! mbedtls_net_accept returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000181 goto exit;
182 }
183
184 /*
185 * 4. Setup the DH parameters (P,G,Ys)
186 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200187 mbedtls_printf( "\n . Sending the server's DH parameters" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000188 fflush( stdout );
189
190 memset( buf, 0, sizeof( buf ) );
191
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 if( ( ret = mbedtls_dhm_make_params( &dhm, (int) mbedtls_mpi_size( &dhm.P ), buf, &n,
193 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200195 mbedtls_printf( " failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000196 goto exit;
197 }
198
199 /*
200 * 5. Sign the parameters and send them
201 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200202 mbedtls_sha1( buf, n, hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000203
204 buf[n ] = (unsigned char)( rsa.len >> 8 );
205 buf[n + 1] = (unsigned char)( rsa.len );
206
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200207 if( ( ret = mbedtls_rsa_pkcs1_sign( &rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
Paul Bakker5121ce52009-01-03 21:22:43 +0000208 0, hash, buf + n + 2 ) ) != 0 )
209 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200210 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 goto exit;
212 }
213
214 buflen = n + 2 + rsa.len;
215 buf2[0] = (unsigned char)( buflen >> 8 );
216 buf2[1] = (unsigned char)( buflen );
217
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200218 if( ( ret = mbedtls_net_send( &client_fd, buf2, 2 ) ) != 2 ||
219 ( ret = mbedtls_net_send( &client_fd, buf, buflen ) ) != (int) buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000220 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200221 mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000222 goto exit;
223 }
224
225 /*
226 * 6. Get the client's public value: Yc = G ^ Xc mod P
227 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 mbedtls_printf( "\n . Receiving the client's public value" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000229 fflush( stdout );
230
231 memset( buf, 0, sizeof( buf ) );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200233 if( ( ret = mbedtls_net_recv( &client_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000234 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 goto exit;
237 }
238
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200239 if( ( ret = mbedtls_dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200241 mbedtls_printf( " failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000242 goto exit;
243 }
244
245 /*
246 * 7. Derive the shared secret: K = Ys ^ Xc mod P
247 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_printf( "\n . Shared secret: " );
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 fflush( stdout );
250
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100251 if( ( ret = mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &n,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200252 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000253 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200254 mbedtls_printf( " failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000255 goto exit;
256 }
257
258 for( n = 0; n < 16; n++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200259 mbedtls_printf( "%02x", buf[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000260
261 /*
262 * 8. Setup the AES-256 encryption key
263 *
264 * This is an overly simplified example; best practice is
265 * to hash the shared secret with a random value to derive
266 * the keying material for the encryption/decryption keys
267 * and MACs.
268 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200269 mbedtls_printf( "...\n . Encrypting and sending the ciphertext" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000270 fflush( stdout );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 mbedtls_aes_setkey_enc( &aes, buf, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 memcpy( buf, PLAINTEXT, 16 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_ENCRYPT, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200276 if( ( ret = mbedtls_net_send( &client_fd, buf, 16 ) ) != 16 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 goto exit;
280 }
281
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200282 mbedtls_printf( "\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000283
284exit:
285
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200286 mbedtls_net_close( &client_fd );
287 mbedtls_net_close( &listen_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200288
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200289 mbedtls_aes_free( &aes );
290 mbedtls_rsa_free( &rsa );
291 mbedtls_dhm_free( &dhm );
292 mbedtls_ctr_drbg_free( &ctr_drbg );
293 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294
Paul Bakkercce9d772011-11-18 14:26:47 +0000295#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200296 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000297 fflush( stdout ); getchar();
298#endif
299
300 return( ret );
301}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200302#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
303 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
304 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */