blob: 11eca754933ef86479b65e943ebc22f5b19f5485 [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;
Paul Bakkercce9d772011-11-18 14:26:47 +000095#if defined(_WIN32_WCE)
96 long filesize, offset;
97#elif defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +010098 LARGE_INTEGER li_size;
Paul Bakker20a78082011-01-21 09:32:12 +000099 __int64 filesize, offset;
100#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 off_t filesize, offset;
Paul Bakker20a78082011-01-21 09:32:12 +0000102#endif
103
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 mbedtls_cipher_init(&cipher_ctx);
105 mbedtls_md_init(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000106
107 /*
108 * Parse the command-line arguments.
109 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100110 if (argc != 7) {
Paul Bakker20a78082011-01-21 09:32:12 +0000111 const int *list;
112
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 mbedtls_printf(USAGE);
Paul Bakker20a78082011-01-21 09:32:12 +0000114
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100115 mbedtls_printf("Available ciphers:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200116 list = mbedtls_cipher_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100117 while (*list) {
118 cipher_info = mbedtls_cipher_info_from_type(*list);
119 mbedtls_printf(" %s\n", cipher_info->name);
Paul Bakker20a78082011-01-21 09:32:12 +0000120 list++;
121 }
122
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100123 mbedtls_printf("\nAvailable message digests:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200124 list = mbedtls_md_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100125 while (*list) {
126 md_info = mbedtls_md_info_from_type(*list);
127 mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000128 list++;
129 }
130
Paul Bakkercce9d772011-11-18 14:26:47 +0000131#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100132 mbedtls_printf("\n Press Enter to exit this program.\n");
133 fflush(stdout); getchar();
Paul Bakker20a78082011-01-21 09:32:12 +0000134#endif
135
136 goto exit;
137 }
138
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100139 mode = atoi(argv[1]);
Paul Bakker20a78082011-01-21 09:32:12 +0000140
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100141 if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
142 mbedtls_fprintf(stderr, "invalid operation mode\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000143 goto exit;
144 }
145
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100146 if (strcmp(argv[2], argv[3]) == 0) {
147 mbedtls_fprintf(stderr, "input and output filenames must differ\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000148 goto exit;
149 }
150
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100151 if ((fin = fopen(argv[2], "rb")) == NULL) {
152 mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
Paul Bakker20a78082011-01-21 09:32:12 +0000153 goto exit;
154 }
155
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100156 if ((fout = fopen(argv[3], "wb+")) == NULL) {
157 mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
Paul Bakker20a78082011-01-21 09:32:12 +0000158 goto exit;
159 }
160
161 /*
162 * Read the Cipher and MD from the command line
163 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100164 cipher_info = mbedtls_cipher_info_from_string(argv[4]);
165 if (cipher_info == NULL) {
166 mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
Paul Bakker20a78082011-01-21 09:32:12 +0000167 goto exit;
168 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100169 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
170 mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200171 goto exit;
172 }
Paul Bakker20a78082011-01-21 09:32:12 +0000173
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100174 md_info = mbedtls_md_info_from_string(argv[5]);
175 if (md_info == NULL) {
176 mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
Paul Bakker20a78082011-01-21 09:32:12 +0000177 goto exit;
178 }
Janos Follath8eb64132016-06-03 15:40:57 +0100179
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100180 if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
181 mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
Janos Follath8eb64132016-06-03 15:40:57 +0100182 goto exit;
183 }
Paul Bakker20a78082011-01-21 09:32:12 +0000184
185 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100186 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000187 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100188 if ((fkey = fopen(argv[6], "rb")) != NULL) {
189 keylen = fread(key, 1, sizeof(key), fkey);
190 fclose(fkey);
191 } else {
192 if (memcmp(argv[6], "hex:", 4) == 0) {
Paul Bakker20a78082011-01-21 09:32:12 +0000193 p = &argv[6][4];
194 keylen = 0;
195
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100196 while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
197 keylen < (int) sizeof(key)) {
Paul Bakker20a78082011-01-21 09:32:12 +0000198 key[keylen++] = (unsigned char) n;
199 p += 2;
200 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100201 } else {
202 keylen = strlen(argv[6]);
Paul Bakker20a78082011-01-21 09:32:12 +0000203
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 if (keylen > (int) sizeof(key)) {
205 keylen = (int) sizeof(key);
206 }
Paul Bakker20a78082011-01-21 09:32:12 +0000207
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100208 memcpy(key, argv[6], keylen);
Paul Bakker20a78082011-01-21 09:32:12 +0000209 }
210 }
211
Paul Bakkercce9d772011-11-18 14:26:47 +0000212#if defined(_WIN32_WCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100213 filesize = fseek(fin, 0L, SEEK_END);
Paul Bakkercce9d772011-11-18 14:26:47 +0000214#else
215#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000216 /*
217 * Support large files (> 2Gb) on Win32
218 */
219 li_size.QuadPart = 0;
220 li_size.LowPart =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100221 SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
222 li_size.LowPart, &li_size.HighPart, FILE_END);
Paul Bakker20a78082011-01-21 09:32:12 +0000223
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
225 mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000226 goto exit;
227 }
228
229 filesize = li_size.QuadPart;
230#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100231 if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
232 perror("lseek");
Paul Bakker20a78082011-01-21 09:32:12 +0000233 goto exit;
234 }
235#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000236#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000237
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100238 if (fseek(fin, 0, SEEK_SET) < 0) {
239 mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000240 goto exit;
241 }
242
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100243 if (mode == MODE_ENCRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000244 /*
245 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200246 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000247 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100248 for (i = 0; i < 8; i++) {
249 buffer[i] = (unsigned char) (filesize >> (i << 3));
250 }
Paul Bakker20a78082011-01-21 09:32:12 +0000251
252 p = argv[2];
253
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 if (mbedtls_md_starts(&md_ctx) != 0) {
255 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000256 goto exit;
257 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100258 if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
259 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000260 goto exit;
261 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100262 if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
263 != 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_finish(&md_ctx, digest) != 0) {
268 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000269 goto exit;
270 }
Paul Bakker20a78082011-01-21 09:32:12 +0000271
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100272 memcpy(IV, digest, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000273
274 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000275 * Append the IV at the beginning of the output.
276 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100277 if (fwrite(IV, 1, 16, fout) != 16) {
278 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000279 goto exit;
280 }
281
282 /*
283 * Hash the IV and the secret key together 8192 times
284 * using the result to setup the AES context and HMAC.
285 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100286 memset(digest, 0, 32);
287 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000288
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100289 for (i = 0; i < 8192; i++) {
290 if (mbedtls_md_starts(&md_ctx) != 0) {
291 mbedtls_fprintf(stderr,
292 "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000293 goto exit;
294 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
296 mbedtls_fprintf(stderr,
297 "mbedtls_md_update() 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, key, keylen) != 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_finish(&md_ctx, digest) != 0) {
306 mbedtls_fprintf(stderr,
307 "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000308 goto exit;
309 }
Paul Bakker20a78082011-01-21 09:32:12 +0000310
311 }
312
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100313 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
314 MBEDTLS_ENCRYPT) != 0) {
315 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000316 goto exit;
317 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100318 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
319 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200320 goto exit;
321 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100322 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
323 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000324 goto exit;
325 }
Paul Bakker20a78082011-01-21 09:32:12 +0000326
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100327 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
328 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100329 goto exit;
330 }
Paul Bakker20a78082011-01-21 09:32:12 +0000331
332 /*
333 * Encrypt and write the ciphertext.
334 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100335 for (offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size(&cipher_ctx)) {
336 ilen = ((unsigned int) filesize - offset > mbedtls_cipher_get_block_size(&cipher_ctx)) ?
337 mbedtls_cipher_get_block_size(&cipher_ctx) : (unsigned int) (filesize - offset);
Paul Bakker20a78082011-01-21 09:32:12 +0000338
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100339 if (fread(buffer, 1, ilen, fin) != ilen) {
340 mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
Paul Bakker20a78082011-01-21 09:32:12 +0000341 goto exit;
342 }
343
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100344 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
345 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200346 goto exit;
347 }
348
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100349 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
350 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100351 goto exit;
352 }
Paul Bakker20a78082011-01-21 09:32:12 +0000353
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100354 if (fwrite(output, 1, olen, fout) != olen) {
355 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000356 goto exit;
357 }
358 }
359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
361 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000362 goto exit;
363 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100364 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
365 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100366 goto exit;
367 }
Paul Bakker20a78082011-01-21 09:32:12 +0000368
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100369 if (fwrite(output, 1, olen, fout) != olen) {
370 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000371 goto exit;
372 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000373
Paul Bakker20a78082011-01-21 09:32:12 +0000374 /*
375 * Finally write the HMAC.
376 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100377 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
378 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100379 goto exit;
380 }
Paul Bakker20a78082011-01-21 09:32:12 +0000381
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100382 if (fwrite(digest, 1, mbedtls_md_get_size(md_info), fout) != mbedtls_md_get_size(md_info)) {
383 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000384 goto exit;
385 }
386 }
387
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100388 if (mode == MODE_DECRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000389 /*
390 * The encrypted file must be structured as follows:
391 *
392 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200393 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000394 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200395 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
396 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000397 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100398 if (filesize < 16 + mbedtls_md_get_size(md_info)) {
399 mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000400 goto exit;
401 }
402
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100403 if (mbedtls_cipher_get_block_size(&cipher_ctx) == 0) {
404 mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
Janos Follath8eb64132016-06-03 15:40:57 +0100405 goto exit;
406 }
407
408 /*
409 * Check the file size.
410 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100411 if (cipher_info->mode != MBEDTLS_MODE_GCM &&
Waleed Elmelegy6eb46262023-06-09 16:58:01 +0100412 cipher_info->mode != MBEDTLS_MODE_CTR &&
413 cipher_info->mode != MBEDTLS_MODE_CFB &&
414 cipher_info->mode != MBEDTLS_MODE_OFB &&
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100415 ((filesize - mbedtls_md_get_size(md_info)) %
416 mbedtls_cipher_get_block_size(&cipher_ctx)) != 0) {
417 mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
418 mbedtls_cipher_get_block_size(&cipher_ctx));
Paul Bakker20a78082011-01-21 09:32:12 +0000419 goto exit;
420 }
421
422 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100423 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000424 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100425 filesize -= (16 + mbedtls_md_get_size(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000426
427 /*
428 * Read the IV and original filesize modulo 16.
429 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100430 if (fread(buffer, 1, 16, fin) != 16) {
431 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000432 goto exit;
433 }
434
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100435 memcpy(IV, buffer, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000436
437 /*
438 * Hash the IV and the secret key together 8192 times
439 * using the result to setup the AES context and HMAC.
440 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 memset(digest, 0, 32);
442 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000443
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100444 for (i = 0; i < 8192; i++) {
445 if (mbedtls_md_starts(&md_ctx) != 0) {
446 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100447 goto exit;
448 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100449 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
450 mbedtls_fprintf(stderr, "mbedtls_md_update() 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, key, keylen) != 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_finish(&md_ctx, digest) != 0) {
458 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100459 goto exit;
460 }
Paul Bakker20a78082011-01-21 09:32:12 +0000461 }
462
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
464 MBEDTLS_DECRYPT) != 0) {
465 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200466 goto exit;
467 }
468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
470 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200471 goto exit;
472 }
473
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100474 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
475 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200476 goto exit;
477 }
Paul Bakker20a78082011-01-21 09:32:12 +0000478
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100479 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
480 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100481 goto exit;
482 }
Paul Bakker20a78082011-01-21 09:32:12 +0000483
484 /*
485 * Decrypt and write the plaintext.
486 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100487 for (offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size(&cipher_ctx)) {
488 ilen = ((unsigned int) filesize - offset > mbedtls_cipher_get_block_size(&cipher_ctx)) ?
489 mbedtls_cipher_get_block_size(&cipher_ctx) : (unsigned int) (filesize - offset);
Paul Bakker243f48e2016-09-02 22:44:09 +0200490
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100491 if (fread(buffer, 1, ilen, fin) != ilen) {
492 mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
493 mbedtls_cipher_get_block_size(&cipher_ctx));
Paul Bakker20a78082011-01-21 09:32:12 +0000494 goto exit;
495 }
496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
498 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100499 goto exit;
500 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100501 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
502 &olen) != 0) {
503 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200504 goto exit;
505 }
Paul Bakker20a78082011-01-21 09:32:12 +0000506
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (fwrite(output, 1, olen, fout) != olen) {
508 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000509 goto exit;
510 }
511 }
512
513 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000514 * Verify the message authentication code.
515 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100516 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
517 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100518 goto exit;
519 }
Paul Bakker20a78082011-01-21 09:32:12 +0000520
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100521 if (fread(buffer, 1, mbedtls_md_get_size(md_info), fin) != mbedtls_md_get_size(md_info)) {
522 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000523 goto exit;
524 }
525
Paul Bakker424cd692013-10-31 14:22:08 +0100526 /* Use constant-time buffer comparison */
527 diff = 0;
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100528 for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
Paul Bakker424cd692013-10-31 14:22:08 +0100529 diff |= digest[i] ^ buffer[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100530 }
Paul Bakker424cd692013-10-31 14:22:08 +0100531
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100532 if (diff != 0) {
533 mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
534 "or file corrupted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000535 goto exit;
536 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100537
538 /*
539 * Write the final block of data
540 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100541 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
542 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100543 goto exit;
544 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100545
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100546 if (fwrite(output, 1, olen, fout) != olen) {
547 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100548 goto exit;
549 }
Paul Bakker20a78082011-01-21 09:32:12 +0000550 }
551
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100552 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000553
554exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100555 if (fin) {
556 fclose(fin);
557 }
558 if (fout) {
559 fclose(fout);
560 }
Paul Bakker20a78082011-01-21 09:32:12 +0000561
Hanno Beckerf601ec52017-06-27 08:22:17 +0100562 /* Zeroize all command line arguments to also cover
563 the case when the user has missed or reordered some,
564 in which case the key might not be in argv[6]. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100565 for (i = 0; i < argc; i++) {
566 mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
567 }
Hanno Beckerf601ec52017-06-27 08:22:17 +0100568
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100569 mbedtls_platform_zeroize(IV, sizeof(IV));
570 mbedtls_platform_zeroize(key, sizeof(key));
571 mbedtls_platform_zeroize(buffer, sizeof(buffer));
572 mbedtls_platform_zeroize(output, sizeof(output));
573 mbedtls_platform_zeroize(digest, sizeof(digest));
Paul Bakker20a78082011-01-21 09:32:12 +0000574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 mbedtls_cipher_free(&cipher_ctx);
576 mbedtls_md_free(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000577
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100578 mbedtls_exit(exit_code);
Paul Bakker20a78082011-01-21 09:32:12 +0000579}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200580#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */