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