crypt_and_hash.c 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651
  1. /*
  2. * \brief Generic file encryption program using generic wrappers for configured
  3. * security.
  4. *
  5. * Copyright The Mbed TLS Contributors
  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. /* Enable definition of fileno() even when compiling with -std=c99. Must be
  21. * set before config.h, which pulls in glibc's features.h indirectly.
  22. * Harmless on other platforms. */
  23. #define _POSIX_C_SOURCE 200112L
  24. #if !defined(MBEDTLS_CONFIG_FILE)
  25. #include "mbedtls/config.h"
  26. #else
  27. #include MBEDTLS_CONFIG_FILE
  28. #endif
  29. #if defined(MBEDTLS_PLATFORM_C)
  30. #include "mbedtls/platform.h"
  31. #else
  32. #include <stdio.h>
  33. #include <stdlib.h>
  34. #define mbedtls_fprintf fprintf
  35. #define mbedtls_printf printf
  36. #define mbedtls_exit exit
  37. #define MBEDTLS_EXIT_SUCCESS EXIT_SUCCESS
  38. #define MBEDTLS_EXIT_FAILURE EXIT_FAILURE
  39. #endif /* MBEDTLS_PLATFORM_C */
  40. #if defined(MBEDTLS_CIPHER_C) && defined(MBEDTLS_MD_C) && \
  41. defined(MBEDTLS_FS_IO)
  42. #include "mbedtls/cipher.h"
  43. #include "mbedtls/md.h"
  44. #include "mbedtls/platform_util.h"
  45. #include <stdio.h>
  46. #include <stdlib.h>
  47. #include <string.h>
  48. #endif
  49. #if defined(_WIN32)
  50. #include <windows.h>
  51. #if !defined(_WIN32_WCE)
  52. #include <io.h>
  53. #endif
  54. #else
  55. #include <sys/types.h>
  56. #include <unistd.h>
  57. #endif
  58. #define MODE_ENCRYPT 0
  59. #define MODE_DECRYPT 1
  60. #define USAGE \
  61. "\n crypt_and_hash <mode> <input filename> <output filename> <cipher> <mbedtls_md> <key>\n" \
  62. "\n <mode>: 0 = encrypt, 1 = decrypt\n" \
  63. "\n example: crypt_and_hash 0 file file.aes AES-128-CBC SHA1 hex:E76B2413958B00E193\n" \
  64. "\n"
  65. #if !defined(MBEDTLS_CIPHER_C) || !defined(MBEDTLS_MD_C) || \
  66. !defined(MBEDTLS_FS_IO)
  67. int main( void )
  68. {
  69. mbedtls_printf("MBEDTLS_CIPHER_C and/or MBEDTLS_MD_C and/or MBEDTLS_FS_IO not defined.\n");
  70. mbedtls_exit( 0 );
  71. }
  72. #else
  73. int main( int argc, char *argv[] )
  74. {
  75. int ret = 1, i;
  76. unsigned n;
  77. int exit_code = MBEDTLS_EXIT_FAILURE;
  78. int mode;
  79. size_t keylen, ilen, olen;
  80. FILE *fkey, *fin = NULL, *fout = NULL;
  81. char *p;
  82. unsigned char IV[16];
  83. unsigned char key[512];
  84. unsigned char digest[MBEDTLS_MD_MAX_SIZE];
  85. unsigned char buffer[1024];
  86. unsigned char output[1024];
  87. unsigned char diff;
  88. const mbedtls_cipher_info_t *cipher_info;
  89. const mbedtls_md_info_t *md_info;
  90. mbedtls_cipher_context_t cipher_ctx;
  91. mbedtls_md_context_t md_ctx;
  92. #if defined(_WIN32_WCE)
  93. long filesize, offset;
  94. #elif defined(_WIN32)
  95. LARGE_INTEGER li_size;
  96. __int64 filesize, offset;
  97. #else
  98. off_t filesize, offset;
  99. #endif
  100. mbedtls_cipher_init( &cipher_ctx );
  101. mbedtls_md_init( &md_ctx );
  102. /*
  103. * Parse the command-line arguments.
  104. */
  105. if( argc != 7 )
  106. {
  107. const int *list;
  108. mbedtls_printf( USAGE );
  109. mbedtls_printf( "Available ciphers:\n" );
  110. list = mbedtls_cipher_list();
  111. while( *list )
  112. {
  113. cipher_info = mbedtls_cipher_info_from_type( *list );
  114. mbedtls_printf( " %s\n", cipher_info->name );
  115. list++;
  116. }
  117. mbedtls_printf( "\nAvailable message digests:\n" );
  118. list = mbedtls_md_list();
  119. while( *list )
  120. {
  121. md_info = mbedtls_md_info_from_type( *list );
  122. mbedtls_printf( " %s\n", mbedtls_md_get_name( md_info ) );
  123. list++;
  124. }
  125. #if defined(_WIN32)
  126. mbedtls_printf( "\n Press Enter to exit this program.\n" );
  127. fflush( stdout ); getchar();
  128. #endif
  129. goto exit;
  130. }
  131. mode = atoi( argv[1] );
  132. if( mode != MODE_ENCRYPT && mode != MODE_DECRYPT )
  133. {
  134. mbedtls_fprintf( stderr, "invalid operation mode\n" );
  135. goto exit;
  136. }
  137. if( strcmp( argv[2], argv[3] ) == 0 )
  138. {
  139. mbedtls_fprintf( stderr, "input and output filenames must differ\n" );
  140. goto exit;
  141. }
  142. if( ( fin = fopen( argv[2], "rb" ) ) == NULL )
  143. {
  144. mbedtls_fprintf( stderr, "fopen(%s,rb) failed\n", argv[2] );
  145. goto exit;
  146. }
  147. if( ( fout = fopen( argv[3], "wb+" ) ) == NULL )
  148. {
  149. mbedtls_fprintf( stderr, "fopen(%s,wb+) failed\n", argv[3] );
  150. goto exit;
  151. }
  152. /*
  153. * Read the Cipher and MD from the command line
  154. */
  155. cipher_info = mbedtls_cipher_info_from_string( argv[4] );
  156. if( cipher_info == NULL )
  157. {
  158. mbedtls_fprintf( stderr, "Cipher '%s' not found\n", argv[4] );
  159. goto exit;
  160. }
  161. if( ( ret = mbedtls_cipher_setup( &cipher_ctx, cipher_info) ) != 0 )
  162. {
  163. mbedtls_fprintf( stderr, "mbedtls_cipher_setup failed\n" );
  164. goto exit;
  165. }
  166. md_info = mbedtls_md_info_from_string( argv[5] );
  167. if( md_info == NULL )
  168. {
  169. mbedtls_fprintf( stderr, "Message Digest '%s' not found\n", argv[5] );
  170. goto exit;
  171. }
  172. if( mbedtls_md_setup( &md_ctx, md_info, 1 ) != 0 )
  173. {
  174. mbedtls_fprintf( stderr, "mbedtls_md_setup failed\n" );
  175. goto exit;
  176. }
  177. /*
  178. * Read the secret key from file or command line
  179. */
  180. if( ( fkey = fopen( argv[6], "rb" ) ) != NULL )
  181. {
  182. keylen = fread( key, 1, sizeof( key ), fkey );
  183. fclose( fkey );
  184. }
  185. else
  186. {
  187. if( memcmp( argv[6], "hex:", 4 ) == 0 )
  188. {
  189. p = &argv[6][4];
  190. keylen = 0;
  191. while( sscanf( p, "%02X", (unsigned int*) &n ) > 0 &&
  192. keylen < (int) sizeof( key ) )
  193. {
  194. key[keylen++] = (unsigned char) n;
  195. p += 2;
  196. }
  197. }
  198. else
  199. {
  200. keylen = strlen( argv[6] );
  201. if( keylen > (int) sizeof( key ) )
  202. keylen = (int) sizeof( key );
  203. memcpy( key, argv[6], keylen );
  204. }
  205. }
  206. #if defined(_WIN32_WCE)
  207. filesize = fseek( fin, 0L, SEEK_END );
  208. #else
  209. #if defined(_WIN32)
  210. /*
  211. * Support large files (> 2Gb) on Win32
  212. */
  213. li_size.QuadPart = 0;
  214. li_size.LowPart =
  215. SetFilePointer( (HANDLE) _get_osfhandle( _fileno( fin ) ),
  216. li_size.LowPart, &li_size.HighPart, FILE_END );
  217. if( li_size.LowPart == 0xFFFFFFFF && GetLastError() != NO_ERROR )
  218. {
  219. mbedtls_fprintf( stderr, "SetFilePointer(0,FILE_END) failed\n" );
  220. goto exit;
  221. }
  222. filesize = li_size.QuadPart;
  223. #else
  224. if( ( filesize = lseek( fileno( fin ), 0, SEEK_END ) ) < 0 )
  225. {
  226. perror( "lseek" );
  227. goto exit;
  228. }
  229. #endif
  230. #endif
  231. if( fseek( fin, 0, SEEK_SET ) < 0 )
  232. {
  233. mbedtls_fprintf( stderr, "fseek(0,SEEK_SET) failed\n" );
  234. goto exit;
  235. }
  236. if( mode == MODE_ENCRYPT )
  237. {
  238. /*
  239. * Generate the initialization vector as:
  240. * IV = MD( filesize || filename )[0..15]
  241. */
  242. for( i = 0; i < 8; i++ )
  243. buffer[i] = (unsigned char)( filesize >> ( i << 3 ) );
  244. p = argv[2];
  245. if( mbedtls_md_starts( &md_ctx ) != 0 )
  246. {
  247. mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
  248. goto exit;
  249. }
  250. if( mbedtls_md_update( &md_ctx, buffer, 8 ) != 0 )
  251. {
  252. mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
  253. goto exit;
  254. }
  255. if( mbedtls_md_update( &md_ctx, ( unsigned char * ) p, strlen( p ) )
  256. != 0 )
  257. {
  258. mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
  259. goto exit;
  260. }
  261. if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
  262. {
  263. mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
  264. goto exit;
  265. }
  266. memcpy( IV, digest, 16 );
  267. /*
  268. * Append the IV at the beginning of the output.
  269. */
  270. if( fwrite( IV, 1, 16, fout ) != 16 )
  271. {
  272. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", 16 );
  273. goto exit;
  274. }
  275. /*
  276. * Hash the IV and the secret key together 8192 times
  277. * using the result to setup the AES context and HMAC.
  278. */
  279. memset( digest, 0, 32 );
  280. memcpy( digest, IV, 16 );
  281. for( i = 0; i < 8192; i++ )
  282. {
  283. if( mbedtls_md_starts( &md_ctx ) != 0 )
  284. {
  285. mbedtls_fprintf( stderr,
  286. "mbedtls_md_starts() returned error\n" );
  287. goto exit;
  288. }
  289. if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
  290. {
  291. mbedtls_fprintf( stderr,
  292. "mbedtls_md_update() returned error\n" );
  293. goto exit;
  294. }
  295. if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
  296. {
  297. mbedtls_fprintf( stderr,
  298. "mbedtls_md_update() returned error\n" );
  299. goto exit;
  300. }
  301. if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
  302. {
  303. mbedtls_fprintf( stderr,
  304. "mbedtls_md_finish() returned error\n" );
  305. goto exit;
  306. }
  307. }
  308. if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
  309. MBEDTLS_ENCRYPT ) != 0 )
  310. {
  311. mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n");
  312. goto exit;
  313. }
  314. if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
  315. {
  316. mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n");
  317. goto exit;
  318. }
  319. if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
  320. {
  321. mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n");
  322. goto exit;
  323. }
  324. if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
  325. {
  326. mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
  327. goto exit;
  328. }
  329. /*
  330. * Encrypt and write the ciphertext.
  331. */
  332. for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
  333. {
  334. ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
  335. mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
  336. if( fread( buffer, 1, ilen, fin ) != ilen )
  337. {
  338. mbedtls_fprintf( stderr, "fread(%ld bytes) failed\n", (long) ilen );
  339. goto exit;
  340. }
  341. if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output, &olen ) != 0 )
  342. {
  343. mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n");
  344. goto exit;
  345. }
  346. if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
  347. {
  348. mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
  349. goto exit;
  350. }
  351. if( fwrite( output, 1, olen, fout ) != olen )
  352. {
  353. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  354. goto exit;
  355. }
  356. }
  357. if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
  358. {
  359. mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
  360. goto exit;
  361. }
  362. if( mbedtls_md_hmac_update( &md_ctx, output, olen ) != 0 )
  363. {
  364. mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
  365. goto exit;
  366. }
  367. if( fwrite( output, 1, olen, fout ) != olen )
  368. {
  369. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  370. goto exit;
  371. }
  372. /*
  373. * Finally write the HMAC.
  374. */
  375. if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
  376. {
  377. mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
  378. goto exit;
  379. }
  380. if( fwrite( digest, 1, mbedtls_md_get_size( md_info ), fout ) != mbedtls_md_get_size( md_info ) )
  381. {
  382. mbedtls_fprintf( stderr, "fwrite(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
  383. goto exit;
  384. }
  385. }
  386. if( mode == MODE_DECRYPT )
  387. {
  388. /*
  389. * The encrypted file must be structured as follows:
  390. *
  391. * 00 .. 15 Initialization Vector
  392. * 16 .. 31 Encrypted Block #1
  393. * ..
  394. * N*16 .. (N+1)*16 - 1 Encrypted Block #N
  395. * (N+1)*16 .. (N+1)*16 + n Hash(ciphertext)
  396. */
  397. if( filesize < 16 + mbedtls_md_get_size( md_info ) )
  398. {
  399. mbedtls_fprintf( stderr, "File too short to be encrypted.\n" );
  400. goto exit;
  401. }
  402. if( mbedtls_cipher_get_block_size( &cipher_ctx ) == 0 )
  403. {
  404. mbedtls_fprintf( stderr, "Invalid cipher block size: 0. \n" );
  405. goto exit;
  406. }
  407. /*
  408. * Check the file size.
  409. */
  410. if( cipher_info->mode != MBEDTLS_MODE_GCM &&
  411. ( ( filesize - mbedtls_md_get_size( md_info ) ) %
  412. mbedtls_cipher_get_block_size( &cipher_ctx ) ) != 0 )
  413. {
  414. mbedtls_fprintf( stderr, "File content not a multiple of the block size (%u).\n",
  415. mbedtls_cipher_get_block_size( &cipher_ctx ));
  416. goto exit;
  417. }
  418. /*
  419. * Subtract the IV + HMAC length.
  420. */
  421. filesize -= ( 16 + mbedtls_md_get_size( md_info ) );
  422. /*
  423. * Read the IV and original filesize modulo 16.
  424. */
  425. if( fread( buffer, 1, 16, fin ) != 16 )
  426. {
  427. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", 16 );
  428. goto exit;
  429. }
  430. memcpy( IV, buffer, 16 );
  431. /*
  432. * Hash the IV and the secret key together 8192 times
  433. * using the result to setup the AES context and HMAC.
  434. */
  435. memset( digest, 0, 32 );
  436. memcpy( digest, IV, 16 );
  437. for( i = 0; i < 8192; i++ )
  438. {
  439. if( mbedtls_md_starts( &md_ctx ) != 0 )
  440. {
  441. mbedtls_fprintf( stderr, "mbedtls_md_starts() returned error\n" );
  442. goto exit;
  443. }
  444. if( mbedtls_md_update( &md_ctx, digest, 32 ) != 0 )
  445. {
  446. mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
  447. goto exit;
  448. }
  449. if( mbedtls_md_update( &md_ctx, key, keylen ) != 0 )
  450. {
  451. mbedtls_fprintf( stderr, "mbedtls_md_update() returned error\n" );
  452. goto exit;
  453. }
  454. if( mbedtls_md_finish( &md_ctx, digest ) != 0 )
  455. {
  456. mbedtls_fprintf( stderr, "mbedtls_md_finish() returned error\n" );
  457. goto exit;
  458. }
  459. }
  460. if( mbedtls_cipher_setkey( &cipher_ctx, digest, cipher_info->key_bitlen,
  461. MBEDTLS_DECRYPT ) != 0 )
  462. {
  463. mbedtls_fprintf( stderr, "mbedtls_cipher_setkey() returned error\n" );
  464. goto exit;
  465. }
  466. if( mbedtls_cipher_set_iv( &cipher_ctx, IV, 16 ) != 0 )
  467. {
  468. mbedtls_fprintf( stderr, "mbedtls_cipher_set_iv() returned error\n" );
  469. goto exit;
  470. }
  471. if( mbedtls_cipher_reset( &cipher_ctx ) != 0 )
  472. {
  473. mbedtls_fprintf( stderr, "mbedtls_cipher_reset() returned error\n" );
  474. goto exit;
  475. }
  476. if( mbedtls_md_hmac_starts( &md_ctx, digest, 32 ) != 0 )
  477. {
  478. mbedtls_fprintf( stderr, "mbedtls_md_hmac_starts() returned error\n" );
  479. goto exit;
  480. }
  481. /*
  482. * Decrypt and write the plaintext.
  483. */
  484. for( offset = 0; offset < filesize; offset += mbedtls_cipher_get_block_size( &cipher_ctx ) )
  485. {
  486. ilen = ( (unsigned int) filesize - offset > mbedtls_cipher_get_block_size( &cipher_ctx ) ) ?
  487. mbedtls_cipher_get_block_size( &cipher_ctx ) : (unsigned int) ( filesize - offset );
  488. if( fread( buffer, 1, ilen, fin ) != ilen )
  489. {
  490. mbedtls_fprintf( stderr, "fread(%u bytes) failed\n",
  491. mbedtls_cipher_get_block_size( &cipher_ctx ) );
  492. goto exit;
  493. }
  494. if( mbedtls_md_hmac_update( &md_ctx, buffer, ilen ) != 0 )
  495. {
  496. mbedtls_fprintf( stderr, "mbedtls_md_hmac_update() returned error\n" );
  497. goto exit;
  498. }
  499. if( mbedtls_cipher_update( &cipher_ctx, buffer, ilen, output,
  500. &olen ) != 0 )
  501. {
  502. mbedtls_fprintf( stderr, "mbedtls_cipher_update() returned error\n" );
  503. goto exit;
  504. }
  505. if( fwrite( output, 1, olen, fout ) != olen )
  506. {
  507. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  508. goto exit;
  509. }
  510. }
  511. /*
  512. * Verify the message authentication code.
  513. */
  514. if( mbedtls_md_hmac_finish( &md_ctx, digest ) != 0 )
  515. {
  516. mbedtls_fprintf( stderr, "mbedtls_md_hmac_finish() returned error\n" );
  517. goto exit;
  518. }
  519. if( fread( buffer, 1, mbedtls_md_get_size( md_info ), fin ) != mbedtls_md_get_size( md_info ) )
  520. {
  521. mbedtls_fprintf( stderr, "fread(%d bytes) failed\n", mbedtls_md_get_size( md_info ) );
  522. goto exit;
  523. }
  524. /* Use constant-time buffer comparison */
  525. diff = 0;
  526. for( i = 0; i < mbedtls_md_get_size( md_info ); i++ )
  527. diff |= digest[i] ^ buffer[i];
  528. if( diff != 0 )
  529. {
  530. mbedtls_fprintf( stderr, "HMAC check failed: wrong key, "
  531. "or file corrupted.\n" );
  532. goto exit;
  533. }
  534. /*
  535. * Write the final block of data
  536. */
  537. if( mbedtls_cipher_finish( &cipher_ctx, output, &olen ) != 0 )
  538. {
  539. mbedtls_fprintf( stderr, "mbedtls_cipher_finish() returned error\n" );
  540. goto exit;
  541. }
  542. if( fwrite( output, 1, olen, fout ) != olen )
  543. {
  544. mbedtls_fprintf( stderr, "fwrite(%ld bytes) failed\n", (long) olen );
  545. goto exit;
  546. }
  547. }
  548. exit_code = MBEDTLS_EXIT_SUCCESS;
  549. exit:
  550. if( fin )
  551. fclose( fin );
  552. if( fout )
  553. fclose( fout );
  554. /* Zeroize all command line arguments to also cover
  555. the case when the user has missed or reordered some,
  556. in which case the key might not be in argv[6]. */
  557. for( i = 0; i < argc; i++ )
  558. mbedtls_platform_zeroize( argv[i], strlen( argv[i] ) );
  559. mbedtls_platform_zeroize( IV, sizeof( IV ) );
  560. mbedtls_platform_zeroize( key, sizeof( key ) );
  561. mbedtls_platform_zeroize( buffer, sizeof( buffer ) );
  562. mbedtls_platform_zeroize( output, sizeof( output ) );
  563. mbedtls_platform_zeroize( digest, sizeof( digest ) );
  564. mbedtls_cipher_free( &cipher_ctx );
  565. mbedtls_md_free( &md_ctx );
  566. mbedtls_exit( exit_code );
  567. }
  568. #endif /* MBEDTLS_CIPHER_C && MBEDTLS_MD_C && MBEDTLS_FS_IO */