2
2
/* SPDX-License-Identifier: Unlicense */
3
3
#include "tomcrypt_private.h"
4
4
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
+
5
13
static void hash_gen (void )
6
14
{
7
15
unsigned char md [MAXBLOCKSIZE ], * buf ;
8
16
unsigned long outlen , x , y , z ;
9
17
FILE * out ;
10
18
int err ;
11
19
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 );
17
21
18
22
fprintf (out , "Hash Test Vectors:\n\nThese are the hashes of nn bytes '00 01 02 03 .. (nn-1)'\n\n" );
19
23
for (x = 0 ; hash_descriptor [x ].name != NULL ; x ++ ) {
@@ -52,7 +56,7 @@ static void cipher_gen(void)
52
56
FILE * out ;
53
57
symmetric_key skey ;
54
58
55
- out = fopen ("cipher_tv.txt" , "w" );
59
+ OPEN_FILE ("cipher_tv.txt" , out );
56
60
57
61
fprintf (out ,
58
62
"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)
126
130
FILE * out ;
127
131
unsigned long len ;
128
132
129
- out = fopen ("hmac_tv.txt" , "w" );
133
+ OPEN_FILE ("hmac_tv.txt" , out );
130
134
131
135
fprintf (out ,
132
136
"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)
179
183
FILE * out ;
180
184
unsigned long len ;
181
185
182
- out = fopen ("omac_tv.txt" , "w" );
186
+ OPEN_FILE ("omac_tv.txt" , out );
183
187
184
188
fprintf (out ,
185
189
"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)
240
244
FILE * out ;
241
245
unsigned long len ;
242
246
243
- out = fopen ("pmac_tv.txt" , "w" );
247
+ OPEN_FILE ("pmac_tv.txt" , out );
244
248
245
249
fprintf (out ,
246
250
"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)
302
306
plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
303
307
unsigned long len ;
304
308
305
- out = fopen ("eax_tv.txt" , "w" );
309
+ OPEN_FILE ("eax_tv.txt" , out );
306
310
fprintf (out , "EAX Test Vectors. Uses the 00010203...NN-1 pattern for header/nonce/plaintext/key. The outputs\n"
307
311
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
308
312
"step repeated sufficiently.\n\n" );
@@ -368,7 +372,7 @@ static void ocb_gen(void)
368
372
plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
369
373
unsigned long len ;
370
374
371
- out = fopen ("ocb_tv.txt" , "w" );
375
+ OPEN_FILE ("ocb_tv.txt" , out );
372
376
fprintf (out , "OCB Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
373
377
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
374
378
"step repeated sufficiently. The nonce is fixed throughout.\n\n" );
@@ -437,7 +441,7 @@ static void ocb3_gen(void)
437
441
plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
438
442
unsigned long len ;
439
443
440
- out = fopen ("ocb3_tv.txt" , "w" );
444
+ OPEN_FILE ("ocb3_tv.txt" , out );
441
445
fprintf (out , "OCB3 Test Vectors. Uses the 00010203...NN-1 pattern for nonce/plaintext/key. The outputs\n"
442
446
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
443
447
"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)
509
513
unsigned long len ;
510
514
const unsigned int taglen [] = {4 , 6 , 8 , 10 , 12 , 14 , 16 };
511
515
512
- out = fopen ("ccm_tv.txt" , "w" );
516
+ OPEN_FILE ("ccm_tv.txt" , out );
513
517
fprintf (out , "CCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
514
518
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
515
519
"step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n" );
@@ -535,7 +539,7 @@ static void ccm_gen(void)
535
539
nonce [z ] = z ;
536
540
}
537
541
538
- for (t = 0 ; t < sizeof (taglen )/ sizeof ( taglen [ 0 ] ); ++ t ) {
542
+ for (t = 0 ; t < LTC_ARRAY_SIZE (taglen ); ++ t ) {
539
543
for (y1 = 0 ; y1 <= (int )(cipher_descriptor [x ].block_length * 2 ); y1 ++ ){
540
544
for (z = 0 ; z < y1 ; z ++ ) {
541
545
plaintext [z ] = (unsigned char )(z & 255 );
@@ -579,7 +583,7 @@ static void gcm_gen(void)
579
583
unsigned char key [MAXBLOCKSIZE ], plaintext [MAXBLOCKSIZE * 2 ], tag [MAXBLOCKSIZE ];
580
584
unsigned long len ;
581
585
582
- out = fopen ("gcm_tv.txt" , "w" );
586
+ OPEN_FILE ("gcm_tv.txt" , out );
583
587
fprintf (out , "GCM Test Vectors. Uses the 00010203...NN-1 pattern for nonce/header/plaintext/key. The outputs\n"
584
588
"are of the form ciphertext,tag for a given NN. The key for step N>1 is the tag of the previous\n"
585
589
"step repeated sufficiently. The nonce is fixed throughout at 13 bytes 000102...\n\n" );
@@ -641,7 +645,7 @@ static void base64_gen(void)
641
645
char dst [256 ];
642
646
unsigned long x , len ;
643
647
644
- out = fopen ("base64_tv.txt" , "w" );
648
+ OPEN_FILE ("base64_tv.txt" , out );
645
649
fprintf (out , "Base64 vectors. These are the base64 encodings of the strings 00,01,02...NN-1\n\n" );
646
650
for (x = 0 ; x <= 32 ; x ++ ) {
647
651
for (ch = 0 ; ch < x ; ch ++ ) {
@@ -666,7 +670,7 @@ static void ecc_gen(void)
666
670
ecc_point * G , * R ;
667
671
int x ;
668
672
669
- out = fopen ("ecc_tv.txt" , "w" );
673
+ OPEN_FILE ("ecc_tv.txt" , out );
670
674
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" );
671
675
G = ltc_ecc_new_point ();
672
676
R = ltc_ecc_new_point ();
@@ -713,7 +717,7 @@ static void lrw_gen(void)
713
717
tweak [x ] = key [x ] = iv [x ] = x ;
714
718
}
715
719
716
- out = fopen ("lrw_tv.txt" , "w" );
720
+ OPEN_FILE ("lrw_tv.txt" , out );
717
721
for (x = 16 ; x < (int )(sizeof (buf )); x += 16 ) {
718
722
if ((err = lrw_start (find_cipher ("aes" ), iv , key , 16 , tweak , 0 , & lrw )) != CRYPT_OK ) {
719
723
fprintf (stderr , "Error starting LRW-AES: %s\n" , error_to_string (err ));
0 commit comments