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