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