Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Diffie-Hellman-Merkle key exchange (client side) |
| 3 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 4 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 7ff7965 | 2023-11-03 12:04:52 +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 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 8 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 9 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 10 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 11 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 12 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 13 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 14 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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) && \ |
Janos Follath | 9fe6f92 | 2016-10-07 14:17:56 +0100 | [diff] [blame] | 19 | defined(MBEDTLS_FS_IO) && defined(MBEDTLS_CTR_DRBG_C) && \ |
| 20 | defined(MBEDTLS_SHA1_C) |
Andres AG | 788aa4a | 2016-09-14 14:32:09 +0100 | [diff] [blame] | 21 | #include "mbedtls/net_sockets.h" |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 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" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 29 | #include <stdio.h> |
| 30 | #include <string.h> |
| 31 | #endif |
| 32 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 33 | #define SERVER_NAME "localhost" |
Manuel Pégourié-Gonnard | c0d7494 | 2015-06-23 12:30:57 +0200 | [diff] [blame] | 34 | #define SERVER_PORT "11999" |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 35 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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) || \ |
Janos Follath | 9fe6f92 | 2016-10-07 14:17:56 +0100 | [diff] [blame] | 39 | !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_CTR_DRBG_C) || \ |
| 40 | !defined(MBEDTLS_SHA1_C) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 41 | int main(void) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 42 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | 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] | 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); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 48 | } |
| 49 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 50 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 51 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 52 | int main(void) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 53 | { |
| 54 | FILE *f; |
| 55 | |
Andres Amaya Garcia | 898841d | 2018-04-29 19:23:39 +0100 | [diff] [blame] | 56 | int ret = 1; |
| 57 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 58 | size_t n, buflen; |
Manuel Pégourié-Gonnard | 5db6432 | 2015-06-30 15:40:39 +0200 | [diff] [blame] | 59 | mbedtls_net_context server_fd; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 60 | |
| 61 | unsigned char *p, *end; |
Paul Bakker | 520ea91 | 2012-10-24 14:17:01 +0000 | [diff] [blame] | 62 | unsigned char buf[2048]; |
Manuel Pégourié-Gonnard | 102a620 | 2015-08-27 21:51:44 +0200 | [diff] [blame] | 63 | unsigned char hash[32]; |
Paul Bakker | ef3f8c7 | 2013-06-24 13:01:08 +0200 | [diff] [blame] | 64 | const char *pers = "dh_client"; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 65 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 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; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 71 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 72 | 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 Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 77 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 78 | /* |
| 79 | * 1. Setup the RNG |
| 80 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 81 | mbedtls_printf("\n . Seeding the random number generator"); |
| 82 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 83 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 84 | 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 Bakker | 508ad5a | 2011-12-04 17:09:26 +0000 | [diff] [blame] | 89 | goto exit; |
| 90 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 91 | |
| 92 | /* |
| 93 | * 2. Read the server's public RSA key |
| 94 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 95 | mbedtls_printf("\n . Reading public key from rsa_pub.txt"); |
| 96 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 97 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 98 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 101 | goto exit; |
| 102 | } |
| 103 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 104 | mbedtls_rsa_init(&rsa, MBEDTLS_RSA_PKCS_V15, 0); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 106 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 110 | goto exit; |
| 111 | } |
| 112 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 113 | rsa.len = (mbedtls_mpi_bitlen(&rsa.N) + 7) >> 3; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 115 | fclose(f); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 116 | |
| 117 | /* |
| 118 | * 3. Initiate the connection |
| 119 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 120 | mbedtls_printf("\n . Connecting to tcp/%s/%s", SERVER_NAME, |
| 121 | SERVER_PORT); |
| 122 | fflush(stdout); |
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_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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 127 | goto exit; |
| 128 | } |
| 129 | |
| 130 | /* |
| 131 | * 4a. First get the buffer length |
| 132 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 133 | mbedtls_printf("\n . Receiving the server's DH parameters"); |
| 134 | fflush(stdout); |
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 | memset(buf, 0, sizeof(buf)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 137 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 138 | if ((ret = mbedtls_net_recv(&server_fd, buf, 2)) != 2) { |
| 139 | mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 140 | goto exit; |
| 141 | } |
| 142 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 143 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 146 | goto exit; |
| 147 | } |
| 148 | |
| 149 | /* |
| 150 | * 4b. Get the DHM parameters: P, G and Ys = G^Xs mod P |
| 151 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 152 | memset(buf, 0, sizeof(buf)); |
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 ((ret = mbedtls_net_recv(&server_fd, buf, n)) != (int) n) { |
| 155 | mbedtls_printf(" failed\n ! mbedtls_net_recv returned %d\n\n", ret); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 156 | goto exit; |
| 157 | } |
| 158 | |
| 159 | p = buf, end = buf + buflen; |
| 160 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 161 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 163 | goto exit; |
| 164 | } |
| 165 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 166 | if (dhm.len < 64 || dhm.len > 512) { |
| 167 | mbedtls_printf(" failed\n ! Invalid DHM modulus size\n\n"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 168 | goto exit; |
| 169 | } |
| 170 | |
| 171 | /* |
| 172 | * 5. Check that the server's RSA signature matches |
Manuel Pégourié-Gonnard | 6f60cd8 | 2015-02-10 10:47:03 +0000 | [diff] [blame] | 173 | * the SHA-256 hash of (P,G,Ys) |
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 | mbedtls_printf("\n . Verifying the server's RSA signature"); |
| 176 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 177 | |
Paul Bakker | 88f17b8 | 2012-04-26 18:52:13 +0000 | [diff] [blame] | 178 | p += 2; |
| 179 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 180 | if ((n = (size_t) (end - p)) != rsa.len) { |
| 181 | mbedtls_printf(" failed\n ! Invalid RSA signature size\n\n"); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 182 | goto exit; |
| 183 | } |
| 184 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 185 | 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 Garcia | 1ff60f4 | 2017-06-28 13:26:36 +0100 | [diff] [blame] | 187 | goto exit; |
| 188 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 190 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 193 | goto exit; |
| 194 | } |
| 195 | |
| 196 | /* |
| 197 | * 6. Send our public value: Yc = G ^ Xc mod P |
| 198 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 199 | mbedtls_printf("\n . Sending own public value to server"); |
| 200 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 201 | |
| 202 | n = dhm.len; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 203 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 206 | goto exit; |
| 207 | } |
| 208 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 209 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 211 | goto exit; |
| 212 | } |
| 213 | |
| 214 | /* |
| 215 | * 7. Derive the shared secret: K = Ys ^ Xc mod P |
| 216 | */ |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 217 | mbedtls_printf("\n . Shared secret: "); |
| 218 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 219 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 220 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 223 | goto exit; |
| 224 | } |
| 225 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 226 | for (n = 0; n < 16; n++) { |
| 227 | mbedtls_printf("%02x", buf[n]); |
| 228 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 229 | |
| 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 Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 238 | mbedtls_printf("...\n . Receiving and decrypting the ciphertext"); |
| 239 | fflush(stdout); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 240 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 241 | ret = mbedtls_aes_setkey_dec(&aes, buf, 256); |
| 242 | if (ret != 0) { |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 243 | goto exit; |
| 244 | } |
| 245 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 246 | 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 Peskine | 377a310 | 2021-07-07 21:08:28 +0200 | [diff] [blame] | 250 | goto exit; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 251 | } |
| 252 | |
| 253 | ret = mbedtls_aes_crypt_ecb(&aes, MBEDTLS_AES_DECRYPT, buf, buf); |
| 254 | if (ret != 0) { |
| 255 | goto exit; |
| 256 | } |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 257 | buf[16] = '\0'; |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 258 | mbedtls_printf("\n . Plaintext is \"%s\"\n\n", (char *) buf); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 259 | |
Andres Amaya Garcia | 898841d | 2018-04-29 19:23:39 +0100 | [diff] [blame] | 260 | exit_code = MBEDTLS_EXIT_SUCCESS; |
| 261 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 262 | exit: |
| 263 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 264 | mbedtls_net_free(&server_fd); |
Paul Bakker | 0c22610 | 2014-04-17 16:02:36 +0200 | [diff] [blame] | 265 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 266 | 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 Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 271 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 272 | #if defined(_WIN32) |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 273 | mbedtls_printf(" + Press Enter to exit this program.\n"); |
| 274 | fflush(stdout); getchar(); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 275 | #endif |
| 276 | |
Gilles Peskine | 1b6c09a | 2023-01-11 14:52:35 +0100 | [diff] [blame] | 277 | mbedtls_exit(exit_code); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 278 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 279 | #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 */ |