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