crypt_and_hash.c 13 KB

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