aescrypt2.c 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470
  1. /*
  2. * AES-256 file encryption program
  3. *
  4. * Copyright (C) 2006-2015, ARM Limited, All Rights Reserved
  5. * SPDX-License-Identifier: Apache-2.0
  6. *
  7. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  8. * not use this file except in compliance with the License.
  9. * You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing, software
  14. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  15. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16. * See the License for the specific language governing permissions and
  17. * limitations under the License.
  18. *
  19. * This file is part of mbed TLS (https://tls.mbed.org)
  20. */
  21. /* Enable definition of fileno() even when compiling with -std=c99. Must be
  22. * set before config.h, which pulls in glibc's features.h indirectly.
  23. * Harmless on other platforms. */
  24. #define _POSIX_C_SOURCE 1
  25. #if !defined(MBEDTLS_CONFIG_FILE)
  26. #include "mbedtls/config.h"
  27. #else
  28. #include MBEDTLS_CONFIG_FILE
  29. #endif
  30. #if defined(MBEDTLS_PLATFORM_C)
  31. #include "mbedtls/platform.h"
  32. #else
  33. #include <stdio.h>
  34. #include <stdlib.h>
  35. #define mbedtls_fprintf fprintf
  36. #define mbedtls_printf printf
  37. #define mbedtls_exit exit
  38. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  39. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  40. #endif /* MBEDTLS_PLATFORM_C */
  41. #include "mbedtls/aes.h"
  42. #include "mbedtls/md.h"
  43. #include "mbedtls/platform_util.h"
  44. #include <stdio.h>
  45. #include <stdlib.h>
  46. #include <string.h>
  47. #if defined(_WIN32)
  48. #include <windows.h>
  49. #if !defined(_WIN32_WCE)
  50. #include <io.h>
  51. #endif
  52. #else
  53. #include <sys/types.h>
  54. #include <unistd.h>
  55. #endif
  56. #define MODE_ENCRYPT 0
  57. #define MODE_DECRYPT 1
  58. #define USAGE \
  59. "\n aescrypt2 <mode> <input filename> <output filename> <key>\n" \
  60. "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
  61. "\n example: aescrypt2 0 file file.aes hex:E76B2413958B00E193\n" \
  62. "\n"
  63. #if !defined(MBEDTLS_AES_C) || !defined(MBEDTLS_SHA256_C) || \
  64. !defined(MBEDTLS_FS_IO) || !defined(MBEDTLS_MD_C)
  65. int main( void )
  66. {
  67. mbedtls_printf("MBEDTLS_AES_C and/or MBEDTLS_SHA256_C "
  68. "and/or MBEDTLS_FS_IO and/or MBEDTLS_MD_C "
  69. "not defined.\n");
  70. return( 0 );
  71. }
  72. #else
  73. int main( int argc, char *argv[] )
  74. {
  75. int ret = 0;
  76. int exit_code = MBEDTLS_EXIT_FAILURE;
  77. unsigned int i, n;
  78. int mode, lastn;
  79. size_t keylen;
  80. FILE *fkey, *fin = NULL, *fout = NULL;
  81. char *p;
  82. unsigned char IV[16];
  83. unsigned char tmp[16];
  84. unsigned char key[512];
  85. unsigned char digest[32];
  86. unsigned char buffer[1024];
  87. unsigned char diff;
  88. mbedtls_aes_context aes_ctx;
  89. mbedtls_md_context_t sha_ctx;
  90. #if defined(_WIN32_WCE)
  91. long filesize, offset;
  92. #elif defined(_WIN32)
  93. LARGE_INTEGER li_size;
  94. __int64 filesize, offset;
  95. #else
  96. off_t filesize, offset;
  97. #endif
  98. mbedtls_aes_init( &aes_ctx );
  99. mbedtls_md_init( &sha_ctx );
  100. ret = mbedtls_md_setup( &sha_ctx, mbedtls_md_info_from_type( MBEDTLS_MD_SHA256 ), 1 );
  101. if( ret != 0 )
  102. {
  103. mbedtls_printf( " ! mbedtls_md_setup() returned -0x%04x\n", -ret );
  104. goto exit;
  105. }
  106. /*
  107. * Parse the command-line arguments.
  108. */
  109. if( argc != 5 )
  110. {
  111. mbedtls_printf( USAGE );
  112. #if defined(_WIN32)
  113. mbedtls_printf( "\n Press Enter to exit this program.\n" );
  114. fflush( stdout ); getchar();
  115. #endif
  116. goto exit;
  117. }
  118. mode = atoi( argv[1] );
  119. memset( IV, 0, sizeof( IV ) );
  120. memset( key, 0, sizeof( key ) );
  121. memset( digest, 0, sizeof( digest ) );
  122. memset( buffer, 0, sizeof( buffer ) );
  123. if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
  124. {
  125. mbedtls_fprintf( stderr, "invalide operation mode\n" );
  126. goto exit;
  127. }
  128. if( strcmp( argv[2], argv[3] ) == 0 )
  129. {
  130. mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
  131. goto exit;
  132. }
  133. if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
  134. {
  135. mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
  136. goto exit;
  137. }
  138. if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
  139. {
  140. mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
  141. goto exit;
  142. }
  143. /*
  144. * Read the secret key from file or command line
  145. */
  146. if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
  147. {
  148. keylen = fread( key, 1, sizeof( key ), fkey );
  149. fclose( fkey );
  150. }
  151. else
  152. {
  153. if( memcmp( argv[4], "hex:", 4 ) == 0 )
  154. {
  155. p = &argv[4][4];
  156. keylen = 0;
  157. while( sscanf( p, "%02X", &n ) > 0 &&
  158. keylen < (int) sizeof( key ) )
  159. {
  160. key[keylen++] = (unsigned char) n;
  161. p += 2;
  162. }
  163. }
  164. else
  165. {
  166. keylen = strlen( argv[4] );
  167. if( keylen > (int) sizeof( key ) )
  168. keylen = (int) sizeof( key );
  169. memcpy( key, argv[4], keylen );
  170. }
  171. }
  172. #if defined(_WIN32_WCE)
  173. filesize = fseek( fin, 0L, SEEK_END );
  174. #else
  175. #if defined(_WIN32)
  176. /*
  177. * Support large files (> 2Gb) on Win32
  178. */
  179. li_size.QuadPart = 0;
  180. li_size.LowPart =
  181. SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
  182. li_size.LowPart, &li_size.HighPart, FILE_END );
  183. if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
  184. {
  185. mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
  186. goto exit;
  187. }
  188. filesize = li_size.QuadPart;
  189. #else
  190. if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
  191. {
  192. perror( "lseek" );
  193. goto exit;
  194. }
  195. #endif
  196. #endif
  197. if( fseek( fin, 0, SEEK_SET ) < 0 )
  198. {
  199. mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
  200. goto exit;
  201. }
  202. if( mode == MODE_ENCRYPT )
  203. {
  204. /*
  205. * Generate the initialization vector as:
  206. * IV = SHA-256( filesize || filename )[0..15]
  207. */
  208. for( i = 0; i < 8; i++ )
  209. buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
  210. p = argv[2];
  211. mbedtls_md_starts( &sha_ctx );
  212. mbedtls_md_update( &sha_ctx, buffer, 8 );
  213. mbedtls_md_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
  214. mbedtls_md_finish( &sha_ctx, digest );
  215. memcpy( IV, digest, 16 );
  216. /*
  217. * The last four bits in the IV are actually used
  218. * to store the file size modulo the AES block size.
  219. */
  220. lastn = (int)( filesize & 0x0F );
  221. IV[15] = (unsigned char)
  222. ( ( IV[15] & 0xF0 ) | lastn );
  223. /*
  224. * Append the IV at the beginning of the output.
  225. */
  226. if( fwrite( IV, 1, 16, fout ) != 16 )
  227. {
  228. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  229. goto exit;
  230. }
  231. /*
  232. * Hash the IV and the secret key together 8192 times
  233. * using the result to setup the AES context and HMAC.
  234. */
  235. memset( digest, 0, 32 );
  236. memcpy( digest, IV, 16 );
  237. for( i = 0; i < 8192; i++ )
  238. {
  239. mbedtls_md_starts( &sha_ctx );
  240. mbedtls_md_update( &sha_ctx, digest, 32 );
  241. mbedtls_md_update( &sha_ctx, key, keylen );
  242. mbedtls_md_finish( &sha_ctx, digest );
  243. }
  244. mbedtls_aes_setkey_enc( &aes_ctx, digest, 256 );
  245. mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
  246. /*
  247. * Encrypt and write the ciphertext.
  248. */
  249. for( offset = 0; offset < filesize; offset += 16 )
  250. {
  251. n = ( filesize - offset > 16 ) ? 16 : (int)
  252. ( filesize - offset );
  253. if( fread( buffer, 1, n, fin ) != (size_t) n )
  254. {
  255. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", n );
  256. goto exit;
  257. }
  258. for( i = 0; i < 16; i++ )
  259. buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
  260. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_ENCRYPT, buffer, buffer );
  261. mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
  262. if( fwrite( buffer, 1, 16, fout ) != 16 )
  263. {
  264. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  265. goto exit;
  266. }
  267. memcpy( IV, buffer, 16 );
  268. }
  269. /*
  270. * Finally write the HMAC.
  271. */
  272. mbedtls_md_hmac_finish( &sha_ctx, digest );
  273. if( fwrite( digest, 1, 32, fout ) != 32 )
  274. {
  275. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  276. goto exit;
  277. }
  278. }
  279. if( mode == MODE_DECRYPT )
  280. {
  281. /*
  282. * The encrypted file must be structured as follows:
  283. *
  284. * 00 .. 15 Initialization Vector
  285. * 16 .. 31 AES Encrypted Block #1
  286. * ..
  287. * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
  288. * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
  289. */
  290. if( filesize < 48 )
  291. {
  292. mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
  293. goto exit;
  294. }
  295. if( ( filesize & 0x0F ) != 0 )
  296. {
  297. mbedtls_fprintf( stderr, "File size not a multiple of 16.\n" );
  298. goto exit;
  299. }
  300. /*
  301. * Subtract the IV + HMAC length.
  302. */
  303. filesize -= ( 16 + 32 );
  304. /*
  305. * Read the IV and original filesize modulo 16.
  306. */
  307. if( fread( buffer, 1, 16, fin ) != 16 )
  308. {
  309. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
  310. goto exit;
  311. }
  312. memcpy( IV, buffer, 16 );
  313. lastn = IV[15] & 0x0F;
  314. /*
  315. * Hash the IV and the secret key together 8192 times
  316. * using the result to setup the AES context and HMAC.
  317. */
  318. memset( digest, 0, 32 );
  319. memcpy( digest, IV, 16 );
  320. for( i = 0; i < 8192; i++ )
  321. {
  322. mbedtls_md_starts( &sha_ctx );
  323. mbedtls_md_update( &sha_ctx, digest, 32 );
  324. mbedtls_md_update( &sha_ctx, key, keylen );
  325. mbedtls_md_finish( &sha_ctx, digest );
  326. }
  327. mbedtls_aes_setkey_dec( &aes_ctx, digest, 256 );
  328. mbedtls_md_hmac_starts( &sha_ctx, digest, 32 );
  329. /*
  330. * Decrypt and write the plaintext.
  331. */
  332. for( offset = 0; offset < filesize; offset += 16 )
  333. {
  334. if( fread( buffer, 1, 16, fin ) != 16 )
  335. {
  336. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
  337. goto exit;
  338. }
  339. memcpy( tmp, buffer, 16 );
  340. mbedtls_md_hmac_update( &sha_ctx, buffer, 16 );
  341. mbedtls_aes_crypt_ecb( &aes_ctx, MBEDTLS_AES_DECRYPT, buffer, buffer );
  342. for( i = 0; i < 16; i++ )
  343. buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
  344. memcpy( IV, tmp, 16 );
  345. n = ( lastn > 0 && offset == filesize - 16 )
  346. ? lastn : 16;
  347. if( fwrite( buffer, 1, n, fout ) != (size_t) n )
  348. {
  349. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", n );
  350. goto exit;
  351. }
  352. }
  353. /*
  354. * Verify the message authentication code.
  355. */
  356. mbedtls_md_hmac_finish( &sha_ctx, digest );
  357. if( fread( buffer, 1, 32, fin ) != 32 )
  358. {
  359. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 32 );
  360. goto exit;
  361. }
  362. /* Use constant-time buffer comparison */
  363. diff = 0;
  364. for( i = 0; i < 32; i++ )
  365. diff |= digest[i] ^ buffer[i];
  366. if( diff != 0 )
  367. {
  368. mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
  369. "or file corrupted.\n" );
  370. goto exit;
  371. }
  372. }
  373. exit_code = MBEDTLS_EXIT_SUCCESS;
  374. exit:
  375. if( fin )
  376. fclose( fin );
  377. if( fout )
  378. fclose( fout );
  379. /* Zeroize all command line arguments to also cover
  380. the case when the user has missed or reordered some,
  381. in which case the key might not be in argv[4]. */
  382. for( i = 0; i < (unsigned int) argc; i++ )
  383. mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
  384. mbedtls_platform_zeroize( IV, sizeof( IV ) );
  385. mbedtls_platform_zeroize( key, sizeof( key ) );
  386. mbedtls_platform_zeroize( tmp, sizeof( tmp ) );
  387. mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
  388. mbedtls_platform_zeroize( digest, sizeof( digest ) );
  389. mbedtls_aes_free( &aes_ctx );
  390. mbedtls_md_free( &sha_ctx );
  391. return( exit_code );
  392. }
  393. #endif /* MBEDTLS_AES_C && MBEDTLS_SHA256_C && MBEDTLS_FS_IO */