blob: 11c2b28c6961fec018c211b8ce29e24912e513e2 [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman16799db2023-11-02 19:47:20 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Bence Szépkútic662b362021-05-27 11:25:03 +02008#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +00009
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000010#include "mbedtls/platform.h"
Andrzej Kurek0af32482023-04-07 03:10:28 -040011/* md.h is included this early since MD_CAN_XXX macros are defined there. */
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040012#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
15 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010016 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) && \
Minos Galanakisa184fd02024-01-11 10:05:00 +000017 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C)
Andres AG788aa4a2016-09-14 14:32:09 +010018#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000019#include "mbedtls/aes.h"
20#include "mbedtls/dhm.h"
21#include "mbedtls/rsa.h"
22#include "mbedtls/sha1.h"
23#include "mbedtls/entropy.h"
24#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Rich Evans18b78c72015-02-11 14:06:19 +000026#include <stdio.h>
27#include <string.h>
28#endif
29
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020030#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000031#define PLAINTEXT "==Hello there!=="
Minos Galanakisa184fd02024-01-11 10:05:00 +000032#define MBEDTLS_MD_CAN_SHA256_MAX_SIZE 32
Paul Bakker5121ce52009-01-03 21:22:43 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
35 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010036 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Minos Galanakisa184fd02024-01-11 10:05:00 +000037 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C)
Gilles Peskine449bd832023-01-11 14:50:10 +010038int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000039{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020040 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine449bd832023-01-11 14:50:10 +010041 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010042 "MBEDTLS_MD_CAN_SHA256 and/or MBEDTLS_FS_IO and/or "
Minos Galanakisa184fd02024-01-11 10:05:00 +000043 "MBEDTLS_CTR_DRBG_C not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010044 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000045}
46#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000047
Simon Butcher63cb97e2018-12-06 17:43:31 +000048
Gilles Peskine449bd832023-01-11 14:50:10 +010049int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000050{
51 FILE *f;
52
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +010053 int ret = 1;
54 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000055 size_t n, buflen;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020056 mbedtls_net_context listen_fd, client_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000057
Paul Bakker520ea912012-10-24 14:17:01 +000058 unsigned char buf[2048];
Minos Galanakisa184fd02024-01-11 10:05:00 +000059 unsigned char hash[MBEDTLS_MD_CAN_SHA256_MAX_SIZE];
Paul Bakker5121ce52009-01-03 21:22:43 +000060 unsigned char buf2[2];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020061 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000062
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020063 mbedtls_entropy_context entropy;
64 mbedtls_ctr_drbg_context ctr_drbg;
65 mbedtls_rsa_context rsa;
66 mbedtls_dhm_context dhm;
67 mbedtls_aes_context aes;
Paul Bakker5121ce52009-01-03 21:22:43 +000068
Hanno Beckerc95fad32017-08-23 06:44:30 +010069 mbedtls_mpi N, P, Q, D, E;
70
Gilles Peskine449bd832023-01-11 14:50:10 +010071 mbedtls_net_init(&listen_fd);
72 mbedtls_net_init(&client_fd);
73 mbedtls_dhm_init(&dhm);
74 mbedtls_aes_init(&aes);
75 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker5121ce52009-01-03 21:22:43 +000076
Gilles Peskine449bd832023-01-11 14:50:10 +010077 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
78 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +010079
Paul Bakker5121ce52009-01-03 21:22:43 +000080 /*
81 * 1. Setup the RNG
82 */
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_printf("\n . Seeding the random number generator");
84 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000085
Gilles Peskine449bd832023-01-11 14:50:10 +010086 mbedtls_entropy_init(&entropy);
87 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
88 (const unsigned char *) pers,
89 strlen(pers))) != 0) {
90 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000091 goto exit;
92 }
Paul Bakker5121ce52009-01-03 21:22:43 +000093
94 /*
95 * 2a. Read the server's private RSA key
96 */
Gilles Peskine449bd832023-01-11 14:50:10 +010097 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
98 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000099
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
101 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
102 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000103 goto exit;
104 }
105
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +0000107
Gilles Peskine449bd832023-01-11 14:50:10 +0100108 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
109 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
110 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
111 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
112 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) {
113 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
114 ret);
115 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000116 goto exit;
117 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
121 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
122 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100123 goto exit;
124 }
125
Gilles Peskine449bd832023-01-11 14:50:10 +0100126 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
127 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
128 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100129 goto exit;
130 }
131
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 /*
133 * 2b. Get the DHM modulus and generator
134 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100135 mbedtls_printf("\n . Reading DH parameters from dh_prime.txt");
136 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((f = fopen("dh_prime.txt", "rb")) == NULL) {
139 mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \
140 " ! Please run dh_genprime first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000141 goto exit;
142 }
143
Gilles Peskine449bd832023-01-11 14:50:10 +0100144 if (mbedtls_mpi_read_file(&dhm.MBEDTLS_PRIVATE(P), 16, f) != 0 ||
145 mbedtls_mpi_read_file(&dhm.MBEDTLS_PRIVATE(G), 16, f) != 0) {
146 mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n");
147 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 goto exit;
149 }
150
Gilles Peskine449bd832023-01-11 14:50:10 +0100151 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000152
153 /*
154 * 3. Wait for a client to connect
155 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 mbedtls_printf("\n . Waiting for a remote connection");
157 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000158
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
160 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000161 goto exit;
162 }
163
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
165 NULL, 0, NULL)) != 0) {
166 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000167 goto exit;
168 }
169
170 /*
171 * 4. Setup the DH parameters (P,G,Ys)
172 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100173 mbedtls_printf("\n . Sending the server's DH parameters");
174 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Gilles Peskine449bd832023-01-11 14:50:10 +0100178 if ((ret =
179 mbedtls_dhm_make_params(&dhm, (int) mbedtls_mpi_size(&dhm.MBEDTLS_PRIVATE(P)), buf, &n,
180 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
181 mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 goto exit;
183 }
184
185 /*
186 * 5. Sign the parameters and send them
187 */
Minos Galanakisa184fd02024-01-11 10:05:00 +0000188 if ((ret = mbedtls_sha256(buf, n, hash, 0)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100189 mbedtls_printf(" failed\n ! mbedtls_sha1 returned %d\n\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100190 goto exit;
191 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000192
Gilles Peskine449bd832023-01-11 14:50:10 +0100193 buf[n] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len) >> 8);
194 buf[n + 1] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000195
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
Minos Galanakisa184fd02024-01-11 10:05:00 +0000197 MBEDTLS_MD_CAN_SHA256_MAX_SIZE, hash, buf + n + 2)) != 0) {
Gilles Peskine449bd832023-01-11 14:50:10 +0100198 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000199 goto exit;
200 }
201
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200202 buflen = n + 2 + rsa.MBEDTLS_PRIVATE(len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 buf2[0] = (unsigned char) (buflen >> 8);
204 buf2[1] = (unsigned char) (buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000205
Gilles Peskine449bd832023-01-11 14:50:10 +0100206 if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 ||
207 (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) {
208 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000209 goto exit;
210 }
211
212 /*
213 * 6. Get the client's public value: Yc = G ^ Xc mod P
214 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 mbedtls_printf("\n . Receiving the client's public value");
216 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Gilles Peskine449bd832023-01-11 14:50:10 +0100220 n = mbedtls_dhm_get_len(&dhm);
221 if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) {
222 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 goto exit;
224 }
225
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((ret = mbedtls_dhm_read_public(&dhm, buf, n)) != 0) {
227 mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000228 goto exit;
229 }
230
231 /*
232 * 7. Derive the shared secret: K = Ys ^ Xc mod P
233 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100234 mbedtls_printf("\n . Shared secret: ");
235 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000236
Gilles Peskine449bd832023-01-11 14:50:10 +0100237 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
238 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
239 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 goto exit;
241 }
242
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 for (n = 0; n < 16; n++) {
244 mbedtls_printf("%02x", buf[n]);
245 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000246
247 /*
248 * 8. Setup the AES-256 encryption key
249 *
250 * This is an overly simplified example; best practice is
251 * to hash the shared secret with a random value to derive
252 * the keying material for the encryption/decryption keys
253 * and MACs.
254 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 mbedtls_printf("...\n . Encrypting and sending the ciphertext");
256 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000257
Gilles Peskine449bd832023-01-11 14:50:10 +0100258 ret = mbedtls_aes_setkey_enc(&aes, buf, 256);
259 if (ret != 0) {
Gilles Peskine7820a572021-07-07 21:08:28 +0200260 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100261 }
262 memcpy(buf, PLAINTEXT, 16);
263 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf);
264 if (ret != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000265 goto exit;
266 }
267
Gilles Peskine449bd832023-01-11 14:50:10 +0100268 if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) {
269 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
270 goto exit;
271 }
272
273 mbedtls_printf("\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000274
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +0100275 exit_code = MBEDTLS_EXIT_SUCCESS;
276
Paul Bakker5121ce52009-01-03 21:22:43 +0000277exit:
278
Gilles Peskine449bd832023-01-11 14:50:10 +0100279 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
280 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100281
Gilles Peskine449bd832023-01-11 14:50:10 +0100282 mbedtls_net_free(&client_fd);
283 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200284
Gilles Peskine449bd832023-01-11 14:50:10 +0100285 mbedtls_aes_free(&aes);
286 mbedtls_rsa_free(&rsa);
287 mbedtls_dhm_free(&dhm);
288 mbedtls_ctr_drbg_free(&ctr_drbg);
289 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000292}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200293#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100294 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200295 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */