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