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