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