| 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); | 
|  | 106 | mbedtls_printf("  %s\n", mbedtls_cipher_info_get_name(cipher_info)); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 107 | list++; | 
|  | 108 | } | 
|  | 109 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 110 | mbedtls_printf("\nAvailable message digests:\n"); | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 111 | list = mbedtls_md_list(); | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 112 | while (*list) { | 
|  | 113 | md_info = mbedtls_md_info_from_type(*list); | 
|  | 114 | mbedtls_printf("  %s\n", mbedtls_md_get_name(md_info)); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 115 | list++; | 
|  | 116 | } | 
|  | 117 |  | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 118 | goto exit; | 
|  | 119 | } | 
|  | 120 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 121 | mode = atoi(argv[1]); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 122 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 123 | if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) { | 
|  | 124 | mbedtls_fprintf(stderr, "invalid operation mode\n"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 125 | goto exit; | 
|  | 126 | } | 
|  | 127 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | if (strcmp(argv[2], argv[3]) == 0) { | 
|  | 129 | mbedtls_fprintf(stderr, "input and output filenames must differ\n"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 130 | goto exit; | 
|  | 131 | } | 
|  | 132 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 133 | if ((fin = fopen(argv[2], "rb")) == NULL) { | 
|  | 134 | mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 135 | goto exit; | 
|  | 136 | } | 
|  | 137 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | if ((fout = fopen(argv[3], "wb+")) == NULL) { | 
|  | 139 | mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 140 | goto exit; | 
|  | 141 | } | 
|  | 142 |  | 
| Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 143 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 144 | mbedtls_setbuf(fin, NULL); | 
|  | 145 | mbedtls_setbuf(fout, NULL); | 
| Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 146 |  | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 147 | /* | 
|  | 148 | * Read the Cipher and MD from the command line | 
|  | 149 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 150 | cipher_info = mbedtls_cipher_info_from_string(argv[4]); | 
|  | 151 | if (cipher_info == NULL) { | 
|  | 152 | mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 153 | goto exit; | 
|  | 154 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 155 | if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) { | 
|  | 156 | mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n"); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 157 | goto exit; | 
|  | 158 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 159 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 160 | md_info = mbedtls_md_info_from_string(argv[5]); | 
|  | 161 | if (md_info == NULL) { | 
|  | 162 | mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 163 | goto exit; | 
|  | 164 | } | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 165 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 166 | if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) { | 
|  | 167 | mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n"); | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 168 | goto exit; | 
|  | 169 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 170 |  | 
|  | 171 | /* | 
| Hanno Becker | 840bace | 2017-06-27 11:36:21 +0100 | [diff] [blame] | 172 | * Read the secret key from file or command line | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 173 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 174 | if ((fkey = fopen(argv[6], "rb")) != NULL) { | 
|  | 175 | keylen = fread(key, 1, sizeof(key), fkey); | 
|  | 176 | fclose(fkey); | 
|  | 177 | } else { | 
|  | 178 | if (memcmp(argv[6], "hex:", 4) == 0) { | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 179 | p = &argv[6][4]; | 
|  | 180 | keylen = 0; | 
|  | 181 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 182 | while (sscanf(p, "%02X", (unsigned int *) &n) > 0 && | 
|  | 183 | keylen < (int) sizeof(key)) { | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 184 | key[keylen++] = (unsigned char) n; | 
|  | 185 | p += 2; | 
|  | 186 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 187 | } else { | 
|  | 188 | keylen = strlen(argv[6]); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 189 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 190 | if (keylen > (int) sizeof(key)) { | 
|  | 191 | keylen = (int) sizeof(key); | 
|  | 192 | } | 
| 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 | memcpy(key, argv[6], keylen); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 195 | } | 
|  | 196 | } | 
|  | 197 |  | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 198 | #if defined(_WIN32_WCE) | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 199 | filesize = fseek(fin, 0L, SEEK_END); | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 200 | #else | 
|  | 201 | #if defined(_WIN32) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 202 | /* | 
|  | 203 | * Support large files (> 2Gb) on Win32 | 
|  | 204 | */ | 
|  | 205 | li_size.QuadPart = 0; | 
|  | 206 | li_size.LowPart  = | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 207 | SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)), | 
|  | 208 | li_size.LowPart, &li_size.HighPart, FILE_END); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 209 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 210 | if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) { | 
|  | 211 | mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 212 | goto exit; | 
|  | 213 | } | 
|  | 214 |  | 
|  | 215 | filesize = li_size.QuadPart; | 
|  | 216 | #else | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 217 | if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) { | 
|  | 218 | perror("lseek"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 219 | goto exit; | 
|  | 220 | } | 
|  | 221 | #endif | 
| Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 222 | #endif | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 223 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 224 | if (fseek(fin, 0, SEEK_SET) < 0) { | 
|  | 225 | mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 226 | goto exit; | 
|  | 227 | } | 
|  | 228 |  | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 229 | md_size = mbedtls_md_get_size(md_info); | 
|  | 230 | cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx); | 
|  | 231 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 232 | if (mode == MODE_ENCRYPT) { | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 233 | /* | 
|  | 234 | * Generate the initialization vector as: | 
| Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 235 | * IV = MD( filesize || filename )[0..15] | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 236 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 237 | for (i = 0; i < 8; i++) { | 
|  | 238 | buffer[i] = (unsigned char) (filesize >> (i << 3)); | 
|  | 239 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 240 |  | 
|  | 241 | p = argv[2]; | 
|  | 242 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 243 | if (mbedtls_md_starts(&md_ctx) != 0) { | 
|  | 244 | mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 245 | goto exit; | 
|  | 246 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 247 | if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) { | 
|  | 248 | mbedtls_fprintf(stderr, "mbedtls_md_update() 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, (unsigned char *) p, strlen(p)) | 
|  | 252 | != 0) { | 
|  | 253 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 254 | goto exit; | 
|  | 255 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 256 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { | 
|  | 257 | mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 258 | goto exit; | 
|  | 259 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 260 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | memcpy(IV, digest, 16); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 262 |  | 
|  | 263 | /* | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 264 | * Append the IV at the beginning of the output. | 
|  | 265 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 266 | if (fwrite(IV, 1, 16, fout) != 16) { | 
|  | 267 | mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 268 | goto exit; | 
|  | 269 | } | 
|  | 270 |  | 
|  | 271 | /* | 
|  | 272 | * Hash the IV and the secret key together 8192 times | 
|  | 273 | * using the result to setup the AES context and HMAC. | 
|  | 274 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 275 | memset(digest, 0,  32); | 
|  | 276 | memcpy(digest, IV, 16); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 277 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 278 | for (i = 0; i < 8192; i++) { | 
|  | 279 | if (mbedtls_md_starts(&md_ctx) != 0) { | 
|  | 280 | mbedtls_fprintf(stderr, | 
|  | 281 | "mbedtls_md_starts() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 282 | goto exit; | 
|  | 283 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 284 | if (mbedtls_md_update(&md_ctx, digest, 32) != 0) { | 
|  | 285 | mbedtls_fprintf(stderr, | 
|  | 286 | "mbedtls_md_update() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 287 | goto exit; | 
|  | 288 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 289 | if (mbedtls_md_update(&md_ctx, key, keylen) != 0) { | 
|  | 290 | mbedtls_fprintf(stderr, | 
|  | 291 | "mbedtls_md_update() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 292 | goto exit; | 
|  | 293 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 294 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { | 
|  | 295 | mbedtls_fprintf(stderr, | 
|  | 296 | "mbedtls_md_finish() returned error\n"); | 
| Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 297 | goto exit; | 
|  | 298 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 299 |  | 
|  | 300 | } | 
|  | 301 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 302 | if (mbedtls_cipher_setkey(&cipher_ctx, | 
|  | 303 | digest, | 
|  | 304 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), | 
|  | 305 | MBEDTLS_ENCRYPT) != 0) { | 
|  | 306 | mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n"); | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 307 | goto exit; | 
|  | 308 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 309 | if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) { | 
|  | 310 | mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n"); | 
| Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 311 | goto exit; | 
|  | 312 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 313 | if (mbedtls_cipher_reset(&cipher_ctx) != 0) { | 
|  | 314 | mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n"); | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 315 | goto exit; | 
|  | 316 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 317 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) { | 
|  | 319 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 320 | goto exit; | 
|  | 321 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 322 |  | 
|  | 323 | /* | 
|  | 324 | * Encrypt and write the ciphertext. | 
|  | 325 | */ | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 326 | for (offset = 0; offset < filesize; offset += cipher_block_size) { | 
|  | 327 | ilen = ((unsigned int) filesize - offset > cipher_block_size) ? | 
|  | 328 | cipher_block_size : (unsigned int) (filesize - offset); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 329 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 330 | if (fread(buffer, 1, ilen, fin) != ilen) { | 
|  | 331 | mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 332 | goto exit; | 
|  | 333 | } | 
|  | 334 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) { | 
|  | 336 | mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n"); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 337 | goto exit; | 
|  | 338 | } | 
|  | 339 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) { | 
|  | 341 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 342 | goto exit; | 
|  | 343 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 344 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | if (fwrite(output, 1, olen, fout) != olen) { | 
|  | 346 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 347 | goto exit; | 
|  | 348 | } | 
|  | 349 | } | 
|  | 350 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 351 | if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) { | 
|  | 352 | mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n"); | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 353 | goto exit; | 
|  | 354 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 355 | if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) { | 
|  | 356 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 357 | goto exit; | 
|  | 358 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 359 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | if (fwrite(output, 1, olen, fout) != olen) { | 
|  | 361 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 362 | goto exit; | 
|  | 363 | } | 
| Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 364 |  | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 365 | /* | 
|  | 366 | * Finally write the HMAC. | 
|  | 367 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 368 | if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) { | 
|  | 369 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 370 | goto exit; | 
|  | 371 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 372 |  | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 373 | if (fwrite(digest, 1, md_size, fout) != md_size) { | 
|  | 374 | mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 375 | goto exit; | 
|  | 376 | } | 
|  | 377 | } | 
|  | 378 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 379 | if (mode == MODE_DECRYPT) { | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 380 | /* | 
|  | 381 | *  The encrypted file must be structured as follows: | 
|  | 382 | * | 
|  | 383 | *        00 .. 15              Initialization Vector | 
| Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 384 | *        16 .. 31              Encrypted Block #1 | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 385 | *           .. | 
| Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 386 | *      N*16 .. (N+1)*16 - 1    Encrypted Block #N | 
|  | 387 | *  (N+1)*16 .. (N+1)*16 + n    Hash(ciphertext) | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 388 | */ | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 389 | if (filesize < 16 + md_size) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 390 | mbedtls_fprintf(stderr, "File too short to be encrypted.\n"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 391 | goto exit; | 
|  | 392 | } | 
|  | 393 |  | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 394 | if (cipher_block_size == 0) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n"); | 
| Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 396 | goto exit; | 
|  | 397 | } | 
|  | 398 |  | 
|  | 399 | /* | 
|  | 400 | * Check the file size. | 
|  | 401 | */ | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 402 | cipher_mode = mbedtls_cipher_info_get_mode(cipher_info); | 
|  | 403 | if (cipher_mode != MBEDTLS_MODE_GCM && | 
|  | 404 | cipher_mode != MBEDTLS_MODE_CTR && | 
|  | 405 | cipher_mode != MBEDTLS_MODE_CFB && | 
|  | 406 | cipher_mode != MBEDTLS_MODE_OFB && | 
|  | 407 | ((filesize - md_size) % cipher_block_size) != 0) { | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 408 | 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] | 409 | cipher_block_size); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 410 | goto exit; | 
|  | 411 | } | 
|  | 412 |  | 
|  | 413 | /* | 
| Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 414 | * Subtract the IV + HMAC length. | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 415 | */ | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 416 | filesize -= (16 + md_size); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 417 |  | 
|  | 418 | /* | 
|  | 419 | * Read the IV and original filesize modulo 16. | 
|  | 420 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 421 | if (fread(buffer, 1, 16, fin) != 16) { | 
|  | 422 | mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 423 | goto exit; | 
|  | 424 | } | 
|  | 425 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | memcpy(IV, buffer, 16); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 427 |  | 
|  | 428 | /* | 
|  | 429 | * Hash the IV and the secret key together 8192 times | 
|  | 430 | * using the result to setup the AES context and HMAC. | 
|  | 431 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 432 | memset(digest, 0,  32); | 
|  | 433 | memcpy(digest, IV, 16); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 434 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 435 | for (i = 0; i < 8192; i++) { | 
|  | 436 | if (mbedtls_md_starts(&md_ctx) != 0) { | 
|  | 437 | mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 438 | goto exit; | 
|  | 439 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | if (mbedtls_md_update(&md_ctx, digest, 32) != 0) { | 
|  | 441 | mbedtls_fprintf(stderr, "mbedtls_md_update() 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, key, keylen) != 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_finish(&md_ctx, digest) != 0) { | 
|  | 449 | mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 450 | goto exit; | 
|  | 451 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 452 | } | 
|  | 453 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 454 | if (mbedtls_cipher_setkey(&cipher_ctx, | 
|  | 455 | digest, | 
|  | 456 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), | 
|  | 457 | MBEDTLS_DECRYPT) != 0) { | 
|  | 458 | mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n"); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 459 | goto exit; | 
|  | 460 | } | 
|  | 461 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 462 | if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) { | 
|  | 463 | mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n"); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 464 | goto exit; | 
|  | 465 | } | 
|  | 466 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | if (mbedtls_cipher_reset(&cipher_ctx) != 0) { | 
|  | 468 | mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n"); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 469 | goto exit; | 
|  | 470 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 471 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) { | 
|  | 473 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 474 | goto exit; | 
|  | 475 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 476 |  | 
|  | 477 | /* | 
|  | 478 | * Decrypt and write the plaintext. | 
|  | 479 | */ | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 480 | for (offset = 0; offset < filesize; offset += cipher_block_size) { | 
|  | 481 | ilen = ((unsigned int) filesize - offset > cipher_block_size) ? | 
|  | 482 | cipher_block_size : (unsigned int) (filesize - offset); | 
| Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 483 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 484 | if (fread(buffer, 1, ilen, fin) != ilen) { | 
|  | 485 | mbedtls_fprintf(stderr, "fread(%u bytes) failed\n", | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 486 | cipher_block_size); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 487 | goto exit; | 
|  | 488 | } | 
|  | 489 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 490 | if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) { | 
|  | 491 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 492 | goto exit; | 
|  | 493 | } | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 494 | if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, | 
|  | 495 | &olen) != 0) { | 
|  | 496 | mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n"); | 
| Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 497 | goto exit; | 
|  | 498 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 499 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 500 | if (fwrite(output, 1, olen, fout) != olen) { | 
|  | 501 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 502 | goto exit; | 
|  | 503 | } | 
|  | 504 | } | 
|  | 505 |  | 
|  | 506 | /* | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 507 | * Verify the message authentication code. | 
|  | 508 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 509 | if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) { | 
|  | 510 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 511 | goto exit; | 
|  | 512 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 513 |  | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 514 | if (fread(buffer, 1, md_size, fin) != md_size) { | 
|  | 515 | mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 516 | goto exit; | 
|  | 517 | } | 
|  | 518 |  | 
| Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 519 | /* Use constant-time buffer comparison */ | 
|  | 520 | diff = 0; | 
| Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 521 | for (i = 0; i < md_size; i++) { | 
| Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 522 | diff |= digest[i] ^ buffer[i]; | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 523 | } | 
| Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 524 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 525 | if (diff != 0) { | 
|  | 526 | mbedtls_fprintf(stderr, "HMAC check failed: wrong key, " | 
|  | 527 | "or file corrupted.\n"); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 528 | goto exit; | 
|  | 529 | } | 
| Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 530 |  | 
|  | 531 | /* | 
|  | 532 | * Write the final block of data | 
|  | 533 | */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 534 | if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) { | 
|  | 535 | mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n"); | 
| Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 536 | goto exit; | 
|  | 537 | } | 
| Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 538 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | if (fwrite(output, 1, olen, fout) != olen) { | 
|  | 540 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); | 
| Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 541 | goto exit; | 
|  | 542 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 543 | } | 
|  | 544 |  | 
| Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 545 | exit_code = MBEDTLS_EXIT_SUCCESS; | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 546 |  | 
|  | 547 | exit: | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 548 | if (fin) { | 
|  | 549 | fclose(fin); | 
|  | 550 | } | 
|  | 551 | if (fout) { | 
|  | 552 | fclose(fout); | 
|  | 553 | } | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 554 |  | 
| Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 555 | /* Zeroize all command line arguments to also cover | 
|  | 556 | the case when the user has missed or reordered some, | 
|  | 557 | in which case the key might not be in argv[6]. */ | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 558 | for (i = 0; i < argc; i++) { | 
|  | 559 | mbedtls_platform_zeroize(argv[i], strlen(argv[i])); | 
|  | 560 | } | 
| Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 561 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 562 | mbedtls_platform_zeroize(IV,     sizeof(IV)); | 
|  | 563 | mbedtls_platform_zeroize(key,    sizeof(key)); | 
|  | 564 | mbedtls_platform_zeroize(buffer, sizeof(buffer)); | 
|  | 565 | mbedtls_platform_zeroize(output, sizeof(output)); | 
|  | 566 | mbedtls_platform_zeroize(digest, sizeof(digest)); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 567 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 568 | mbedtls_cipher_free(&cipher_ctx); | 
|  | 569 | mbedtls_md_free(&md_ctx); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 570 |  | 
| Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 571 | mbedtls_exit(exit_code); | 
| Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 572 | } | 
| Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */ |