Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * \brief Generic file encryption program using generic wrappers for configured |
| 3 | * security. |
| 4 | * |
Bence Szépkúti | 1e14827 | 2020-08-07 13:07:28 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Dave Rodgman | 16799db | 2023-11-02 19:47:20 +0000 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 7 | */ |
| 8 | |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 9 | /* Enable definition of fileno() even when compiling with -std=c99. Must be |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 10 | * set before mbedtls_config.h, which pulls in glibc's features.h indirectly. |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 11 | * Harmless on other platforms. */ |
nia | 1c0c837 | 2020-06-11 12:03:45 +0100 | [diff] [blame] | 12 | #define _POSIX_C_SOURCE 200112L |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 13 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 14 | #include "mbedtls/build_info.h" |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 15 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 16 | #include "mbedtls/platform.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 17 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 18 | #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 19 | defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 20 | #include "mbedtls/cipher.h" |
| 21 | #include "mbedtls/md.h" |
Hanno Becker | 0b44d5c | 2018-10-12 16:46:37 +0100 | [diff] [blame] | 22 | #include "mbedtls/platform_util.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 23 | |
| 24 | #include <stdio.h> |
| 25 | #include <stdlib.h> |
| 26 | #include <string.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 27 | #endif |
| 28 | |
Paul Bakker | 494c0b8 | 2011-04-24 15:30:07 +0000 | [diff] [blame] | 29 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 30 | #include <windows.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 31 | #if !defined(_WIN32_WCE) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 32 | #include <io.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 33 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 34 | #else |
| 35 | #include <sys/types.h> |
| 36 | #include <unistd.h> |
| 37 | #endif |
| 38 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 39 | #define MODE_ENCRYPT 0 |
| 40 | #define MODE_DECRYPT 1 |
| 41 | |
| 42 | #define USAGE \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 43 | "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \ |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 44 | "\n <mode>: 0 = encrypt, 1 = decrypt\n" \ |
| 45 | "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \ |
| 46 | "\n" |
| 47 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 48 | #if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \ |
| 49 | !defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 50 | int main(void) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 51 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 52 | mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n"); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 53 | mbedtls_exit(0); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 54 | } |
| 55 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 56 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 57 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 58 | int main(int argc, char *argv[]) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 59 | { |
Gilles Peskine | a5fc939 | 2020-04-14 19:34:19 +0200 | [diff] [blame] | 60 | int ret = 1, i; |
| 61 | unsigned n; |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 62 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 63 | int mode; |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 64 | size_t keylen, ilen, olen; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 65 | FILE *fkey, *fin = NULL, *fout = NULL; |
| 66 | |
| 67 | char *p; |
| 68 | unsigned char IV[16]; |
| 69 | unsigned char key[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 70 | unsigned char digest[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 71 | unsigned char buffer[1024]; |
| 72 | unsigned char output[1024]; |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 73 | unsigned char diff; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 74 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 75 | const mbedtls_cipher_info_t *cipher_info; |
| 76 | const mbedtls_md_info_t *md_info; |
| 77 | mbedtls_cipher_context_t cipher_ctx; |
| 78 | mbedtls_md_context_t md_ctx; |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 79 | mbedtls_cipher_mode_t cipher_mode; |
| 80 | unsigned int cipher_block_size; |
| 81 | unsigned char md_size; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 82 | #if defined(_WIN32_WCE) |
| 83 | long filesize, offset; |
| 84 | #elif defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 85 | LARGE_INTEGER li_size; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 86 | __int64 filesize, offset; |
| 87 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 88 | off_t filesize, offset; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 89 | #endif |
| 90 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 91 | mbedtls_cipher_init(&cipher_ctx); |
| 92 | mbedtls_md_init(&md_ctx); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 93 | |
| 94 | /* |
| 95 | * Parse the command-line arguments. |
| 96 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 97 | if (argc != 7) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 98 | const int *list; |
| 99 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 100 | mbedtls_printf(USAGE); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 101 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 102 | mbedtls_printf("Available ciphers:\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 103 | list = mbedtls_cipher_list(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 104 | while (*list) { |
| 105 | cipher_info = mbedtls_cipher_info_from_type(*list); |
Paul Elliott | e4b3f75 | 2023-12-11 17:57:16 +0000 | [diff] [blame] | 106 | const char *name = mbedtls_cipher_info_get_name(cipher_info); |
Paul Elliott | 80fa88e | 2023-11-24 17:12:24 +0000 | [diff] [blame] | 107 | |
Paul Elliott | e4b3f75 | 2023-12-11 17:57:16 +0000 | [diff] [blame] | 108 | if (name) { |
| 109 | mbedtls_printf(" %s\n", mbedtls_cipher_info_get_name(cipher_info)); |
Paul Elliott | 80fa88e | 2023-11-24 17:12:24 +0000 | [diff] [blame] | 110 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 111 | list++; |
| 112 | } |
| 113 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 114 | mbedtls_printf("\nAvailable message digests:\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 115 | list = mbedtls_md_list(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 116 | while (*list) { |
| 117 | md_info = mbedtls_md_info_from_type(*list); |
| 118 | mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info)); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 119 | list++; |
| 120 | } |
| 121 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 122 | goto exit; |
| 123 | } |
| 124 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 125 | mode = atoi(argv[1]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 126 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 127 | if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) { |
| 128 | mbedtls_fprintf(stderr, "invalid operation mode\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 129 | goto exit; |
| 130 | } |
| 131 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 132 | if (strcmp(argv[2], argv[3]) == 0) { |
| 133 | mbedtls_fprintf(stderr, "input and output filenames must differ\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 134 | goto exit; |
| 135 | } |
| 136 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 137 | if ((fin = fopen(argv[2], "rb")) == NULL) { |
| 138 | mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 139 | goto exit; |
| 140 | } |
| 141 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 142 | if ((fout = fopen(argv[3], "wb+")) == NULL) { |
| 143 | mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 144 | goto exit; |
| 145 | } |
| 146 | |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 147 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 148 | mbedtls_setbuf(fin, NULL); |
| 149 | mbedtls_setbuf(fout, NULL); |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 150 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 151 | /* |
| 152 | * Read the Cipher and MD from the command line |
| 153 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 154 | cipher_info = mbedtls_cipher_info_from_string(argv[4]); |
| 155 | if (cipher_info == NULL) { |
| 156 | mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 157 | goto exit; |
| 158 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 159 | if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) { |
| 160 | mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 161 | goto exit; |
| 162 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 163 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 164 | md_info = mbedtls_md_info_from_string(argv[5]); |
| 165 | if (md_info == NULL) { |
| 166 | mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 167 | goto exit; |
| 168 | } |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 169 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 170 | if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) { |
| 171 | mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n"); |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 172 | goto exit; |
| 173 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 174 | |
| 175 | /* |
Hanno Becker | 840bace | 2017-06-27 11:36:21 +0100 | [diff] [blame] | 176 | * Read the secret key from file or command line |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 177 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 178 | if ((fkey = fopen(argv[6], "rb")) != NULL) { |
| 179 | keylen = fread(key, 1, sizeof(key), fkey); |
| 180 | fclose(fkey); |
| 181 | } else { |
| 182 | if (memcmp(argv[6], "hex:", 4) == 0) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 183 | p = &argv[6][4]; |
| 184 | keylen = 0; |
| 185 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 186 | while (sscanf(p, "%02X", (unsigned int *) &n) > 0 && |
| 187 | keylen < (int) sizeof(key)) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 188 | key[keylen++] = (unsigned char) n; |
| 189 | p += 2; |
| 190 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 191 | } else { |
| 192 | keylen = strlen(argv[6]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 193 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 194 | if (keylen > (int) sizeof(key)) { |
| 195 | keylen = (int) sizeof(key); |
| 196 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 197 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 198 | memcpy(key, argv[6], keylen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 199 | } |
| 200 | } |
| 201 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 202 | #if defined(_WIN32_WCE) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 203 | filesize = fseek(fin, 0L, SEEK_END); |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 204 | #else |
| 205 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 206 | /* |
| 207 | * Support large files (> 2Gb) on Win32 |
| 208 | */ |
| 209 | li_size.QuadPart = 0; |
| 210 | li_size.LowPart = |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 211 | SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)), |
| 212 | li_size.LowPart, &li_size.HighPart, FILE_END); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 213 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 214 | if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) { |
| 215 | mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 216 | goto exit; |
| 217 | } |
| 218 | |
| 219 | filesize = li_size.QuadPart; |
| 220 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 221 | if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) { |
| 222 | perror("lseek"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 223 | goto exit; |
| 224 | } |
| 225 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 226 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 227 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 228 | if (fseek(fin, 0, SEEK_SET) < 0) { |
| 229 | mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 230 | goto exit; |
| 231 | } |
| 232 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 233 | md_size = mbedtls_md_get_size(md_info); |
| 234 | cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx); |
| 235 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 236 | if (mode == MODE_ENCRYPT) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 237 | /* |
| 238 | * Generate the initialization vector as: |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 239 | * IV = MD( filesize || filename )[0..15] |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 240 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 241 | for (i = 0; i < 8; i++) { |
| 242 | buffer[i] = (unsigned char) (filesize >> (i << 3)); |
| 243 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 244 | |
| 245 | p = argv[2]; |
| 246 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | if (mbedtls_md_starts(&md_ctx) != 0) { |
| 248 | mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 249 | goto exit; |
| 250 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 251 | if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) { |
| 252 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 253 | goto exit; |
| 254 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 255 | if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p)) |
| 256 | != 0) { |
| 257 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 258 | goto exit; |
| 259 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 260 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { |
| 261 | mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 262 | goto exit; |
| 263 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 264 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 265 | memcpy(IV, digest, 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 266 | |
| 267 | /* |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 268 | * Append the IV at the beginning of the output. |
| 269 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 270 | if (fwrite(IV, 1, 16, fout) != 16) { |
| 271 | mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 272 | goto exit; |
| 273 | } |
| 274 | |
| 275 | /* |
| 276 | * Hash the IV and the secret key together 8192 times |
| 277 | * using the result to setup the AES context and HMAC. |
| 278 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 279 | memset(digest, 0, 32); |
| 280 | memcpy(digest, IV, 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 281 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 282 | for (i = 0; i < 8192; i++) { |
| 283 | if (mbedtls_md_starts(&md_ctx) != 0) { |
| 284 | mbedtls_fprintf(stderr, |
| 285 | "mbedtls_md_starts() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 286 | goto exit; |
| 287 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 288 | if (mbedtls_md_update(&md_ctx, digest, 32) != 0) { |
| 289 | mbedtls_fprintf(stderr, |
| 290 | "mbedtls_md_update() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 291 | goto exit; |
| 292 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 293 | if (mbedtls_md_update(&md_ctx, key, keylen) != 0) { |
| 294 | mbedtls_fprintf(stderr, |
| 295 | "mbedtls_md_update() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 296 | goto exit; |
| 297 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 298 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { |
| 299 | mbedtls_fprintf(stderr, |
| 300 | "mbedtls_md_finish() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 301 | goto exit; |
| 302 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 303 | |
| 304 | } |
| 305 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 306 | if (mbedtls_cipher_setkey(&cipher_ctx, |
| 307 | digest, |
| 308 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 309 | MBEDTLS_ENCRYPT) != 0) { |
| 310 | mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 311 | goto exit; |
| 312 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) { |
| 314 | mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n"); |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 315 | goto exit; |
| 316 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 317 | if (mbedtls_cipher_reset(&cipher_ctx) != 0) { |
| 318 | mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 319 | goto exit; |
| 320 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 321 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 322 | if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) { |
| 323 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 324 | goto exit; |
| 325 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 326 | |
| 327 | /* |
| 328 | * Encrypt and write the ciphertext. |
| 329 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 330 | for (offset = 0; offset < filesize; offset += cipher_block_size) { |
| 331 | ilen = ((unsigned int) filesize - offset > cipher_block_size) ? |
| 332 | cipher_block_size : (unsigned int) (filesize - offset); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 333 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 334 | if (fread(buffer, 1, ilen, fin) != ilen) { |
| 335 | mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 336 | goto exit; |
| 337 | } |
| 338 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 339 | if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) { |
| 340 | mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 341 | goto exit; |
| 342 | } |
| 343 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 344 | if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) { |
| 345 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 346 | goto exit; |
| 347 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 348 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 349 | if (fwrite(output, 1, olen, fout) != olen) { |
| 350 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 351 | goto exit; |
| 352 | } |
| 353 | } |
| 354 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) { |
| 356 | mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 357 | goto exit; |
| 358 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 359 | if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) { |
| 360 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 361 | goto exit; |
| 362 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 363 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 364 | if (fwrite(output, 1, olen, fout) != olen) { |
| 365 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 366 | goto exit; |
| 367 | } |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 368 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 369 | /* |
| 370 | * Finally write the HMAC. |
| 371 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 372 | if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) { |
| 373 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 374 | goto exit; |
| 375 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 376 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 377 | if (fwrite(digest, 1, md_size, fout) != md_size) { |
| 378 | mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 379 | goto exit; |
| 380 | } |
| 381 | } |
| 382 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 383 | if (mode == MODE_DECRYPT) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 384 | /* |
| 385 | * The encrypted file must be structured as follows: |
| 386 | * |
| 387 | * 00 .. 15 Initialization Vector |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 388 | * 16 .. 31 Encrypted Block #1 |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 389 | * .. |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 390 | * N*16 .. (N+1)*16 - 1 Encrypted Block #N |
| 391 | * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 392 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 393 | if (filesize < 16 + md_size) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 394 | mbedtls_fprintf(stderr, "File too short to be encrypted.\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 395 | goto exit; |
| 396 | } |
| 397 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 398 | if (cipher_block_size == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 399 | mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n"); |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 400 | goto exit; |
| 401 | } |
| 402 | |
| 403 | /* |
| 404 | * Check the file size. |
| 405 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 406 | cipher_mode = mbedtls_cipher_info_get_mode(cipher_info); |
| 407 | if (cipher_mode != MBEDTLS_MODE_GCM && |
| 408 | cipher_mode != MBEDTLS_MODE_CTR && |
| 409 | cipher_mode != MBEDTLS_MODE_CFB && |
| 410 | cipher_mode != MBEDTLS_MODE_OFB && |
| 411 | ((filesize - md_size) % cipher_block_size) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 412 | mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n", |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 413 | cipher_block_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 414 | goto exit; |
| 415 | } |
| 416 | |
| 417 | /* |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 418 | * Subtract the IV + HMAC length. |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 419 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 420 | filesize -= (16 + md_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 421 | |
| 422 | /* |
| 423 | * Read the IV and original filesize modulo 16. |
| 424 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 425 | if (fread(buffer, 1, 16, fin) != 16) { |
| 426 | mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 427 | goto exit; |
| 428 | } |
| 429 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 430 | memcpy(IV, buffer, 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 431 | |
| 432 | /* |
| 433 | * Hash the IV and the secret key together 8192 times |
| 434 | * using the result to setup the AES context and HMAC. |
| 435 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 436 | memset(digest, 0, 32); |
| 437 | memcpy(digest, IV, 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 438 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 439 | for (i = 0; i < 8192; i++) { |
| 440 | if (mbedtls_md_starts(&md_ctx) != 0) { |
| 441 | mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 442 | goto exit; |
| 443 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 444 | if (mbedtls_md_update(&md_ctx, digest, 32) != 0) { |
| 445 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 446 | goto exit; |
| 447 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 448 | if (mbedtls_md_update(&md_ctx, key, keylen) != 0) { |
| 449 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 450 | goto exit; |
| 451 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 452 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { |
| 453 | mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 454 | goto exit; |
| 455 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 456 | } |
| 457 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 458 | if (mbedtls_cipher_setkey(&cipher_ctx, |
| 459 | digest, |
| 460 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 461 | MBEDTLS_DECRYPT) != 0) { |
| 462 | mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 463 | goto exit; |
| 464 | } |
| 465 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 466 | if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) { |
| 467 | mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 468 | goto exit; |
| 469 | } |
| 470 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 471 | if (mbedtls_cipher_reset(&cipher_ctx) != 0) { |
| 472 | mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 473 | goto exit; |
| 474 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 475 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 476 | if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) { |
| 477 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 478 | goto exit; |
| 479 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 480 | |
| 481 | /* |
| 482 | * Decrypt and write the plaintext. |
| 483 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 484 | for (offset = 0; offset < filesize; offset += cipher_block_size) { |
| 485 | ilen = ((unsigned int) filesize - offset > cipher_block_size) ? |
| 486 | cipher_block_size : (unsigned int) (filesize - offset); |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 487 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 488 | if (fread(buffer, 1, ilen, fin) != ilen) { |
| 489 | mbedtls_fprintf(stderr, "fread(%u bytes) failed\n", |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 490 | cipher_block_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 491 | goto exit; |
| 492 | } |
| 493 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) { |
| 495 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 496 | goto exit; |
| 497 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 498 | if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, |
| 499 | &olen) != 0) { |
| 500 | mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 501 | goto exit; |
| 502 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 503 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 504 | if (fwrite(output, 1, olen, fout) != olen) { |
| 505 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 506 | goto exit; |
| 507 | } |
| 508 | } |
| 509 | |
| 510 | /* |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 511 | * Verify the message authentication code. |
| 512 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 513 | if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) { |
| 514 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 515 | goto exit; |
| 516 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 517 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 518 | if (fread(buffer, 1, md_size, fin) != md_size) { |
| 519 | mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 520 | goto exit; |
| 521 | } |
| 522 | |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 523 | /* Use constant-time buffer comparison */ |
| 524 | diff = 0; |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 525 | for (i = 0; i < md_size; i++) { |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 526 | diff |= digest[i] ^ buffer[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 527 | } |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 528 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 529 | if (diff != 0) { |
| 530 | mbedtls_fprintf(stderr, "HMAC check failed: wrong key, " |
| 531 | "or file corrupted.\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 532 | goto exit; |
| 533 | } |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 534 | |
| 535 | /* |
| 536 | * Write the final block of data |
| 537 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 538 | if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) { |
| 539 | mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 540 | goto exit; |
| 541 | } |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 542 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 543 | if (fwrite(output, 1, olen, fout) != olen) { |
| 544 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 545 | goto exit; |
| 546 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 547 | } |
| 548 | |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 549 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 550 | |
| 551 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 552 | if (fin) { |
| 553 | fclose(fin); |
| 554 | } |
| 555 | if (fout) { |
| 556 | fclose(fout); |
| 557 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 558 | |
Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 559 | /* Zeroize all command line arguments to also cover |
| 560 | the case when the user has missed or reordered some, |
| 561 | in which case the key might not be in argv[6]. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | for (i = 0; i < argc; i++) { |
| 563 | mbedtls_platform_zeroize(argv[i], strlen(argv[i])); |
| 564 | } |
Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 565 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 566 | mbedtls_platform_zeroize(IV, sizeof(IV)); |
| 567 | mbedtls_platform_zeroize(key, sizeof(key)); |
| 568 | mbedtls_platform_zeroize(buffer, sizeof(buffer)); |
| 569 | mbedtls_platform_zeroize(output, sizeof(output)); |
| 570 | mbedtls_platform_zeroize(digest, sizeof(digest)); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 571 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 572 | mbedtls_cipher_free(&cipher_ctx); |
| 573 | mbedtls_md_free(&md_ctx); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 574 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 575 | mbedtls_exit(exit_code); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 576 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */ |