1 /*
2 * Diffie-Hellman-Merkle key exchange (server side)
3 *
4 * Copyright The Mbed TLS Contributors
5 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
6 */
7
8 #if !defined(MBEDTLS_CONFIG_FILE)
9 #include "mbedtls/config.h"
10 #else
11 #include MBEDTLS_CONFIG_FILE
12 #endif
13
14 #include "mbedtls/platform.h"
15
16 #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) && \
19 defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \
20 defined(MBEDTLS_SHA1_C)
21 #include "mbedtls/net_sockets.h"
22 #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"
28
29 #include <stdio.h>
30 #include <string.h>
31 #endif
32
33 #define SERVER_PORT "11999"
34 #define PLAINTEXT "==Hello there!=="
35
36 #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) || \
39 !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \
40 !defined(MBEDTLS_SHA1_C)
main(void)41 int main(void)
42 {
43 mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_DHM_C and/or MBEDTLS_ENTROPY_C "
44 "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);
48 }
49 #else
50
51
main(void)52 int main(void)
53 {
54 FILE *f;
55
56 int ret = 1;
57 int exit_code = MBEDTLS_EXIT_FAILURE;
58 size_t n, buflen;
59 mbedtls_net_context listen_fd, client_fd;
60
61 unsigned char buf[2048];
62 unsigned char hash[32];
63 unsigned char buf2[2];
64 const char *pers = "dh_server";
65
66 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;
71
72 mbedtls_mpi N, P, Q, D, E;
73
74 mbedtls_net_init(&listen_fd);
75 mbedtls_net_init(&client_fd);
76 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, MBEDTLS_MD_SHA256);
77 mbedtls_dhm_init(&dhm);
78 mbedtls_aes_init(&aes);
79 mbedtls_ctr_drbg_init(&ctr_drbg);
80
81 mbedtls_mpi_init(&N); mbedtls_mpi_init(&P); mbedtls_mpi_init(&Q);
82 mbedtls_mpi_init(&D); mbedtls_mpi_init(&E);
83
84 /*
85 * 1. Setup the RNG
86 */
87 mbedtls_printf("\n . Seeding the random number generator");
88 fflush(stdout);
89
90 mbedtls_entropy_init(&entropy);
91 if ((ret = mbedtls_ctr_drbg_seed(&ctr_drbg, mbedtls_entropy_func, &entropy,
92 (const unsigned char *) pers,
93 strlen(pers))) != 0) {
94 mbedtls_printf(" failed\n ! mbedtls_ctr_drbg_seed returned %d\n", ret);
95 goto exit;
96 }
97
98 /*
99 * 2a. Read the server's private RSA key
100 */
101 mbedtls_printf("\n . Reading private key from rsa_priv.txt");
102 fflush(stdout);
103
104 if ((f = fopen("rsa_priv.txt", "rb")) == NULL) {
105 mbedtls_printf(" failed\n ! Could not open rsa_priv.txt\n" \
106 " ! Please run rsa_genkey first\n\n");
107 goto exit;
108 }
109
110 mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0);
111
112 if ((ret = mbedtls_mpi_read_file(&N, 16, f)) != 0 ||
113 (ret = mbedtls_mpi_read_file(&E, 16, f)) != 0 ||
114 (ret = mbedtls_mpi_read_file(&D, 16, f)) != 0 ||
115 (ret = mbedtls_mpi_read_file(&P, 16, f)) != 0 ||
116 (ret = mbedtls_mpi_read_file(&Q, 16, f)) != 0) {
117 mbedtls_printf(" failed\n ! mbedtls_mpi_read_file returned %d\n\n",
118 ret);
119 fclose(f);
120 goto exit;
121 }
122 fclose(f);
123
124 if ((ret = mbedtls_rsa_import(&rsa, &N, &P, &Q, &D, &E)) != 0) {
125 mbedtls_printf(" failed\n ! mbedtls_rsa_import returned %d\n\n",
126 ret);
127 goto exit;
128 }
129
130 if ((ret = mbedtls_rsa_complete(&rsa)) != 0) {
131 mbedtls_printf(" failed\n ! mbedtls_rsa_complete returned %d\n\n",
132 ret);
133 goto exit;
134 }
135
136 /*
137 * 2b. Get the DHM modulus and generator
138 */
139 mbedtls_printf("\n . Reading DH parameters from dh_prime.txt");
140 fflush(stdout);
141
142 if ((f = fopen("dh_prime.txt", "rb")) == NULL) {
143 mbedtls_printf(" failed\n ! Could not open dh_prime.txt\n" \
144 " ! Please run dh_genprime first\n\n");
145 goto exit;
146 }
147
148 if (mbedtls_mpi_read_file(&dhm.P, 16, f) != 0 ||
149 mbedtls_mpi_read_file(&dhm.G, 16, f) != 0) {
150 mbedtls_printf(" failed\n ! Invalid DH parameter file\n\n");
151 fclose(f);
152 goto exit;
153 }
154
155 fclose(f);
156
157 /*
158 * 3. Wait for a client to connect
159 */
160 mbedtls_printf("\n . Waiting for a remote connection");
161 fflush(stdout);
162
163 if ((ret = mbedtls_net_bind(&listen_fd, NULL, SERVER_PORT, MBEDTLS_NET_PROTO_TCP)) != 0) {
164 mbedtls_printf(" failed\n ! mbedtls_net_bind returned %d\n\n", ret);
165 goto exit;
166 }
167
168 if ((ret = mbedtls_net_accept(&listen_fd, &client_fd,
169 NULL, 0, NULL)) != 0) {
170 mbedtls_printf(" failed\n ! mbedtls_net_accept returned %d\n\n", ret);
171 goto exit;
172 }
173
174 /*
175 * 4. Setup the DH parameters (P,G,Ys)
176 */
177 mbedtls_printf("\n . Sending the server's DH parameters");
178 fflush(stdout);
179
180 memset(buf, 0, sizeof(buf));
181
182 if ((ret = mbedtls_dhm_make_params(&dhm, (int) mbedtls_mpi_size(&dhm.P), buf, &n,
183 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
184 mbedtls_printf(" failed\n ! mbedtls_dhm_make_params returned %d\n\n", ret);
185 goto exit;
186 }
187
188 /*
189 * 5. Sign the parameters and send them
190 */
191 if ((ret = mbedtls_sha1_ret(buf, n, hash)) != 0) {
192 mbedtls_printf(" failed\n ! mbedtls_sha1_ret returned %d\n\n", ret);
193 goto exit;
194 }
195
196 buf[n] = (unsigned char) (rsa.len >> 8);
197 buf[n + 1] = (unsigned char) (rsa.len);
198
199 if ((ret = mbedtls_rsa_pkcs1_sign(&rsa, NULL, NULL, MBEDTLS_RSA_PRIVATE, MBEDTLS_MD_SHA256,
200 0, hash, buf + n + 2)) != 0) {
201 mbedtls_printf(" failed\n ! mbedtls_rsa_pkcs1_sign returned %d\n\n", ret);
202 goto exit;
203 }
204
205 buflen = n + 2 + rsa.len;
206 buf2[0] = (unsigned char) (buflen >> 8);
207 buf2[1] = (unsigned char) (buflen);
208
209 if ((ret = mbedtls_net_send(&client_fd, buf2, 2)) != 2 ||
210 (ret = mbedtls_net_send(&client_fd, buf, buflen)) != (int) buflen) {
211 mbedtls_printf(" failed\n ! mbedtls_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 mbedtls_printf("\n . Receiving the client's public value");
219 fflush(stdout);
220
221 memset(buf, 0, sizeof(buf));
222
223 n = dhm.len;
224 if ((ret = mbedtls_net_recv(&client_fd, buf, n)) != (int) n) {
225 mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret);
226 goto exit;
227 }
228
229 if ((ret = mbedtls_dhm_read_public(&dhm, buf, dhm.len)) != 0) {
230 mbedtls_printf(" failed\n ! mbedtls_dhm_read_public returned %d\n\n", ret);
231 goto exit;
232 }
233
234 /*
235 * 7. Derive the shared secret: K = Ys ^ Xc mod P
236 */
237 mbedtls_printf("\n . Shared secret: ");
238 fflush(stdout);
239
240 if ((ret = mbedtls_dhm_calc_secret(&dhm, buf, sizeof(buf), &n,
241 mbedtls_ctr_drbg_random, &ctr_drbg)) != 0) {
242 mbedtls_printf(" failed\n ! mbedtls_dhm_calc_secret returned %d\n\n", ret);
243 goto exit;
244 }
245
246 for (n = 0; n < 16; n++) {
247 mbedtls_printf("%02x", buf[n]);
248 }
249
250 /*
251 * 8. Setup the AES-256 encryption key
252 *
253 * This is an overly simplified example; best practice is
254 * to hash the shared secret with a random value to derive
255 * the keying material for the encryption/decryption keys
256 * and MACs.
257 */
258 mbedtls_printf("...\n . Encrypting and sending the ciphertext");
259 fflush(stdout);
260
261 ret = mbedtls_aes_setkey_enc(&aes, buf, 256);
262 if (ret != 0) {
263 goto exit;
264 }
265 memcpy(buf, PLAINTEXT, 16);
266 ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_ENCRYPT, buf, buf);
267 if (ret != 0) {
268 goto exit;
269 }
270
271 if ((ret = mbedtls_net_send(&client_fd, buf, 16)) != 16) {
272 mbedtls_printf(" failed\n ! mbedtls_net_send returned %d\n\n", ret);
273 goto exit;
274 }
275
276 mbedtls_printf("\n\n");
277
278 exit_code = MBEDTLS_EXIT_SUCCESS;
279
280 exit:
281
282 mbedtls_mpi_free(&N); mbedtls_mpi_free(&P); mbedtls_mpi_free(&Q);
283 mbedtls_mpi_free(&D); mbedtls_mpi_free(&E);
284
285 mbedtls_net_free(&client_fd);
286 mbedtls_net_free(&listen_fd);
287
288 mbedtls_aes_free(&aes);
289 mbedtls_rsa_free(&rsa);
290 mbedtls_dhm_free(&dhm);
291 mbedtls_ctr_drbg_free(&ctr_drbg);
292 mbedtls_entropy_free(&entropy);
293
294 #if defined(_WIN32)
295 mbedtls_printf(" + Press Enter to exit this program.\n");
296 fflush(stdout); getchar();
297 #endif
298
299 mbedtls_exit(exit_code);
300 }
301 #endif /* MBEDTLS_AES_C && MBEDTLS_DHM_C && MBEDTLS_ENTROPY_C &&
302 MBEDTLS_NET_C && MBEDTLS_RSA_C && MBEDTLS_SHA256_C &&
303 MBEDTLS_FS_IO && MBEDTLS_CTR_DRBG_C */
304