1 /*
2 * \brief Generic file encryption program using generic wrappers for configured
3 * security.
4 *
5 * Copyright The Mbed TLS Contributors
6 * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later
7 */
8
9 /* Enable definition of fileno() even when compiling with -std=c99. Must be
10 * set before mbedtls_config.h, which pulls in glibc's features.h indirectly.
11 * Harmless on other platforms. */
12 #define _POSIX_C_SOURCE 200112L
13
14 #include "mbedtls/build_info.h"
15
16 #include "mbedtls/platform.h"
17
18 #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
19 defined(MBEDTLS_FS_IO)
20 #include "mbedtls/cipher.h"
21 #include "mbedtls/md.h"
22 #include "mbedtls/platform_util.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #endif
28
29 #if defined(_WIN32)
30 #include <windows.h>
31 #if !defined(_WIN32_WCE)
32 #include <io.h>
33 #endif
34 #else
35 #include <sys/types.h>
36 #include <unistd.h>
37 #endif
38
39 #define MODE_ENCRYPT 0
40 #define MODE_DECRYPT 1
41
42 #define USAGE \
43 "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
44 "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
45 "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
46 "\n"
47
48 #if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
49 !defined(MBEDTLS_FS_IO)
main(void)50 int main(void)
51 {
52 mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
53 mbedtls_exit(0);
54 }
55 #else
56
57
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60 int ret = 1, i;
61 unsigned n;
62 int exit_code = MBEDTLS_EXIT_FAILURE;
63 int mode;
64 size_t keylen, ilen, olen;
65 FILE *fkey, *fin = NULL, *fout = NULL;
66
67 char *p;
68 unsigned char IV[16];
69 unsigned char key[512];
70 unsigned char digest[MBEDTLS_MD_MAX_SIZE];
71 unsigned char buffer[1024];
72 unsigned char output[1024];
73 unsigned char diff;
74
75 const mbedtls_cipher_info_t *cipher_info;
76 const mbedtls_md_info_t *md_info;
77 mbedtls_cipher_context_t cipher_ctx;
78 mbedtls_md_context_t md_ctx;
79 mbedtls_cipher_mode_t cipher_mode;
80 unsigned int cipher_block_size;
81 unsigned char md_size;
82 #if defined(_WIN32_WCE)
83 long filesize, offset;
84 #elif defined(_WIN32)
85 LARGE_INTEGER li_size;
86 __int64 filesize, offset;
87 #else
88 off_t filesize, offset;
89 #endif
90
91 mbedtls_cipher_init(&cipher_ctx);
92 mbedtls_md_init(&md_ctx);
93
94 /*
95 * Parse the command-line arguments.
96 */
97 if (argc != 7) {
98 const int *list;
99
100 mbedtls_printf(USAGE);
101
102 mbedtls_printf("Available ciphers:\n");
103 list = mbedtls_cipher_list();
104 while (*list) {
105 cipher_info = mbedtls_cipher_info_from_type(*list);
106 mbedtls_printf(" %s\n", mbedtls_cipher_info_get_name(cipher_info));
107 list++;
108 }
109
110 mbedtls_printf("\nAvailable message digests:\n");
111 list = mbedtls_md_list();
112 while (*list) {
113 md_info = mbedtls_md_info_from_type(*list);
114 mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info));
115 list++;
116 }
117
118 goto exit;
119 }
120
121 mode = atoi(argv[1]);
122
123 if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) {
124 mbedtls_fprintf(stderr, "invalid operation mode\n");
125 goto exit;
126 }
127
128 if (strcmp(argv[2], argv[3]) == 0) {
129 mbedtls_fprintf(stderr, "input and output filenames must differ\n");
130 goto exit;
131 }
132
133 if ((fin = fopen(argv[2], "rb")) == NULL) {
134 mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]);
135 goto exit;
136 }
137
138 if ((fout = fopen(argv[3], "wb+")) == NULL) {
139 mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]);
140 goto exit;
141 }
142
143 /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */
144 mbedtls_setbuf(fin, NULL);
145 mbedtls_setbuf(fout, NULL);
146
147 /*
148 * Read the Cipher and MD from the command line
149 */
150 cipher_info = mbedtls_cipher_info_from_string(argv[4]);
151 if (cipher_info == NULL) {
152 mbedtls_fprintf(stderr, "Cipher '%s' not found\n", argv[4]);
153 goto exit;
154 }
155 if ((ret = mbedtls_cipher_setup(&cipher_ctx, cipher_info)) != 0) {
156 mbedtls_fprintf(stderr, "mbedtls_cipher_setup failed\n");
157 goto exit;
158 }
159
160 md_info = mbedtls_md_info_from_string(argv[5]);
161 if (md_info == NULL) {
162 mbedtls_fprintf(stderr, "Message Digest '%s' not found\n", argv[5]);
163 goto exit;
164 }
165
166 if (mbedtls_md_setup(&md_ctx, md_info, 1) != 0) {
167 mbedtls_fprintf(stderr, "mbedtls_md_setup failed\n");
168 goto exit;
169 }
170
171 /*
172 * Read the secret key from file or command line
173 */
174 if ((fkey = fopen(argv[6], "rb")) != NULL) {
175 keylen = fread(key, 1, sizeof(key), fkey);
176 fclose(fkey);
177 } else {
178 if (memcmp(argv[6], "hex:", 4) == 0) {
179 p = &argv[6][4];
180 keylen = 0;
181
182 while (sscanf(p, "%02X", (unsigned int *) &n) > 0 &&
183 keylen < (int) sizeof(key)) {
184 key[keylen++] = (unsigned char) n;
185 p += 2;
186 }
187 } else {
188 keylen = strlen(argv[6]);
189
190 if (keylen > (int) sizeof(key)) {
191 keylen = (int) sizeof(key);
192 }
193
194 memcpy(key, argv[6], keylen);
195 }
196 }
197
198 #if defined(_WIN32_WCE)
199 filesize = fseek(fin, 0L, SEEK_END);
200 #else
201 #if defined(_WIN32)
202 /*
203 * Support large files (> 2Gb) on Win32
204 */
205 li_size.QuadPart = 0;
206 li_size.LowPart =
207 SetFilePointer((HANDLE) _get_osfhandle(_fileno(fin)),
208 li_size.LowPart, &li_size.HighPart, FILE_END);
209
210 if (li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR) {
211 mbedtls_fprintf(stderr, "SetFilePointer(0,FILE_END) failed\n");
212 goto exit;
213 }
214
215 filesize = li_size.QuadPart;
216 #else
217 if ((filesize = lseek(fileno(fin), 0, SEEK_END)) < 0) {
218 perror("lseek");
219 goto exit;
220 }
221 #endif
222 #endif
223
224 if (fseek(fin, 0, SEEK_SET) < 0) {
225 mbedtls_fprintf(stderr, "fseek(0,SEEK_SET) failed\n");
226 goto exit;
227 }
228
229 md_size = mbedtls_md_get_size(md_info);
230 cipher_block_size = mbedtls_cipher_get_block_size(&cipher_ctx);
231
232 if (mode == MODE_ENCRYPT) {
233 /*
234 * Generate the initialization vector as:
235 * IV = MD( filesize || filename )[0..15]
236 */
237 for (i = 0; i < 8; i++) {
238 buffer[i] = (unsigned char) (filesize >> (i << 3));
239 }
240
241 p = argv[2];
242
243 if (mbedtls_md_starts(&md_ctx) != 0) {
244 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
245 goto exit;
246 }
247 if (mbedtls_md_update(&md_ctx, buffer, 8) != 0) {
248 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
249 goto exit;
250 }
251 if (mbedtls_md_update(&md_ctx, (unsigned char *) p, strlen(p))
252 != 0) {
253 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
254 goto exit;
255 }
256 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
257 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
258 goto exit;
259 }
260
261 memcpy(IV, digest, 16);
262
263 /*
264 * Append the IV at the beginning of the output.
265 */
266 if (fwrite(IV, 1, 16, fout) != 16) {
267 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", 16);
268 goto exit;
269 }
270
271 /*
272 * Hash the IV and the secret key together 8192 times
273 * using the result to setup the AES context and HMAC.
274 */
275 memset(digest, 0, 32);
276 memcpy(digest, IV, 16);
277
278 for (i = 0; i < 8192; i++) {
279 if (mbedtls_md_starts(&md_ctx) != 0) {
280 mbedtls_fprintf(stderr,
281 "mbedtls_md_starts() returned error\n");
282 goto exit;
283 }
284 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
285 mbedtls_fprintf(stderr,
286 "mbedtls_md_update() returned error\n");
287 goto exit;
288 }
289 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
290 mbedtls_fprintf(stderr,
291 "mbedtls_md_update() returned error\n");
292 goto exit;
293 }
294 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
295 mbedtls_fprintf(stderr,
296 "mbedtls_md_finish() returned error\n");
297 goto exit;
298 }
299
300 }
301
302 if (mbedtls_cipher_setkey(&cipher_ctx,
303 digest,
304 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
305 MBEDTLS_ENCRYPT) != 0) {
306 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
307 goto exit;
308 }
309 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
310 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
311 goto exit;
312 }
313 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
314 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
315 goto exit;
316 }
317
318 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
319 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
320 goto exit;
321 }
322
323 /*
324 * Encrypt and write the ciphertext.
325 */
326 for (offset = 0; offset < filesize; offset += cipher_block_size) {
327 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
328 cipher_block_size : (unsigned int) (filesize - offset);
329
330 if (fread(buffer, 1, ilen, fin) != ilen) {
331 mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen);
332 goto exit;
333 }
334
335 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) {
336 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
337 goto exit;
338 }
339
340 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
341 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
342 goto exit;
343 }
344
345 if (fwrite(output, 1, olen, fout) != olen) {
346 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
347 goto exit;
348 }
349 }
350
351 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
352 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
353 goto exit;
354 }
355 if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) {
356 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
357 goto exit;
358 }
359
360 if (fwrite(output, 1, olen, fout) != olen) {
361 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
362 goto exit;
363 }
364
365 /*
366 * Finally write the HMAC.
367 */
368 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
369 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
370 goto exit;
371 }
372
373 if (fwrite(digest, 1, md_size, fout) != md_size) {
374 mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size);
375 goto exit;
376 }
377 }
378
379 if (mode == MODE_DECRYPT) {
380 /*
381 * The encrypted file must be structured as follows:
382 *
383 * 00 .. 15 Initialization Vector
384 * 16 .. 31 Encrypted Block #1
385 * ..
386 * N*16 .. (N+1)*16 - 1 Encrypted Block #N
387 * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
388 */
389 if (filesize < 16 + md_size) {
390 mbedtls_fprintf(stderr, "File too short to be encrypted.\n");
391 goto exit;
392 }
393
394 if (cipher_block_size == 0) {
395 mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n");
396 goto exit;
397 }
398
399 /*
400 * Check the file size.
401 */
402 cipher_mode = mbedtls_cipher_info_get_mode(cipher_info);
403 if (cipher_mode != MBEDTLS_MODE_GCM &&
404 cipher_mode != MBEDTLS_MODE_CTR &&
405 cipher_mode != MBEDTLS_MODE_CFB &&
406 cipher_mode != MBEDTLS_MODE_OFB &&
407 ((filesize - md_size) % cipher_block_size) != 0) {
408 mbedtls_fprintf(stderr, "File content not a multiple of the block size (%u).\n",
409 cipher_block_size);
410 goto exit;
411 }
412
413 /*
414 * Subtract the IV + HMAC length.
415 */
416 filesize -= (16 + md_size);
417
418 /*
419 * Read the IV and original filesize modulo 16.
420 */
421 if (fread(buffer, 1, 16, fin) != 16) {
422 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16);
423 goto exit;
424 }
425
426 memcpy(IV, buffer, 16);
427
428 /*
429 * Hash the IV and the secret key together 8192 times
430 * using the result to setup the AES context and HMAC.
431 */
432 memset(digest, 0, 32);
433 memcpy(digest, IV, 16);
434
435 for (i = 0; i < 8192; i++) {
436 if (mbedtls_md_starts(&md_ctx) != 0) {
437 mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n");
438 goto exit;
439 }
440 if (mbedtls_md_update(&md_ctx, digest, 32) != 0) {
441 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
442 goto exit;
443 }
444 if (mbedtls_md_update(&md_ctx, key, keylen) != 0) {
445 mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n");
446 goto exit;
447 }
448 if (mbedtls_md_finish(&md_ctx, digest) != 0) {
449 mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n");
450 goto exit;
451 }
452 }
453
454 if (mbedtls_cipher_setkey(&cipher_ctx,
455 digest,
456 (int) mbedtls_cipher_info_get_key_bitlen(cipher_info),
457 MBEDTLS_DECRYPT) != 0) {
458 mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n");
459 goto exit;
460 }
461
462 if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) {
463 mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n");
464 goto exit;
465 }
466
467 if (mbedtls_cipher_reset(&cipher_ctx) != 0) {
468 mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n");
469 goto exit;
470 }
471
472 if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) {
473 mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n");
474 goto exit;
475 }
476
477 /*
478 * Decrypt and write the plaintext.
479 */
480 for (offset = 0; offset < filesize; offset += cipher_block_size) {
481 ilen = ((unsigned int) filesize - offset > cipher_block_size) ?
482 cipher_block_size : (unsigned int) (filesize - offset);
483
484 if (fread(buffer, 1, ilen, fin) != ilen) {
485 mbedtls_fprintf(stderr, "fread(%u bytes) failed\n",
486 cipher_block_size);
487 goto exit;
488 }
489
490 if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) {
491 mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n");
492 goto exit;
493 }
494 if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output,
495 &olen) != 0) {
496 mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n");
497 goto exit;
498 }
499
500 if (fwrite(output, 1, olen, fout) != olen) {
501 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
502 goto exit;
503 }
504 }
505
506 /*
507 * Verify the message authentication code.
508 */
509 if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) {
510 mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n");
511 goto exit;
512 }
513
514 if (fread(buffer, 1, md_size, fin) != md_size) {
515 mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size);
516 goto exit;
517 }
518
519 /* Use constant-time buffer comparison */
520 diff = 0;
521 for (i = 0; i < md_size; i++) {
522 diff |= digest[i] ^ buffer[i];
523 }
524
525 if (diff != 0) {
526 mbedtls_fprintf(stderr, "HMAC check failed: wrong key, "
527 "or file corrupted.\n");
528 goto exit;
529 }
530
531 /*
532 * Write the final block of data
533 */
534 if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) {
535 mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n");
536 goto exit;
537 }
538
539 if (fwrite(output, 1, olen, fout) != olen) {
540 mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen);
541 goto exit;
542 }
543 }
544
545 exit_code = MBEDTLS_EXIT_SUCCESS;
546
547 exit:
548 if (fin) {
549 fclose(fin);
550 }
551 if (fout) {
552 fclose(fout);
553 }
554
555 /* Zeroize all command line arguments to also cover
556 the case when the user has missed or reordered some,
557 in which case the key might not be in argv[6]. */
558 for (i = 0; i < argc; i++) {
559 mbedtls_platform_zeroize(argv[i], strlen(argv[i]));
560 }
561
562 mbedtls_platform_zeroize(IV, sizeof(IV));
563 mbedtls_platform_zeroize(key, sizeof(key));
564 mbedtls_platform_zeroize(buffer, sizeof(buffer));
565 mbedtls_platform_zeroize(output, sizeof(output));
566 mbedtls_platform_zeroize(digest, sizeof(digest));
567
568 mbedtls_cipher_free(&cipher_ctx);
569 mbedtls_md_free(&md_ctx);
570
571 mbedtls_exit(exit_code);
572 }
573 #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */
574