crypt_and_hash.c 13 KB

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