Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 1 | /* |
| 2 | * AES-256 file encryption program |
| 3 | * |
Manuel Pégourié-Gonnard | a658a40 | 2015-01-23 09:45:19 +0000 | [diff] [blame] | 4 | * Copyright (C) 2006-2013, ARM Limited, All Rights Reserved |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 5 | * |
Manuel Pégourié-Gonnard | fe44643 | 2015-03-06 13:17:10 +0000 | [diff] [blame] | 6 | * This file is part of mbed TLS (https://tls.mbed.org) |
Paul Bakker | b96f154 | 2010-07-18 20:36:00 +0000 | [diff] [blame] | 7 | * |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 8 | * This program is free software; you can redistribute it and/or modify |
| 9 | * it under the terms of the GNU General Public License as published by |
| 10 | * the Free Software Foundation; either version 2 of the License, or |
| 11 | * (at your option) any later version. |
| 12 | * |
| 13 | * This program is distributed in the hope that it will be useful, |
| 14 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | * GNU General Public License for more details. |
| 17 | * |
| 18 | * You should have received a copy of the GNU General Public License along |
| 19 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 20 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 21 | */ |
| 22 | |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 23 | #if !defined(POLARSSL_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 24 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 25 | #else |
| 26 | #include POLARSSL_CONFIG_FILE |
| 27 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 28 | |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 29 | #if defined(POLARSSL_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 30 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 31 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 32 | #include <stdio.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 33 | #define polarssl_fprintf fprintf |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 34 | #define polarssl_printf printf |
| 35 | #endif |
| 36 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 37 | #if defined(POLARSSL_AES_C) && defined(POLARSSL_SHA256_C) && \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 38 | defined(POLARSSL_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame^] | 39 | #include "mbedtls/aes.h" |
| 40 | #include "mbedtls/sha256.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 41 | |
| 42 | #include <stdio.h> |
| 43 | #include <stdlib.h> |
| 44 | #include <string.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 45 | #endif |
| 46 | |
Paul Bakker | 494c0b8 | 2011-04-24 15:30:07 +0000 | [diff] [blame] | 47 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 48 | #include <windows.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 49 | #if !defined(_WIN32_WCE) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 50 | #include <io.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 51 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 52 | #else |
| 53 | #include <sys/types.h> |
| 54 | #include <unistd.h> |
| 55 | #endif |
| 56 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 57 | #define MODE_ENCRYPT 0 |
| 58 | #define MODE_DECRYPT 1 |
| 59 | |
| 60 | #define USAGE \ |
| 61 | "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \ |
| 62 | "\n <mode>: 0 = encrypt, 1 = decrypt\n" \ |
| 63 | "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \ |
| 64 | "\n" |
| 65 | |
Manuel Pégourié-Gonnard | 013bffe | 2015-02-13 14:09:44 +0000 | [diff] [blame] | 66 | #if !defined(POLARSSL_AES_C) || !defined(POLARSSL_SHA256_C) || \ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 67 | !defined(POLARSSL_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 68 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 69 | { |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 70 | polarssl_printf("POLARSSL_AES_C and/or POLARSSL_SHA256_C and/or POLARSSL_FS_IO not defined.\n"); |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 71 | return( 0 ); |
| 72 | } |
| 73 | #else |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 74 | int main( int argc, char *argv[] ) |
| 75 | { |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 76 | int ret = 1; |
| 77 | |
| 78 | int i, n; |
Paul Bakker | 23986e5 | 2011-04-24 08:57:21 +0000 | [diff] [blame] | 79 | int mode, lastn; |
| 80 | size_t keylen; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 81 | FILE *fkey, *fin = NULL, *fout = NULL; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 82 | |
| 83 | char *p; |
| 84 | unsigned char IV[16]; |
| 85 | unsigned char key[512]; |
| 86 | unsigned char digest[32]; |
| 87 | unsigned char buffer[1024]; |
Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 88 | unsigned char diff; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 89 | |
| 90 | aes_context aes_ctx; |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 91 | sha256_context sha_ctx; |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 92 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 93 | #if defined(_WIN32_WCE) |
| 94 | long filesize, offset; |
| 95 | #elif defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 96 | LARGE_INTEGER li_size; |
| 97 | __int64 filesize, offset; |
| 98 | #else |
| 99 | off_t filesize, offset; |
| 100 | #endif |
| 101 | |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 102 | aes_init( &aes_ctx ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 103 | sha256_init( &sha_ctx ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 104 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 105 | /* |
| 106 | * Parse the command-line arguments. |
| 107 | */ |
| 108 | if( argc != 5 ) |
| 109 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 110 | polarssl_printf( USAGE ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 111 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 112 | #if defined(_WIN32) |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 113 | polarssl_printf( "\n Press Enter to exit this program.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 114 | fflush( stdout ); getchar(); |
| 115 | #endif |
| 116 | |
| 117 | goto exit; |
| 118 | } |
| 119 | |
| 120 | mode = atoi( argv[1] ); |
wslfa | cc334ef | 2014-12-28 00:55:41 +0800 | [diff] [blame] | 121 | memset(IV, 0, sizeof(IV)); |
| 122 | memset(key, 0, sizeof(key)); |
| 123 | memset(digest, 0, sizeof(digest)); |
| 124 | memset(buffer, 0, sizeof(buffer)); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 125 | |
| 126 | if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT ) |
| 127 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 128 | polarssl_fprintf( stderr, "invalide operation mode\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 129 | goto exit; |
| 130 | } |
| 131 | |
| 132 | if( strcmp( argv[2], argv[3] ) == 0 ) |
| 133 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 134 | polarssl_fprintf( stderr, "input and output filenames must differ\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 135 | goto exit; |
| 136 | } |
| 137 | |
| 138 | if( ( fin = fopen( argv[2], "rb" ) ) == NULL ) |
| 139 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 140 | polarssl_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 141 | goto exit; |
| 142 | } |
| 143 | |
| 144 | if( ( fout = fopen( argv[3], "wb+" ) ) == NULL ) |
| 145 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 146 | polarssl_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 147 | goto exit; |
| 148 | } |
| 149 | |
| 150 | /* |
| 151 | * Read the secret key and clean the command line. |
| 152 | */ |
| 153 | if( ( fkey = fopen( argv[4], "rb" ) ) != NULL ) |
| 154 | { |
| 155 | keylen = fread( key, 1, sizeof( key ), fkey ); |
| 156 | fclose( fkey ); |
| 157 | } |
| 158 | else |
| 159 | { |
| 160 | if( memcmp( argv[4], "hex:", 4 ) == 0 ) |
| 161 | { |
| 162 | p = &argv[4][4]; |
| 163 | keylen = 0; |
| 164 | |
| 165 | while( sscanf( p, "%02X", &n ) > 0 && |
| 166 | keylen < (int) sizeof( key ) ) |
| 167 | { |
| 168 | key[keylen++] = (unsigned char) n; |
| 169 | p += 2; |
| 170 | } |
| 171 | } |
| 172 | else |
| 173 | { |
| 174 | keylen = strlen( argv[4] ); |
| 175 | |
| 176 | if( keylen > (int) sizeof( key ) ) |
| 177 | keylen = (int) sizeof( key ); |
| 178 | |
| 179 | memcpy( key, argv[4], keylen ); |
| 180 | } |
| 181 | } |
| 182 | |
| 183 | memset( argv[4], 0, strlen( argv[4] ) ); |
| 184 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 185 | #if defined(_WIN32_WCE) |
| 186 | filesize = fseek( fin, 0L, SEEK_END ); |
| 187 | #else |
| 188 | #if defined(_WIN32) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 189 | /* |
| 190 | * Support large files (> 2Gb) on Win32 |
| 191 | */ |
| 192 | li_size.QuadPart = 0; |
| 193 | li_size.LowPart = |
| 194 | SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ), |
| 195 | li_size.LowPart, &li_size.HighPart, FILE_END ); |
| 196 | |
| 197 | if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) |
| 198 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 199 | polarssl_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 200 | goto exit; |
| 201 | } |
| 202 | |
| 203 | filesize = li_size.QuadPart; |
| 204 | #else |
| 205 | if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ) |
| 206 | { |
| 207 | perror( "lseek" ); |
| 208 | goto exit; |
| 209 | } |
| 210 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 211 | #endif |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 212 | |
| 213 | if( fseek( fin, 0, SEEK_SET ) < 0 ) |
| 214 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 215 | polarssl_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 216 | goto exit; |
| 217 | } |
| 218 | |
| 219 | if( mode == MODE_ENCRYPT ) |
| 220 | { |
| 221 | /* |
| 222 | * Generate the initialization vector as: |
| 223 | * IV = SHA-256( filesize || filename )[0..15] |
| 224 | */ |
| 225 | for( i = 0; i < 8; i++ ) |
| 226 | buffer[i] = (unsigned char)( filesize >> ( i << 3 ) ); |
| 227 | |
| 228 | p = argv[2]; |
| 229 | |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 230 | sha256_starts( &sha_ctx, 0 ); |
| 231 | sha256_update( &sha_ctx, buffer, 8 ); |
| 232 | sha256_update( &sha_ctx, (unsigned char *) p, strlen( p ) ); |
| 233 | sha256_finish( &sha_ctx, digest ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 234 | |
| 235 | memcpy( IV, digest, 16 ); |
| 236 | |
| 237 | /* |
| 238 | * The last four bits in the IV are actually used |
| 239 | * to store the file size modulo the AES block size. |
| 240 | */ |
| 241 | lastn = (int)( filesize & 0x0F ); |
| 242 | |
| 243 | IV[15] = (unsigned char) |
| 244 | ( ( IV[15] & 0xF0 ) | lastn ); |
| 245 | |
| 246 | /* |
| 247 | * Append the IV at the beginning of the output. |
| 248 | */ |
| 249 | if( fwrite( IV, 1, 16, fout ) != 16 ) |
| 250 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 251 | polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 252 | goto exit; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | * Hash the IV and the secret key together 8192 times |
| 257 | * using the result to setup the AES context and HMAC. |
| 258 | */ |
| 259 | memset( digest, 0, 32 ); |
| 260 | memcpy( digest, IV, 16 ); |
| 261 | |
| 262 | for( i = 0; i < 8192; i++ ) |
| 263 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 264 | sha256_starts( &sha_ctx, 0 ); |
| 265 | sha256_update( &sha_ctx, digest, 32 ); |
| 266 | sha256_update( &sha_ctx, key, keylen ); |
| 267 | sha256_finish( &sha_ctx, digest ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | memset( key, 0, sizeof( key ) ); |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 271 | aes_setkey_enc( &aes_ctx, digest, 256 ); |
| 272 | sha256_hmac_starts( &sha_ctx, digest, 32, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 273 | |
| 274 | /* |
| 275 | * Encrypt and write the ciphertext. |
| 276 | */ |
| 277 | for( offset = 0; offset < filesize; offset += 16 ) |
| 278 | { |
| 279 | n = ( filesize - offset > 16 ) ? 16 : (int) |
| 280 | ( filesize - offset ); |
| 281 | |
| 282 | if( fread( buffer, 1, n, fin ) != (size_t) n ) |
| 283 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 284 | polarssl_fprintf( stderr, "fread(%d bytes) failed\n", n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 285 | goto exit; |
| 286 | } |
| 287 | |
| 288 | for( i = 0; i < 16; i++ ) |
| 289 | buffer[i] = (unsigned char)( buffer[i] ^ IV[i] ); |
| 290 | |
| 291 | aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer ); |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 292 | sha256_hmac_update( &sha_ctx, buffer, 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 293 | |
| 294 | if( fwrite( buffer, 1, 16, fout ) != 16 ) |
| 295 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 296 | polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 297 | goto exit; |
| 298 | } |
| 299 | |
| 300 | memcpy( IV, buffer, 16 ); |
| 301 | } |
| 302 | |
| 303 | /* |
| 304 | * Finally write the HMAC. |
| 305 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 306 | sha256_hmac_finish( &sha_ctx, digest ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 307 | |
| 308 | if( fwrite( digest, 1, 32, fout ) != 32 ) |
| 309 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 310 | polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 311 | goto exit; |
| 312 | } |
| 313 | } |
| 314 | |
| 315 | if( mode == MODE_DECRYPT ) |
| 316 | { |
| 317 | unsigned char tmp[16]; |
| 318 | |
| 319 | /* |
| 320 | * The encrypted file must be structured as follows: |
| 321 | * |
| 322 | * 00 .. 15 Initialization Vector |
| 323 | * 16 .. 31 AES Encrypted Block #1 |
| 324 | * .. |
| 325 | * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N |
| 326 | * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext) |
| 327 | */ |
| 328 | if( filesize < 48 ) |
| 329 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 330 | polarssl_fprintf( stderr, "File too short to be encrypted.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 331 | goto exit; |
| 332 | } |
| 333 | |
| 334 | if( ( filesize & 0x0F ) != 0 ) |
| 335 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 336 | polarssl_fprintf( stderr, "File size not a multiple of 16.\n" ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 337 | goto exit; |
| 338 | } |
| 339 | |
| 340 | /* |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 341 | * Subtract the IV + HMAC length. |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 342 | */ |
| 343 | filesize -= ( 16 + 32 ); |
| 344 | |
| 345 | /* |
| 346 | * Read the IV and original filesize modulo 16. |
| 347 | */ |
| 348 | if( fread( buffer, 1, 16, fin ) != 16 ) |
| 349 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 350 | polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 351 | goto exit; |
| 352 | } |
| 353 | |
| 354 | memcpy( IV, buffer, 16 ); |
| 355 | lastn = IV[15] & 0x0F; |
| 356 | |
| 357 | /* |
| 358 | * Hash the IV and the secret key together 8192 times |
| 359 | * using the result to setup the AES context and HMAC. |
| 360 | */ |
| 361 | memset( digest, 0, 32 ); |
| 362 | memcpy( digest, IV, 16 ); |
| 363 | |
| 364 | for( i = 0; i < 8192; i++ ) |
| 365 | { |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 366 | sha256_starts( &sha_ctx, 0 ); |
| 367 | sha256_update( &sha_ctx, digest, 32 ); |
| 368 | sha256_update( &sha_ctx, key, keylen ); |
| 369 | sha256_finish( &sha_ctx, digest ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | memset( key, 0, sizeof( key ) ); |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 373 | aes_setkey_dec( &aes_ctx, digest, 256 ); |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 374 | sha256_hmac_starts( &sha_ctx, digest, 32, 0 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 375 | |
| 376 | /* |
| 377 | * Decrypt and write the plaintext. |
| 378 | */ |
| 379 | for( offset = 0; offset < filesize; offset += 16 ) |
| 380 | { |
| 381 | if( fread( buffer, 1, 16, fin ) != 16 ) |
| 382 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 383 | polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 384 | goto exit; |
| 385 | } |
| 386 | |
| 387 | memcpy( tmp, buffer, 16 ); |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 388 | |
| 389 | sha256_hmac_update( &sha_ctx, buffer, 16 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 390 | aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer ); |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 391 | |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 392 | for( i = 0; i < 16; i++ ) |
| 393 | buffer[i] = (unsigned char)( buffer[i] ^ IV[i] ); |
| 394 | |
| 395 | memcpy( IV, tmp, 16 ); |
| 396 | |
| 397 | n = ( lastn > 0 && offset == filesize - 16 ) |
| 398 | ? lastn : 16; |
| 399 | |
| 400 | if( fwrite( buffer, 1, n, fout ) != (size_t) n ) |
| 401 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 402 | polarssl_fprintf( stderr, "fwrite(%d bytes) failed\n", n ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 403 | goto exit; |
| 404 | } |
| 405 | } |
| 406 | |
| 407 | /* |
| 408 | * Verify the message authentication code. |
| 409 | */ |
Paul Bakker | 9e36f04 | 2013-06-30 14:34:05 +0200 | [diff] [blame] | 410 | sha256_hmac_finish( &sha_ctx, digest ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 411 | |
| 412 | if( fread( buffer, 1, 32, fin ) != 32 ) |
| 413 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 414 | polarssl_fprintf( stderr, "fread(%d bytes) failed\n", 32 ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 415 | goto exit; |
| 416 | } |
| 417 | |
Manuel Pégourié-Gonnard | 291f9af | 2013-10-28 12:51:32 +0100 | [diff] [blame] | 418 | /* Use constant-time buffer comparison */ |
| 419 | diff = 0; |
| 420 | for( i = 0; i < 32; i++ ) |
| 421 | diff |= digest[i] ^ buffer[i]; |
| 422 | |
| 423 | if( diff != 0 ) |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 424 | { |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 425 | polarssl_fprintf( stderr, "HMAC check failed: wrong key, " |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 426 | "or file corrupted.\n" ); |
| 427 | goto exit; |
| 428 | } |
| 429 | } |
| 430 | |
| 431 | ret = 0; |
| 432 | |
| 433 | exit: |
Paul Bakker | 6d44032 | 2011-02-06 12:49:19 +0000 | [diff] [blame] | 434 | if( fin ) |
| 435 | fclose( fin ); |
| 436 | if( fout ) |
| 437 | fclose( fout ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 438 | |
| 439 | memset( buffer, 0, sizeof( buffer ) ); |
| 440 | memset( digest, 0, sizeof( digest ) ); |
| 441 | |
Paul Bakker | 8cfd9d8 | 2014-06-18 11:16:11 +0200 | [diff] [blame] | 442 | aes_free( &aes_ctx ); |
Paul Bakker | 14e8be4 | 2014-06-26 12:25:06 +0200 | [diff] [blame] | 443 | sha256_free( &sha_ctx ); |
Paul Bakker | 5121ce5 | 2009-01-03 21:22:43 +0000 | [diff] [blame] | 444 | |
| 445 | return( ret ); |
| 446 | } |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 447 | #endif /* POLARSSL_AES_C && POLARSSL_SHA256_C && POLARSSL_FS_IO */ |