blob: 60bb75f620c3ce7bed03cb32dd66ebfd271c0470 [file] [log] [blame]
Paul Bakker20a78082011-01-21 09:32:12 +00001/*
2 * \brief Generic file encryption program using generic wrappers for configured
3 * security.
4 *
Bence Szépkúti1e148272020-08-07 13:07:28 +02005 * Copyright The Mbed TLS Contributors
Dave Rodgman7ff79652023-11-03 12:04:52 +00006 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
Paul Bakker20a78082011-01-21 09:32:12 +00007 */
8
Nicholas Wilson2682edf2017-12-05 12:08:15 +00009/* Enable definition of fileno() even when compiling with -std=c99. Must be
10 * set before config.h, which pulls in glibc's features.h indirectly.
11 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010012#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000013
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020014#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000015#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020016#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020017#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020018#endif
Paul Bakker20a78082011-01-21 09:32:12 +000019
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000020#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000021
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020022#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010023 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000024#include "mbedtls/cipher.h"
25#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010026#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000027
28#include <stdio.h>
29#include <stdlib.h>
30#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000031#endif
32
Paul Bakker494c0b82011-04-24 15:30:07 +000033#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000034#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000035#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000036#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000037#endif
Paul Bakker20a78082011-01-21 09:32:12 +000038#else
39#include <sys/types.h>
40#include <unistd.h>
41#endif
42
Paul Bakker20a78082011-01-21 09:32:12 +000043#define MODE_ENCRYPT 0
44#define MODE_DECRYPT 1
45
46#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020047 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000048 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
49 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
50 "\n"
51
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020052#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
53 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010054int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000055{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020056 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010057 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000058}
59#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000060
Simon Butcher63cb97e2018-12-06 17:43:31 +000061
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010062int main(int argc, char *argv[])
Paul Bakker20a78082011-01-21 09:32:12 +000063{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020064 int ret = 1, i;
65 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010066 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020067 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000068 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000069 FILE *fkey, *fin = NULL, *fout = NULL;
70
71 char *p;
72 unsigned char IV[16];
73 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020074 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000075 unsigned char buffer[1024];
76 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010077 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000078
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020079 const mbedtls_cipher_info_t *cipher_info;
80 const mbedtls_md_info_t *md_info;
81 mbedtls_cipher_context_t cipher_ctx;
82 mbedtls_md_context_t md_ctx;
Waleed Elmelegy6a4af482023-06-26 11:28:42 +010083 mbedtls_cipher_mode_t cipher_mode;
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +010084 unsigned int cipher_block_size;
85 unsigned char md_size;
Paul Bakkercce9d772011-11-18 14:26:47 +000086#if defined(_WIN32_WCE)
87 long filesize, offset;
88#elif defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010089 LARGE_INTEGER li_size;
Paul Bakker20a78082011-01-21 09:32:12 +000090 __int64 filesize, offset;
91#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010092 off_t filesize, offset;
Paul Bakker20a78082011-01-21 09:32:12 +000093#endif
94
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010095 mbedtls_cipher_init(&cipher_ctx);
96 mbedtls_md_init(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +000097
98 /*
99 * Parse the command-line arguments.
100 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 if (argc != 7) {
Paul Bakker20a78082011-01-21 09:32:12 +0000102 const int *list;
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 mbedtls_printf(USAGE);
Paul Bakker20a78082011-01-21 09:32:12 +0000105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_printf("Available ciphers:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200107 list = mbedtls_cipher_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100108 while (*list) {
109 cipher_info = mbedtls_cipher_info_from_type(*list);
110 mbedtls_printf(" %s\n", cipher_info->name);
Paul Bakker20a78082011-01-21 09:32:12 +0000111 list++;
112 }
113
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100114 mbedtls_printf("\nAvailable message digests:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200115 list = mbedtls_md_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 while (*list) {
117 md_info = mbedtls_md_info_from_type(*list);
118 mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000119 list++;
120 }
121
Paul Bakkercce9d772011-11-18 14:26:47 +0000122#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 mbedtls_printf("\n Press Enter to exit this program.\n");
124 fflush(stdout); getchar();
Paul Bakker20a78082011-01-21 09:32:12 +0000125#endif
126
127 goto exit;
128 }
129
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100130 mode = atoi(argv[1]);
Paul Bakker20a78082011-01-21 09:32:12 +0000131
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
133 mbedtls_fprintf(stderr, "invalid operation mode\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000134 goto exit;
135 }
136
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100137 if (strcmp(argv[2], argv[3]) == 0) {
138 mbedtls_fprintf(stderr, "input and output filenames must differ\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000139 goto exit;
140 }
141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 if ((fin = fopen(argv[2], "rb")) == NULL) {
143 mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
Paul Bakker20a78082011-01-21 09:32:12 +0000144 goto exit;
145 }
146
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100147 if ((fout = fopen(argv[3], "wb+")) == NULL) {
148 mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
Paul Bakker20a78082011-01-21 09:32:12 +0000149 goto exit;
150 }
151
152 /*
153 * Read the Cipher and MD from the command line
154 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100155 cipher_info = mbedtls_cipher_info_from_string(argv[4]);
156 if (cipher_info == NULL) {
157 mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
Paul Bakker20a78082011-01-21 09:32:12 +0000158 goto exit;
159 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100160 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
161 mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200162 goto exit;
163 }
Paul Bakker20a78082011-01-21 09:32:12 +0000164
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100165 md_info = mbedtls_md_info_from_string(argv[5]);
166 if (md_info == NULL) {
167 mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
Paul Bakker20a78082011-01-21 09:32:12 +0000168 goto exit;
169 }
Janos Follath8eb64132016-06-03 15:40:57 +0100170
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
172 mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
Janos Follath8eb64132016-06-03 15:40:57 +0100173 goto exit;
174 }
Paul Bakker20a78082011-01-21 09:32:12 +0000175
176 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100177 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000178 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100179 if ((fkey = fopen(argv[6], "rb")) != NULL) {
180 keylen = fread(key, 1, sizeof(key), fkey);
181 fclose(fkey);
182 } else {
183 if (memcmp(argv[6], "hex:", 4) == 0) {
Paul Bakker20a78082011-01-21 09:32:12 +0000184 p = &argv[6][4];
185 keylen = 0;
186
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100187 while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
188 keylen < (int) sizeof(key)) {
Paul Bakker20a78082011-01-21 09:32:12 +0000189 key[keylen++] = (unsigned char) n;
190 p += 2;
191 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100192 } else {
193 keylen = strlen(argv[6]);
Paul Bakker20a78082011-01-21 09:32:12 +0000194
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100195 if (keylen > (int) sizeof(key)) {
196 keylen = (int) sizeof(key);
197 }
Paul Bakker20a78082011-01-21 09:32:12 +0000198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 memcpy(key, argv[6], keylen);
Paul Bakker20a78082011-01-21 09:32:12 +0000200 }
201 }
202
Paul Bakkercce9d772011-11-18 14:26:47 +0000203#if defined(_WIN32_WCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 filesize = fseek(fin, 0L, SEEK_END);
Paul Bakkercce9d772011-11-18 14:26:47 +0000205#else
206#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000207 /*
208 * Support large files (> 2Gb) on Win32
209 */
210 li_size.QuadPart = 0;
211 li_size.LowPart =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100212 SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
213 li_size.LowPart, &li_size.HighPart, FILE_END);
Paul Bakker20a78082011-01-21 09:32:12 +0000214
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
216 mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000217 goto exit;
218 }
219
220 filesize = li_size.QuadPart;
221#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100222 if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
223 perror("lseek");
Paul Bakker20a78082011-01-21 09:32:12 +0000224 goto exit;
225 }
226#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000227#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000228
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100229 if (fseek(fin, 0, SEEK_SET) < 0) {
230 mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000231 goto exit;
232 }
233
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100234 md_size = mbedtls_md_get_size(md_info);
235 cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx);
236
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100237 if (mode == MODE_ENCRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000238 /*
239 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200240 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000241 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100242 for (i = 0; i < 8; i++) {
243 buffer[i] = (unsigned char) (filesize >> (i << 3));
244 }
Paul Bakker20a78082011-01-21 09:32:12 +0000245
246 p = argv[2];
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if (mbedtls_md_starts(&md_ctx) != 0) {
249 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000250 goto exit;
251 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100252 if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
253 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000254 goto exit;
255 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100256 if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
257 != 0) {
258 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000259 goto exit;
260 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100261 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
262 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000263 goto exit;
264 }
Paul Bakker20a78082011-01-21 09:32:12 +0000265
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100266 memcpy(IV, digest, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000267
268 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000269 * Append the IV at the beginning of the output.
270 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100271 if (fwrite(IV, 1, 16, fout) != 16) {
272 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000273 goto exit;
274 }
275
276 /*
277 * Hash the IV and the secret key together 8192 times
278 * using the result to setup the AES context and HMAC.
279 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100280 memset(digest, 0, 32);
281 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000282
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 for (i = 0; i < 8192; i++) {
284 if (mbedtls_md_starts(&md_ctx) != 0) {
285 mbedtls_fprintf(stderr,
286 "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000287 goto exit;
288 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
290 mbedtls_fprintf(stderr,
291 "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000292 goto exit;
293 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
295 mbedtls_fprintf(stderr,
296 "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000297 goto exit;
298 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100299 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
300 mbedtls_fprintf(stderr,
301 "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000302 goto exit;
303 }
Paul Bakker20a78082011-01-21 09:32:12 +0000304
305 }
306
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100307 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
308 MBEDTLS_ENCRYPT) != 0) {
309 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000310 goto exit;
311 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100312 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
313 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200314 goto exit;
315 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100316 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
317 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000318 goto exit;
319 }
Paul Bakker20a78082011-01-21 09:32:12 +0000320
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100321 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
322 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100323 goto exit;
324 }
Paul Bakker20a78082011-01-21 09:32:12 +0000325
326 /*
327 * Encrypt and write the ciphertext.
328 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100329 for (offset = 0; offset < filesize; offset += cipher_block_size) {
330 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
331 cipher_block_size : (unsigned int) (filesize - offset);
Paul Bakker20a78082011-01-21 09:32:12 +0000332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if (fread(buffer, 1, ilen, fin) != ilen) {
334 mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
Paul Bakker20a78082011-01-21 09:32:12 +0000335 goto exit;
336 }
337
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100338 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
339 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200340 goto exit;
341 }
342
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100343 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
344 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100345 goto exit;
346 }
Paul Bakker20a78082011-01-21 09:32:12 +0000347
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100348 if (fwrite(output, 1, olen, fout) != olen) {
349 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000350 goto exit;
351 }
352 }
353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
355 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000356 goto exit;
357 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100358 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
359 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100360 goto exit;
361 }
Paul Bakker20a78082011-01-21 09:32:12 +0000362
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100363 if (fwrite(output, 1, olen, fout) != olen) {
364 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000365 goto exit;
366 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000367
Paul Bakker20a78082011-01-21 09:32:12 +0000368 /*
369 * Finally write the HMAC.
370 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100371 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
372 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100373 goto exit;
374 }
Paul Bakker20a78082011-01-21 09:32:12 +0000375
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100376 if (fwrite(digest, 1, md_size, fout) != md_size) {
377 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000378 goto exit;
379 }
380 }
381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (mode == MODE_DECRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000383 /*
384 * The encrypted file must be structured as follows:
385 *
386 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200387 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000388 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200389 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
390 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000391 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100392 if (filesize < 16 + md_size) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000394 goto exit;
395 }
396
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100397 if (cipher_block_size == 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
Janos Follath8eb64132016-06-03 15:40:57 +0100399 goto exit;
400 }
401
402 /*
403 * Check the file size.
404 */
Waleed Elmelegy6a4af482023-06-26 11:28:42 +0100405 cipher_mode = cipher_info->mode;
406 if (cipher_mode != MBEDTLS_MODE_GCM &&
407 cipher_mode != MBEDTLS_MODE_CTR &&
408 cipher_mode != MBEDTLS_MODE_CFB &&
409 cipher_mode != MBEDTLS_MODE_OFB &&
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100410 ((filesize - md_size) % cipher_block_size) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100412 cipher_block_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000413 goto exit;
414 }
415
416 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100417 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000418 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100419 filesize -= (16 + md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000420
421 /*
422 * Read the IV and original filesize modulo 16.
423 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100424 if (fread(buffer, 1, 16, fin) != 16) {
425 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000426 goto exit;
427 }
428
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100429 memcpy(IV, buffer, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000430
431 /*
432 * Hash the IV and the secret key together 8192 times
433 * using the result to setup the AES context and HMAC.
434 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 memset(digest, 0, 32);
436 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000437
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100438 for (i = 0; i < 8192; i++) {
439 if (mbedtls_md_starts(&md_ctx) != 0) {
440 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100441 goto exit;
442 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100443 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
444 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100445 goto exit;
446 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
448 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100449 goto exit;
450 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100451 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
452 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100453 goto exit;
454 }
Paul Bakker20a78082011-01-21 09:32:12 +0000455 }
456
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
458 MBEDTLS_DECRYPT) != 0) {
459 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200460 goto exit;
461 }
462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
464 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200465 goto exit;
466 }
467
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100468 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
469 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200470 goto exit;
471 }
Paul Bakker20a78082011-01-21 09:32:12 +0000472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
474 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100475 goto exit;
476 }
Paul Bakker20a78082011-01-21 09:32:12 +0000477
478 /*
479 * Decrypt and write the plaintext.
480 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100481 for (offset = 0; offset < filesize; offset += cipher_block_size) {
482 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
483 cipher_block_size : (unsigned int) (filesize - offset);
Paul Bakker243f48e2016-09-02 22:44:09 +0200484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 if (fread(buffer, 1, ilen, fin) != ilen) {
486 mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100487 cipher_block_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000488 goto exit;
489 }
490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
492 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100493 goto exit;
494 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
496 &olen) != 0) {
497 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200498 goto exit;
499 }
Paul Bakker20a78082011-01-21 09:32:12 +0000500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 if (fwrite(output, 1, olen, fout) != olen) {
502 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000503 goto exit;
504 }
505 }
506
507 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000508 * Verify the message authentication code.
509 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100510 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
511 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100512 goto exit;
513 }
Paul Bakker20a78082011-01-21 09:32:12 +0000514
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100515 if (fread(buffer, 1, md_size, fin) != md_size) {
516 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000517 goto exit;
518 }
519
Paul Bakker424cd692013-10-31 14:22:08 +0100520 /* Use constant-time buffer comparison */
521 diff = 0;
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100522 for (i = 0; i < md_size; i++) {
Paul Bakker424cd692013-10-31 14:22:08 +0100523 diff |= digest[i] ^ buffer[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100524 }
Paul Bakker424cd692013-10-31 14:22:08 +0100525
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100526 if (diff != 0) {
527 mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
528 "or file corrupted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000529 goto exit;
530 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100531
532 /*
533 * Write the final block of data
534 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100535 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
536 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100537 goto exit;
538 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100539
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100540 if (fwrite(output, 1, olen, fout) != olen) {
541 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100542 goto exit;
543 }
Paul Bakker20a78082011-01-21 09:32:12 +0000544 }
545
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100546 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000547
548exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100549 if (fin) {
550 fclose(fin);
551 }
552 if (fout) {
553 fclose(fout);
554 }
Paul Bakker20a78082011-01-21 09:32:12 +0000555
Hanno Beckerf601ec52017-06-27 08:22:17 +0100556 /* Zeroize all command line arguments to also cover
557 the case when the user has missed or reordered some,
558 in which case the key might not be in argv[6]. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 for (i = 0; i < argc; i++) {
560 mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
561 }
Hanno Beckerf601ec52017-06-27 08:22:17 +0100562
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100563 mbedtls_platform_zeroize(IV, sizeof(IV));
564 mbedtls_platform_zeroize(key, sizeof(key));
565 mbedtls_platform_zeroize(buffer, sizeof(buffer));
566 mbedtls_platform_zeroize(output, sizeof(output));
567 mbedtls_platform_zeroize(digest, sizeof(digest));
Paul Bakker20a78082011-01-21 09:32:12 +0000568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 mbedtls_cipher_free(&cipher_ctx);
570 mbedtls_md_free(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000571
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100572 mbedtls_exit(exit_code);
Paul Bakker20a78082011-01-21 09:32:12 +0000573}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200574#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */