blob: 17c8a313283da7a3ffca655ed9516cb3e2e6532e [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
Manuel Pégourié-Gonnard37ff1402015-09-04 14:21:07 +02006 * SPDX-License-Identifier: Apache-2.0
7 *
8 * Licensed under the Apache License, Version 2.0 (the "License"); you may
9 * not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
11 *
12 * http://www.apache.org/licenses/LICENSE-2.0
13 *
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
16 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
Paul Bakker20a78082011-01-21 09:32:12 +000019 */
20
Nicholas Wilson2682edf2017-12-05 12:08:15 +000021/* Enable definition of fileno() even when compiling with -std=c99. Must be
22 * set before config.h, which pulls in glibc's features.h indirectly.
23 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010024#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000025
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020026#if !defined(MBEDTLS_CONFIG_FILE)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000027#include "mbedtls/config.h"
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020028#else
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020029#include MBEDTLS_CONFIG_FILE
Manuel Pégourié-Gonnardcef4ad22014-04-29 12:39:06 +020030#endif
Paul Bakker20a78082011-01-21 09:32:12 +000031
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000033
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020034#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010035 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000036#include "mbedtls/cipher.h"
37#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010038#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000039
40#include <stdio.h>
41#include <stdlib.h>
42#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000043#endif
44
Paul Bakker494c0b82011-04-24 15:30:07 +000045#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000046#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000047#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000048#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000049#endif
Paul Bakker20a78082011-01-21 09:32:12 +000050#else
51#include <sys/types.h>
52#include <unistd.h>
53#endif
54
Paul Bakker20a78082011-01-21 09:32:12 +000055#define MODE_ENCRYPT 0
56#define MODE_DECRYPT 1
57
58#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020059 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000060 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
61 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
62 "\n"
63
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
65 !defined(MBEDTLS_FS_IO)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010066int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000067{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020068 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 +010069 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000070}
71#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000072
Simon Butcher63cb97e2018-12-06 17:43:31 +000073
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010074int main(int argc, char *argv[])
Paul Bakker20a78082011-01-21 09:32:12 +000075{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020076 int ret = 1, i;
77 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010078 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020079 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000080 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000081 FILE *fkey, *fin = NULL, *fout = NULL;
82
83 char *p;
84 unsigned char IV[16];
85 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020086 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000087 unsigned char buffer[1024];
88 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010089 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000090
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020091 const mbedtls_cipher_info_t *cipher_info;
92 const mbedtls_md_info_t *md_info;
93 mbedtls_cipher_context_t cipher_ctx;
94 mbedtls_md_context_t md_ctx;
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +010095 unsigned int cipher_block_size;
96 unsigned char md_size;
Paul Bakkercce9d772011-11-18 14:26:47 +000097#if defined(_WIN32_WCE)
98 long filesize, offset;
99#elif defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100100 LARGE_INTEGER li_size;
Paul Bakker20a78082011-01-21 09:32:12 +0000101 __int64 filesize, offset;
102#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100103 off_t filesize, offset;
Paul Bakker20a78082011-01-21 09:32:12 +0000104#endif
105
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100106 mbedtls_cipher_init(&cipher_ctx);
107 mbedtls_md_init(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000108
109 /*
110 * Parse the command-line arguments.
111 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100112 if (argc != 7) {
Paul Bakker20a78082011-01-21 09:32:12 +0000113 const int *list;
114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 mbedtls_printf(USAGE);
Paul Bakker20a78082011-01-21 09:32:12 +0000116
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 mbedtls_printf("Available ciphers:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200118 list = mbedtls_cipher_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100119 while (*list) {
120 cipher_info = mbedtls_cipher_info_from_type(*list);
121 mbedtls_printf(" %s\n", cipher_info->name);
Paul Bakker20a78082011-01-21 09:32:12 +0000122 list++;
123 }
124
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 mbedtls_printf("\nAvailable message digests:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200126 list = mbedtls_md_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100127 while (*list) {
128 md_info = mbedtls_md_info_from_type(*list);
129 mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000130 list++;
131 }
132
Paul Bakkercce9d772011-11-18 14:26:47 +0000133#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100134 mbedtls_printf("\n Press Enter to exit this program.\n");
135 fflush(stdout); getchar();
Paul Bakker20a78082011-01-21 09:32:12 +0000136#endif
137
138 goto exit;
139 }
140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 mode = atoi(argv[1]);
Paul Bakker20a78082011-01-21 09:32:12 +0000142
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100143 if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
144 mbedtls_fprintf(stderr, "invalid operation mode\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000145 goto exit;
146 }
147
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100148 if (strcmp(argv[2], argv[3]) == 0) {
149 mbedtls_fprintf(stderr, "input and output filenames must differ\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000150 goto exit;
151 }
152
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100153 if ((fin = fopen(argv[2], "rb")) == NULL) {
154 mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
Paul Bakker20a78082011-01-21 09:32:12 +0000155 goto exit;
156 }
157
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100158 if ((fout = fopen(argv[3], "wb+")) == NULL) {
159 mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
Paul Bakker20a78082011-01-21 09:32:12 +0000160 goto exit;
161 }
162
163 /*
164 * Read the Cipher and MD from the command line
165 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100166 cipher_info = mbedtls_cipher_info_from_string(argv[4]);
167 if (cipher_info == NULL) {
168 mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
Paul Bakker20a78082011-01-21 09:32:12 +0000169 goto exit;
170 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100171 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
172 mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200173 goto exit;
174 }
Paul Bakker20a78082011-01-21 09:32:12 +0000175
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100176 md_info = mbedtls_md_info_from_string(argv[5]);
177 if (md_info == NULL) {
178 mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
Paul Bakker20a78082011-01-21 09:32:12 +0000179 goto exit;
180 }
Janos Follath8eb64132016-06-03 15:40:57 +0100181
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100182 if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
183 mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
Janos Follath8eb64132016-06-03 15:40:57 +0100184 goto exit;
185 }
Paul Bakker20a78082011-01-21 09:32:12 +0000186
187 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100188 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000189 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100190 if ((fkey = fopen(argv[6], "rb")) != NULL) {
191 keylen = fread(key, 1, sizeof(key), fkey);
192 fclose(fkey);
193 } else {
194 if (memcmp(argv[6], "hex:", 4) == 0) {
Paul Bakker20a78082011-01-21 09:32:12 +0000195 p = &argv[6][4];
196 keylen = 0;
197
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100198 while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
199 keylen < (int) sizeof(key)) {
Paul Bakker20a78082011-01-21 09:32:12 +0000200 key[keylen++] = (unsigned char) n;
201 p += 2;
202 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100203 } else {
204 keylen = strlen(argv[6]);
Paul Bakker20a78082011-01-21 09:32:12 +0000205
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100206 if (keylen > (int) sizeof(key)) {
207 keylen = (int) sizeof(key);
208 }
Paul Bakker20a78082011-01-21 09:32:12 +0000209
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100210 memcpy(key, argv[6], keylen);
Paul Bakker20a78082011-01-21 09:32:12 +0000211 }
212 }
213
Paul Bakkercce9d772011-11-18 14:26:47 +0000214#if defined(_WIN32_WCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100215 filesize = fseek(fin, 0L, SEEK_END);
Paul Bakkercce9d772011-11-18 14:26:47 +0000216#else
217#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000218 /*
219 * Support large files (> 2Gb) on Win32
220 */
221 li_size.QuadPart = 0;
222 li_size.LowPart =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100223 SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
224 li_size.LowPart, &li_size.HighPart, FILE_END);
Paul Bakker20a78082011-01-21 09:32:12 +0000225
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100226 if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
227 mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000228 goto exit;
229 }
230
231 filesize = li_size.QuadPart;
232#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100233 if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
234 perror("lseek");
Paul Bakker20a78082011-01-21 09:32:12 +0000235 goto exit;
236 }
237#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000238#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000239
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100240 if (fseek(fin, 0, SEEK_SET) < 0) {
241 mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000242 goto exit;
243 }
244
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100245 md_size = mbedtls_md_get_size(md_info);
246 cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx);
247
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 if (mode == MODE_ENCRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000249 /*
250 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200251 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000252 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100253 for (i = 0; i < 8; i++) {
254 buffer[i] = (unsigned char) (filesize >> (i << 3));
255 }
Paul Bakker20a78082011-01-21 09:32:12 +0000256
257 p = argv[2];
258
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100259 if (mbedtls_md_starts(&md_ctx) != 0) {
260 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000261 goto exit;
262 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100263 if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
264 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000265 goto exit;
266 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100267 if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
268 != 0) {
269 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000270 goto exit;
271 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
273 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000274 goto exit;
275 }
Paul Bakker20a78082011-01-21 09:32:12 +0000276
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 memcpy(IV, digest, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000278
279 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000280 * Append the IV at the beginning of the output.
281 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100282 if (fwrite(IV, 1, 16, fout) != 16) {
283 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000284 goto exit;
285 }
286
287 /*
288 * Hash the IV and the secret key together 8192 times
289 * using the result to setup the AES context and HMAC.
290 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100291 memset(digest, 0, 32);
292 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000293
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100294 for (i = 0; i < 8192; i++) {
295 if (mbedtls_md_starts(&md_ctx) != 0) {
296 mbedtls_fprintf(stderr,
297 "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000298 goto exit;
299 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100300 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
301 mbedtls_fprintf(stderr,
302 "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000303 goto exit;
304 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100305 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
306 mbedtls_fprintf(stderr,
307 "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000308 goto exit;
309 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100310 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
311 mbedtls_fprintf(stderr,
312 "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000313 goto exit;
314 }
Paul Bakker20a78082011-01-21 09:32:12 +0000315
316 }
317
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
319 MBEDTLS_ENCRYPT) != 0) {
320 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000321 goto exit;
322 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100323 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
324 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200325 goto exit;
326 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
328 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000329 goto exit;
330 }
Paul Bakker20a78082011-01-21 09:32:12 +0000331
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100332 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
333 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100334 goto exit;
335 }
Paul Bakker20a78082011-01-21 09:32:12 +0000336
337 /*
338 * Encrypt and write the ciphertext.
339 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100340 for (offset = 0; offset < filesize; offset += cipher_block_size) {
341 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
342 cipher_block_size : (unsigned int) (filesize - offset);
Paul Bakker20a78082011-01-21 09:32:12 +0000343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 if (fread(buffer, 1, ilen, fin) != ilen) {
345 mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
Paul Bakker20a78082011-01-21 09:32:12 +0000346 goto exit;
347 }
348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
350 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200351 goto exit;
352 }
353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
355 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100356 goto exit;
357 }
Paul Bakker20a78082011-01-21 09:32:12 +0000358
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100359 if (fwrite(output, 1, olen, fout) != olen) {
360 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000361 goto exit;
362 }
363 }
364
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100365 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
366 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000367 goto exit;
368 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
370 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100371 goto exit;
372 }
Paul Bakker20a78082011-01-21 09:32:12 +0000373
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100374 if (fwrite(output, 1, olen, fout) != olen) {
375 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000376 goto exit;
377 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000378
Paul Bakker20a78082011-01-21 09:32:12 +0000379 /*
380 * Finally write the HMAC.
381 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
383 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100384 goto exit;
385 }
Paul Bakker20a78082011-01-21 09:32:12 +0000386
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100387 if (fwrite(digest, 1, md_size, fout) != md_size) {
388 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000389 goto exit;
390 }
391 }
392
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100393 if (mode == MODE_DECRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000394 /*
395 * The encrypted file must be structured as follows:
396 *
397 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200398 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000399 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200400 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
401 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000402 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100403 if (filesize < 16 + md_size) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100404 mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000405 goto exit;
406 }
407
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100408 if (cipher_block_size == 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100409 mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
Janos Follath8eb64132016-06-03 15:40:57 +0100410 goto exit;
411 }
412
413 /*
414 * Check the file size.
415 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100416 if (cipher_info->mode != MBEDTLS_MODE_GCM &&
Waleed Elmelegy6eb46262023-06-09 16:58:01 +0100417 cipher_info->mode != MBEDTLS_MODE_CTR &&
418 cipher_info->mode != MBEDTLS_MODE_CFB &&
419 cipher_info->mode != MBEDTLS_MODE_OFB &&
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100420 ((filesize - md_size) % cipher_block_size) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100421 mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100422 cipher_block_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000423 goto exit;
424 }
425
426 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100427 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000428 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100429 filesize -= (16 + md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000430
431 /*
432 * Read the IV and original filesize modulo 16.
433 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100434 if (fread(buffer, 1, 16, fin) != 16) {
435 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000436 goto exit;
437 }
438
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100439 memcpy(IV, buffer, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000440
441 /*
442 * Hash the IV and the secret key together 8192 times
443 * using the result to setup the AES context and HMAC.
444 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100445 memset(digest, 0, 32);
446 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000447
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100448 for (i = 0; i < 8192; i++) {
449 if (mbedtls_md_starts(&md_ctx) != 0) {
450 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100451 goto exit;
452 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100453 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
454 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100455 goto exit;
456 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100457 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
458 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100459 goto exit;
460 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100461 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
462 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100463 goto exit;
464 }
Paul Bakker20a78082011-01-21 09:32:12 +0000465 }
466
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100467 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
468 MBEDTLS_DECRYPT) != 0) {
469 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200470 goto exit;
471 }
472
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100473 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
474 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200475 goto exit;
476 }
477
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100478 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
479 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200480 goto exit;
481 }
Paul Bakker20a78082011-01-21 09:32:12 +0000482
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100483 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
484 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100485 goto exit;
486 }
Paul Bakker20a78082011-01-21 09:32:12 +0000487
488 /*
489 * Decrypt and write the plaintext.
490 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100491 for (offset = 0; offset < filesize; offset += cipher_block_size) {
492 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
493 cipher_block_size : (unsigned int) (filesize - offset);
Paul Bakker243f48e2016-09-02 22:44:09 +0200494
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100495 if (fread(buffer, 1, ilen, fin) != ilen) {
496 mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100497 cipher_block_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000498 goto exit;
499 }
500
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
502 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100503 goto exit;
504 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100505 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
506 &olen) != 0) {
507 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200508 goto exit;
509 }
Paul Bakker20a78082011-01-21 09:32:12 +0000510
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100511 if (fwrite(output, 1, olen, fout) != olen) {
512 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000513 goto exit;
514 }
515 }
516
517 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000518 * Verify the message authentication code.
519 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100520 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
521 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100522 goto exit;
523 }
Paul Bakker20a78082011-01-21 09:32:12 +0000524
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100525 if (fread(buffer, 1, md_size, fin) != md_size) {
526 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000527 goto exit;
528 }
529
Paul Bakker424cd692013-10-31 14:22:08 +0100530 /* Use constant-time buffer comparison */
531 diff = 0;
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100532 for (i = 0; i < md_size; i++) {
Paul Bakker424cd692013-10-31 14:22:08 +0100533 diff |= digest[i] ^ buffer[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100534 }
Paul Bakker424cd692013-10-31 14:22:08 +0100535
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 if (diff != 0) {
537 mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
538 "or file corrupted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000539 goto exit;
540 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100541
542 /*
543 * Write the final block of data
544 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100545 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
546 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100547 goto exit;
548 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100549
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100550 if (fwrite(output, 1, olen, fout) != olen) {
551 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100552 goto exit;
553 }
Paul Bakker20a78082011-01-21 09:32:12 +0000554 }
555
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100556 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000557
558exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100559 if (fin) {
560 fclose(fin);
561 }
562 if (fout) {
563 fclose(fout);
564 }
Paul Bakker20a78082011-01-21 09:32:12 +0000565
Hanno Beckerf601ec52017-06-27 08:22:17 +0100566 /* Zeroize all command line arguments to also cover
567 the case when the user has missed or reordered some,
568 in which case the key might not be in argv[6]. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 for (i = 0; i < argc; i++) {
570 mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
571 }
Hanno Beckerf601ec52017-06-27 08:22:17 +0100572
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100573 mbedtls_platform_zeroize(IV, sizeof(IV));
574 mbedtls_platform_zeroize(key, sizeof(key));
575 mbedtls_platform_zeroize(buffer, sizeof(buffer));
576 mbedtls_platform_zeroize(output, sizeof(output));
577 mbedtls_platform_zeroize(digest, sizeof(digest));
Paul Bakker20a78082011-01-21 09:32:12 +0000578
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100579 mbedtls_cipher_free(&cipher_ctx);
580 mbedtls_md_free(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000581
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100582 mbedtls_exit(exit_code);
Paul Bakker20a78082011-01-21 09:32:12 +0000583}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200584#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */