22/* SPDX-License-Identifier: Unlicense */
33#include "tomcrypt_private.h"
44
5+ #define OPEN_FILE (f , o ) do { \
6+ o = fopen(f, "w"); \
7+ if (o == NULL) { \
8+ perror("can't open " f); \
9+ return; \
10+ } \
11+ } while(0)
12+
513static void hash_gen (void )
614{
715 unsigned char md [MAXBLOCKSIZE ], * buf ;
816 unsigned long outlen , x , y , z ;
917 FILE * out ;
1018 int err ;
1119
12- out = fopen ("hash_tv.txt" , "w" );
13- if (out == NULL ) {
14- perror ("can't open hash_tv.txt" );
15- return ;
16- }
20+ OPEN_FILE ("hash_tv.txt" , out );
1721
1822 fprintf (out , "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n" );
1923 for (x = 0 ; hash_descriptor [x ].name != NULL ; x ++ ) {
@@ -52,7 +56,7 @@ static void cipher_gen(void)
5256 FILE * out ;
5357 symmetric_key skey ;
5458
55- out = fopen ("cipher_tv.txt" , "w" );
59+ OPEN_FILE ("cipher_tv.txt" , out );
5660
5761 fprintf (out ,
5862"Cipher Test Vectors\n\nThese are test encryptions with key of nn bytes '00 01 02 03 .. (nn-1)' and original PT of the same style.\n"
@@ -126,7 +130,7 @@ static void hmac_gen(void)
126130 FILE * out ;
127131 unsigned long len ;
128132
129- out = fopen ("hmac_tv.txt" , "w" );
133+ OPEN_FILE ("hmac_tv.txt" , out );
130134
131135 fprintf (out ,
132136"HMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are HMACed. The initial key is\n"
@@ -179,7 +183,7 @@ static void omac_gen(void)
179183 FILE * out ;
180184 unsigned long len ;
181185
182- out = fopen ("omac_tv.txt" , "w" );
186+ OPEN_FILE ("omac_tv.txt" , out );
183187
184188 fprintf (out ,
185189"OMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are OMAC'ed. The initial key is\n"
@@ -240,7 +244,7 @@ static void pmac_gen(void)
240244 FILE * out ;
241245 unsigned long len ;
242246
243- out = fopen ("pmac_tv.txt" , "w" );
247+ OPEN_FILE ("pmac_tv.txt" , out );
244248
245249 fprintf (out ,
246250"PMAC Tests. In these tests messages of N bytes long (00,01,02,...,NN-1) are PMAC'ed. The initial key is\n"
@@ -302,7 +306,7 @@ static void eax_gen(void)
302306 plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
303307 unsigned long len ;
304308
305- out = fopen ("eax_tv.txt" , "w" );
309+ OPEN_FILE ("eax_tv.txt" , out );
306310 fprintf (out , "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
307311 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
308312 "step repeated sufficiently.\n\n" );
@@ -368,7 +372,7 @@ static void ocb_gen(void)
368372 plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
369373 unsigned long len ;
370374
371- out = fopen ("ocb_tv.txt" , "w" );
375+ OPEN_FILE ("ocb_tv.txt" , out );
372376 fprintf (out , "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
373377 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
374378 "step repeated sufficiently. The nonce is fixed throughout.\n\n" );
@@ -437,7 +441,7 @@ static void ocb3_gen(void)
437441 plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
438442 unsigned long len ;
439443
440- out = fopen ("ocb3_tv.txt" , "w" );
444+ OPEN_FILE ("ocb3_tv.txt" , out );
441445 fprintf (out , "OCB3 Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
442446 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
443447 "step repeated sufficiently. The nonce is fixed throughout. AAD is fixed to 3 bytes (ASCII) 'AAD'.\n\n" );
@@ -509,7 +513,7 @@ static void ccm_gen(void)
509513 unsigned long len ;
510514 const unsigned int taglen [] = {4 , 6 , 8 , 10 , 12 , 14 , 16 };
511515
512- out = fopen ("ccm_tv.txt" , "w" );
516+ OPEN_FILE ("ccm_tv.txt" , out );
513517 fprintf (out , "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
514518 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
515519 "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n" );
@@ -535,7 +539,7 @@ static void ccm_gen(void)
535539 nonce [z ] = z ;
536540 }
537541
538- for (t = 0 ; t < sizeof (taglen )/ sizeof ( taglen [ 0 ] ); ++ t ) {
542+ for (t = 0 ; t < LTC_ARRAY_SIZE (taglen ); ++ t ) {
539543 for (y1 = 0 ; y1 <= (int )(cipher_descriptor [x ].block_length * 2 ); y1 ++ ){
540544 for (z = 0 ; z < y1 ; z ++ ) {
541545 plaintext [z ] = (unsigned char )(z & 255 );
@@ -579,7 +583,7 @@ static void gcm_gen(void)
579583 unsigned char key [MAXBLOCKSIZE ], plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
580584 unsigned long len ;
581585
582- out = fopen ("gcm_tv.txt" , "w" );
586+ OPEN_FILE ("gcm_tv.txt" , out );
583587 fprintf (out , "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
584588 "are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
585589 "step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n" );
@@ -641,7 +645,7 @@ static void base64_gen(void)
641645 char dst [256 ];
642646 unsigned long x , len ;
643647
644- out = fopen ("base64_tv.txt" , "w" );
648+ OPEN_FILE ("base64_tv.txt" , out );
645649 fprintf (out , "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n" );
646650 for (x = 0 ; x <= 32 ; x ++ ) {
647651 for (ch = 0 ; ch < x ; ch ++ ) {
@@ -666,7 +670,7 @@ static void ecc_gen(void)
666670 ecc_point * G , * R ;
667671 int x ;
668672
669- out = fopen ("ecc_tv.txt" , "w" );
673+ OPEN_FILE ("ecc_tv.txt" , out );
670674 fprintf (out , "ecc vectors. These are for kG for k=1,3,9,27,...,3**n until k > order of the curve outputs are <k,x,y> triplets\n\n" );
671675 G = ltc_ecc_new_point ();
672676 R = ltc_ecc_new_point ();
@@ -713,7 +717,7 @@ static void lrw_gen(void)
713717 tweak [x ] = key [x ] = iv [x ] = x ;
714718 }
715719
716- out = fopen ("lrw_tv.txt" , "w" );
720+ OPEN_FILE ("lrw_tv.txt" , out );
717721 for (x = 16 ; x < (int )(sizeof (buf )); x += 16 ) {
718722 if ((err = lrw_start (find_cipher ("aes" ), iv , key , 16 , tweak , 0 , & lrw )) != CRYPT_OK ) {
719723 fprintf (stderr , "Error starting LRW-AES: %s\n" , error_to_string (err ));
0 commit comments