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