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