blob: 895326b6ba6539e28d093716149619e4988ebe20 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
Manuel Pégourié-Gonnard0edee5e2015-01-26 15:29:40 +00004 * Copyright (C) 2006-2011, ARM Limited, All Rights Reserved
Paul Bakkerb96f1542010-07-18 20:36:00 +00005 *
Manuel Pégourié-Gonnarde12abf92015-01-28 17:13:45 +00006 * This file is part of mbed TLS (https://polarssl.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
23#ifndef _CRT_SECURE_NO_DEPRECATE
24#define _CRT_SECURE_NO_DEPRECATE 1
25#endif
26
27#include <string.h>
28#include <stdio.h>
29
Paul Bakker5690efc2011-05-26 13:16:06 +000030#include "polarssl/config.h"
31
Paul Bakker40e46942009-01-03 21:51:57 +000032#include "polarssl/net.h"
33#include "polarssl/aes.h"
34#include "polarssl/dhm.h"
35#include "polarssl/rsa.h"
36#include "polarssl/sha1.h"
Paul Bakker508ad5a2011-12-04 17:09:26 +000037#include "polarssl/entropy.h"
38#include "polarssl/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000039
40#define SERVER_PORT 11999
41#define PLAINTEXT "==Hello there!=="
42
Paul Bakker5690efc2011-05-26 13:16:06 +000043#if !defined(POLARSSL_AES_C) || !defined(POLARSSL_DHM_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000044 !defined(POLARSSL_ENTROPY_C) || !defined(POLARSSL_NET_C) || \
Paul Bakker5690efc2011-05-26 13:16:06 +000045 !defined(POLARSSL_RSA_C) || !defined(POLARSSL_SHA1_C) || \
Paul Bakker508ad5a2011-12-04 17:09:26 +000046 !defined(POLARSSL_FS_IO) || !defined(POLARSSL_CTR_DRBG_C)
Paul Bakkercce9d772011-11-18 14:26:47 +000047int main( int argc, char *argv[] )
Paul Bakker5690efc2011-05-26 13:16:06 +000048{
Paul Bakkercce9d772011-11-18 14:26:47 +000049 ((void) argc);
50 ((void) argv);
51
Paul Bakker508ad5a2011-12-04 17:09:26 +000052 printf("POLARSSL_AES_C and/or POLARSSL_DHM_C and/or POLARSSL_ENTROPY_C "
Paul Bakker5690efc2011-05-26 13:16:06 +000053 "and/or POLARSSL_NET_C and/or POLARSSL_RSA_C and/or "
Paul Bakker508ad5a2011-12-04 17:09:26 +000054 "POLARSSL_SHA1_C and/or POLARSSL_FS_IO and/or "
55 "POLARSSL_CTR_DBRG_C not defined.\n");
Paul Bakker5690efc2011-05-26 13:16:06 +000056 return( 0 );
57}
58#else
Paul Bakkercce9d772011-11-18 14:26:47 +000059int main( int argc, char *argv[] )
Paul Bakker5121ce52009-01-03 21:22:43 +000060{
61 FILE *f;
62
Paul Bakker23986e52011-04-24 08:57:21 +000063 int ret;
64 size_t n, buflen;
Paul Bakker5121ce52009-01-03 21:22:43 +000065 int listen_fd = -1;
66 int client_fd = -1;
67
Paul Bakker520ea912012-10-24 14:17:01 +000068 unsigned char buf[2048];
Paul Bakker5121ce52009-01-03 21:22:43 +000069 unsigned char hash[20];
70 unsigned char buf2[2];
Paul Bakkere0225e42013-06-06 12:52:24 +020071 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker508ad5a2011-12-04 17:09:26 +000073 entropy_context entropy;
74 ctr_drbg_context ctr_drbg;
Paul Bakker5121ce52009-01-03 21:22:43 +000075 rsa_context rsa;
76 dhm_context dhm;
77 aes_context aes;
78
Paul Bakkercce9d772011-11-18 14:26:47 +000079 ((void) argc);
80 ((void) argv);
81
Paul Bakker5121ce52009-01-03 21:22:43 +000082 memset( &rsa, 0, sizeof( rsa ) );
83 memset( &dhm, 0, sizeof( dhm ) );
84
85 /*
86 * 1. Setup the RNG
87 */
88 printf( "\n . Seeding the random number generator" );
89 fflush( stdout );
90
Paul Bakker508ad5a2011-12-04 17:09:26 +000091 entropy_init( &entropy );
92 if( ( ret = ctr_drbg_init( &ctr_drbg, entropy_func, &entropy,
Paul Bakkere0225e42013-06-06 12:52:24 +020093 (const unsigned char *) pers,
94 strlen( pers ) ) ) != 0 )
Paul Bakker508ad5a2011-12-04 17:09:26 +000095 {
96 printf( " failed\n ! ctr_drbg_init returned %d\n", ret );
97 goto exit;
98 }
Paul Bakker5121ce52009-01-03 21:22:43 +000099
100 /*
101 * 2a. Read the server's private RSA key
102 */
103 printf( "\n . Reading private key from rsa_priv.txt" );
104 fflush( stdout );
105
106 if( ( f = fopen( "rsa_priv.txt", "rb" ) ) == NULL )
107 {
108 ret = 1;
109 printf( " failed\n ! Could not open rsa_priv.txt\n" \
110 " ! Please run rsa_genkey first\n\n" );
111 goto exit;
112 }
113
Paul Bakkera802e1a2010-08-16 11:56:45 +0000114 rsa_init( &rsa, RSA_PKCS_V15, 0 );
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
116 if( ( ret = mpi_read_file( &rsa.N , 16, f ) ) != 0 ||
117 ( ret = mpi_read_file( &rsa.E , 16, f ) ) != 0 ||
118 ( ret = mpi_read_file( &rsa.D , 16, f ) ) != 0 ||
119 ( ret = mpi_read_file( &rsa.P , 16, f ) ) != 0 ||
120 ( ret = mpi_read_file( &rsa.Q , 16, f ) ) != 0 ||
121 ( ret = mpi_read_file( &rsa.DP, 16, f ) ) != 0 ||
122 ( ret = mpi_read_file( &rsa.DQ, 16, f ) ) != 0 ||
123 ( ret = mpi_read_file( &rsa.QP, 16, f ) ) != 0 )
124 {
125 printf( " failed\n ! mpi_read_file returned %d\n\n", ret );
126 goto exit;
127 }
128
129 rsa.len = ( mpi_msb( &rsa.N ) + 7 ) >> 3;
130
131 fclose( f );
132
133 /*
134 * 2b. Get the DHM modulus and generator
135 */
136 printf( "\n . Reading DH parameters from dh_prime.txt" );
137 fflush( stdout );
138
139 if( ( f = fopen( "dh_prime.txt", "rb" ) ) == NULL )
140 {
141 ret = 1;
142 printf( " failed\n ! Could not open dh_prime.txt\n" \
143 " ! Please run dh_genprime first\n\n" );
144 goto exit;
145 }
146
147 if( mpi_read_file( &dhm.P, 16, f ) != 0 ||
148 mpi_read_file( &dhm.G, 16, f ) != 0 )
149 {
150 printf( " failed\n ! Invalid DH parameter file\n\n" );
151 goto exit;
152 }
153
154 fclose( f );
155
156 /*
157 * 3. Wait for a client to connect
158 */
159 printf( "\n . Waiting for a remote connection" );
160 fflush( stdout );
161
162 if( ( ret = net_bind( &listen_fd, NULL, SERVER_PORT ) ) != 0 )
163 {
164 printf( " failed\n ! net_bind returned %d\n\n", ret );
165 goto exit;
166 }
167
168 if( ( ret = net_accept( listen_fd, &client_fd, NULL ) ) != 0 )
169 {
170 printf( " failed\n ! net_accept returned %d\n\n", ret );
171 goto exit;
172 }
173
174 /*
175 * 4. Setup the DH parameters (P,G,Ys)
176 */
177 printf( "\n . Sending the server's DH parameters" );
178 fflush( stdout );
179
180 memset( buf, 0, sizeof( buf ) );
181
Paul Bakker07488952013-11-30 15:14:38 +0100182 if( ( ret = dhm_make_params( &dhm, (int) mpi_size( &dhm.P ), buf, &n,
Paul Bakker508ad5a2011-12-04 17:09:26 +0000183 ctr_drbg_random, &ctr_drbg ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000184 {
185 printf( " failed\n ! dhm_make_params returned %d\n\n", ret );
186 goto exit;
187 }
188
189 /*
190 * 5. Sign the parameters and send them
191 */
192 sha1( buf, n, hash );
193
194 buf[n ] = (unsigned char)( rsa.len >> 8 );
195 buf[n + 1] = (unsigned char)( rsa.len );
196
Paul Bakker43f97992013-09-23 11:23:31 +0200197 if( ( ret = rsa_pkcs1_sign( &rsa, ctr_drbg_random, &ctr_drbg, RSA_PRIVATE,
198 SIG_RSA_SHA1, 0, hash, buf + n + 2 ) ) != 0 )
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 {
200 printf( " failed\n ! rsa_pkcs1_sign returned %d\n\n", ret );
201 goto exit;
202 }
203
204 buflen = n + 2 + rsa.len;
205 buf2[0] = (unsigned char)( buflen >> 8 );
206 buf2[1] = (unsigned char)( buflen );
207
208 if( ( ret = net_send( &client_fd, buf2, 2 ) ) != 2 ||
Paul Bakker23986e52011-04-24 08:57:21 +0000209 ( ret = net_send( &client_fd, buf, buflen ) ) != (int) buflen )
Paul Bakker5121ce52009-01-03 21:22:43 +0000210 {
211 printf( " failed\n ! net_send returned %d\n\n", ret );
212 goto exit;
213 }
214
215 /*
216 * 6. Get the client's public value: Yc = G ^ Xc mod P
217 */
218 printf( "\n . Receiving the client's public value" );
219 fflush( stdout );
220
221 memset( buf, 0, sizeof( buf ) );
222 n = dhm.len;
223
Paul Bakker23986e52011-04-24 08:57:21 +0000224 if( ( ret = net_recv( &client_fd, buf, n ) ) != (int) n )
Paul Bakker5121ce52009-01-03 21:22:43 +0000225 {
226 printf( " failed\n ! net_recv returned %d\n\n", ret );
227 goto exit;
228 }
229
230 if( ( ret = dhm_read_public( &dhm, buf, dhm.len ) ) != 0 )
231 {
232 printf( " failed\n ! dhm_read_public returned %d\n\n", ret );
233 goto exit;
234 }
235
236 /*
237 * 7. Derive the shared secret: K = Ys ^ Xc mod P
238 */
239 printf( "\n . Shared secret: " );
240 fflush( stdout );
241
242 if( ( ret = dhm_calc_secret( &dhm, buf, &n ) ) != 0 )
243 {
244 printf( " failed\n ! dhm_calc_secret returned %d\n\n", ret );
245 goto exit;
246 }
247
248 for( n = 0; n < 16; n++ )
249 printf( "%02x", buf[n] );
250
251 /*
252 * 8. Setup the AES-256 encryption key
253 *
254 * This is an overly simplified example; best practice is
255 * to hash the shared secret with a random value to derive
256 * the keying material for the encryption/decryption keys
257 * and MACs.
258 */
259 printf( "...\n . Encrypting and sending the ciphertext" );
260 fflush( stdout );
261
262 aes_setkey_enc( &aes, buf, 256 );
263 memcpy( buf, PLAINTEXT, 16 );
264 aes_crypt_ecb( &aes, AES_ENCRYPT, buf, buf );
265
266 if( ( ret = net_send( &client_fd, buf, 16 ) ) != 16 )
267 {
268 printf( " failed\n ! net_send returned %d\n\n", ret );
269 goto exit;
270 }
271
272 printf( "\n\n" );
273
274exit:
275
Paul Bakker39148402014-04-17 16:02:36 +0200276 if( client_fd != -1 )
277 net_close( client_fd );
278
Paul Bakker5121ce52009-01-03 21:22:43 +0000279 rsa_free( &rsa );
280 dhm_free( &dhm );
281
Paul Bakkercce9d772011-11-18 14:26:47 +0000282#if defined(_WIN32)
Paul Bakker5121ce52009-01-03 21:22:43 +0000283 printf( " + Press Enter to exit this program.\n" );
284 fflush( stdout ); getchar();
285#endif
286
287 return( ret );
288}
Paul Bakker508ad5a2011-12-04 17:09:26 +0000289#endif /* POLARSSL_AES_C && POLARSSL_DHM_C && POLARSSL_ENTROPY_C &&
Paul Bakker5690efc2011-05-26 13:16:06 +0000290 POLARSSL_NET_C && POLARSSL_RSA_C && POLARSSL_SHA1_C &&
Paul Bakker508ad5a2011-12-04 17:09:26 +0000291 POLARSSL_FS_IO && POLARSSL_CTR_DRBG_C */