blob: 1bf3e516b98728e8c9010b620c2f29f63546968a [file] [log] [blame]
Paul Bakker5121ce52009-01-03 21:22:43 +00001/*
2 * Diffie-Hellman-Merkle key exchange (client side)
3 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02004 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00005 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker5121ce52009-01-03 21:22:43 +00006 */
7
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +02008#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +00009#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020010#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020011#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020012#endif
Paul Bakker5121ce52009-01-03 21:22:43 +000013
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000014#include "mbedtls/platform.h"
Rich Evansf90016a2015-01-19 14:26:37 +000015
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020016#if defined(MBEDTLS_AES_C) && defined(MBEDTLS_DHM_C) && \
17 defined(MBEDTLS_ENTROPY_C) && defined(MBEDTLS_NET_C) && \
18 defined(MBEDTLS_RSA_C) && defined(MBEDTLS_SHA256_C) && \
Janos Follath9fe6f922016-10-07 14:17:56 +010019 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
20 defined(MBEDTLS_SHA1_C)
Andres AG788aa4a2016-09-14 14:32:09 +010021#include "mbedtls/net_sockets.h"
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000022#include "mbedtls/aes.h"
23#include "mbedtls/dhm.h"
24#include "mbedtls/rsa.h"
25#include "mbedtls/sha1.h"
26#include "mbedtls/entropy.h"
27#include "mbedtls/ctr_drbg.h"
Paul Bakker5121ce52009-01-03 21:22:43 +000028
Rich Evans18b78c72015-02-11 14:06:19 +000029#include <stdio.h>
30#include <string.h>
31#endif
32
Paul Bakker5121ce52009-01-03 21:22:43 +000033#define SERVER_NAME "localhost"
Manuel Pégourié-Gonnardc0d74942015-06-23 12:30:57 +020034#define SERVER_PORT "11999"
Paul Bakker5121ce52009-01-03 21:22:43 +000035
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) || \
Janos Follath9fe6f922016-10-07 14:17:56 +010039 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_SHA1_C)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010041int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000042{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020043 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010044 "and/or MBEDTLS_NET_C and/or MBEDTLS_RSA_C and/or "
45 "MBEDTLS_SHA256_C and/or MBEDTLS_FS_IO and/or "
46 "MBEDTLS_CTR_DRBG_C not defined.\n");
47 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000048}
49#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000050
Simon Butcher63cb97e2018-12-06 17:43:31 +000051
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010052int main(void)
Paul Bakker5121ce52009-01-03 21:22:43 +000053{
54 FILE *f;
55
Andres Amaya Garcia898841d2018-04-29 19:23:39 +010056 int ret = 1;
57 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker23986e52011-04-24 08:57:21 +000058 size_t n, buflen;
Manuel Pégourié-Gonnard5db64322015-06-30 15:40:39 +020059 mbedtls_net_context server_fd;
Paul Bakker5121ce52009-01-03 21:22:43 +000060
61 unsigned char *p, *end;
Paul Bakker520ea912012-10-24 14:17:01 +000062 unsigned char buf[2048];
Manuel Pégourié-Gonnard102a6202015-08-27 21:51:44 +020063 unsigned char hash[32];
Paul Bakkeref3f8c72013-06-24 13:01:08 +020064 const char *pers = "dh_client";
Paul Bakker5121ce52009-01-03 21:22:43 +000065
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020066 mbedtls_entropy_context entropy;
67 mbedtls_ctr_drbg_context ctr_drbg;
68 mbedtls_rsa_context rsa;
69 mbedtls_dhm_context dhm;
70 mbedtls_aes_context aes;
Paul Bakker5121ce52009-01-03 21:22:43 +000071
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010072 mbedtls_net_init(&server_fd);
73 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
74 mbedtls_dhm_init(&dhm);
75 mbedtls_aes_init(&aes);
76 mbedtls_ctr_drbg_init(&ctr_drbg);
Paul Bakker8cfd9d82014-06-18 11:16:11 +020077
Paul Bakker5121ce52009-01-03 21:22:43 +000078 /*
79 * 1. Setup the RNG
80 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010081 mbedtls_printf("\n . Seeding the random number generator");
82 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000083
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010084 mbedtls_entropy_init(&entropy);
85 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
86 (const unsigned char *) pers,
87 strlen(pers))) != 0) {
88 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
Paul Bakker508ad5a2011-12-04 17:09:26 +000089 goto exit;
90 }
Paul Bakker5121ce52009-01-03 21:22:43 +000091
92 /*
93 * 2. Read the server's public RSA key
94 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 mbedtls_printf("\n . Reading public key from rsa_pub.txt");
96 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +000097
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 if ((f = fopen("rsa_pub.txt", "rb")) == NULL) {
99 mbedtls_printf(" failed\n ! Could not open rsa_pub.txt\n" \
100 " ! Please run rsa_genkey first\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000101 goto exit;
102 }
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
Paul Bakker5121ce52009-01-03 21:22:43 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 if ((ret = mbedtls_mpi_read_file(&rsa.N, 16, f)) != 0 ||
107 (ret = mbedtls_mpi_read_file(&rsa.E, 16, f)) != 0) {
108 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n", ret);
109 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000110 goto exit;
111 }
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3;
Paul Bakker5121ce52009-01-03 21:22:43 +0000114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 fclose(f);
Paul Bakker5121ce52009-01-03 21:22:43 +0000116
117 /*
118 * 3. Initiate the connection
119 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 mbedtls_printf("\n . Connecting to tcp/%s/%s", SERVER_NAME,
121 SERVER_PORT);
122 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000123
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100124 if ((ret = mbedtls_net_connect(&server_fd, SERVER_NAME,
125 SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
126 mbedtls_printf(" failed\n ! mbedtls_net_connect returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000127 goto exit;
128 }
129
130 /*
131 * 4a. First get the buffer length
132 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100133 mbedtls_printf("\n . Receiving the server's DH parameters");
134 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000135
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100136 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000137
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100138 if ((ret = mbedtls_net_recv(&server_fd, buf, 2)) != 2) {
139 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000140 goto exit;
141 }
142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 n = buflen = (buf[0] << 8) | buf[1];
144 if (buflen < 1 || buflen > sizeof(buf)) {
145 mbedtls_printf(" failed\n ! Got an invalid buffer length\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000146 goto exit;
147 }
148
149 /*
150 * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P
151 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100152 memset(buf, 0, sizeof(buf));
Paul Bakker5121ce52009-01-03 21:22:43 +0000153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 if ((ret = mbedtls_net_recv(&server_fd, buf, n)) != (int) n) {
155 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000156 goto exit;
157 }
158
159 p = buf, end = buf + buflen;
160
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100161 if ((ret = mbedtls_dhm_read_params(&dhm, &p, end)) != 0) {
162 mbedtls_printf(" failed\n ! mbedtls_dhm_read_params returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000163 goto exit;
164 }
165
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 if (dhm.len < 64 || dhm.len > 512) {
167 mbedtls_printf(" failed\n ! Invalid DHM modulus size\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000168 goto exit;
169 }
170
171 /*
172 * 5. Check that the server's RSA signature matches
Manuel Pégourié-Gonnard6f60cd82015-02-10 10:47:03 +0000173 * the SHA-256 hash of (P,G,Ys)
Paul Bakker5121ce52009-01-03 21:22:43 +0000174 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100175 mbedtls_printf("\n . Verifying the server's RSA signature");
176 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000177
Paul Bakker88f17b82012-04-26 18:52:13 +0000178 p += 2;
179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if ((n = (size_t) (end - p)) != rsa.len) {
181 mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n");
Paul Bakker5121ce52009-01-03 21:22:43 +0000182 goto exit;
183 }
184
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100185 if ((ret = mbedtls_sha1_ret(buf, (int) (p - 2 - buf), hash)) != 0) {
186 mbedtls_printf(" failed\n ! mbedtls_sha1_ret returned %d\n\n", ret);
Andres Amaya Garcia1ff60f42017-06-28 13:26:36 +0100187 goto exit;
188 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000189
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if ((ret = mbedtls_rsa_pkcs1_verify(&rsa, NULL, NULL, MBEDTLS_RSA_PUBLIC,
191 MBEDTLS_MD_SHA256, 0, hash, p)) != 0) {
192 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_verify returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000193 goto exit;
194 }
195
196 /*
197 * 6. Send our public value: Yc = G ^ Xc mod P
198 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 mbedtls_printf("\n . Sending own public value to server");
200 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000201
202 n = dhm.len;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 if ((ret = mbedtls_dhm_make_public(&dhm, (int) dhm.len, buf, n,
204 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
205 mbedtls_printf(" failed\n ! mbedtls_dhm_make_public returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000206 goto exit;
207 }
208
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100209 if ((ret = mbedtls_net_send(&server_fd, buf, n)) != (int) n) {
210 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000211 goto exit;
212 }
213
214 /*
215 * 7. Derive the shared secret: K = Ys ^ Xc mod P
216 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100217 mbedtls_printf("\n . Shared secret: ");
218 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000219
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100220 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
221 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
222 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
Paul Bakker5121ce52009-01-03 21:22:43 +0000223 goto exit;
224 }
225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 for (n = 0; n < 16; n++) {
227 mbedtls_printf("%02x", buf[n]);
228 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000229
230 /*
231 * 8. Setup the AES-256 decryption key
232 *
233 * This is an overly simplified example; best practice is
234 * to hash the shared secret with a random value to derive
235 * the keying material for the encryption/decryption keys,
236 * IVs and MACs.
237 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 mbedtls_printf("...\n . Receiving and decrypting the ciphertext");
239 fflush(stdout);
Paul Bakker5121ce52009-01-03 21:22:43 +0000240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 ret = mbedtls_aes_setkey_dec(&aes, buf, 256);
242 if (ret != 0) {
Paul Bakker5121ce52009-01-03 21:22:43 +0000243 goto exit;
244 }
245
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100246 memset(buf, 0, sizeof(buf));
247
248 if ((ret = mbedtls_net_recv(&server_fd, buf, 16)) != 16) {
249 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
Gilles Peskine377a3102021-07-07 21:08:28 +0200250 goto exit;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100251 }
252
253 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, buf);
254 if (ret != 0) {
255 goto exit;
256 }
Paul Bakker5121ce52009-01-03 21:22:43 +0000257 buf[16] = '\0';
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 mbedtls_printf("\n . Plaintext is \"%s\"\n\n", (char *) buf);
Paul Bakker5121ce52009-01-03 21:22:43 +0000259
Andres Amaya Garcia898841d2018-04-29 19:23:39 +0100260 exit_code = MBEDTLS_EXIT_SUCCESS;
261
Paul Bakker5121ce52009-01-03 21:22:43 +0000262exit:
263
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 mbedtls_net_free(&server_fd);
Paul Bakker0c226102014-04-17 16:02:36 +0200265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 mbedtls_aes_free(&aes);
267 mbedtls_rsa_free(&rsa);
268 mbedtls_dhm_free(&dhm);
269 mbedtls_ctr_drbg_free(&ctr_drbg);
270 mbedtls_entropy_free(&entropy);
Paul Bakker5121ce52009-01-03 21:22:43 +0000271
Paul Bakkercce9d772011-11-18 14:26:47 +0000272#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 mbedtls_printf(" + Press Enter to exit this program.\n");
274 fflush(stdout); getchar();
Paul Bakker5121ce52009-01-03 21:22:43 +0000275#endif
276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 mbedtls_exit(exit_code);
Paul Bakker5121ce52009-01-03 21:22:43 +0000278}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200279#endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
280 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
281 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */