Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * \brief Generic file encryption program using generic wrappers for configured |
| 3 | * security. |
| 4 | * |
Bence Szépkúti | a2947ac | 2020-08-19 16:37:36 +0200 | [diff] [blame] | 5 | * Copyright The Mbed TLS Contributors |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 6 | * SPDX-License-Identifier: Apache-2.0 OR GPL-2.0-or-later |
| 7 | * |
| 8 | * This file is provided under the Apache License 2.0, or the |
| 9 | * GNU General Public License v2.0 or later. |
| 10 | * |
| 11 | * ********** |
| 12 | * Apache License 2.0: |
Manuel Pégourié-Gonnard | 37ff140 | 2015-09-04 14:21:07 +0200 | [diff] [blame] | 13 | * |
| 14 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 15 | * not use this file except in compliance with the License. |
| 16 | * You may obtain a copy of the License at |
| 17 | * |
| 18 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 19 | * |
| 20 | * Unless required by applicable law or agreed to in writing, software |
| 21 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 22 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 23 | * See the License for the specific language governing permissions and |
| 24 | * limitations under the License. |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 25 | * |
Bence Szépkúti | f744bd7 | 2020-06-05 13:02:18 +0200 | [diff] [blame] | 26 | * ********** |
| 27 | * |
| 28 | * ********** |
| 29 | * GNU General Public License v2.0 or later: |
| 30 | * |
| 31 | * This program is free software; you can redistribute it and/or modify |
| 32 | * it under the terms of the GNU General Public License as published by |
| 33 | * the Free Software Foundation; either version 2 of the License, or |
| 34 | * (at your option) any later version. |
| 35 | * |
| 36 | * This program is distributed in the hope that it will be useful, |
| 37 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 38 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 39 | * GNU General Public License for more details. |
| 40 | * |
| 41 | * You should have received a copy of the GNU General Public License along |
| 42 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 43 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 44 | * |
| 45 | * ********** |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 46 | */ |
| 47 | |
Nicholas Wilson | 2682edf | 2017-12-05 12:08:15 +0000 | [diff] [blame] | 48 | /* Enable definition of fileno() even when compiling with -std=c99. Must be |
| 49 | * set before config.h, which pulls in glibc's features.h indirectly. |
| 50 | * Harmless on other platforms. */ |
| 51 | #define _POSIX_C_SOURCE 1 |
| 52 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 53 | #if !defined(MBEDTLS_CONFIG_FILE) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 54 | #include "mbedtls/config.h" |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 55 | #else |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 56 | #include MBEDTLS_CONFIG_FILE |
Manuel Pégourié-Gonnard | cef4ad2 | 2014-04-29 12:39:06 +0200 | [diff] [blame] | 57 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 58 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 59 | #if defined(MBEDTLS_PLATFORM_C) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 60 | #include "mbedtls/platform.h" |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 61 | #else |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 62 | #include <stdio.h> |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 63 | #include <stdlib.h> |
| 64 | #define mbedtls_fprintf fprintf |
| 65 | #define mbedtls_printf printf |
Manuel Pégourié-Gonnard | 3ef6a6d | 2018-12-10 14:31:45 +0100 | [diff] [blame] | 66 | #define mbedtls_exit exit |
Andres Amaya Garcia | 7d42965 | 2018-04-30 22:42:33 +0100 | [diff] [blame] | 67 | #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 68 | #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE |
| 69 | #endif /* MBEDTLS_PLATFORM_C */ |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 70 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 71 | #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \ |
| 72 | defined(MBEDTLS_FS_IO) |
Manuel Pégourié-Gonnard | 7f80997 | 2015-03-09 17:05:11 +0000 | [diff] [blame] | 73 | #include "mbedtls/cipher.h" |
| 74 | #include "mbedtls/md.h" |
Hanno Becker | 0b44d5c | 2018-10-12 16:46:37 +0100 | [diff] [blame] | 75 | #include "mbedtls/platform_util.h" |
Rich Evans | 18b78c7 | 2015-02-11 14:06:19 +0000 | [diff] [blame] | 76 | |
| 77 | #include <stdio.h> |
| 78 | #include <stdlib.h> |
| 79 | #include <string.h> |
Rich Evans | f90016a | 2015-01-19 14:26:37 +0000 | [diff] [blame] | 80 | #endif |
| 81 | |
Paul Bakker | 494c0b8 | 2011-04-24 15:30:07 +0000 | [diff] [blame] | 82 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 83 | #include <windows.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 84 | #if !defined(_WIN32_WCE) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 85 | #include <io.h> |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 86 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 87 | #else |
| 88 | #include <sys/types.h> |
| 89 | #include <unistd.h> |
| 90 | #endif |
| 91 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 92 | #define MODE_ENCRYPT 0 |
| 93 | #define MODE_DECRYPT 1 |
| 94 | |
| 95 | #define USAGE \ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 96 | "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \ |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 97 | "\n <mode>: 0 = encrypt, 1 = decrypt\n" \ |
| 98 | "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \ |
| 99 | "\n" |
| 100 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 101 | #if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \ |
| 102 | !defined(MBEDTLS_FS_IO) |
Rich Evans | 85b05ec | 2015-02-12 11:37:29 +0000 | [diff] [blame] | 103 | int main( void ) |
Paul Bakker | 5690efc | 2011-05-26 13:16:06 +0000 | [diff] [blame] | 104 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 105 | mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO 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 | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 111 | int main( int argc, char *argv[] ) |
| 112 | { |
Gilles Peskine | 971e5e9 | 2020-04-14 19:34:19 +0200 | [diff] [blame] | 113 | int ret = 1, i; |
| 114 | unsigned n; |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 115 | int exit_code = MBEDTLS_EXIT_FAILURE; |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 116 | int mode; |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 117 | size_t keylen, ilen, olen; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 118 | FILE *fkey, *fin = NULL, *fout = NULL; |
| 119 | |
| 120 | char *p; |
| 121 | unsigned char IV[16]; |
| 122 | unsigned char key[512]; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 123 | unsigned char digest[MBEDTLS_MD_MAX_SIZE]; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 124 | unsigned char buffer[1024]; |
| 125 | unsigned char output[1024]; |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 126 | unsigned char diff; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 127 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 128 | const mbedtls_cipher_info_t *cipher_info; |
| 129 | const mbedtls_md_info_t *md_info; |
| 130 | mbedtls_cipher_context_t cipher_ctx; |
| 131 | mbedtls_md_context_t md_ctx; |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 132 | #if defined(_WIN32_WCE) |
| 133 | long filesize, offset; |
| 134 | #elif defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 135 | LARGE_INTEGER li_size; |
| 136 | __int64 filesize, offset; |
| 137 | #else |
| 138 | off_t filesize, offset; |
| 139 | #endif |
| 140 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 141 | mbedtls_cipher_init( &cipher_ctx ); |
| 142 | mbedtls_md_init( &md_ctx ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 143 | |
| 144 | /* |
| 145 | * Parse the command-line arguments. |
| 146 | */ |
| 147 | if( argc != 7 ) |
| 148 | { |
| 149 | const int *list; |
| 150 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 151 | mbedtls_printf( USAGE ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 152 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 153 | mbedtls_printf( "Available ciphers:\n" ); |
| 154 | list = mbedtls_cipher_list(); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 155 | while( *list ) |
| 156 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 157 | cipher_info = mbedtls_cipher_info_from_type( *list ); |
| 158 | mbedtls_printf( " %s\n", cipher_info->name ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 159 | list++; |
| 160 | } |
| 161 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 162 | mbedtls_printf( "\nAvailable message digests:\n" ); |
| 163 | list = mbedtls_md_list(); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 164 | while( *list ) |
| 165 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 166 | md_info = mbedtls_md_info_from_type( *list ); |
| 167 | mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 168 | list++; |
| 169 | } |
| 170 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 171 | #if defined(_WIN32) |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 172 | mbedtls_printf( "\n Press Enter to exit this program.\n" ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 173 | fflush( stdout ); getchar(); |
| 174 | #endif |
| 175 | |
| 176 | goto exit; |
| 177 | } |
| 178 | |
| 179 | mode = atoi( argv[1] ); |
| 180 | |
| 181 | if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT ) |
| 182 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 183 | mbedtls_fprintf( stderr, "invalid operation mode\n" ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 184 | goto exit; |
| 185 | } |
| 186 | |
| 187 | if( strcmp( argv[2], argv[3] ) == 0 ) |
| 188 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 189 | mbedtls_fprintf( stderr, "input and output filenames must differ\n" ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 190 | goto exit; |
| 191 | } |
| 192 | |
| 193 | if( ( fin = fopen( argv[2], "rb" ) ) == NULL ) |
| 194 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 195 | mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 196 | goto exit; |
| 197 | } |
| 198 | |
| 199 | if( ( fout = fopen( argv[3], "wb+" ) ) == NULL ) |
| 200 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 201 | mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 202 | goto exit; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | * Read the Cipher and MD from the command line |
| 207 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 208 | cipher_info = mbedtls_cipher_info_from_string( argv[4] ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 209 | if( cipher_info == NULL ) |
| 210 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 211 | mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 212 | goto exit; |
| 213 | } |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 214 | if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 215 | { |
Manuel Pégourié-Gonnard | 8473f87 | 2015-05-14 13:51:45 +0200 | [diff] [blame] | 216 | mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 217 | goto exit; |
| 218 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 219 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 220 | md_info = mbedtls_md_info_from_string( argv[5] ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 221 | if( md_info == NULL ) |
| 222 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 223 | mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 224 | goto exit; |
| 225 | } |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 226 | |
| 227 | if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 ) |
| 228 | { |
Janos Follath | 352dbe2 | 2016-06-07 10:29:05 +0100 | [diff] [blame] | 229 | mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" ); |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 230 | goto exit; |
| 231 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 232 | |
| 233 | /* |
Hanno Becker | 840bace | 2017-06-27 11:36:21 +0100 | [diff] [blame] | 234 | * Read the secret key from file or command line |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 235 | */ |
| 236 | if( ( fkey = fopen( argv[6], "rb" ) ) != NULL ) |
| 237 | { |
| 238 | keylen = fread( key, 1, sizeof( key ), fkey ); |
| 239 | fclose( fkey ); |
| 240 | } |
| 241 | else |
| 242 | { |
| 243 | if( memcmp( argv[6], "hex:", 4 ) == 0 ) |
| 244 | { |
| 245 | p = &argv[6][4]; |
| 246 | keylen = 0; |
| 247 | |
| 248 | while( sscanf( p, "%02X", &n ) > 0 && |
| 249 | keylen < (int) sizeof( key ) ) |
| 250 | { |
| 251 | key[keylen++] = (unsigned char) n; |
| 252 | p += 2; |
| 253 | } |
| 254 | } |
| 255 | else |
| 256 | { |
| 257 | keylen = strlen( argv[6] ); |
| 258 | |
| 259 | if( keylen > (int) sizeof( key ) ) |
| 260 | keylen = (int) sizeof( key ); |
| 261 | |
| 262 | memcpy( key, argv[6], keylen ); |
| 263 | } |
| 264 | } |
| 265 | |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 266 | #if defined(_WIN32_WCE) |
| 267 | filesize = fseek( fin, 0L, SEEK_END ); |
| 268 | #else |
| 269 | #if defined(_WIN32) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 270 | /* |
| 271 | * Support large files (> 2Gb) on Win32 |
| 272 | */ |
| 273 | li_size.QuadPart = 0; |
| 274 | li_size.LowPart = |
| 275 | SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ), |
| 276 | li_size.LowPart, &li_size.HighPart, FILE_END ); |
| 277 | |
| 278 | if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR ) |
| 279 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 280 | mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 281 | goto exit; |
| 282 | } |
| 283 | |
| 284 | filesize = li_size.QuadPart; |
| 285 | #else |
| 286 | if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 ) |
| 287 | { |
| 288 | perror( "lseek" ); |
| 289 | goto exit; |
| 290 | } |
| 291 | #endif |
Paul Bakker | cce9d77 | 2011-11-18 14:26:47 +0000 | [diff] [blame] | 292 | #endif |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 293 | |
| 294 | if( fseek( fin, 0, SEEK_SET ) < 0 ) |
| 295 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 296 | mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 297 | goto exit; |
| 298 | } |
| 299 | |
| 300 | if( mode == MODE_ENCRYPT ) |
| 301 | { |
| 302 | /* |
| 303 | * Generate the initialization vector as: |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 304 | * IV = MD( filesize || filename )[0..15] |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 305 | */ |
| 306 | for( i = 0; i < 8; i++ ) |
| 307 | buffer[i] = (unsigned char)( filesize >> ( i << 3 ) ); |
| 308 | |
| 309 | p = argv[2]; |
| 310 | |
Paul Elliott | 32c4224 | 2021-12-09 17:18:10 +0000 | [diff] [blame^] | 311 | if( mbedtls_md_starts( &md_ctx ) != 0 ) |
| 312 | { |
| 313 | mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" ); |
| 314 | goto exit; |
| 315 | } |
| 316 | if( mbedtls_md_update( &md_ctx, buffer, 8 ) != 0 ) |
| 317 | { |
| 318 | mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" ); |
| 319 | goto exit; |
| 320 | } |
| 321 | if( mbedtls_md_update( &md_ctx, ( unsigned char * ) p, strlen( p ) ) |
| 322 | != 0 ) |
| 323 | { |
| 324 | mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" ); |
| 325 | goto exit; |
| 326 | } |
| 327 | if( mbedtls_md_finish( &md_ctx, digest ) != 0 ) |
| 328 | { |
| 329 | mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" ); |
| 330 | goto exit; |
| 331 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 332 | |
| 333 | memcpy( IV, digest, 16 ); |
| 334 | |
| 335 | /* |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 336 | * Append the IV at the beginning of the output. |
| 337 | */ |
| 338 | if( fwrite( IV, 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 | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 341 | goto exit; |
| 342 | } |
| 343 | |
| 344 | /* |
| 345 | * Hash the IV and the secret key together 8192 times |
| 346 | * using the result to setup the AES context and HMAC. |
| 347 | */ |
| 348 | memset( digest, 0, 32 ); |
| 349 | memcpy( digest, IV, 16 ); |
| 350 | |
| 351 | for( i = 0; i < 8192; i++ ) |
| 352 | { |
Paul Elliott | 32c4224 | 2021-12-09 17:18:10 +0000 | [diff] [blame^] | 353 | if( mbedtls_md_starts( &md_ctx ) != 0 ) |
| 354 | { |
| 355 | mbedtls_fprintf( stderr, |
| 356 | "mbedtls_md_starts() returned error\n" ); |
| 357 | goto exit; |
| 358 | } |
| 359 | if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 ) |
| 360 | { |
| 361 | mbedtls_fprintf( stderr, |
| 362 | "mbedtls_md_update() returned error\n" ); |
| 363 | goto exit; |
| 364 | } |
| 365 | if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 ) |
| 366 | { |
| 367 | mbedtls_fprintf( stderr, |
| 368 | "mbedtls_md_update() returned error\n" ); |
| 369 | goto exit; |
| 370 | } |
| 371 | if( mbedtls_md_finish( &md_ctx, digest ) != 0 ) |
| 372 | { |
| 373 | mbedtls_fprintf( stderr, |
| 374 | "mbedtls_md_finish() returned error\n" ); |
| 375 | goto exit; |
| 376 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 377 | |
| 378 | } |
| 379 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 380 | if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 381 | MBEDTLS_ENCRYPT ) != 0 ) |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 382 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 383 | mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 384 | goto exit; |
| 385 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 386 | if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 ) |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 387 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 388 | mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n"); |
Manuel Pégourié-Gonnard | 9c853b9 | 2013-09-03 13:04:44 +0200 | [diff] [blame] | 389 | goto exit; |
| 390 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 391 | if( mbedtls_cipher_reset( &cipher_ctx ) != 0 ) |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 392 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 393 | mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n"); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 394 | goto exit; |
| 395 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 396 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 397 | mbedtls_md_hmac_starts( &md_ctx, digest, 32 ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 398 | |
| 399 | /* |
| 400 | * Encrypt and write the ciphertext. |
| 401 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 402 | for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 403 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 404 | ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ? |
| 405 | mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 406 | |
Paul Bakker | 25b5fe5 | 2011-05-26 14:02:58 +0000 | [diff] [blame] | 407 | if( fread( buffer, 1, ilen, fin ) != ilen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 408 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 409 | mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 410 | goto exit; |
| 411 | } |
| 412 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 413 | if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 414 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 415 | mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n"); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 416 | goto exit; |
| 417 | } |
| 418 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 419 | mbedtls_md_hmac_update( &md_ctx, output, olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 420 | |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 421 | if( fwrite( output, 1, olen, fout ) != olen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 422 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 423 | mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 424 | goto exit; |
| 425 | } |
| 426 | } |
| 427 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 428 | if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 ) |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 429 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 430 | mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" ); |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 431 | goto exit; |
| 432 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 433 | mbedtls_md_hmac_update( &md_ctx, output, olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 434 | |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 435 | if( fwrite( output, 1, olen, fout ) != olen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 436 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 437 | mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 438 | goto exit; |
| 439 | } |
Paul Bakker | 26c4e3c | 2012-07-04 17:08:33 +0000 | [diff] [blame] | 440 | |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 441 | /* |
| 442 | * Finally write the HMAC. |
| 443 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 444 | mbedtls_md_hmac_finish( &md_ctx, digest ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 445 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 446 | if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 447 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 448 | mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 449 | goto exit; |
| 450 | } |
| 451 | } |
| 452 | |
| 453 | if( mode == MODE_DECRYPT ) |
| 454 | { |
| 455 | /* |
| 456 | * The encrypted file must be structured as follows: |
| 457 | * |
| 458 | * 00 .. 15 Initialization Vector |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 459 | * 16 .. 31 Encrypted Block #1 |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 460 | * .. |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 461 | * N*16 .. (N+1)*16 - 1 Encrypted Block #N |
| 462 | * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 463 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 464 | if( filesize < 16 + mbedtls_md_get_size( md_info ) ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 465 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 466 | mbedtls_fprintf( stderr, "File too short to be encrypted.\n" ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 467 | goto exit; |
| 468 | } |
| 469 | |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 470 | if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 ) |
| 471 | { |
Janos Follath | 352dbe2 | 2016-06-07 10:29:05 +0100 | [diff] [blame] | 472 | mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" ); |
Janos Follath | 8eb6413 | 2016-06-03 15:40:57 +0100 | [diff] [blame] | 473 | goto exit; |
| 474 | } |
| 475 | |
| 476 | /* |
| 477 | * Check the file size. |
| 478 | */ |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 479 | if( cipher_info->mode != MBEDTLS_MODE_GCM && |
| 480 | ( ( filesize - mbedtls_md_get_size( md_info ) ) % |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 481 | mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 482 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 483 | mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n", |
| 484 | mbedtls_cipher_get_block_size( &cipher_ctx )); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 485 | goto exit; |
| 486 | } |
| 487 | |
| 488 | /* |
Paul Bakker | 60b1d10 | 2013-10-29 10:02:51 +0100 | [diff] [blame] | 489 | * Subtract the IV + HMAC length. |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 490 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 491 | filesize -= ( 16 + mbedtls_md_get_size( md_info ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 492 | |
| 493 | /* |
| 494 | * Read the IV and original filesize modulo 16. |
| 495 | */ |
| 496 | if( fread( buffer, 1, 16, fin ) != 16 ) |
| 497 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 498 | mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 499 | goto exit; |
| 500 | } |
| 501 | |
| 502 | memcpy( IV, buffer, 16 ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 503 | |
| 504 | /* |
| 505 | * Hash the IV and the secret key together 8192 times |
| 506 | * using the result to setup the AES context and HMAC. |
| 507 | */ |
| 508 | memset( digest, 0, 32 ); |
| 509 | memcpy( digest, IV, 16 ); |
| 510 | |
| 511 | for( i = 0; i < 8192; i++ ) |
| 512 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 513 | mbedtls_md_starts( &md_ctx ); |
| 514 | mbedtls_md_update( &md_ctx, digest, 32 ); |
| 515 | mbedtls_md_update( &md_ctx, key, keylen ); |
| 516 | mbedtls_md_finish( &md_ctx, digest ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 517 | } |
| 518 | |
Manuel Pégourié-Gonnard | 898e0aa | 2015-06-18 15:28:12 +0200 | [diff] [blame] | 519 | if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen, |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 520 | MBEDTLS_DECRYPT ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 521 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 522 | mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 523 | goto exit; |
| 524 | } |
| 525 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 526 | if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 527 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 528 | mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 529 | goto exit; |
| 530 | } |
| 531 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 532 | if( mbedtls_cipher_reset( &cipher_ctx ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 533 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 534 | mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 535 | goto exit; |
| 536 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 537 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 538 | mbedtls_md_hmac_starts( &md_ctx, digest, 32 ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 539 | |
| 540 | /* |
| 541 | * Decrypt and write the plaintext. |
| 542 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 543 | for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 544 | { |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 545 | ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ? |
| 546 | mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset ); |
| 547 | |
| 548 | if( fread( buffer, 1, ilen, fin ) != ilen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 549 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 550 | mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", |
| 551 | mbedtls_cipher_get_block_size( &cipher_ctx ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 552 | goto exit; |
| 553 | } |
| 554 | |
Paul Bakker | 243f48e | 2016-09-02 22:44:09 +0200 | [diff] [blame] | 555 | mbedtls_md_hmac_update( &md_ctx, buffer, ilen ); |
| 556 | if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, |
| 557 | &olen ) != 0 ) |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 558 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 559 | mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" ); |
Paul Bakker | cbe3d0d | 2014-04-17 16:00:59 +0200 | [diff] [blame] | 560 | goto exit; |
| 561 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 562 | |
Paul Bakker | 2c0994e | 2011-05-25 13:51:57 +0000 | [diff] [blame] | 563 | if( fwrite( output, 1, olen, fout ) != olen ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 564 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 565 | mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 566 | goto exit; |
| 567 | } |
| 568 | } |
| 569 | |
| 570 | /* |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 571 | * Verify the message authentication code. |
| 572 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 573 | mbedtls_md_hmac_finish( &md_ctx, digest ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 574 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 575 | if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 576 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 577 | mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 578 | goto exit; |
| 579 | } |
| 580 | |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 581 | /* Use constant-time buffer comparison */ |
| 582 | diff = 0; |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 583 | for( i = 0; i < mbedtls_md_get_size( md_info ); i++ ) |
Paul Bakker | 424cd69 | 2013-10-31 14:22:08 +0100 | [diff] [blame] | 584 | diff |= digest[i] ^ buffer[i]; |
| 585 | |
| 586 | if( diff != 0 ) |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 587 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 588 | mbedtls_fprintf( stderr, "HMAC check failed: wrong key, " |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 589 | "or file corrupted.\n" ); |
| 590 | goto exit; |
| 591 | } |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 592 | |
| 593 | /* |
| 594 | * Write the final block of data |
| 595 | */ |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 596 | mbedtls_cipher_finish( &cipher_ctx, output, &olen ); |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 597 | |
| 598 | if( fwrite( output, 1, olen, fout ) != olen ) |
| 599 | { |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 600 | mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen ); |
Manuel Pégourié-Gonnard | 0f2eacb | 2013-11-25 17:55:17 +0100 | [diff] [blame] | 601 | goto exit; |
| 602 | } |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 603 | } |
| 604 | |
Andres Amaya Garcia | 4c47df6 | 2018-04-29 19:11:26 +0100 | [diff] [blame] | 605 | exit_code = MBEDTLS_EXIT_SUCCESS; |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 606 | |
| 607 | exit: |
Paul Bakker | 6d44032 | 2011-02-06 12:49:19 +0000 | [diff] [blame] | 608 | if( fin ) |
| 609 | fclose( fin ); |
| 610 | if( fout ) |
| 611 | fclose( fout ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 612 | |
Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 613 | /* Zeroize all command line arguments to also cover |
| 614 | the case when the user has missed or reordered some, |
| 615 | in which case the key might not be in argv[6]. */ |
| 616 | for( i = 0; i < argc; i++ ) |
Hanno Becker | 0b44d5c | 2018-10-12 16:46:37 +0100 | [diff] [blame] | 617 | mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) ); |
Hanno Becker | f601ec5 | 2017-06-27 08:22:17 +0100 | [diff] [blame] | 618 | |
Hanno Becker | 0b44d5c | 2018-10-12 16:46:37 +0100 | [diff] [blame] | 619 | mbedtls_platform_zeroize( IV, sizeof( IV ) ); |
| 620 | mbedtls_platform_zeroize( key, sizeof( key ) ); |
| 621 | mbedtls_platform_zeroize( buffer, sizeof( buffer ) ); |
| 622 | mbedtls_platform_zeroize( output, sizeof( output ) ); |
| 623 | mbedtls_platform_zeroize( digest, sizeof( digest ) ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 624 | |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 625 | mbedtls_cipher_free( &cipher_ctx ); |
| 626 | mbedtls_md_free( &md_ctx ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 627 | |
Krzysztof Stachowiak | 3b0c430 | 2019-04-24 14:24:46 +0200 | [diff] [blame] | 628 | mbedtls_exit( exit_code ); |
Paul Bakker | 20a7808 | 2011-01-21 09:32:12 +0000 | [diff] [blame] | 629 | } |
Manuel Pégourié-Gonnard | 2cf5a7c | 2015-04-08 12:49:31 +0200 | [diff] [blame] | 630 | #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */ |