aescrypt2.c 11 KB

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