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