4949
5050#include "wolfboot/version.h"
5151
52+ #ifdef DEBUG_SIGNTOOL
53+ #define DEBUG_PRINT (...) fprintf(stderr, __VA_ARGS__)
54+ #define DEBUG_BUFFER (buf ,sz ) WOLFSSL_BUFFER(buf,sz)
55+ #else
56+ #define DEBUG_PRINT (...) do{}while(0)
57+ #define DEBUG_BUFFER (buf ,sz ) do{}while(0)
58+ #endif
59+
5260#ifdef _WIN32
5361#include <io.h>
5462#define HAVE_MMAP 0
@@ -534,7 +542,7 @@ static uint8_t *load_key(uint8_t **key_buffer, uint32_t *key_buffer_sz,
534542 printf ("Key buffer malloc error!\n" );
535543 goto failure ;
536544 }
537- printf ("Key buffer size: %d\n" , * key_buffer_sz );
545+ DEBUG_PRINT ("Key buffer size: %d\n" , * key_buffer_sz );
538546
539547 switch (sign ) {
540548 /* auto, just try them all, no harm no foul */
@@ -769,8 +777,8 @@ static uint8_t *load_key(uint8_t **key_buffer, uint32_t *key_buffer_sz,
769777 break ;
770778 }
771779
772- printf ("info: xmss sk len: %d\n" , priv_sz );
773- printf ("info: xmss pk len: %d\n" , KEYSTORE_PUBKEY_SIZE_XMSS );
780+ DEBUG_PRINT ("info: xmss sk len: %d\n" , priv_sz );
781+ DEBUG_PRINT ("info: xmss pk len: %d\n" , KEYSTORE_PUBKEY_SIZE_XMSS );
774782
775783 if (* key_buffer_sz == (priv_sz + KEYSTORE_PUBKEY_SIZE_XMSS )) {
776784 /* priv + pub */
@@ -823,8 +831,8 @@ static uint8_t *load_key(uint8_t **key_buffer, uint32_t *key_buffer_sz,
823831 priv_sz -= pub_sz ;
824832 }
825833
826- printf ("info: ml-dsa priv len: %d\n" , priv_sz );
827- printf ("info: ml-dsa pub len: %d\n" , pub_sz );
834+ DEBUG_PRINT ("info: ml-dsa priv len: %d\n" , priv_sz );
835+ DEBUG_PRINT ("info: ml-dsa pub len: %d\n" , pub_sz );
828836
829837 if ((int )* key_buffer_sz == (priv_sz + pub_sz )) {
830838 /* priv + pub */
@@ -868,10 +876,8 @@ static uint8_t *load_key(uint8_t **key_buffer, uint32_t *key_buffer_sz,
868876 printf ("image header size calculated at runtime (%u bytes)\n" , CMD .header_sz );
869877 }
870878
871- #ifdef DEBUG_SIGNTOOL
872- printf ("Pubkey %d\n" , * pubkey_sz );
873- WOLFSSL_BUFFER (* pubkey , * pubkey_sz );
874- #endif
879+ DEBUG_PRINT ("Pubkey %d\n" , * pubkey_sz );
880+ DEBUG_BUFFER (* pubkey , * pubkey_sz );
875881 return * key_buffer ;
876882
877883failure :
@@ -1185,16 +1191,14 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
11851191 wc_Sha256Free (& sha );
11861192 /* Add Pubkey Hash to header */
11871193 header_append_tag (header , & header_idx , HDR_PUBKEY , digest_sz , buf );
1188- #ifdef DEBUG_SIGNTOOL
1189- printf ("Pubkey hash %d\n" , digest_sz );
1190- WOLFSSL_BUFFER (buf , digest_sz );
1191- #endif
1194+ DEBUG_PRINT ("Pubkey hash %d\n" , digest_sz );
1195+ DEBUG_BUFFER (buf , digest_sz );
11921196 ALIGN_8 (header_idx );
11931197 }
11941198 /* secondary public key in hybrid mode */
11951199 if (ret == 0 && CMD .hybrid && secondary_key_sz > 0 ) {
11961200 ret = wc_InitSha256_ex (& sha , NULL , INVALID_DEVID );
1197- printf ("Hashing secondary pubkey, size: %d\n" , secondary_key_sz );
1201+ DEBUG_PRINT ("Hashing secondary pubkey, size: %d\n" , secondary_key_sz );
11981202 if (ret == 0 ) {
11991203 ret = wc_Sha256Update (& sha , secondary_key , secondary_key_sz );
12001204 if (ret == 0 )
@@ -1205,10 +1209,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12051209 ALIGN_8 (header_idx );
12061210 /* Add Secondary Pubkey Hash to header */
12071211 header_append_tag (header , & header_idx , HDR_SECONDARY_PUBKEY , digest_sz , second_buf );
1208- #ifdef DEBUG_SIGNTOOL
1209- printf ("Secondary pubkey hash %d\n" , digest_sz );
1210- WOLFSSL_BUFFER (second_buf , digest_sz );
1211- #endif
1212+ DEBUG_PRINT ("Secondary pubkey hash %d\n" , digest_sz );
1213+ DEBUG_BUFFER (second_buf , digest_sz );
12121214 }
12131215 }
12141216 if (ret == 0 ) {
@@ -1261,10 +1263,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12611263 /* Add Pubkey Hash to header */
12621264 ALIGN_8 (header_idx );
12631265 header_append_tag (header , & header_idx , HDR_PUBKEY , digest_sz , buf );
1264- #ifdef DEBUG_SIGNTOOL
1265- printf ("Pubkey hash %d\n" , digest_sz );
1266- WOLFSSL_BUFFER (buf , digest_sz );
1267- #endif
1266+ DEBUG_PRINT ("Pubkey hash %d\n" , digest_sz );
1267+ DEBUG_BUFFER (buf , digest_sz );
12681268 ALIGN_8 (header_idx );
12691269 }
12701270 wc_Sha384Free (& sha );
@@ -1279,10 +1279,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
12791279 header_append_tag (header , & header_idx , HDR_SECONDARY_CIPHER , 2 , & CMD .secondary_sign );
12801280 /* Add Secondary Pubkey Hash to header */
12811281 header_append_tag (header , & header_idx , HDR_SECONDARY_PUBKEY , digest_sz , second_buf );
1282- #ifdef DEBUG_SIGNTOOL
1283- printf ("Secondary pubkey hash %d\n" , digest_sz );
1284- WOLFSSL_BUFFER (second_buf , digest_sz );
1285- #endif
1282+ DEBUG_PRINT ("Secondary pubkey hash %d\n" , digest_sz );
1283+ DEBUG_BUFFER (second_buf , digest_sz );
12861284 ALIGN_8 (header_idx );
12871285 }
12881286 wc_Sha384Free (& sha );
@@ -1329,10 +1327,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
13291327 if (ret == 0 ) {
13301328 ret = wc_Sha3_384_Final (& sha , buf );
13311329 header_append_tag (header , & header_idx , HDR_PUBKEY , digest_sz , buf );
1332- #ifdef DEBUG_SIGNTOOL
1333- printf ("Pubkey hash %d\n" , digest_sz );
1334- WOLFSSL_BUFFER (buf , digest_sz );
1335- #endif
1330+ DEBUG_PRINT ("Pubkey hash %d\n" , digest_sz );
1331+ DEBUG_BUFFER (buf , digest_sz );
13361332 ALIGN_8 (header_idx );
13371333 }
13381334 wc_Sha3_384_Free (& sha );
@@ -1347,10 +1343,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
13471343 /* Add Secondary cipher to header */
13481344 header_append_tag (header , & header_idx , HDR_SECONDARY_CIPHER , 2 , & CMD .secondary_sign );
13491345 header_append_tag (header , & header_idx , HDR_SECONDARY_PUBKEY , digest_sz , second_buf );
1350- #ifdef DEBUG_SIGNTOOL
1351- printf ("Secondary pubkey hash %d\n" , digest_sz );
1352- WOLFSSL_BUFFER (second_buf , digest_sz );
1353- #endif
1346+ DEBUG_PRINT ("Secondary pubkey hash %d\n" , digest_sz );
1347+ DEBUG_BUFFER (second_buf , digest_sz );
13541348 ALIGN_8 (header_idx );
13551349 }
13561350 wc_Sha3_384_Free (& sha );
@@ -1394,10 +1388,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
13941388 printf ("Hash algorithm error %d\n" , ret );
13951389 goto failure ;
13961390 }
1397- #ifdef DEBUG_SIGNTOOL
1398- printf ("Image hash %d\n" , digest_sz );
1399- WOLFSSL_BUFFER (digest , digest_sz );
1400- #endif
1391+ DEBUG_PRINT ("Image hash %d\n" , digest_sz );
1392+ DEBUG_BUFFER (digest , digest_sz );
14011393
14021394 /* Add image hash to header */
14031395 header_append_tag (header , & header_idx , CMD .hash_algo , digest_sz , digest );
@@ -1425,15 +1417,12 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
14251417 }
14261418
14271419 memset (signature , 0 , CMD .signature_sz );
1428- printf ("Signature sz (malloc): %d\n" , CMD .signature_sz );
1420+ DEBUG_PRINT ("Signature sz (malloc): %d\n" , CMD .signature_sz );
14291421 if (!CMD .manual_sign ) {
14301422 printf ("Signing the digest...\n" );
1431- #ifdef DEBUG_SIGNTOOL
1432- printf ("Digest %d\n" , digest_sz );
1433- WOLFSSL_BUFFER (digest , digest_sz );
1434- #endif
1423+ DEBUG_PRINT ("Digest %d\n" , digest_sz );
1424+ DEBUG_BUFFER (digest , digest_sz );
14351425 /* Sign the digest */
1436- printf ("CMD.sign == %02x\n" , CMD .sign );
14371426 ret = sign_digest (CMD .sign , CMD .hash_algo ,
14381427 signature , & CMD .signature_sz , digest , digest_sz , 0 );
14391428 if (ret != 0 ) {
@@ -1442,7 +1431,7 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
14421431 }
14431432 }
14441433 else {
1445- printf ("Opening signature file %s\n" , CMD .signature_file );
1434+ DEBUG_PRINT ("Opening signature file %s\n" , CMD .signature_file );
14461435
14471436 f = fopen (CMD .signature_file , "rb" );
14481437 if (f == NULL ) {
@@ -1511,10 +1500,8 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
15111500 }
15121501
15131502 printf ("Signing the policy digest...\n" );
1514- #ifdef DEBUG_SIGNTOOL
1515- printf ("Policy Digest %d\n" , digest_sz );
1516- WOLFSSL_BUFFER (digest , digest_sz );
1517- #endif
1503+ DEBUG_PRINT ("Policy Digest %d\n" , digest_sz );
1504+ DEBUG_BUFFER (digest , digest_sz );
15181505
15191506 /* Policy is always SHA2-256 */
15201507 ret = sign_digest (CMD .sign , HASH_SHA256 ,
@@ -1546,15 +1533,13 @@ static int make_header_ex(int is_diff, uint8_t *pubkey, uint32_t pubkey_sz,
15461533 }
15471534 }
15481535
1549- #ifdef DEBUG_SIGNTOOL
1550- printf ("Signature %d\n" , CMD .signature_sz );
1551- WOLFSSL_BUFFER (signature , CMD .signature_sz );
1536+ DEBUG_PRINT ("Signature %d\n" , CMD .signature_sz );
1537+ DEBUG_BUFFER (signature , CMD .signature_sz );
15521538 if (CMD .policy_sign ) {
1553- printf ("PCR Mask 0x%08x\n" , * ((uint32_t * )policy ));
1554- printf ("Policy Signature %d\n" , CMD .policy_sz );
1555- WOLFSSL_BUFFER (policy + sizeof (uint32_t ), CMD .policy_sz );
1539+ DEBUG_PRINT ("PCR Mask 0x%08x\n" , * ((uint32_t * )policy ));
1540+ DEBUG_PRINT ("Policy Signature %d\n" , CMD .policy_sz );
1541+ DEBUG_BUFFER (policy + sizeof (uint32_t ), CMD .policy_sz );
15561542 }
1557- #endif
15581543
15591544 /* Add signature to header */
15601545 ALIGN_8 (header_idx );
@@ -2109,7 +2094,7 @@ static void set_signature_sizes(int secondary)
21092094 exit (1 );
21102095 }
21112096
2112- printf ("info: LMS signature size: %d\n" , sig_sz );
2097+ DEBUG_PRINT ("info: LMS signature size: %d\n" , sig_sz );
21132098
21142099 CMD .header_sz = 2 * sig_sz ;
21152100 * sz = sig_sz ;
@@ -2142,7 +2127,7 @@ static void set_signature_sizes(int secondary)
21422127 exit (1 );
21432128 }
21442129
2145- printf ("info: XMSS signature size: %d\n" , sig_sz );
2130+ DEBUG_PRINT ("info: XMSS signature size: %d\n" , sig_sz );
21462131
21472132 CMD .header_sz = 2 * sig_sz ;
21482133 * sz = sig_sz ;
@@ -2175,7 +2160,7 @@ static void set_signature_sizes(int secondary)
21752160 exit (1 );
21762161 }
21772162
2178- printf ("info: ML-DSA signature size: %d\n" , sig_sz );
2163+ DEBUG_PRINT ("info: ML-DSA signature size: %d\n" , sig_sz );
21792164
21802165 CMD .header_sz = 2 * sig_sz ;
21812166 * sz = sig_sz ;
@@ -2540,7 +2525,7 @@ int main(int argc, char** argv)
25402525 }
25412526 }
25422527 if ((CMD .sign == CMD .secondary_sign ) && (CMD .hybrid )) {
2543- printf ("Waring : Duplicate signature algorithm detected. Fix your command line!\n" );
2528+ printf ("Warning : Duplicate signature algorithm detected. Fix your command line!\n" );
25442529 CMD .hybrid = 0 ;
25452530 CMD .secondary_key_file = NULL ;
25462531 CMD .secondary_signature_sz = 0 ;
@@ -2682,14 +2667,14 @@ int main(int argc, char** argv)
26822667 uint8_t * kbuf2 = NULL ;
26832668 uint8_t * pubkey2 = NULL ;
26842669 uint32_t pubkey_sz2 ;
2685- printf ("Loading secondary key\n" );
2670+ DEBUG_PRINT ("Loading secondary key\n" );
26862671 kbuf2 = load_key (& key_buffer2 , & key_buffer_sz2 , & pubkey2 , & pubkey_sz2 , 1 );
26872672 printf ("Creating hybrid signature\n" );
26882673 make_hybrid_header (pubkey , pubkey_sz , CMD .image_file , CMD .output_image_file ,
26892674 pubkey2 , pubkey_sz2 );
2690- printf ("Signature size: %u\n" , CMD .signature_sz );
2691- printf ("Secondary signature size: %u\n" , CMD .secondary_signature_sz );
2692- printf ("Header size: %u\n" , CMD .header_sz );
2675+ DEBUG_PRINT ("Signature size: %u\n" , CMD .signature_sz );
2676+ DEBUG_PRINT ("Secondary signature size: %u\n" , CMD .secondary_signature_sz );
2677+ DEBUG_PRINT ("Header size: %u\n" , CMD .header_sz );
26932678 if (kbuf2 )
26942679 free (kbuf2 );
26952680 } else {
0 commit comments