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