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