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