blob: 0187648be34b94e31eb93e4c295a34daf84bdcd4 [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
Bence Szépkútibb0cfeb2021-05-28 09:42:25 +020022 * set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
Nicholas Wilson2682edf2017-12-05 12:08:15 +000023 * Harmless on other platforms. */
nia1c0c8372020-06-11 12:03:45 +010024#define _POSIX_C_SOURCE 200112L
Nicholas Wilson2682edf2017-12-05 12:08:15 +000025
Bence Szépkútic662b362021-05-27 11:25:03 +020026#include "mbedtls/build_info.h"
Paul Bakker20a78082011-01-21 09:32:12 +000027
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000028#include "mbedtls/platform.h"
Rich Evans18b78c72015-02-11 14:06:19 +000029
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020030#if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
Gilles Peskine449bd832023-01-11 14:50:10 +010031 defined(MBEDTLS_FS_IO)
Manuel Pégourié-Gonnard7f809972015-03-09 17:05:11 +000032#include "mbedtls/cipher.h"
33#include "mbedtls/md.h"
Hanno Becker0b44d5c2018-10-12 16:46:37 +010034#include "mbedtls/platform_util.h"
Rich Evans18b78c72015-02-11 14:06:19 +000035
36#include <stdio.h>
37#include <stdlib.h>
38#include <string.h>
Rich Evansf90016a2015-01-19 14:26:37 +000039#endif
40
Paul Bakker494c0b82011-04-24 15:30:07 +000041#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +000042#include <windows.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000043#if !defined(_WIN32_WCE)
Paul Bakker20a78082011-01-21 09:32:12 +000044#include <io.h>
Paul Bakkercce9d772011-11-18 14:26:47 +000045#endif
Paul Bakker20a78082011-01-21 09:32:12 +000046#else
47#include <sys/types.h>
48#include <unistd.h>
49#endif
50
Paul Bakker20a78082011-01-21 09:32:12 +000051#define MODE_ENCRYPT 0
52#define MODE_DECRYPT 1
53
54#define USAGE \
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020055 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
Paul Bakker20a78082011-01-21 09:32:12 +000056 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
57 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
58 "\n"
59
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020060#if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
61 !defined(MBEDTLS_FS_IO)
Gilles Peskine449bd832023-01-11 14:50:10 +010062int main(void)
Paul Bakker5690efc2011-05-26 13:16:06 +000063{
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020064 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
Gilles Peskine449bd832023-01-11 14:50:10 +010065 mbedtls_exit(0);
Paul Bakker5690efc2011-05-26 13:16:06 +000066}
67#else
Simon Butcher63cb97e2018-12-06 17:43:31 +000068
Simon Butcher63cb97e2018-12-06 17:43:31 +000069
Gilles Peskine449bd832023-01-11 14:50:10 +010070int main(int argc, char *argv[])
Paul Bakker20a78082011-01-21 09:32:12 +000071{
Gilles Peskinea5fc9392020-04-14 19:34:19 +020072 int ret = 1, i;
73 unsigned n;
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +010074 int exit_code = MBEDTLS_EXIT_FAILURE;
Paul Bakker243f48e2016-09-02 22:44:09 +020075 int mode;
Paul Bakker25b5fe52011-05-26 14:02:58 +000076 size_t keylen, ilen, olen;
Paul Bakker20a78082011-01-21 09:32:12 +000077 FILE *fkey, *fin = NULL, *fout = NULL;
78
79 char *p;
80 unsigned char IV[16];
81 unsigned char key[512];
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020082 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
Paul Bakker20a78082011-01-21 09:32:12 +000083 unsigned char buffer[1024];
84 unsigned char output[1024];
Paul Bakker424cd692013-10-31 14:22:08 +010085 unsigned char diff;
Paul Bakker20a78082011-01-21 09:32:12 +000086
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +020087 const mbedtls_cipher_info_t *cipher_info;
88 const mbedtls_md_info_t *md_info;
89 mbedtls_cipher_context_t cipher_ctx;
90 mbedtls_md_context_t md_ctx;
Paul Bakkercce9d772011-11-18 14:26:47 +000091#if defined(_WIN32_WCE)
92 long filesize, offset;
93#elif defined(_WIN32)
Gilles Peskine449bd832023-01-11 14:50:10 +010094 LARGE_INTEGER li_size;
Paul Bakker20a78082011-01-21 09:32:12 +000095 __int64 filesize, offset;
96#else
Gilles Peskine449bd832023-01-11 14:50:10 +010097 off_t filesize, offset;
Paul Bakker20a78082011-01-21 09:32:12 +000098#endif
99
Gilles Peskine449bd832023-01-11 14:50:10 +0100100 mbedtls_cipher_init(&cipher_ctx);
101 mbedtls_md_init(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000102
103 /*
104 * Parse the command-line arguments.
105 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100106 if (argc != 7) {
Paul Bakker20a78082011-01-21 09:32:12 +0000107 const int *list;
108
Gilles Peskine449bd832023-01-11 14:50:10 +0100109 mbedtls_printf(USAGE);
Paul Bakker20a78082011-01-21 09:32:12 +0000110
Gilles Peskine449bd832023-01-11 14:50:10 +0100111 mbedtls_printf("Available ciphers:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200112 list = mbedtls_cipher_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100113 while (*list) {
114 cipher_info = mbedtls_cipher_info_from_type(*list);
115 mbedtls_printf(" %s\n", mbedtls_cipher_info_get_name(cipher_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000116 list++;
117 }
118
Gilles Peskine449bd832023-01-11 14:50:10 +0100119 mbedtls_printf("\nAvailable message digests:\n");
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200120 list = mbedtls_md_list();
Gilles Peskine449bd832023-01-11 14:50:10 +0100121 while (*list) {
122 md_info = mbedtls_md_info_from_type(*list);
123 mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000124 list++;
125 }
126
Paul Bakker20a78082011-01-21 09:32:12 +0000127 goto exit;
128 }
129
Gilles Peskine449bd832023-01-11 14:50:10 +0100130 mode = atoi(argv[1]);
Paul Bakker20a78082011-01-21 09:32:12 +0000131
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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
Gilles Peskine6d576c92022-06-30 17:06:11 +0200152 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100153 mbedtls_setbuf(fin, NULL);
154 mbedtls_setbuf(fout, NULL);
Gilles Peskine6d576c92022-06-30 17:06:11 +0200155
Paul Bakker20a78082011-01-21 09:32:12 +0000156 /*
157 * Read the Cipher and MD from the command line
158 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100159 cipher_info = mbedtls_cipher_info_from_string(argv[4]);
160 if (cipher_info == NULL) {
161 mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
Paul Bakker20a78082011-01-21 09:32:12 +0000162 goto exit;
163 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100164 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
165 mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200166 goto exit;
167 }
Paul Bakker20a78082011-01-21 09:32:12 +0000168
Gilles Peskine449bd832023-01-11 14:50:10 +0100169 md_info = mbedtls_md_info_from_string(argv[5]);
170 if (md_info == NULL) {
171 mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
Paul Bakker20a78082011-01-21 09:32:12 +0000172 goto exit;
173 }
Janos Follath8eb64132016-06-03 15:40:57 +0100174
Gilles Peskine449bd832023-01-11 14:50:10 +0100175 if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
176 mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
Janos Follath8eb64132016-06-03 15:40:57 +0100177 goto exit;
178 }
Paul Bakker20a78082011-01-21 09:32:12 +0000179
180 /*
Hanno Becker840bace2017-06-27 11:36:21 +0100181 * Read the secret key from file or command line
Paul Bakker20a78082011-01-21 09:32:12 +0000182 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100183 if ((fkey = fopen(argv[6], "rb")) != NULL) {
184 keylen = fread(key, 1, sizeof(key), fkey);
185 fclose(fkey);
186 } else {
187 if (memcmp(argv[6], "hex:", 4) == 0) {
Paul Bakker20a78082011-01-21 09:32:12 +0000188 p = &argv[6][4];
189 keylen = 0;
190
Gilles Peskine449bd832023-01-11 14:50:10 +0100191 while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
192 keylen < (int) sizeof(key)) {
Paul Bakker20a78082011-01-21 09:32:12 +0000193 key[keylen++] = (unsigned char) n;
194 p += 2;
195 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100196 } else {
197 keylen = strlen(argv[6]);
Paul Bakker20a78082011-01-21 09:32:12 +0000198
Gilles Peskine449bd832023-01-11 14:50:10 +0100199 if (keylen > (int) sizeof(key)) {
200 keylen = (int) sizeof(key);
201 }
Paul Bakker20a78082011-01-21 09:32:12 +0000202
Gilles Peskine449bd832023-01-11 14:50:10 +0100203 memcpy(key, argv[6], keylen);
Paul Bakker20a78082011-01-21 09:32:12 +0000204 }
205 }
206
Paul Bakkercce9d772011-11-18 14:26:47 +0000207#if defined(_WIN32_WCE)
Gilles Peskine449bd832023-01-11 14:50:10 +0100208 filesize = fseek(fin, 0L, SEEK_END);
Paul Bakkercce9d772011-11-18 14:26:47 +0000209#else
210#if defined(_WIN32)
Paul Bakker20a78082011-01-21 09:32:12 +0000211 /*
212 * Support large files (> 2Gb) on Win32
213 */
214 li_size.QuadPart = 0;
215 li_size.LowPart =
Gilles Peskine449bd832023-01-11 14:50:10 +0100216 SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
217 li_size.LowPart, &li_size.HighPart, FILE_END);
Paul Bakker20a78082011-01-21 09:32:12 +0000218
Gilles Peskine449bd832023-01-11 14:50:10 +0100219 if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
220 mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000221 goto exit;
222 }
223
224 filesize = li_size.QuadPart;
225#else
Gilles Peskine449bd832023-01-11 14:50:10 +0100226 if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
227 perror("lseek");
Paul Bakker20a78082011-01-21 09:32:12 +0000228 goto exit;
229 }
230#endif
Paul Bakkercce9d772011-11-18 14:26:47 +0000231#endif
Paul Bakker20a78082011-01-21 09:32:12 +0000232
Gilles Peskine449bd832023-01-11 14:50:10 +0100233 if (fseek(fin, 0, SEEK_SET) < 0) {
234 mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000235 goto exit;
236 }
237
Gilles Peskine449bd832023-01-11 14:50:10 +0100238 if (mode == MODE_ENCRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000239 /*
240 * Generate the initialization vector as:
Paul Bakker243f48e2016-09-02 22:44:09 +0200241 * IV = MD( filesize || filename )[0..15]
Paul Bakker20a78082011-01-21 09:32:12 +0000242 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100243 for (i = 0; i < 8; i++) {
244 buffer[i] = (unsigned char) (filesize >> (i << 3));
245 }
Paul Bakker20a78082011-01-21 09:32:12 +0000246
247 p = argv[2];
248
Gilles Peskine449bd832023-01-11 14:50:10 +0100249 if (mbedtls_md_starts(&md_ctx) != 0) {
250 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000251 goto exit;
252 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100253 if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
254 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000255 goto exit;
256 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100257 if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
258 != 0) {
259 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000260 goto exit;
261 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100262 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
263 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000264 goto exit;
265 }
Paul Bakker20a78082011-01-21 09:32:12 +0000266
Gilles Peskine449bd832023-01-11 14:50:10 +0100267 memcpy(IV, digest, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000268
269 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000270 * Append the IV at the beginning of the output.
271 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100272 if (fwrite(IV, 1, 16, fout) != 16) {
273 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000274 goto exit;
275 }
276
277 /*
278 * Hash the IV and the secret key together 8192 times
279 * using the result to setup the AES context and HMAC.
280 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100281 memset(digest, 0, 32);
282 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000283
Gilles Peskine449bd832023-01-11 14:50:10 +0100284 for (i = 0; i < 8192; i++) {
285 if (mbedtls_md_starts(&md_ctx) != 0) {
286 mbedtls_fprintf(stderr,
287 "mbedtls_md_starts() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000288 goto exit;
289 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100290 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
291 mbedtls_fprintf(stderr,
292 "mbedtls_md_update() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000293 goto exit;
294 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100295 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
296 mbedtls_fprintf(stderr,
297 "mbedtls_md_update() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000298 goto exit;
299 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100300 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
301 mbedtls_fprintf(stderr,
302 "mbedtls_md_finish() returned error\n");
Paul Elliottd79d3eb2021-12-09 17:18:10 +0000303 goto exit;
304 }
Paul Bakker20a78082011-01-21 09:32:12 +0000305
306 }
307
Gilles Peskine449bd832023-01-11 14:50:10 +0100308 if (mbedtls_cipher_setkey(&cipher_ctx,
309 digest,
310 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
311 MBEDTLS_ENCRYPT) != 0) {
312 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000313 goto exit;
314 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100315 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
316 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Manuel Pégourié-Gonnard9c853b92013-09-03 13:04:44 +0200317 goto exit;
318 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100319 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
320 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000321 goto exit;
322 }
Paul Bakker20a78082011-01-21 09:32:12 +0000323
Gilles Peskine449bd832023-01-11 14:50:10 +0100324 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
325 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100326 goto exit;
327 }
Paul Bakker20a78082011-01-21 09:32:12 +0000328
329 /*
330 * Encrypt and write the ciphertext.
331 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100332 for (offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size(&cipher_ctx)) {
333 ilen = ((unsigned int) filesize - offset > mbedtls_cipher_get_block_size(&cipher_ctx)) ?
334 mbedtls_cipher_get_block_size(&cipher_ctx) : (unsigned int) (filesize - offset);
Paul Bakker20a78082011-01-21 09:32:12 +0000335
Gilles Peskine449bd832023-01-11 14:50:10 +0100336 if (fread(buffer, 1, ilen, fin) != ilen) {
337 mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
Paul Bakker20a78082011-01-21 09:32:12 +0000338 goto exit;
339 }
340
Gilles Peskine449bd832023-01-11 14:50:10 +0100341 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
342 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200343 goto exit;
344 }
345
Gilles Peskine449bd832023-01-11 14:50:10 +0100346 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
347 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100348 goto exit;
349 }
Paul Bakker20a78082011-01-21 09:32:12 +0000350
Gilles Peskine449bd832023-01-11 14:50:10 +0100351 if (fwrite(output, 1, olen, fout) != olen) {
352 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000353 goto exit;
354 }
355 }
356
Gilles Peskine449bd832023-01-11 14:50:10 +0100357 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
358 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000359 goto exit;
360 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100361 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
362 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100363 goto exit;
364 }
Paul Bakker20a78082011-01-21 09:32:12 +0000365
Gilles Peskine449bd832023-01-11 14:50:10 +0100366 if (fwrite(output, 1, olen, fout) != olen) {
367 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000368 goto exit;
369 }
Paul Bakker26c4e3c2012-07-04 17:08:33 +0000370
Paul Bakker20a78082011-01-21 09:32:12 +0000371 /*
372 * Finally write the HMAC.
373 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100374 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
375 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100376 goto exit;
377 }
Paul Bakker20a78082011-01-21 09:32:12 +0000378
Gilles Peskine449bd832023-01-11 14:50:10 +0100379 if (fwrite(digest, 1, mbedtls_md_get_size(md_info), fout) != mbedtls_md_get_size(md_info)) {
380 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000381 goto exit;
382 }
383 }
384
Gilles Peskine449bd832023-01-11 14:50:10 +0100385 if (mode == MODE_DECRYPT) {
Paul Bakker20a78082011-01-21 09:32:12 +0000386 /*
387 * The encrypted file must be structured as follows:
388 *
389 * 00 .. 15 Initialization Vector
Paul Bakker243f48e2016-09-02 22:44:09 +0200390 * 16 .. 31 Encrypted Block #1
Paul Bakker20a78082011-01-21 09:32:12 +0000391 * ..
Paul Bakker243f48e2016-09-02 22:44:09 +0200392 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
393 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
Paul Bakker20a78082011-01-21 09:32:12 +0000394 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100395 if (filesize < 16 + mbedtls_md_get_size(md_info)) {
396 mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000397 goto exit;
398 }
399
Gilles Peskine449bd832023-01-11 14:50:10 +0100400 if (mbedtls_cipher_get_block_size(&cipher_ctx) == 0) {
401 mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
Janos Follath8eb64132016-06-03 15:40:57 +0100402 goto exit;
403 }
404
405 /*
406 * Check the file size.
407 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100408 if (mbedtls_cipher_info_get_mode(cipher_info) != MBEDTLS_MODE_GCM &&
409 ((filesize - mbedtls_md_get_size(md_info)) %
410 mbedtls_cipher_get_block_size(&cipher_ctx)) != 0) {
411 mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
412 mbedtls_cipher_get_block_size(&cipher_ctx));
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 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100419 filesize -= (16 + mbedtls_md_get_size(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000420
421 /*
422 * Read the IV and original filesize modulo 16.
423 */
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +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 Peskine449bd832023-01-11 14:50:10 +0100435 memset(digest, 0, 32);
436 memcpy(digest, IV, 16);
Paul Bakker20a78082011-01-21 09:32:12 +0000437
Gilles Peskine449bd832023-01-11 14:50:10 +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 Peskinef1c30b22021-12-10 14:25:45 +0100441 goto exit;
442 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100443 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
444 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100445 goto exit;
446 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100447 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
448 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100449 goto exit;
450 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100451 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
452 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100453 goto exit;
454 }
Paul Bakker20a78082011-01-21 09:32:12 +0000455 }
456
Gilles Peskine449bd832023-01-11 14:50:10 +0100457 if (mbedtls_cipher_setkey(&cipher_ctx,
458 digest,
459 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
460 MBEDTLS_DECRYPT) != 0) {
461 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200462 goto exit;
463 }
464
Gilles Peskine449bd832023-01-11 14:50:10 +0100465 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
466 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200467 goto exit;
468 }
469
Gilles Peskine449bd832023-01-11 14:50:10 +0100470 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
471 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200472 goto exit;
473 }
Paul Bakker20a78082011-01-21 09:32:12 +0000474
Gilles Peskine449bd832023-01-11 14:50:10 +0100475 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
476 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100477 goto exit;
478 }
Paul Bakker20a78082011-01-21 09:32:12 +0000479
480 /*
481 * Decrypt and write the plaintext.
482 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100483 for (offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size(&cipher_ctx)) {
484 ilen = ((unsigned int) filesize - offset > mbedtls_cipher_get_block_size(&cipher_ctx)) ?
485 mbedtls_cipher_get_block_size(&cipher_ctx) : (unsigned int) (filesize - offset);
Paul Bakker243f48e2016-09-02 22:44:09 +0200486
Gilles Peskine449bd832023-01-11 14:50:10 +0100487 if (fread(buffer, 1, ilen, fin) != ilen) {
488 mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
489 mbedtls_cipher_get_block_size(&cipher_ctx));
Paul Bakker20a78082011-01-21 09:32:12 +0000490 goto exit;
491 }
492
Gilles Peskine449bd832023-01-11 14:50:10 +0100493 if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
494 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100495 goto exit;
496 }
Gilles Peskine449bd832023-01-11 14:50:10 +0100497 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
498 &olen) != 0) {
499 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
Paul Bakkercbe3d0d2014-04-17 16:00:59 +0200500 goto exit;
501 }
Paul Bakker20a78082011-01-21 09:32:12 +0000502
Gilles Peskine449bd832023-01-11 14:50:10 +0100503 if (fwrite(output, 1, olen, fout) != olen) {
504 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Paul Bakker20a78082011-01-21 09:32:12 +0000505 goto exit;
506 }
507 }
508
509 /*
Paul Bakker20a78082011-01-21 09:32:12 +0000510 * Verify the message authentication code.
511 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100512 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
513 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100514 goto exit;
515 }
Paul Bakker20a78082011-01-21 09:32:12 +0000516
Gilles Peskine449bd832023-01-11 14:50:10 +0100517 if (fread(buffer, 1, mbedtls_md_get_size(md_info), fin) != mbedtls_md_get_size(md_info)) {
518 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size(md_info));
Paul Bakker20a78082011-01-21 09:32:12 +0000519 goto exit;
520 }
521
Paul Bakker424cd692013-10-31 14:22:08 +0100522 /* Use constant-time buffer comparison */
523 diff = 0;
Gilles Peskine449bd832023-01-11 14:50:10 +0100524 for (i = 0; i < mbedtls_md_get_size(md_info); i++) {
Paul Bakker424cd692013-10-31 14:22:08 +0100525 diff |= digest[i] ^ buffer[i];
Gilles Peskine449bd832023-01-11 14:50:10 +0100526 }
Paul Bakker424cd692013-10-31 14:22:08 +0100527
Gilles Peskine449bd832023-01-11 14:50:10 +0100528 if (diff != 0) {
529 mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
530 "or file corrupted.\n");
Paul Bakker20a78082011-01-21 09:32:12 +0000531 goto exit;
532 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100533
534 /*
535 * Write the final block of data
536 */
Gilles Peskine449bd832023-01-11 14:50:10 +0100537 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
538 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
Gilles Peskinef1c30b22021-12-10 14:25:45 +0100539 goto exit;
540 }
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100541
Gilles Peskine449bd832023-01-11 14:50:10 +0100542 if (fwrite(output, 1, olen, fout) != olen) {
543 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
Manuel Pégourié-Gonnard0f2eacb2013-11-25 17:55:17 +0100544 goto exit;
545 }
Paul Bakker20a78082011-01-21 09:32:12 +0000546 }
547
Andres Amaya Garcia4c47df62018-04-29 19:11:26 +0100548 exit_code = MBEDTLS_EXIT_SUCCESS;
Paul Bakker20a78082011-01-21 09:32:12 +0000549
550exit:
Gilles Peskine449bd832023-01-11 14:50:10 +0100551 if (fin) {
552 fclose(fin);
553 }
554 if (fout) {
555 fclose(fout);
556 }
Paul Bakker20a78082011-01-21 09:32:12 +0000557
Hanno Beckerf601ec52017-06-27 08:22:17 +0100558 /* Zeroize all command line arguments to also cover
559 the case when the user has missed or reordered some,
560 in which case the key might not be in argv[6]. */
Gilles Peskine449bd832023-01-11 14:50:10 +0100561 for (i = 0; i < argc; i++) {
562 mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
563 }
Hanno Beckerf601ec52017-06-27 08:22:17 +0100564
Gilles Peskine449bd832023-01-11 14:50:10 +0100565 mbedtls_platform_zeroize(IV, sizeof(IV));
566 mbedtls_platform_zeroize(key, sizeof(key));
567 mbedtls_platform_zeroize(buffer, sizeof(buffer));
568 mbedtls_platform_zeroize(output, sizeof(output));
569 mbedtls_platform_zeroize(digest, sizeof(digest));
Paul Bakker20a78082011-01-21 09:32:12 +0000570
Gilles Peskine449bd832023-01-11 14:50:10 +0100571 mbedtls_cipher_free(&cipher_ctx);
572 mbedtls_md_free(&md_ctx);
Paul Bakker20a78082011-01-21 09:32:12 +0000573
Gilles Peskine449bd832023-01-11 14:50:10 +0100574 mbedtls_exit(exit_code);
Paul Bakker20a78082011-01-21 09:32:12 +0000575}
Manuel Pégourié-Gonnard2cf5a7c2015-04-08 12:49:31 +0200576#endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */