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