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