blob: 0200b2ea76201af0dc972c928d589b090df7eaec [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
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 Bakker5121ce52009-01-03 21:22:43 +000018 */
19
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020020#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000021#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020022#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020023#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020024#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000025
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000026#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000027
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020028#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
29 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
30 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
Janos Follath9fe6f922016-10-07 14:17:56 +010031 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
32 defined(MBEDTLS_SHA1_C)
Andres AG788aa4a2016-09-14 14:32:09 +010033#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000034#include "mbedtls/aes.h"
35#include "mbedtls/dhm.h"
36#include "mbedtls/rsa.h"
37#include "mbedtls/sha1.h"
38#include "mbedtls/entropy.h"
39#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000040
Rich Evans18b78c72015-02-11 14:06:19 +000041#include <stdio.h>
42#include <string.h>
43#endif
44
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020045#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000046#define PLAINTEXT "==Hello there!=="
47
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020048#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
49 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
50 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_SHA256_C) || \
Janos Follath9fe6f922016-10-07 14:17:56 +010051 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
52 !defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010053int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000054{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010056 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
57 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
58 "MBEDTLS_CTR_DRBG_C not defined.\n");
59 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000060}
61#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000062
Simon Butcher63cb97e2018-12-06 17:43:31 +000063
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010064int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000065{
66 FILE *f;
67
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +010068 int ret = 1;
69 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000070 size_t n, buflen;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020071 mbedtls_net_context listen_fd, client_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000072
Paul Bakker520ea912012-10-24 14:17:01 +000073 unsigned char buf[2048];
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020074 unsigned char hash[32];
Paul Bakker5121ce52009-01-03 21:22:43 +000075 unsigned char buf2[2];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020076 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000077
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020078 mbedtls_entropy_context entropy;
79 mbedtls_ctr_drbg_context ctr_drbg;
80 mbedtls_rsa_context rsa;
81 mbedtls_dhm_context dhm;
82 mbedtls_aes_context aes;
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Hanno Beckerc95fad32017-08-23 06:44:30 +010084 mbedtls_mpi N, P, Q, D, E;
85
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010086 mbedtls_net_init(&listen_fd);
87 mbedtls_net_init(&client_fd);
88 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
89 mbedtls_dhm_init(&dhm);
90 mbedtls_aes_init(&aes);
91 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker5121ce52009-01-03 21:22:43 +000092
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010093 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
94 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +010095
Paul Bakker5121ce52009-01-03 21:22:43 +000096 /*
97 * 1. Setup the RNG
98 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010099 mbedtls_printf("\n . Seeding the random number generator");
100 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000101
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100102 mbedtls_entropy_init(&entropy);
103 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
104 (const unsigned char *) pers,
105 strlen(pers))) != 0) {
106 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000107 goto exit;
108 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000109
110 /*
111 * 2a. Read the server's private RSA key
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
114 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
117 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
118 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000119 goto exit;
120 }
121
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100122 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
125 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
126 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
127 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
128 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) {
129 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
130 ret);
131 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000132 goto exit;
133 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
137 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
138 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100139 goto exit;
140 }
141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
143 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
144 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100145 goto exit;
146 }
147
Paul Bakker5121ce52009-01-03 21:22:43 +0000148 /*
149 * 2b. Get the DHM modulus and generator
150 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 mbedtls_printf("\n . Reading DH parameters from dh_prime.txt");
152 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 if ((f = fopen("dh_prime.txt", "rb")) == NULL) {
155 mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \
156 " ! Please run dh_genprime first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000157 goto exit;
158 }
159
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if (mbedtls_mpi_read_file(&dhm.P, 16, f) != 0 ||
161 mbedtls_mpi_read_file(&dhm.G, 16, f) != 0) {
162 mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n");
163 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000164 goto exit;
165 }
166
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000168
169 /*
170 * 3. Wait for a client to connect
171 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 mbedtls_printf("\n . Waiting for a remote connection");
173 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000174
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
176 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000177 goto exit;
178 }
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
181 NULL, 0, NULL)) != 0) {
182 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000183 goto exit;
184 }
185
186 /*
187 * 4. Setup the DH parameters (P,G,Ys)
188 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100189 mbedtls_printf("\n . Sending the server's DH parameters");
190 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000191
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000193
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100194 if ((ret = mbedtls_dhm_make_params(&dhm, (int) mbedtls_mpi_size(&dhm.P), buf, &n,
195 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
196 mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000197 goto exit;
198 }
199
200 /*
201 * 5. Sign the parameters and send them
202 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if ((ret = mbedtls_sha1_ret(buf, n, hash)) != 0) {
204 mbedtls_printf(" failed\n ! mbedtls_sha1_ret returned %d\n\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100205 goto exit;
206 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 buf[n] = (unsigned char) (rsa.len >> 8);
209 buf[n + 1] = (unsigned char) (rsa.len);
Paul Bakker5121ce52009-01-03 21:22:43 +0000210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
212 0, hash, buf + n + 2)) != 0) {
213 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000214 goto exit;
215 }
216
217 buflen = n + 2 + rsa.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100218 buf2[0] = (unsigned char) (buflen >> 8);
219 buf2[1] = (unsigned char) (buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000220
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 ||
222 (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) {
223 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000224 goto exit;
225 }
226
227 /*
228 * 6. Get the client's public value: Yc = G ^ Xc mod P
229 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100230 mbedtls_printf("\n . Receiving the client's public value");
231 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000232
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000234
Martijn de Millianob194a282017-07-06 23:55:59 +0200235 n = dhm.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100236 if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) {
237 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000238 goto exit;
239 }
240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if ((ret = mbedtls_dhm_read_public(&dhm, buf, dhm.len)) != 0) {
242 mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 goto exit;
244 }
245
246 /*
247 * 7. Derive the shared secret: K = Ys ^ Xc mod P
248 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 mbedtls_printf("\n . Shared secret: ");
250 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000251
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
253 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
254 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
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 for (n = 0; n < 16; n++) {
259 mbedtls_printf("%02x", buf[n]);
260 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000261
262 /*
263 * 8. Setup the AES-256 encryption key
264 *
265 * This is an overly simplified example; best practice is
266 * to hash the shared secret with a random value to derive
267 * the keying material for the encryption/decryption keys
268 * and MACs.
269 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100270 mbedtls_printf("...\n . Encrypting and sending the ciphertext");
271 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000272
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 ret = mbedtls_aes_setkey_enc(&aes, buf, 256);
274 if (ret != 0) {
Gilles Peskine377a3102021-07-07 21:08:28 +0200275 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100276 }
277 memcpy(buf, PLAINTEXT, 16);
278 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf);
279 if (ret != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000280 goto exit;
281 }
282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) {
284 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
285 goto exit;
286 }
287
288 mbedtls_printf("\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000289
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +0100290 exit_code = MBEDTLS_EXIT_SUCCESS;
291
Paul Bakker5121ce52009-01-03 21:22:43 +0000292exit:
293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
295 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100296
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100297 mbedtls_net_free(&client_fd);
298 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200299
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 mbedtls_aes_free(&aes);
301 mbedtls_rsa_free(&rsa);
302 mbedtls_dhm_free(&dhm);
303 mbedtls_ctr_drbg_free(&ctr_drbg);
304 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000305
Paul Bakkercce9d772011-11-18 14:26:47 +0000306#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 mbedtls_printf(" + Press Enter to exit this program.\n");
308 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000309#endif
310
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000312}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200313#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
314 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
315 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */