aescrypt2.c 12 KB

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