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