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