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