55#include <time.h>
66#include <stdint.h>
77
8- size_t encrypt_file (const char * input_file , const char * output_file , const char * key , const char * iv , size_t buffer_size ) {
8+ size_t encrypt_file (const char * input_file , const char * output_file , const uint8_t * key , const uint8_t * iv , size_t buffer_size ) {
99 FILE * in = fopen (input_file , "rb" );
1010 if (in == NULL ) {
1111 printf ("Error: Could not open input file\n" );
@@ -18,13 +18,6 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
1818 return 0 ;
1919 }
2020
21- if (strlen (key ) != 32 || strlen (iv ) != 16 ) {
22- printf ("Error: Key and IV must be 32 and 16 characters long respectively\n" );
23- fclose (in );
24- fclose (out );
25- return 0 ;
26- }
27-
2821 if (buffer_size % 16 != 0 ) {
2922 printf ("Error: Buffer size must be a multiple of 16\n" );
3023 fclose (in );
@@ -51,13 +44,8 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
5144
5245 uint8_t * tag = (uint8_t * )malloc (16 );
5346
54- uint8_t key_data [32 ];
55- uint8_t iv_data [16 ];
56- memcpy (key_data , key , 32 );
57- memcpy (iv_data , iv , 16 );
58-
5947 DATA128b state [16 ];
60- HiAE_stream_init (state , key_data , iv_data );
48+ HiAE_stream_init (state , key , iv );
6149 fseek (in , 0 , SEEK_END );
6250 size_t total_size = ftell (in );
6351 fseek (in , 0 , SEEK_SET );
@@ -86,7 +74,7 @@ size_t encrypt_file(const char* input_file, const char* output_file, const char*
8674 return total_size ;
8775}
8876
89- size_t decrypt_file (const char * input_file , const char * output_file , const char * key , const char * iv , size_t buffer_size ) {
77+ size_t decrypt_file (const char * input_file , const char * output_file , const uint8_t * key , const uint8_t * iv , size_t buffer_size ) {
9078 FILE * in = fopen (input_file , "rb" );
9179 if (in == NULL ) {
9280 printf ("Error: Could not open input file\n" );
@@ -130,13 +118,8 @@ size_t decrypt_file(const char* input_file, const char* output_file, const char*
130118
131119 fseek (in , 0 , SEEK_SET );
132120
133- uint8_t key_data [32 ];
134- uint8_t iv_data [16 ];
135- memcpy (key_data , key , 32 );
136- memcpy (iv_data , iv , 16 );
137-
138121 DATA128b state [16 ];
139- HiAE_stream_init (state , key_data , iv_data );
122+ HiAE_stream_init (state , key , iv );
140123
141124 //read in[0, -16] into buffer, decrypt, write to out, dont read last 16 bytes
142125 fseek (in , 0 , SEEK_END );
@@ -193,15 +176,26 @@ int main(int argc, char** argv) {
193176 }
194177
195178 // check key and iv length
196- if (strlen (argv [4 ]) != 32 ) {
197- printf ("Error: Key must be 32 characters\n" );
179+ if (strlen (argv [4 ]) > 64 ) {
180+ printf ("Error: Key must be in 32 characters\n" );
198181 return 1 ;
199182 }
200- if (strlen (argv [5 ]) != 16 ) {
201- printf ("Error: IV must be 16 characters\n" );
183+ if (strlen (argv [5 ]) > 32 ) {
184+ printf ("Error: IV must be in 16 characters\n" );
202185 return 1 ;
203186 }
204187
188+ uint8_t key [32 ];
189+ uint8_t iv [16 ];
190+ memset (key , 0 , 32 );
191+ memset (iv , 0 , 16 );
192+ //str to hex
193+ for (int i = 0 ; i < strlen (argv [4 ]); i += 2 ) {
194+ sscanf (argv [4 ] + i , "%2hhx" , & key [i / 2 ]);
195+ }
196+ for (int i = 0 ; i < strlen (argv [5 ]); i += 2 ) {
197+ sscanf (argv [5 ] + i , "%2hhx" , & iv [i / 2 ]);
198+ }
205199 size_t buffer_size = 4 * 1024 * 1024 ; // default buffer size is 4MB
206200
207201 if (argc > 6 ) {
@@ -213,16 +207,26 @@ int main(int argc, char** argv) {
213207 buffer_size = buffer_size_temp * 1024 * 1024 ; // convert MB to bytes
214208 }
215209
210+ printf ("KEY = " );
211+ for (int i = 0 ; i < 32 ; i ++ ) {
212+ printf ("%02x" , key [i ]);
213+ }
214+ printf ("\nIV = " );
215+ for (int i = 0 ; i < 16 ; i ++ ) {
216+ printf ("%02x" , iv [i ]);
217+ }
218+ printf ("\n" );
219+
216220 if (strcmp (argv [1 ], "encrypt" ) == 0 ) {
217221 clock_t start = clock ();
218- size_t file_size = encrypt_file (argv [2 ], argv [3 ], argv [ 4 ], argv [ 5 ] , buffer_size );
222+ size_t file_size = encrypt_file (argv [2 ], argv [3 ], key , iv , buffer_size );
219223 clock_t end = clock ();
220224 printf ("Encrypted %s to %s (%lu bytes) in %f seconds\n" , argv [2 ], argv [3 ], file_size , (double )(end - start ) / CLOCKS_PER_SEC );
221225 double speed = (double )file_size / (1024 * 1024 * 1024 ) / ((double )(end - start ) / CLOCKS_PER_SEC );
222226 printf ("Speed: %f GB/s\n" , speed );
223227 } else if (strcmp (argv [1 ], "decrypt" ) == 0 ) {
224228 clock_t start = clock ();
225- size_t file_size = decrypt_file (argv [2 ], argv [3 ], argv [ 4 ], argv [ 5 ] , buffer_size );
229+ size_t file_size = decrypt_file (argv [2 ], argv [3 ], key , iv , buffer_size );
226230 clock_t end = clock ();
227231 printf ("Decrypted %s to %s (%lu bytes) in %f seconds\n" , argv [2 ], argv [3 ], file_size , (double )(end - start ) / CLOCKS_PER_SEC );
228232 double speed = (double )file_size / (1024 * 1024 * 1024 ) / ((double )(end - start ) / CLOCKS_PER_SEC );
0 commit comments