aescrypt2.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  1. /*
  2. * AES-256 file encryption program
  3. *
  4. * Copyright (C) 2006-2010, Brainspark B.V.
  5. *
  6. * This file is part of PolarSSL (http://www.polarssl.org)
  7. * Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  8. *
  9. * All rights reserved.
  10. *
  11. * This program is free software; you can redistribute it and/or modify
  12. * it under the terms of the GNU General Public License as published by
  13. * the Free Software Foundation; either version 2 of the License, or
  14. * (at your option) any later version.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU General Public License along
  22. * with this program; if not, write to the Free Software Foundation, Inc.,
  23. * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  24. */
  25. #ifndef _CRT_SECURE_NO_DEPRECATE
  26. #define _CRT_SECURE_NO_DEPRECATE 1
  27. #endif
  28. #if defined(_WIN32)
  29. #include <windows.h>
  30. #include <io.h>
  31. #else
  32. #include <sys/types.h>
  33. #include <unistd.h>
  34. #endif
  35. #include <string.h>
  36. #include <stdlib.h>
  37. #ifdef PRINTF_STDLIB
  38. #include <stdio.h>
  39. #endif
  40. #ifdef PRINTF_CUSTOM
  41. #include "tinystdio.h"
  42. #endif
  43. #include <time.h>
  44. #include "polarssl/config.h"
  45. #include "polarssl/aes.h"
  46. #include "polarssl/sha2.h"
  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(POLARSSL_AES_C) || !defined(POLARSSL_SHA2_C)
  55. int main( void )
  56. {
  57. printf("POLARSSL_AES_C and/or POLARSSL_SHA2_C not defined.\n");
  58. return( 0 );
  59. }
  60. #else
  61. int main( int argc, char *argv[] )
  62. {
  63. int ret = 1;
  64. int i, n;
  65. int mode, lastn;
  66. size_t keylen;
  67. FILE *fkey, *fin = NULL, *fout = NULL;
  68. char *p;
  69. unsigned char IV[16];
  70. unsigned char key[512];
  71. unsigned char digest[32];
  72. unsigned char buffer[1024];
  73. aes_context aes_ctx;
  74. sha2_context sha_ctx;
  75. #if defined(WIN32)
  76. LARGE_INTEGER li_size;
  77. __int64 filesize, offset;
  78. #else
  79. off_t filesize, offset;
  80. #endif
  81. /*
  82. * Parse the command-line arguments.
  83. */
  84. if( argc != 5 )
  85. {
  86. printf( USAGE );
  87. #if defined(WIN32)
  88. printf( "\n Press Enter to exit this program.\n" );
  89. fflush( stdout ); getchar();
  90. #endif
  91. goto exit;
  92. }
  93. mode = atoi( argv[1] );
  94. if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
  95. {
  96. fprintf( stderr, "invalide operation mode\n" );
  97. goto exit;
  98. }
  99. if( strcmp( argv[2], argv[3] ) == 0 )
  100. {
  101. fprintf( stderr, "input and output filenames must differ\n" );
  102. goto exit;
  103. }
  104. if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
  105. {
  106. fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
  107. goto exit;
  108. }
  109. if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
  110. {
  111. fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
  112. goto exit;
  113. }
  114. /*
  115. * Read the secret key and clean the command line.
  116. */
  117. if( ( fkey = fopen( argv[4], "rb" ) ) != NULL )
  118. {
  119. keylen = fread( key, 1, sizeof( key ), fkey );
  120. fclose( fkey );
  121. }
  122. else
  123. {
  124. if( memcmp( argv[4], "hex:", 4 ) == 0 )
  125. {
  126. p = &argv[4][4];
  127. keylen = 0;
  128. while( sscanf( p, "%02X", &n ) > 0 &&
  129. keylen < (int) sizeof( key ) )
  130. {
  131. key[keylen++] = (unsigned char) n;
  132. p += 2;
  133. }
  134. }
  135. else
  136. {
  137. keylen = strlen( argv[4] );
  138. if( keylen > (int) sizeof( key ) )
  139. keylen = (int) sizeof( key );
  140. memcpy( key, argv[4], keylen );
  141. }
  142. }
  143. memset( argv[4], 0, strlen( argv[4] ) );
  144. #if defined(WIN32)
  145. /*
  146. * Support large files (> 2Gb) on Win32
  147. */
  148. li_size.QuadPart = 0;
  149. li_size.LowPart =
  150. SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
  151. li_size.LowPart, &li_size.HighPart, FILE_END );
  152. if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
  153. {
  154. fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
  155. goto exit;
  156. }
  157. filesize = li_size.QuadPart;
  158. #else
  159. if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
  160. {
  161. perror( "lseek" );
  162. goto exit;
  163. }
  164. #endif
  165. if( fseek( fin, 0, SEEK_SET ) < 0 )
  166. {
  167. fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
  168. goto exit;
  169. }
  170. if( mode == MODE_ENCRYPT )
  171. {
  172. /*
  173. * Generate the initialization vector as:
  174. * IV = SHA-256( filesize || filename )[0..15]
  175. */
  176. for( i = 0; i < 8; i++ )
  177. buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
  178. p = argv[2];
  179. sha2_starts( &sha_ctx, 0 );
  180. sha2_update( &sha_ctx, buffer, 8 );
  181. sha2_update( &sha_ctx, (unsigned char *) p, strlen( p ) );
  182. sha2_finish( &sha_ctx, digest );
  183. memcpy( IV, digest, 16 );
  184. /*
  185. * The last four bits in the IV are actually used
  186. * to store the file size modulo the AES block size.
  187. */
  188. lastn = (int)( filesize & 0x0F );
  189. IV[15] = (unsigned char)
  190. ( ( IV[15] & 0xF0 ) | lastn );
  191. /*
  192. * Append the IV at the beginning of the output.
  193. */
  194. if( fwrite( IV, 1, 16, fout ) != 16 )
  195. {
  196. fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  197. goto exit;
  198. }
  199. /*
  200. * Hash the IV and the secret key together 8192 times
  201. * using the result to setup the AES context and HMAC.
  202. */
  203. memset( digest, 0, 32 );
  204. memcpy( digest, IV, 16 );
  205. for( i = 0; i < 8192; i++ )
  206. {
  207. sha2_starts( &sha_ctx, 0 );
  208. sha2_update( &sha_ctx, digest, 32 );
  209. sha2_update( &sha_ctx, key, keylen );
  210. sha2_finish( &sha_ctx, digest );
  211. }
  212. memset( key, 0, sizeof( key ) );
  213. aes_setkey_enc( &aes_ctx, digest, 256 );
  214. sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
  215. /*
  216. * Encrypt and write the ciphertext.
  217. */
  218. for( offset = 0; offset < filesize; offset += 16 )
  219. {
  220. n = ( filesize - offset > 16 ) ? 16 : (int)
  221. ( filesize - offset );
  222. if( fread( buffer, 1, n, fin ) != (size_t) n )
  223. {
  224. fprintf( stderr, "fread(%d bytes) failed\n", n );
  225. goto exit;
  226. }
  227. for( i = 0; i < 16; i++ )
  228. buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
  229. aes_crypt_ecb( &aes_ctx, AES_ENCRYPT, buffer, buffer );
  230. sha2_hmac_update( &sha_ctx, buffer, 16 );
  231. if( fwrite( buffer, 1, 16, fout ) != 16 )
  232. {
  233. fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  234. goto exit;
  235. }
  236. memcpy( IV, buffer, 16 );
  237. }
  238. /*
  239. * Finally write the HMAC.
  240. */
  241. sha2_hmac_finish( &sha_ctx, digest );
  242. if( fwrite( digest, 1, 32, fout ) != 32 )
  243. {
  244. fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  245. goto exit;
  246. }
  247. }
  248. if( mode == MODE_DECRYPT )
  249. {
  250. unsigned char tmp[16];
  251. /*
  252. * The encrypted file must be structured as follows:
  253. *
  254. * 00 .. 15 Initialization Vector
  255. * 16 .. 31 AES Encrypted Block #1
  256. * ..
  257. * N*16 .. (N+1)*16 - 1 AES Encrypted Block #N
  258. * (N+1)*16 .. (N+1)*16 + 32 HMAC-SHA-256(ciphertext)
  259. */
  260. if( filesize < 48 )
  261. {
  262. fprintf( stderr, "File too short to be encrypted.\n" );
  263. goto exit;
  264. }
  265. if( ( filesize & 0x0F ) != 0 )
  266. {
  267. fprintf( stderr, "File size not a multiple of 16.\n" );
  268. goto exit;
  269. }
  270. /*
  271. * Substract the IV + HMAC length.
  272. */
  273. filesize -= ( 16 + 32 );
  274. /*
  275. * Read the IV and original filesize modulo 16.
  276. */
  277. if( fread( buffer, 1, 16, fin ) != 16 )
  278. {
  279. fprintf( stderr, "fread(%d bytes) failed\n", 16 );
  280. goto exit;
  281. }
  282. memcpy( IV, buffer, 16 );
  283. lastn = IV[15] & 0x0F;
  284. /*
  285. * Hash the IV and the secret key together 8192 times
  286. * using the result to setup the AES context and HMAC.
  287. */
  288. memset( digest, 0, 32 );
  289. memcpy( digest, IV, 16 );
  290. for( i = 0; i < 8192; i++ )
  291. {
  292. sha2_starts( &sha_ctx, 0 );
  293. sha2_update( &sha_ctx, digest, 32 );
  294. sha2_update( &sha_ctx, key, keylen );
  295. sha2_finish( &sha_ctx, digest );
  296. }
  297. memset( key, 0, sizeof( key ) );
  298. aes_setkey_dec( &aes_ctx, digest, 256 );
  299. sha2_hmac_starts( &sha_ctx, digest, 32, 0 );
  300. /*
  301. * Decrypt and write the plaintext.
  302. */
  303. for( offset = 0; offset < filesize; offset += 16 )
  304. {
  305. if( fread( buffer, 1, 16, fin ) != 16 )
  306. {
  307. fprintf( stderr, "fread(%d bytes) failed\n", 16 );
  308. goto exit;
  309. }
  310. memcpy( tmp, buffer, 16 );
  311. sha2_hmac_update( &sha_ctx, buffer, 16 );
  312. aes_crypt_ecb( &aes_ctx, AES_DECRYPT, buffer, buffer );
  313. for( i = 0; i < 16; i++ )
  314. buffer[i] = (unsigned char)( buffer[i] ^ IV[i] );
  315. memcpy( IV, tmp, 16 );
  316. n = ( lastn > 0 && offset == filesize - 16 )
  317. ? lastn : 16;
  318. if( fwrite( buffer, 1, n, fout ) != (size_t) n )
  319. {
  320. fprintf( stderr, "fwrite(%d bytes) failed\n", n );
  321. goto exit;
  322. }
  323. }
  324. /*
  325. * Verify the message authentication code.
  326. */
  327. sha2_hmac_finish( &sha_ctx, digest );
  328. if( fread( buffer, 1, 32, fin ) != 32 )
  329. {
  330. fprintf( stderr, "fread(%d bytes) failed\n", 32 );
  331. goto exit;
  332. }
  333. if( memcmp( digest, buffer, 32 ) != 0 )
  334. {
  335. fprintf( stderr, "HMAC check failed: wrong key, "
  336. "or file corrupted.\n" );
  337. goto exit;
  338. }
  339. }
  340. ret = 0;
  341. exit:
  342. if( fin )
  343. fclose( fin );
  344. if( fout )
  345. fclose( fout );
  346. memset( buffer, 0, sizeof( buffer ) );
  347. memset( digest, 0, sizeof( digest ) );
  348. memset( &aes_ctx, 0, sizeof( aes_context ) );
  349. memset( &sha_ctx, 0, sizeof( sha2_context ) );
  350. return( ret );
  351. }
  352. #endif /* POLARSSL_AES_C && POLARSSL_SHA2_C */