blob: 3ccfa6d9fe6d5f554339300b93565a72b6b8465b [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (client 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
Paul Bakker5121ce52009-01-03 21:22:43 +000052#define SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020053#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000054
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 server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000075
76 unsigned char *p, *end;
Paul Bakker520ea912012-10-24 14:17:01 +000077 unsigned char buf[2048];
Paul Bakker5121ce52009-01-03 21:22:43 +000078 unsigned char hash[20];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020079 const char *pers = "dh_client";
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( &server_fd );
88 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256 );
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020089 mbedtls_dhm_init( &dhm );
90 mbedtls_aes_init( &aes );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +020091 mbedtls_ctr_drbg_init( &ctr_drbg );
Paul Bakker8cfd9d82014-06-18 11:16:11 +020092
Paul Bakker5121ce52009-01-03 21:22:43 +000093 /*
94 * 1. Setup the RNG
95 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020096 mbedtls_printf( "\n . Seeding the random number generator" );
Paul Bakker5121ce52009-01-03 21:22:43 +000097 fflush( stdout );
98
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020099 mbedtls_entropy_init( &entropy );
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200100 if( ( ret = mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
Paul Bakkeref3f8c72013-06-24 13:01:08 +0200101 (const unsigned char *) pers,
102 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103 {
Manuel Pégourié-Gonnardec160c02015-04-28 22:52:30 +0200104 mbedtls_printf( " failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret );
Paul Bakker508ad5a2011-12-04 17:09:26 +0000105 goto exit;
106 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
108 /*
109 * 2. Read the server's public RSA key
110 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200111 mbedtls_printf( "\n . Reading public key from rsa_pub.txt" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000112 fflush( stdout );
113
114 if( ( f = fopen( "rsa_pub.txt", "rb" ) ) == NULL )
115 {
116 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200117 mbedtls_printf( " failed\n ! Could not open rsa_pub.txt\n" \
Paul Bakker5121ce52009-01-03 21:22:43 +0000118 " ! Please run rsa_genkey first\n\n" );
119 goto exit;
120 }
121
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200122 mbedtls_rsa_init( &rsa, MBEDTLS_RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 if( ( ret = mbedtls_mpi_read_file( &rsa.N, 16, f ) ) != 0 ||
125 ( ret = mbedtls_mpi_read_file( &rsa.E, 16, f ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000126 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 mbedtls_printf( " failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 goto exit;
129 }
130
Manuel Pégourié-Gonnardc0696c22015-06-18 16:47:17 +0200131 rsa.len = ( mbedtls_mpi_bitlen( &rsa.N ) + 7 ) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000132
133 fclose( f );
134
135 /*
136 * 3. Initiate the connection
137 */
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +0200138 mbedtls_printf( "\n . Connecting to tcp/%s/%s", SERVER_NAME,
Paul Bakker5121ce52009-01-03 21:22:43 +0000139 SERVER_PORT );
140 fflush( stdout );
141
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200142 if( ( ret = mbedtls_net_connect( &server_fd, SERVER_NAME,
143 SERVER_PORT, MBEDTLS_NET_PROTO_TCP ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200145 mbedtls_printf( " failed\n ! mbedtls_net_connect returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
149 /*
150 * 4a. First get the buffer length
151 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200152 mbedtls_printf( "\n . Receiving the server's DH parameters" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 fflush( stdout );
154
155 memset( buf, 0, sizeof( buf ) );
156
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200157 if( ( ret = mbedtls_net_recv( &server_fd, buf, 2 ) ) != 2 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000158 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200159 mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 goto exit;
161 }
162
163 n = buflen = ( buf[0] << 8 ) | buf[1];
Paul Bakker23986e52011-04-24 08:57:21 +0000164 if( buflen < 1 || buflen > sizeof( buf ) )
Paul Bakker5121ce52009-01-03 21:22:43 +0000165 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200166 mbedtls_printf( " failed\n ! Got an invalid buffer length\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 goto exit;
168 }
169
170 /*
171 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
172 */
173 memset( buf, 0, sizeof( buf ) );
174
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200175 if( ( ret = mbedtls_net_recv( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000176 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200177 mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000178 goto exit;
179 }
180
181 p = buf, end = buf + buflen;
182
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200183 if( ( ret = mbedtls_dhm_read_params( &dhm, &p, end ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200185 mbedtls_printf( " failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000186 goto exit;
187 }
188
Paul Bakker520ea912012-10-24 14:17:01 +0000189 if( dhm.len < 64 || dhm.len > 512 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000190 {
191 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200192 mbedtls_printf( " failed\n ! Invalid DHM modulus size\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 goto exit;
194 }
195
196 /*
197 * 5. Check that the server's RSA signature matches
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000198 * the SHA-256 hash of (P,G,Ys)
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200200 mbedtls_printf( "\n . Verifying the server's RSA signature" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000201 fflush( stdout );
202
Paul Bakker88f17b82012-04-26 18:52:13 +0000203 p += 2;
204
Paul Bakker23986e52011-04-24 08:57:21 +0000205 if( ( n = (size_t) ( end - p ) ) != rsa.len )
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 {
207 ret = 1;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200208 mbedtls_printf( " failed\n ! Invalid RSA signature size\n\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 goto exit;
210 }
211
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200212 mbedtls_sha1( buf, (int)( p - 2 - buf ), hash );
Paul Bakker5121ce52009-01-03 21:22:43 +0000213
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200214 if( ( ret = mbedtls_rsa_pkcs1_verify( &rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
215 MBEDTLS_MD_SHA256, 0, hash, p ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000216 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200217 mbedtls_printf( " failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000218 goto exit;
219 }
220
221 /*
222 * 6. Send our public value: Yc = G ^ Xc mod P
223 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200224 mbedtls_printf( "\n . Sending own public value to server" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 fflush( stdout );
226
227 n = dhm.len;
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200228 if( ( ret = mbedtls_dhm_make_public( &dhm, (int) dhm.len, buf, n,
229 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000230 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200231 mbedtls_printf( " failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000232 goto exit;
233 }
234
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200235 if( ( ret = mbedtls_net_send( &server_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000236 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200237 mbedtls_printf( " failed\n ! mbedtls_net_send returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 goto exit;
239 }
240
241 /*
242 * 7. Derive the shared secret: K = Ys ^ Xc mod P
243 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200244 mbedtls_printf( "\n . Shared secret: " );
Paul Bakker5121ce52009-01-03 21:22:43 +0000245 fflush( stdout );
246
Manuel Pégourié-Gonnard33352052015-06-02 16:17:08 +0100247 if( ( ret = mbedtls_dhm_calc_secret( &dhm, buf, sizeof( buf ), &n,
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200248 mbedtls_ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000249 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200250 mbedtls_printf( " failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000251 goto exit;
252 }
253
254 for( n = 0; n < 16; n++ )
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200255 mbedtls_printf( "%02x", buf[n] );
Paul Bakker5121ce52009-01-03 21:22:43 +0000256
257 /*
258 * 8. Setup the AES-256 decryption key
259 *
260 * This is an overly simplified example; best practice is
261 * to hash the shared secret with a random value to derive
262 * the keying material for the encryption/decryption keys,
263 * IVs and MACs.
264 */
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200265 mbedtls_printf( "...\n . Receiving and decrypting the ciphertext" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000266 fflush( stdout );
267
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200268 mbedtls_aes_setkey_dec( &aes, buf, 256 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
270 memset( buf, 0, sizeof( buf ) );
271
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200272 if( ( ret = mbedtls_net_recv( &server_fd, buf, 16 ) ) != 16 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000273 {
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200274 mbedtls_printf( " failed\n ! mbedtls_net_recv returned %d\n\n", ret );
Paul Bakker5121ce52009-01-03 21:22:43 +0000275 goto exit;
276 }
277
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200278 mbedtls_aes_crypt_ecb( &aes, MBEDTLS_AES_DECRYPT, buf, buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 buf[16] = '\0';
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200280 mbedtls_printf( "\n . Plaintext is \"%s\"\n\n", (char *) buf );
Paul Bakker5121ce52009-01-03 21:22:43 +0000281
282exit:
283
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +0200284 mbedtls_net_close( &server_fd );
Paul Bakker0c226102014-04-17 16:02:36 +0200285
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200286 mbedtls_aes_free( &aes );
287 mbedtls_rsa_free( &rsa );
288 mbedtls_dhm_free( &dhm );
289 mbedtls_ctr_drbg_free( &ctr_drbg );
290 mbedtls_entropy_free( &entropy );
Paul Bakker5121ce52009-01-03 21:22:43 +0000291
Paul Bakkercce9d772011-11-18 14:26:47 +0000292#if defined(_WIN32)
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293 mbedtls_printf( " + Press Enter to exit this program.\n" );
Paul Bakker5121ce52009-01-03 21:22:43 +0000294 fflush( stdout ); getchar();
295#endif
296
297 return( ret );
298}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200299#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
300 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
301 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */