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