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