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 | 16799db | 2023-11-02 19:47:20 +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 |
Bence Szépkúti | bb0cfeb | 2021-05-28 09:42:25 +0200 | [diff] [blame] | 10 | * 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] | 11 | * Harmless on other platforms. */ |
nia | 1c0c837 | 2020-06-11 12:03:45 +0100 | [diff] [blame] | 12 | #define _POSIX_C_SOURCE 200112L |
Felix Conway | 998760a | 2025-03-24 11:37:33 +0000 | [diff] [blame^] | 13 | #define MBEDTLS_DECLARE_PRIVATE_IDENTIFIERS |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 14 | |
Bence Szépkúti | c662b36 | 2021-05-27 11:25:03 +0200 | [diff] [blame] | 15 | #include "mbedtls/build_info.h" |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 16 | |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 17 | #include "mbedtls/platform.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 18 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 19 | #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 20 | defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 21 | #include "mbedtls/cipher.h" |
| 22 | #include "mbedtls/md.h" |
Hanno Becker | 0b44d5c | 2018-10-12 16:46:37 +0100 | [diff] [blame] | 23 | #include "mbedtls/platform_util.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 24 | |
| 25 | #include <stdio.h> |
| 26 | #include <stdlib.h> |
| 27 | #include <string.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 28 | #endif |
| 29 | |
Paul Bakker | 494c0b8 | 2011-04-24 15:30:07 +0000 | [diff] [blame] | 30 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 31 | #include <windows.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 32 | #if !defined(_WIN32_WCE) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 33 | #include <io.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 34 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 35 | #else |
| 36 | #include <sys/types.h> |
| 37 | #include <unistd.h> |
| 38 | #endif |
| 39 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 40 | #define MODE_ENCRYPT 0 |
| 41 | #define MODE_DECRYPT 1 |
| 42 | |
| 43 | #define USAGE \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 44 | "\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] | 45 | "\n <mode>: 0 = encrypt, 1 = decrypt\n" \ |
| 46 | "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \ |
| 47 | "\n" |
| 48 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 49 | #if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \ |
| 50 | !defined(MBEDTLS_FS_IO) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 51 | int main(void) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 52 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | 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] | 54 | mbedtls_exit(0); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 55 | } |
| 56 | #else |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 57 | |
Simon Butcher | 63cb97e | 2018-12-06 17:43:31 +0000 | [diff] [blame] | 58 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 59 | int main(int argc, char *argv[]) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 60 | { |
Gilles Peskine | a5fc939 | 2020-04-14 19:34:19 +0200 | [diff] [blame] | 61 | int ret = 1, i; |
| 62 | unsigned n; |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 63 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 64 | int mode; |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 65 | size_t keylen, ilen, olen; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 66 | FILE *fkey, *fin = NULL, *fout = NULL; |
| 67 | |
| 68 | char *p; |
| 69 | unsigned char IV[16]; |
| 70 | unsigned char key[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | unsigned char digest[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 72 | unsigned char buffer[1024]; |
| 73 | unsigned char output[1024]; |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 74 | unsigned char diff; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 75 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 76 | const mbedtls_cipher_info_t *cipher_info; |
| 77 | const mbedtls_md_info_t *md_info; |
| 78 | mbedtls_cipher_context_t cipher_ctx; |
| 79 | mbedtls_md_context_t md_ctx; |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 80 | mbedtls_cipher_mode_t cipher_mode; |
| 81 | unsigned int cipher_block_size; |
| 82 | unsigned char md_size; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 83 | #if defined(_WIN32_WCE) |
| 84 | long filesize, offset; |
| 85 | #elif defined(_WIN32) |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 86 | LARGE_INTEGER li_size; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 87 | __int64 filesize, offset; |
| 88 | #else |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 89 | off_t filesize, offset; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 90 | #endif |
| 91 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 92 | mbedtls_cipher_init(&cipher_ctx); |
| 93 | mbedtls_md_init(&md_ctx); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 94 | |
| 95 | /* |
| 96 | * Parse the command-line arguments. |
| 97 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 98 | if (argc != 7) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 99 | const int *list; |
| 100 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 101 | mbedtls_printf(USAGE); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 102 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 103 | mbedtls_printf("Available ciphers:\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 104 | list = mbedtls_cipher_list(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 105 | while (*list) { |
| 106 | cipher_info = mbedtls_cipher_info_from_type(*list); |
Paul Elliott | e4b3f75 | 2023-12-11 17:57:16 +0000 | [diff] [blame] | 107 | const char *name = mbedtls_cipher_info_get_name(cipher_info); |
Paul Elliott | 80fa88e | 2023-11-24 17:12:24 +0000 | [diff] [blame] | 108 | |
Paul Elliott | e4b3f75 | 2023-12-11 17:57:16 +0000 | [diff] [blame] | 109 | if (name) { |
| 110 | mbedtls_printf(" %s\n", mbedtls_cipher_info_get_name(cipher_info)); |
Paul Elliott | 80fa88e | 2023-11-24 17:12:24 +0000 | [diff] [blame] | 111 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 112 | list++; |
| 113 | } |
| 114 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 115 | mbedtls_printf("\nAvailable message digests:\n"); |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 116 | list = mbedtls_md_list(); |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 117 | while (*list) { |
| 118 | md_info = mbedtls_md_info_from_type(*list); |
| 119 | mbedtls_printf(" %s\n", mbedtls_md_get_name(md_info)); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 120 | list++; |
| 121 | } |
| 122 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 123 | goto exit; |
| 124 | } |
| 125 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 126 | mode = atoi(argv[1]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 127 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 128 | if (mode != MODE_ENCRYPT && mode != MODE_DECRYPT) { |
| 129 | mbedtls_fprintf(stderr, "invalid operation mode\n"); |
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 | if (strcmp(argv[2], argv[3]) == 0) { |
| 134 | mbedtls_fprintf(stderr, "input and output filenames must differ\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 135 | goto exit; |
| 136 | } |
| 137 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 138 | if ((fin = fopen(argv[2], "rb")) == NULL) { |
| 139 | mbedtls_fprintf(stderr, "fopen(%s,rb) failed\n", argv[2]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 140 | goto exit; |
| 141 | } |
| 142 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 143 | if ((fout = fopen(argv[3], "wb+")) == NULL) { |
| 144 | mbedtls_fprintf(stderr, "fopen(%s,wb+) failed\n", argv[3]); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 145 | goto exit; |
| 146 | } |
| 147 | |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 148 | /* Ensure no stdio buffering of secrets, as such buffers cannot be wiped. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 149 | mbedtls_setbuf(fin, NULL); |
| 150 | mbedtls_setbuf(fout, NULL); |
Gilles Peskine | 6d576c9 | 2022-06-30 17:06:11 +0200 | [diff] [blame] | 151 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 152 | /* |
| 153 | * Read the Cipher and MD from the command line |
| 154 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 46549cb | 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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 248 | if (mbedtls_md_starts(&md_ctx) != 0) { |
| 249 | mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n"); |
Paul Elliott | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 250 | goto exit; |
| 251 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 254 | goto exit; |
| 255 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 259 | goto exit; |
| 260 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 261 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { |
| 262 | mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n"); |
Paul Elliott | d79d3eb | 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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | 449bd83 | 2023-01-11 14:50:10 +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 | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 287 | goto exit; |
| 288 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 292 | goto exit; |
| 293 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | d79d3eb | 2021-12-09 17:18:10 +0000 | [diff] [blame] | 297 | goto exit; |
| 298 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +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 | d79d3eb | 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 | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 307 | if (mbedtls_cipher_setkey(&cipher_ctx, |
| 308 | digest, |
| 309 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 310 | MBEDTLS_ENCRYPT) != 0) { |
| 311 | mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 312 | goto exit; |
| 313 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 314 | if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) { |
| 315 | mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n"); |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 316 | goto exit; |
| 317 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 318 | if (mbedtls_cipher_reset(&cipher_ctx) != 0) { |
| 319 | mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 320 | goto exit; |
| 321 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 322 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 323 | if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) { |
| 324 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 325 | goto exit; |
| 326 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 327 | |
| 328 | /* |
| 329 | * Encrypt and write the ciphertext. |
| 330 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 331 | for (offset = 0; offset < filesize; offset += cipher_block_size) { |
| 332 | ilen = ((unsigned int) filesize - offset > cipher_block_size) ? |
| 333 | cipher_block_size : (unsigned int) (filesize - offset); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 334 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 335 | if (fread(buffer, 1, ilen, fin) != ilen) { |
| 336 | mbedtls_fprintf(stderr, "fread(%ld bytes) failed\n", (long) ilen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 337 | goto exit; |
| 338 | } |
| 339 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 340 | if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, &olen) != 0) { |
| 341 | mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 342 | goto exit; |
| 343 | } |
| 344 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 345 | if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) { |
| 346 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 347 | goto exit; |
| 348 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 349 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 350 | if (fwrite(output, 1, olen, fout) != olen) { |
| 351 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 352 | goto exit; |
| 353 | } |
| 354 | } |
| 355 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 356 | if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) { |
| 357 | mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 358 | goto exit; |
| 359 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 360 | if (mbedtls_md_hmac_update(&md_ctx, output, olen) != 0) { |
| 361 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 362 | goto exit; |
| 363 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 364 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 365 | if (fwrite(output, 1, olen, fout) != olen) { |
| 366 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 367 | goto exit; |
| 368 | } |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 369 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 370 | /* |
| 371 | * Finally write the HMAC. |
| 372 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 373 | if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) { |
| 374 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 375 | goto exit; |
| 376 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 377 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 378 | if (fwrite(digest, 1, md_size, fout) != md_size) { |
| 379 | mbedtls_fprintf(stderr, "fwrite(%d bytes) failed\n", md_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 380 | goto exit; |
| 381 | } |
| 382 | } |
| 383 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 384 | if (mode == MODE_DECRYPT) { |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 385 | /* |
| 386 | * The encrypted file must be structured as follows: |
| 387 | * |
| 388 | * 00 .. 15 Initialization Vector |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 389 | * 16 .. 31 Encrypted Block #1 |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 390 | * .. |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 391 | * N*16 .. (N+1)*16 - 1 Encrypted Block #N |
| 392 | * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 393 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 394 | if (filesize < 16 + md_size) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 395 | mbedtls_fprintf(stderr, "File too short to be encrypted.\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 396 | goto exit; |
| 397 | } |
| 398 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 399 | if (cipher_block_size == 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 400 | mbedtls_fprintf(stderr, "Invalid cipher block size: 0. \n"); |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 401 | goto exit; |
| 402 | } |
| 403 | |
| 404 | /* |
| 405 | * Check the file size. |
| 406 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 407 | cipher_mode = mbedtls_cipher_info_get_mode(cipher_info); |
| 408 | if (cipher_mode != MBEDTLS_MODE_GCM && |
| 409 | cipher_mode != MBEDTLS_MODE_CTR && |
| 410 | cipher_mode != MBEDTLS_MODE_CFB && |
| 411 | cipher_mode != MBEDTLS_MODE_OFB && |
| 412 | ((filesize - md_size) % cipher_block_size) != 0) { |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 413 | 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] | 414 | cipher_block_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 415 | goto exit; |
| 416 | } |
| 417 | |
| 418 | /* |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 419 | * Subtract the IV + HMAC length. |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 420 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 421 | filesize -= (16 + md_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 422 | |
| 423 | /* |
| 424 | * Read the IV and original filesize modulo 16. |
| 425 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 426 | if (fread(buffer, 1, 16, fin) != 16) { |
| 427 | mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 428 | goto exit; |
| 429 | } |
| 430 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 431 | memcpy(IV, buffer, 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 432 | |
| 433 | /* |
| 434 | * Hash the IV and the secret key together 8192 times |
| 435 | * using the result to setup the AES context and HMAC. |
| 436 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 437 | memset(digest, 0, 32); |
| 438 | memcpy(digest, IV, 16); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 439 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 440 | for (i = 0; i < 8192; i++) { |
| 441 | if (mbedtls_md_starts(&md_ctx) != 0) { |
| 442 | mbedtls_fprintf(stderr, "mbedtls_md_starts() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 443 | goto exit; |
| 444 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 445 | if (mbedtls_md_update(&md_ctx, digest, 32) != 0) { |
| 446 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 447 | goto exit; |
| 448 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 449 | if (mbedtls_md_update(&md_ctx, key, keylen) != 0) { |
| 450 | mbedtls_fprintf(stderr, "mbedtls_md_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 451 | goto exit; |
| 452 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 453 | if (mbedtls_md_finish(&md_ctx, digest) != 0) { |
| 454 | mbedtls_fprintf(stderr, "mbedtls_md_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 455 | goto exit; |
| 456 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 457 | } |
| 458 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 459 | if (mbedtls_cipher_setkey(&cipher_ctx, |
| 460 | digest, |
| 461 | (int) mbedtls_cipher_info_get_key_bitlen(cipher_info), |
| 462 | MBEDTLS_DECRYPT) != 0) { |
| 463 | mbedtls_fprintf(stderr, "mbedtls_cipher_setkey() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 464 | goto exit; |
| 465 | } |
| 466 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 467 | if (mbedtls_cipher_set_iv(&cipher_ctx, IV, 16) != 0) { |
| 468 | mbedtls_fprintf(stderr, "mbedtls_cipher_set_iv() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 469 | goto exit; |
| 470 | } |
| 471 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 472 | if (mbedtls_cipher_reset(&cipher_ctx) != 0) { |
| 473 | mbedtls_fprintf(stderr, "mbedtls_cipher_reset() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 474 | goto exit; |
| 475 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 476 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 477 | if (mbedtls_md_hmac_starts(&md_ctx, digest, 32) != 0) { |
| 478 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_starts() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 479 | goto exit; |
| 480 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 481 | |
| 482 | /* |
| 483 | * Decrypt and write the plaintext. |
| 484 | */ |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 485 | for (offset = 0; offset < filesize; offset += cipher_block_size) { |
| 486 | ilen = ((unsigned int) filesize - offset > cipher_block_size) ? |
| 487 | cipher_block_size : (unsigned int) (filesize - offset); |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 488 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 489 | if (fread(buffer, 1, ilen, fin) != ilen) { |
| 490 | mbedtls_fprintf(stderr, "fread(%u bytes) failed\n", |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 491 | cipher_block_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 492 | goto exit; |
| 493 | } |
| 494 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 495 | if (mbedtls_md_hmac_update(&md_ctx, buffer, ilen) != 0) { |
| 496 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_update() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 497 | goto exit; |
| 498 | } |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 499 | if (mbedtls_cipher_update(&cipher_ctx, buffer, ilen, output, |
| 500 | &olen) != 0) { |
| 501 | mbedtls_fprintf(stderr, "mbedtls_cipher_update() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 502 | goto exit; |
| 503 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 504 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 505 | if (fwrite(output, 1, olen, fout) != olen) { |
| 506 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 507 | goto exit; |
| 508 | } |
| 509 | } |
| 510 | |
| 511 | /* |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 512 | * Verify the message authentication code. |
| 513 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 514 | if (mbedtls_md_hmac_finish(&md_ctx, digest) != 0) { |
| 515 | mbedtls_fprintf(stderr, "mbedtls_md_hmac_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 516 | goto exit; |
| 517 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 518 | |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 519 | if (fread(buffer, 1, md_size, fin) != md_size) { |
| 520 | mbedtls_fprintf(stderr, "fread(%d bytes) failed\n", md_size); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 521 | goto exit; |
| 522 | } |
| 523 | |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 524 | /* Use constant-time buffer comparison */ |
| 525 | diff = 0; |
Waleed Elmelegy | 46549cb | 2023-06-12 14:53:02 +0100 | [diff] [blame] | 526 | for (i = 0; i < md_size; i++) { |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 527 | diff |= digest[i] ^ buffer[i]; |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 528 | } |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 529 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 530 | if (diff != 0) { |
| 531 | mbedtls_fprintf(stderr, "HMAC check failed: wrong key, " |
| 532 | "or file corrupted.\n"); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 533 | goto exit; |
| 534 | } |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 535 | |
| 536 | /* |
| 537 | * Write the final block of data |
| 538 | */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 539 | if (mbedtls_cipher_finish(&cipher_ctx, output, &olen) != 0) { |
| 540 | mbedtls_fprintf(stderr, "mbedtls_cipher_finish() returned error\n"); |
Gilles Peskine | f1c30b2 | 2021-12-10 14:25:45 +0100 | [diff] [blame] | 541 | goto exit; |
| 542 | } |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 543 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 544 | if (fwrite(output, 1, olen, fout) != olen) { |
| 545 | mbedtls_fprintf(stderr, "fwrite(%ld bytes) failed\n", (long) olen); |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 546 | goto exit; |
| 547 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 548 | } |
| 549 | |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 550 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 551 | |
| 552 | exit: |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 553 | if (fin) { |
| 554 | fclose(fin); |
| 555 | } |
| 556 | if (fout) { |
| 557 | fclose(fout); |
| 558 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 559 | |
Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 560 | /* Zeroize all command line arguments to also cover |
| 561 | the case when the user has missed or reordered some, |
| 562 | in which case the key might not be in argv[6]. */ |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 563 | for (i = 0; i < argc; i++) { |
| 564 | mbedtls_platform_zeroize(argv[i], strlen(argv[i])); |
| 565 | } |
Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 566 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 567 | mbedtls_platform_zeroize(IV, sizeof(IV)); |
| 568 | mbedtls_platform_zeroize(key, sizeof(key)); |
| 569 | mbedtls_platform_zeroize(buffer, sizeof(buffer)); |
| 570 | mbedtls_platform_zeroize(output, sizeof(output)); |
| 571 | mbedtls_platform_zeroize(digest, sizeof(digest)); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 572 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 573 | mbedtls_cipher_free(&cipher_ctx); |
| 574 | mbedtls_md_free(&md_ctx); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 575 | |
Gilles Peskine | 449bd83 | 2023-01-11 14:50:10 +0100 | [diff] [blame] | 576 | mbedtls_exit(exit_code); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 577 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 578 | #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */ |