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