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