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