blob: 0e033a50d7942f0b363ab83f506d13731a28a246 [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
Bence Szépkútic662b362021-05-27 11:25:03 +020020#include "mbedtls/build_info.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000021
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/platform.h"
Andrzej Kurek1b75e5f2023-04-04 09:55:06 -040023#include "mbedtls/md.h"
Rich Evansf90016a2015-01-19 14:26:37 +000024
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020025#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
26 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010027 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_MD_CAN_SHA256) && \
Janos Follath9fe6f922016-10-07 14:17:56 +010028 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010029 defined(MBEDTLS_MD_CAN_SHA1)
Andres AG788aa4a2016-09-14 14:32:09 +010030#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000031#include "mbedtls/aes.h"
32#include "mbedtls/dhm.h"
33#include "mbedtls/rsa.h"
34#include "mbedtls/sha1.h"
35#include "mbedtls/entropy.h"
36#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000037
Rich Evans18b78c72015-02-11 14:06:19 +000038#include <stdio.h>
39#include <string.h>
40#endif
41
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020042#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000043#define PLAINTEXT "==Hello there!=="
44
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020045#if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_DHM_C) || \
46 !defined(MBEDTLS_ENTROPY_C) || !defined(MBEDTLS_NET_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010047 !defined(MBEDTLS_RSA_C) || !defined(MBEDTLS_MD_CAN_SHA256) || \
Janos Follath9fe6f922016-10-07 14:17:56 +010048 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010049 !defined(MBEDTLS_MD_CAN_SHA1)
Gilles Peskine449bd832023-01-11 14:50:10 +010050int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000051{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine449bd832023-01-11 14:50:10 +010053 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +010054 "MBEDTLS_MD_CAN_SHA256 and/or MBEDTLS_FS_IO and/or "
Gilles Peskine449bd832023-01-11 14:50:10 +010055 "MBEDTLS_CTR_DRBG_C not defined.\n");
56 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000057}
58#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000059
Simon Butcher63cb97e2018-12-06 17:43:31 +000060
Gilles Peskine449bd832023-01-11 14:50:10 +010061int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000062{
63 FILE *f;
64
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +010065 int ret = 1;
66 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000067 size_t n, buflen;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020068 mbedtls_net_context listen_fd, client_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000069
Paul Bakker520ea912012-10-24 14:17:01 +000070 unsigned char buf[2048];
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020071 unsigned char hash[32];
Paul Bakker5121ce52009-01-03 21:22:43 +000072 unsigned char buf2[2];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020073 const char *pers = "dh_server";
Paul Bakker5121ce52009-01-03 21:22:43 +000074
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020075 mbedtls_entropy_context entropy;
76 mbedtls_ctr_drbg_context ctr_drbg;
77 mbedtls_rsa_context rsa;
78 mbedtls_dhm_context dhm;
79 mbedtls_aes_context aes;
Paul Bakker5121ce52009-01-03 21:22:43 +000080
Hanno Beckerc95fad32017-08-23 06:44:30 +010081 mbedtls_mpi N, P, Q, D, E;
82
Gilles Peskine449bd832023-01-11 14:50:10 +010083 mbedtls_net_init(&listen_fd);
84 mbedtls_net_init(&client_fd);
85 mbedtls_dhm_init(&dhm);
86 mbedtls_aes_init(&aes);
87 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker5121ce52009-01-03 21:22:43 +000088
Gilles Peskine449bd832023-01-11 14:50:10 +010089 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
90 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +010091
Paul Bakker5121ce52009-01-03 21:22:43 +000092 /*
93 * 1. Setup the RNG
94 */
Gilles Peskine449bd832023-01-11 14:50:10 +010095 mbedtls_printf("\n . Seeding the random number generator");
96 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Gilles Peskine449bd832023-01-11 14:50:10 +010098 mbedtls_entropy_init(&entropy);
99 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
100 (const unsigned char *) pers,
101 strlen(pers))) != 0) {
102 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +0000103 goto exit;
104 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
106 /*
107 * 2a. Read the server's private RSA key
108 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
110 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000111
Gilles Peskine449bd832023-01-11 14:50:10 +0100112 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
113 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
114 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000115 goto exit;
116 }
117
Gilles Peskine449bd832023-01-11 14:50:10 +0100118 mbedtls_rsa_init(&rsa);
Paul Bakker5121ce52009-01-03 21:22:43 +0000119
Gilles Peskine449bd832023-01-11 14:50:10 +0100120 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
121 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
122 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
123 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
124 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) {
125 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
126 ret);
127 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000128 goto exit;
129 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000131
Gilles Peskine449bd832023-01-11 14:50:10 +0100132 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
133 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
134 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100135 goto exit;
136 }
137
Gilles Peskine449bd832023-01-11 14:50:10 +0100138 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
139 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
140 ret);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100141 goto exit;
142 }
143
Paul Bakker5121ce52009-01-03 21:22:43 +0000144 /*
145 * 2b. Get the DHM modulus and generator
146 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100147 mbedtls_printf("\n . Reading DH parameters from dh_prime.txt");
148 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000149
Gilles Peskine449bd832023-01-11 14:50:10 +0100150 if ((f = fopen("dh_prime.txt", "rb")) == NULL) {
151 mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \
152 " ! Please run dh_genprime first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000153 goto exit;
154 }
155
Gilles Peskine449bd832023-01-11 14:50:10 +0100156 if (mbedtls_mpi_read_file(&dhm.MBEDTLS_PRIVATE(P), 16, f) != 0 ||
157 mbedtls_mpi_read_file(&dhm.MBEDTLS_PRIVATE(G), 16, f) != 0) {
158 mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n");
159 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000160 goto exit;
161 }
162
Gilles Peskine449bd832023-01-11 14:50:10 +0100163 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000164
165 /*
166 * 3. Wait for a client to connect
167 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100168 mbedtls_printf("\n . Waiting for a remote connection");
169 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000170
Gilles Peskine449bd832023-01-11 14:50:10 +0100171 if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
172 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000173 goto exit;
174 }
175
Gilles Peskine449bd832023-01-11 14:50:10 +0100176 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
177 NULL, 0, NULL)) != 0) {
178 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000179 goto exit;
180 }
181
182 /*
183 * 4. Setup the DH parameters (P,G,Ys)
184 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100185 mbedtls_printf("\n . Sending the server's DH parameters");
186 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000187
Gilles Peskine449bd832023-01-11 14:50:10 +0100188 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Gilles Peskine449bd832023-01-11 14:50:10 +0100190 if ((ret =
191 mbedtls_dhm_make_params(&dhm, (int) mbedtls_mpi_size(&dhm.MBEDTLS_PRIVATE(P)), buf, &n,
192 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
193 mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000194 goto exit;
195 }
196
197 /*
198 * 5. Sign the parameters and send them
199 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100200 if ((ret = mbedtls_sha1(buf, n, hash)) != 0) {
201 mbedtls_printf(" failed\n ! mbedtls_sha1 returned %d\n\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100202 goto exit;
203 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000204
Gilles Peskine449bd832023-01-11 14:50:10 +0100205 buf[n] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len) >> 8);
206 buf[n + 1] = (unsigned char) (rsa.MBEDTLS_PRIVATE(len));
Paul Bakker5121ce52009-01-03 21:22:43 +0000207
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_MD_SHA256,
209 32, hash, buf + n + 2)) != 0) {
210 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
Mateusz Starzyk5dd4f6e2021-05-19 19:35:35 +0200214 buflen = n + 2 + rsa.MBEDTLS_PRIVATE(len);
Gilles Peskine449bd832023-01-11 14:50:10 +0100215 buf2[0] = (unsigned char) (buflen >> 8);
216 buf2[1] = (unsigned char) (buflen);
Paul Bakker5121ce52009-01-03 21:22:43 +0000217
Gilles Peskine449bd832023-01-11 14:50:10 +0100218 if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 ||
219 (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) {
220 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000221 goto exit;
222 }
223
224 /*
225 * 6. Get the client's public value: Yc = G ^ Xc mod P
226 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100227 mbedtls_printf("\n . Receiving the client's public value");
228 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
Gilles Peskine449bd832023-01-11 14:50:10 +0100230 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000231
Gilles Peskine449bd832023-01-11 14:50:10 +0100232 n = mbedtls_dhm_get_len(&dhm);
233 if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) {
234 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000235 goto exit;
236 }
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if ((ret = mbedtls_dhm_read_public(&dhm, buf, n)) != 0) {
239 mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000240 goto exit;
241 }
242
243 /*
244 * 7. Derive the shared secret: K = Ys ^ Xc mod P
245 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100246 mbedtls_printf("\n . Shared secret: ");
247 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
250 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
251 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000252 goto exit;
253 }
254
Gilles Peskine449bd832023-01-11 14:50:10 +0100255 for (n = 0; n < 16; n++) {
256 mbedtls_printf("%02x", buf[n]);
257 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000258
259 /*
260 * 8. Setup the AES-256 encryption key
261 *
262 * This is an overly simplified example; best practice is
263 * to hash the shared secret with a random value to derive
264 * the keying material for the encryption/decryption keys
265 * and MACs.
266 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 mbedtls_printf("...\n . Encrypting and sending the ciphertext");
268 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000269
Gilles Peskine449bd832023-01-11 14:50:10 +0100270 ret = mbedtls_aes_setkey_enc(&aes, buf, 256);
271 if (ret != 0) {
Gilles Peskine7820a572021-07-07 21:08:28 +0200272 goto exit;
Gilles Peskine449bd832023-01-11 14:50:10 +0100273 }
274 memcpy(buf, PLAINTEXT, 16);
275 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf);
276 if (ret != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000277 goto exit;
278 }
279
Gilles Peskine449bd832023-01-11 14:50:10 +0100280 if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) {
281 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
282 goto exit;
283 }
284
285 mbedtls_printf("\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000286
Andres Amaya Garcia03a992c2018-04-29 19:40:45 +0100287 exit_code = MBEDTLS_EXIT_SUCCESS;
288
Paul Bakker5121ce52009-01-03 21:22:43 +0000289exit:
290
Gilles Peskine449bd832023-01-11 14:50:10 +0100291 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
292 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
Hanno Beckerc95fad32017-08-23 06:44:30 +0100293
Gilles Peskine449bd832023-01-11 14:50:10 +0100294 mbedtls_net_free(&client_fd);
295 mbedtls_net_free(&listen_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200296
Gilles Peskine449bd832023-01-11 14:50:10 +0100297 mbedtls_aes_free(&aes);
298 mbedtls_rsa_free(&rsa);
299 mbedtls_dhm_free(&dhm);
300 mbedtls_ctr_drbg_free(&ctr_drbg);
301 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000302
Gilles Peskine449bd832023-01-11 14:50:10 +0100303 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000304}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200305#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
Manuel Pégourié-Gonnard93302422023-03-21 17:23:08 +0100306 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_MD_CAN_SHA256 &&
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200307 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */