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