crypt_and_hash.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551
  1. /*
  2. * \brief Generic file encryption program using generic wrappers for configured
  3. * security.
  4. *
  5. * Copyright (C) 2006-2016, ARM Limited, All Rights Reserved
  6. * SPDX-License-Identifier: Apache-2.0
  7. *
  8. * Licensed under the Apache License, Version 2.0 (the "License"); you may
  9. * not use this file except in compliance with the License.
  10. * You may obtain a copy of the License at
  11. *
  12. * http://www.apache.org/licenses/LICENSE-2.0
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
  16. * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. *
  20. * This file is part of mbed TLS (https://tls.mbed.org)
  21. */
  22. #if !defined(MBEDTLS_CONFIG_FILE)
  23. #include "mbedtls/config.h"
  24. #else
  25. #include MBEDTLS_CONFIG_FILE
  26. #endif
  27. #if defined(MBEDTLS_PLATFORM_C)
  28. #include "mbedtls/platform.h"
  29. #else
  30. #include <stdio.h>
  31. #define mbedtls_fprintf fprintf
  32. #define mbedtls_printf printf
  33. #endif
  34. #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
  35. defined(MBEDTLS_FS_IO)
  36. #include "mbedtls/cipher.h"
  37. #include "mbedtls/md.h"
  38. #include <stdio.h>
  39. #include <stdlib.h>
  40. #include <string.h>
  41. #endif
  42. #if defined(_WIN32)
  43. #include <windows.h>
  44. #if !defined(_WIN32_WCE)
  45. #include <io.h>
  46. #endif
  47. #else
  48. #include <sys/types.h>
  49. #include <unistd.h>
  50. #endif
  51. #define MODE_ENCRYPT 0
  52. #define MODE_DECRYPT 1
  53. #define USAGE \
  54. "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
  55. "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
  56. "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
  57. "\n"
  58. #if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
  59. !defined(MBEDTLS_FS_IO)
  60. int main( void )
  61. {
  62. mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
  63. return( 0 );
  64. }
  65. #else
  66. int main( int argc, char *argv[] )
  67. {
  68. int ret = 1, i, n;
  69. int mode;
  70. size_t keylen, ilen, olen;
  71. FILE *fkey, *fin = NULL, *fout = NULL;
  72. char *p;
  73. unsigned char IV[16];
  74. unsigned char key[512];
  75. unsigned char digest[MBEDTLS_MD_MAX_SIZE];
  76. unsigned char buffer[1024];
  77. unsigned char output[1024];
  78. unsigned char diff;
  79. const mbedtls_cipher_info_t *cipher_info;
  80. const mbedtls_md_info_t *md_info;
  81. mbedtls_cipher_context_t cipher_ctx;
  82. mbedtls_md_context_t md_ctx;
  83. #if defined(_WIN32_WCE)
  84. long filesize, offset;
  85. #elif defined(_WIN32)
  86. LARGE_INTEGER li_size;
  87. __int64 filesize, offset;
  88. #else
  89. off_t filesize, offset;
  90. #endif
  91. mbedtls_cipher_init( &cipher_ctx );
  92. mbedtls_md_init( &md_ctx );
  93. /*
  94. * Parse the command-line arguments.
  95. */
  96. if( argc != 7 )
  97. {
  98. const int *list;
  99. mbedtls_printf( USAGE );
  100. mbedtls_printf( "Available ciphers:\n" );
  101. list = mbedtls_cipher_list();
  102. while( *list )
  103. {
  104. cipher_info = mbedtls_cipher_info_from_type( *list );
  105. mbedtls_printf( " %s\n", cipher_info->name );
  106. list++;
  107. }
  108. mbedtls_printf( "\nAvailable message digests:\n" );
  109. list = mbedtls_md_list();
  110. while( *list )
  111. {
  112. md_info = mbedtls_md_info_from_type( *list );
  113. mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
  114. list++;
  115. }
  116. #if defined(_WIN32)
  117. mbedtls_printf( "\n Press Enter to exit this program.\n" );
  118. fflush( stdout ); getchar();
  119. #endif
  120. goto exit;
  121. }
  122. mode = atoi( argv[1] );
  123. if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
  124. {
  125. mbedtls_fprintf( stderr, "invalid operation mode\n" );
  126. goto exit;
  127. }
  128. if( strcmp( argv[2], argv[3] ) == 0 )
  129. {
  130. mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
  131. goto exit;
  132. }
  133. if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
  134. {
  135. mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
  136. goto exit;
  137. }
  138. if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
  139. {
  140. mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
  141. goto exit;
  142. }
  143. /*
  144. * Read the Cipher and MD from the command line
  145. */
  146. cipher_info = mbedtls_cipher_info_from_string( argv[4] );
  147. if( cipher_info == NULL )
  148. {
  149. mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
  150. goto exit;
  151. }
  152. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
  153. {
  154. mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
  155. goto exit;
  156. }
  157. md_info = mbedtls_md_info_from_string( argv[5] );
  158. if( md_info == NULL )
  159. {
  160. mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
  161. goto exit;
  162. }
  163. if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
  164. {
  165. mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
  166. goto exit;
  167. }
  168. /*
  169. * Read the secret key and clean the command line.
  170. */
  171. if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
  172. {
  173. keylen = fread( key, 1, sizeof( key ), fkey );
  174. fclose( fkey );
  175. }
  176. else
  177. {
  178. if( memcmp( argv[6], "hex:", 4 ) == 0 )
  179. {
  180. p = &argv[6][4];
  181. keylen = 0;
  182. while( sscanf( p, "%02X", &n ) > 0 &&
  183. keylen < (int) sizeof( key ) )
  184. {
  185. key[keylen++] = (unsigned char) n;
  186. p += 2;
  187. }
  188. }
  189. else
  190. {
  191. keylen = strlen( argv[6] );
  192. if( keylen > (int) sizeof( key ) )
  193. keylen = (int) sizeof( key );
  194. memcpy( key, argv[6], keylen );
  195. }
  196. }
  197. memset( argv[6], 0, strlen( argv[6] ) );
  198. #if defined(_WIN32_WCE)
  199. filesize = fseek( fin, 0L, SEEK_END );
  200. #else
  201. #if defined(_WIN32)
  202. /*
  203. * Support large files (> 2Gb) on Win32
  204. */
  205. li_size.QuadPart = 0;
  206. li_size.LowPart =
  207. SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
  208. li_size.LowPart, &li_size.HighPart, FILE_END );
  209. if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
  210. {
  211. mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
  212. goto exit;
  213. }
  214. filesize = li_size.QuadPart;
  215. #else
  216. if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
  217. {
  218. perror( "lseek" );
  219. goto exit;
  220. }
  221. #endif
  222. #endif
  223. if( fseek( fin, 0, SEEK_SET ) < 0 )
  224. {
  225. mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
  226. goto exit;
  227. }
  228. if( mode == MODE_ENCRYPT )
  229. {
  230. /*
  231. * Generate the initialization vector as:
  232. * IV = MD( filesize || filename )[0..15]
  233. */
  234. for( i = 0; i < 8; i++ )
  235. buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
  236. p = argv[2];
  237. mbedtls_md_starts( &md_ctx );
  238. mbedtls_md_update( &md_ctx, buffer, 8 );
  239. mbedtls_md_update( &md_ctx, (unsigned char *) p, strlen( p ) );
  240. mbedtls_md_finish( &md_ctx, digest );
  241. memcpy( IV, digest, 16 );
  242. /*
  243. * Append the IV at the beginning of the output.
  244. */
  245. if( fwrite( IV, 1, 16, fout ) != 16 )
  246. {
  247. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  248. goto exit;
  249. }
  250. /*
  251. * Hash the IV and the secret key together 8192 times
  252. * using the result to setup the AES context and HMAC.
  253. */
  254. memset( digest, 0, 32 );
  255. memcpy( digest, IV, 16 );
  256. for( i = 0; i < 8192; i++ )
  257. {
  258. mbedtls_md_starts( &md_ctx );
  259. mbedtls_md_update( &md_ctx, digest, 32 );
  260. mbedtls_md_update( &md_ctx, key, keylen );
  261. mbedtls_md_finish( &md_ctx, digest );
  262. }
  263. memset( key, 0, sizeof( key ) );
  264. if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
  265. MBEDTLS_ENCRYPT ) != 0 )
  266. {
  267. mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
  268. goto exit;
  269. }
  270. if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
  271. {
  272. mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
  273. goto exit;
  274. }
  275. if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
  276. {
  277. mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
  278. goto exit;
  279. }
  280. mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
  281. /*
  282. * Encrypt and write the ciphertext.
  283. */
  284. for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
  285. {
  286. ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
  287. mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
  288. if( fread( buffer, 1, ilen, fin ) != ilen )
  289. {
  290. mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
  291. goto exit;
  292. }
  293. if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
  294. {
  295. mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
  296. goto exit;
  297. }
  298. mbedtls_md_hmac_update( &md_ctx, output, olen );
  299. if( fwrite( output, 1, olen, fout ) != olen )
  300. {
  301. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  302. goto exit;
  303. }
  304. }
  305. if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
  306. {
  307. mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
  308. goto exit;
  309. }
  310. mbedtls_md_hmac_update( &md_ctx, output, olen );
  311. if( fwrite( output, 1, olen, fout ) != olen )
  312. {
  313. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  314. goto exit;
  315. }
  316. /*
  317. * Finally write the HMAC.
  318. */
  319. mbedtls_md_hmac_finish( &md_ctx, digest );
  320. if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
  321. {
  322. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
  323. goto exit;
  324. }
  325. }
  326. if( mode == MODE_DECRYPT )
  327. {
  328. /*
  329. * The encrypted file must be structured as follows:
  330. *
  331. * 00 .. 15 Initialization Vector
  332. * 16 .. 31 Encrypted Block #1
  333. * ..
  334. * N*16 .. (N+1)*16 - 1 Encrypted Block #N
  335. * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
  336. */
  337. if( filesize < 16 + mbedtls_md_get_size( md_info ) )
  338. {
  339. mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
  340. goto exit;
  341. }
  342. if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
  343. {
  344. mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
  345. goto exit;
  346. }
  347. /*
  348. * Check the file size.
  349. */
  350. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  351. ( ( filesize - mbedtls_md_get_size( md_info ) ) %
  352. mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
  353. {
  354. mbedtls_fprintf( stderr, "File content not a multiple of the block size (%d).\n",
  355. mbedtls_cipher_get_block_size( &cipher_ctx ));
  356. goto exit;
  357. }
  358. /*
  359. * Subtract the IV + HMAC length.
  360. */
  361. filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
  362. /*
  363. * Read the IV and original filesize modulo 16.
  364. */
  365. if( fread( buffer, 1, 16, fin ) != 16 )
  366. {
  367. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
  368. goto exit;
  369. }
  370. memcpy( IV, buffer, 16 );
  371. /*
  372. * Hash the IV and the secret key together 8192 times
  373. * using the result to setup the AES context and HMAC.
  374. */
  375. memset( digest, 0, 32 );
  376. memcpy( digest, IV, 16 );
  377. for( i = 0; i < 8192; i++ )
  378. {
  379. mbedtls_md_starts( &md_ctx );
  380. mbedtls_md_update( &md_ctx, digest, 32 );
  381. mbedtls_md_update( &md_ctx, key, keylen );
  382. mbedtls_md_finish( &md_ctx, digest );
  383. }
  384. memset( key, 0, sizeof( key ) );
  385. if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
  386. MBEDTLS_DECRYPT ) != 0 )
  387. {
  388. mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
  389. goto exit;
  390. }
  391. if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
  392. {
  393. mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
  394. goto exit;
  395. }
  396. if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
  397. {
  398. mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
  399. goto exit;
  400. }
  401. mbedtls_md_hmac_starts( &md_ctx, digest, 32 );
  402. /*
  403. * Decrypt and write the plaintext.
  404. */
  405. for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
  406. {
  407. ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
  408. mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
  409. if( fread( buffer, 1, ilen, fin ) != ilen )
  410. {
  411. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n",
  412. mbedtls_cipher_get_block_size( &cipher_ctx ) );
  413. goto exit;
  414. }
  415. mbedtls_md_hmac_update( &md_ctx, buffer, ilen );
  416. if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
  417. &olen ) != 0 )
  418. {
  419. mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
  420. goto exit;
  421. }
  422. if( fwrite( output, 1, olen, fout ) != olen )
  423. {
  424. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  425. goto exit;
  426. }
  427. }
  428. /*
  429. * Verify the message authentication code.
  430. */
  431. mbedtls_md_hmac_finish( &md_ctx, digest );
  432. if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
  433. {
  434. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
  435. goto exit;
  436. }
  437. /* Use constant-time buffer comparison */
  438. diff = 0;
  439. for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
  440. diff |= digest[i] ^ buffer[i];
  441. if( diff != 0 )
  442. {
  443. mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
  444. "or file corrupted.\n" );
  445. goto exit;
  446. }
  447. /*
  448. * Write the final block of data
  449. */
  450. mbedtls_cipher_finish( &cipher_ctx, output, &olen );
  451. if( fwrite( output, 1, olen, fout ) != olen )
  452. {
  453. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  454. goto exit;
  455. }
  456. }
  457. ret = 0;
  458. exit:
  459. if( fin )
  460. fclose( fin );
  461. if( fout )
  462. fclose( fout );
  463. memset( buffer, 0, sizeof( buffer ) );
  464. memset( digest, 0, sizeof( digest ) );
  465. mbedtls_cipher_free( &cipher_ctx );
  466. mbedtls_md_free( &md_ctx );
  467. return( ret );
  468. }
  469. #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */