blob: 98253c6d7e901ecb5173805894fd2d4399b3e7a0 [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 Elmelegy6a4af482023-06-26 11:28:42 +010095 mbedtls_cipher_mode_t cipher_mode;
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +010096 unsigned int cipher_block_size;
97 unsigned char md_size;
Paul Bakkercce9d772011-11-18 14:26:47 +000098#if defined(_WIN32_WCE)
99 long filesize, offset;
100#elif defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100101 LARGE_INTEGER li_size;
Paul Bakker20a78082011-01-21 09:32:12 +0000102 __int64 filesize, offset;
103#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100104 off_t filesize, offset;
Paul Bakker20a78082011-01-21 09:32:12 +0000105#endif
106
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100107 mbedtls_cipher_init(&cipher_ctx);
108 mbedtls_md_init(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000109
110 /*
111 * Parse the command-line arguments.
112 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100113 if (argc != 7) {
Paul Bakker20a78082011-01-21 09:32:12 +0000114 const int *list;
115
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100116 mbedtls_printf(USAGE);
Paul Bakker20a78082011-01-21 09:32:12 +0000117
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100118 mbedtls_printf("Available ciphers:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200119 list = mbedtls_cipher_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100120 while (*list) {
121 cipher_info = mbedtls_cipher_info_from_type(*list);
122 mbedtls_printf(" %s\n", cipher_info->name);
Paul Bakker20a78082011-01-21 09:32:12 +0000123 list++;
124 }
125
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100126 mbedtls_printf("\nAvailable message digests:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200127 list = mbedtls_md_list();
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100128 while (*list) {
129 md_info = mbedtls_md_info_from_type(*list);
130 mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000131 list++;
132 }
133
Paul Bakkercce9d772011-11-18 14:26:47 +0000134#if defined(_WIN32)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100135 mbedtls_printf("\n Press Enter to exit this program.\n");
136 fflush(stdout); getchar();
Paul Bakker20a78082011-01-21 09:32:12 +0000137#endif
138
139 goto exit;
140 }
141
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100142 mode = atoi(argv[1]);
Paul Bakker20a78082011-01-21 09:32:12 +0000143
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100144 if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
145 mbedtls_fprintf(stderr, "invalid operation mode\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000146 goto exit;
147 }
148
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100149 if (strcmp(argv[2], argv[3]) == 0) {
150 mbedtls_fprintf(stderr, "input and output filenames must differ\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000151 goto exit;
152 }
153
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100154 if ((fin = fopen(argv[2], "rb")) == NULL) {
155 mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
Paul Bakker20a78082011-01-21 09:32:12 +0000156 goto exit;
157 }
158
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100159 if ((fout = fopen(argv[3], "wb+")) == NULL) {
160 mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
Paul Bakker20a78082011-01-21 09:32:12 +0000161 goto exit;
162 }
163
164 /*
165 * Read the Cipher and MD from the command line
166 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100167 cipher_info = mbedtls_cipher_info_from_string(argv[4]);
168 if (cipher_info == NULL) {
169 mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
Paul Bakker20a78082011-01-21 09:32:12 +0000170 goto exit;
171 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100172 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
173 mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200174 goto exit;
175 }
Paul Bakker20a78082011-01-21 09:32:12 +0000176
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100177 md_info = mbedtls_md_info_from_string(argv[5]);
178 if (md_info == NULL) {
179 mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
Paul Bakker20a78082011-01-21 09:32:12 +0000180 goto exit;
181 }
Janos Follath8eb64132016-06-03 15:40:57 +0100182
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100183 if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
184 mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
Janos Follath8eb64132016-06-03 15:40:57 +0100185 goto exit;
186 }
Paul Bakker20a78082011-01-21 09:32:12 +0000187
188 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100189 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000190 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100191 if ((fkey = fopen(argv[6], "rb")) != NULL) {
192 keylen = fread(key, 1, sizeof(key), fkey);
193 fclose(fkey);
194 } else {
195 if (memcmp(argv[6], "hex:", 4) == 0) {
Paul Bakker20a78082011-01-21 09:32:12 +0000196 p = &argv[6][4];
197 keylen = 0;
198
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100199 while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
200 keylen < (int) sizeof(key)) {
Paul Bakker20a78082011-01-21 09:32:12 +0000201 key[keylen++] = (unsigned char) n;
202 p += 2;
203 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100204 } else {
205 keylen = strlen(argv[6]);
Paul Bakker20a78082011-01-21 09:32:12 +0000206
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100207 if (keylen > (int) sizeof(key)) {
208 keylen = (int) sizeof(key);
209 }
Paul Bakker20a78082011-01-21 09:32:12 +0000210
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100211 memcpy(key, argv[6], keylen);
Paul Bakker20a78082011-01-21 09:32:12 +0000212 }
213 }
214
Paul Bakkercce9d772011-11-18 14:26:47 +0000215#if defined(_WIN32_WCE)
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100216 filesize = fseek(fin, 0L, SEEK_END);
Paul Bakkercce9d772011-11-18 14:26:47 +0000217#else
218#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000219 /*
220 * Support large files (> 2Gb) on Win32
221 */
222 li_size.QuadPart = 0;
223 li_size.LowPart =
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100224 SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
225 li_size.LowPart, &li_size.HighPart, FILE_END);
Paul Bakker20a78082011-01-21 09:32:12 +0000226
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100227 if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
228 mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000229 goto exit;
230 }
231
232 filesize = li_size.QuadPart;
233#else
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100234 if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
235 perror("lseek");
Paul Bakker20a78082011-01-21 09:32:12 +0000236 goto exit;
237 }
238#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000239#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000240
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100241 if (fseek(fin, 0, SEEK_SET) < 0) {
242 mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000243 goto exit;
244 }
245
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100246 md_size = mbedtls_md_get_size(md_info);
247 cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx);
248
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100249 if (mode == MODE_ENCRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000250 /*
251 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200252 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000253 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100254 for (i = 0; i < 8; i++) {
255 buffer[i] = (unsigned char) (filesize >> (i << 3));
256 }
Paul Bakker20a78082011-01-21 09:32:12 +0000257
258 p = argv[2];
259
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100260 if (mbedtls_md_starts(&md_ctx) != 0) {
261 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000262 goto exit;
263 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100264 if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
265 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000266 goto exit;
267 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100268 if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
269 != 0) {
270 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000271 goto exit;
272 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100273 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
274 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000275 goto exit;
276 }
Paul Bakker20a78082011-01-21 09:32:12 +0000277
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100278 memcpy(IV, digest, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000279
280 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000281 * Append the IV at the beginning of the output.
282 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100283 if (fwrite(IV, 1, 16, fout) != 16) {
284 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000285 goto exit;
286 }
287
288 /*
289 * Hash the IV and the secret key together 8192 times
290 * using the result to setup the AES context and HMAC.
291 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100292 memset(digest, 0, 32);
293 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000294
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100295 for (i = 0; i < 8192; i++) {
296 if (mbedtls_md_starts(&md_ctx) != 0) {
297 mbedtls_fprintf(stderr,
298 "mbedtls_md_starts() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000299 goto exit;
300 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100301 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
302 mbedtls_fprintf(stderr,
303 "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000304 goto exit;
305 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100306 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
307 mbedtls_fprintf(stderr,
308 "mbedtls_md_update() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000309 goto exit;
310 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100311 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
312 mbedtls_fprintf(stderr,
313 "mbedtls_md_finish() returned error\n");
Paul Elliottd0688762021-12-09 17:18:10 +0000314 goto exit;
315 }
Paul Bakker20a78082011-01-21 09:32:12 +0000316
317 }
318
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100319 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
320 MBEDTLS_ENCRYPT) != 0) {
321 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000322 goto exit;
323 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100324 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
325 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200326 goto exit;
327 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100328 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
329 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000330 goto exit;
331 }
Paul Bakker20a78082011-01-21 09:32:12 +0000332
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100333 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
334 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100335 goto exit;
336 }
Paul Bakker20a78082011-01-21 09:32:12 +0000337
338 /*
339 * Encrypt and write the ciphertext.
340 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100341 for (offset = 0; offset < filesize; offset += cipher_block_size) {
342 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
343 cipher_block_size : (unsigned int) (filesize - offset);
Paul Bakker20a78082011-01-21 09:32:12 +0000344
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100345 if (fread(buffer, 1, ilen, fin) != ilen) {
346 mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
Paul Bakker20a78082011-01-21 09:32:12 +0000347 goto exit;
348 }
349
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100350 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
351 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200352 goto exit;
353 }
354
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100355 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
356 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100357 goto exit;
358 }
Paul Bakker20a78082011-01-21 09:32:12 +0000359
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100360 if (fwrite(output, 1, olen, fout) != olen) {
361 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000362 goto exit;
363 }
364 }
365
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100366 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
367 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000368 goto exit;
369 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100370 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
371 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100372 goto exit;
373 }
Paul Bakker20a78082011-01-21 09:32:12 +0000374
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100375 if (fwrite(output, 1, olen, fout) != olen) {
376 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000377 goto exit;
378 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000379
Paul Bakker20a78082011-01-21 09:32:12 +0000380 /*
381 * Finally write the HMAC.
382 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100383 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
384 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100385 goto exit;
386 }
Paul Bakker20a78082011-01-21 09:32:12 +0000387
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100388 if (fwrite(digest, 1, md_size, fout) != md_size) {
389 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000390 goto exit;
391 }
392 }
393
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100394 if (mode == MODE_DECRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000395 /*
396 * The encrypted file must be structured as follows:
397 *
398 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200399 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000400 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200401 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
402 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000403 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100404 if (filesize < 16 + md_size) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100405 mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000406 goto exit;
407 }
408
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100409 if (cipher_block_size == 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100410 mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
Janos Follath8eb64132016-06-03 15:40:57 +0100411 goto exit;
412 }
413
414 /*
415 * Check the file size.
416 */
Waleed Elmelegy6a4af482023-06-26 11:28:42 +0100417 cipher_mode = cipher_info->mode;
418 if (cipher_mode != MBEDTLS_MODE_GCM &&
419 cipher_mode != MBEDTLS_MODE_CTR &&
420 cipher_mode != MBEDTLS_MODE_CFB &&
421 cipher_mode != MBEDTLS_MODE_OFB &&
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100422 ((filesize - md_size) % cipher_block_size) != 0) {
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100423 mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100424 cipher_block_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000425 goto exit;
426 }
427
428 /*
Paul Bakker60b1d102013-10-29 10:02:51 +0100429 * Subtract the IV + HMAC length.
Paul Bakker20a78082011-01-21 09:32:12 +0000430 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100431 filesize -= (16 + md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000432
433 /*
434 * Read the IV and original filesize modulo 16.
435 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100436 if (fread(buffer, 1, 16, fin) != 16) {
437 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000438 goto exit;
439 }
440
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100441 memcpy(IV, buffer, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000442
443 /*
444 * Hash the IV and the secret key together 8192 times
445 * using the result to setup the AES context and HMAC.
446 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100447 memset(digest, 0, 32);
448 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000449
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100450 for (i = 0; i < 8192; i++) {
451 if (mbedtls_md_starts(&md_ctx) != 0) {
452 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100453 goto exit;
454 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100455 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
456 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100457 goto exit;
458 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100459 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
460 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100461 goto exit;
462 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100463 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
464 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100465 goto exit;
466 }
Paul Bakker20a78082011-01-21 09:32:12 +0000467 }
468
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100469 if (mbedtls_cipher_setkey(&cipher_ctx, digest, cipher_info->key_bitlen,
470 MBEDTLS_DECRYPT) != 0) {
471 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200472 goto exit;
473 }
474
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100475 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
476 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200477 goto exit;
478 }
479
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100480 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
481 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200482 goto exit;
483 }
Paul Bakker20a78082011-01-21 09:32:12 +0000484
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100485 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
486 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100487 goto exit;
488 }
Paul Bakker20a78082011-01-21 09:32:12 +0000489
490 /*
491 * Decrypt and write the plaintext.
492 */
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100493 for (offset = 0; offset < filesize; offset += cipher_block_size) {
494 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
495 cipher_block_size : (unsigned int) (filesize - offset);
Paul Bakker243f48e2016-09-02 22:44:09 +0200496
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100497 if (fread(buffer, 1, ilen, fin) != ilen) {
498 mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100499 cipher_block_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000500 goto exit;
501 }
502
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100503 if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
504 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100505 goto exit;
506 }
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100507 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
508 &olen) != 0) {
509 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200510 goto exit;
511 }
Paul Bakker20a78082011-01-21 09:32:12 +0000512
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100513 if (fwrite(output, 1, olen, fout) != olen) {
514 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000515 goto exit;
516 }
517 }
518
519 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000520 * Verify the message authentication code.
521 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100522 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
523 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100524 goto exit;
525 }
Paul Bakker20a78082011-01-21 09:32:12 +0000526
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100527 if (fread(buffer, 1, md_size, fin) != md_size) {
528 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size);
Paul Bakker20a78082011-01-21 09:32:12 +0000529 goto exit;
530 }
531
Paul Bakker424cd692013-10-31 14:22:08 +0100532 /* Use constant-time buffer comparison */
533 diff = 0;
Waleed Elmelegyc451b4a2023-06-12 14:53:02 +0100534 for (i = 0; i < md_size; i++) {
Paul Bakker424cd692013-10-31 14:22:08 +0100535 diff |= digest[i] ^ buffer[i];
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100536 }
Paul Bakker424cd692013-10-31 14:22:08 +0100537
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100538 if (diff != 0) {
539 mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
540 "or file corrupted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000541 goto exit;
542 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100543
544 /*
545 * Write the final block of data
546 */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100547 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
548 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Gilles Peskine3d283782021-12-10 14:25:45 +0100549 goto exit;
550 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100551
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100552 if (fwrite(output, 1, olen, fout) != olen) {
553 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100554 goto exit;
555 }
Paul Bakker20a78082011-01-21 09:32:12 +0000556 }
557
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100558 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000559
560exit:
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100561 if (fin) {
562 fclose(fin);
563 }
564 if (fout) {
565 fclose(fout);
566 }
Paul Bakker20a78082011-01-21 09:32:12 +0000567
Hanno Beckerf601ec52017-06-27 08:22:17 +0100568 /* Zeroize all command line arguments to also cover
569 the case when the user has missed or reordered some,
570 in which case the key might not be in argv[6]. */
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100571 for (i = 0; i < argc; i++) {
572 mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
573 }
Hanno Beckerf601ec52017-06-27 08:22:17 +0100574
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100575 mbedtls_platform_zeroize(IV, sizeof(IV));
576 mbedtls_platform_zeroize(key, sizeof(key));
577 mbedtls_platform_zeroize(buffer, sizeof(buffer));
578 mbedtls_platform_zeroize(output, sizeof(output));
579 mbedtls_platform_zeroize(digest, sizeof(digest));
Paul Bakker20a78082011-01-21 09:32:12 +0000580
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100581 mbedtls_cipher_free(&cipher_ctx);
582 mbedtls_md_free(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000583
Gilles Peskine1b6c09a2023-01-11 14:52:35 +0100584 mbedtls_exit(exit_code);
Paul Bakker20a78082011-01-21 09:32:12 +0000585}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200586#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */