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