4747static void usage (void )
4848{
4949 printf ("Expected usage:\n" );
50- printf ("./examples/pcr/extend [pcr] [filename]\n" );
50+ printf ("./examples/pcr/extend [-sha1/-sha256/-sha384/-sha512] [ pcr] [filename]\n" );
5151 printf ("* pcr: PCR index between 0-23 (default %d)\n" , TPM2_TEST_PCR );
5252 printf ("* filename: points to file(data) to measure\n" );
5353 printf ("\tIf wolfTPM is built with --disable-wolfcrypt the file\n"
@@ -61,18 +61,19 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
6161{
6262 int i , j , pcrIndex = TPM2_TEST_PCR , rc = -1 ;
6363 WOLFTPM2_DEV dev ;
64+ TPM_ALG_ID alg = TPM_ALG_SHA256 ;
6465 /* Arbitrary user data provided through a file */
6566 const char * filename = "input.data" ;
67+ int hashSz ;
6668#if !defined(NO_FILESYSTEM ) && !defined(NO_WRITE_TEMP_FILES ) && \
6769 !defined(WOLFTPM2_NO_WOLFCRYPT )
6870 XFILE fp = NULL ;
6971 size_t len ;
70- BYTE hash [TPM_SHA256_DIGEST_SIZE ];
71- #if !defined(NO_SHA256 )
72- /* Using wolfcrypt to hash input data */
72+ BYTE hash [TPM_MAX_DIGEST_SIZE ];
73+
7374 BYTE dataBuffer [1024 ];
74- wc_Sha256 sha256 ;
75- #endif
75+ enum wc_HashType hashType ;
76+ wc_HashAlg dig ;
7677#endif
7778
7879 union {
@@ -92,22 +93,43 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
9293 usage ();
9394 return 0 ;
9495 }
96+ }
97+
98+ while (argc > 1 ) {
99+ if (XSTRCMP (argv [argc - 1 ], "-sha1" ) == 0 ) {
100+ alg = TPM_ALG_SHA ;
101+ }
102+ else if (XSTRCMP (argv [argc - 1 ], "-sha256" ) == 0 ) {
103+ alg = TPM_ALG_SHA256 ;
104+ }
105+ else if (XSTRCMP (argv [argc - 1 ], "-sha384" ) == 0 ) {
106+ alg = TPM_ALG_SHA384 ;
107+ }
108+ else if (XSTRCMP (argv [argc - 1 ], "-sha512" ) == 0 ) {
109+ alg = TPM_ALG_SHA512 ;
110+ }
95111
96- /* Advanced usage */
97- if (argv [1 ][ 0 ] != '-' ) {
98- if (pcrIndex < 0 || pcrIndex > 23 || * argv [ 1 ] < '0' || * argv [ 1 ] > '9' ) {
112+ else if ( * argv [ argc - 1 ] >= '0' && * argv [ argc - 1 ] <= '9' ) {
113+ pcrIndex = XATOI (argv [argc - 1 ]);
114+ if (pcrIndex < 0 || pcrIndex > 23 ) {
99115 printf ("PCR index is out of range (0-23)\n" );
100116 usage ();
101117 return 0 ;
102118 }
103- pcrIndex = XATOI (argv [1 ]);
104119 }
105-
106- if (argc >= 3 && argv [2 ][0 ] != '-' )
107- filename = argv [2 ];
120+ else if (* argv [argc - 1 ] != '-' ) {
121+ filename = argv [argc - 1 ];
122+ }
123+ else {
124+ printf ("Warning: Unrecognized option: %s\n" , argv [argc - 1 ]);
125+ }
126+ argc -- ;
108127 }
109128
129+ hashSz = TPM2_GetHashDigestSize (alg );
130+
110131 printf ("Demo how to extend data into a PCR (TPM2.0 measurement)\n" );
132+ printf ("\tHash Algorithm: %s (sz %d)\n" , TPM2_GetAlgName (alg ), hashSz );
111133 printf ("\tData file: %s\n" , filename );
112134 printf ("\tPCR Index: %d\n" , pcrIndex );
113135
@@ -122,7 +144,7 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
122144 XMEMSET (& cmdIn .pcrExtend , 0 , sizeof (cmdIn .pcrExtend ));
123145 cmdIn .pcrExtend .pcrHandle = pcrIndex ;
124146 cmdIn .pcrExtend .digests .count = 1 ;
125- cmdIn .pcrExtend .digests .digests [0 ].hashAlg = TPM_ALG_SHA256 ;
147+ cmdIn .pcrExtend .digests .digests [0 ].hashAlg = alg ;
126148
127149 /* Prepare the hash from user file or predefined value */
128150#if !defined(NO_FILESYSTEM ) && !defined(NO_WRITE_TEMP_FILES ) && \
@@ -131,36 +153,32 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
131153 fp = XFOPEN (filename , "rb" );
132154 }
133155 if (filename && fp != XBADFILE ) {
134- #if !defined(NO_SHA256 )
135- wc_InitSha256 (& sha256 );
156+ rc = TPM2_GetHashType (alg );
157+ hashType = (enum wc_HashType )rc ;
158+ rc = 0 ;
159+ wc_HashInit (& dig , hashType );
136160 while (!XFEOF (fp )) {
137161 len = XFREAD (dataBuffer , 1 , sizeof (dataBuffer ), fp );
138162 if (len ) {
139- wc_Sha256Update ( & sha256 , dataBuffer , (int )len );
163+ wc_HashUpdate ( & dig , hashType , dataBuffer , (int )len );
140164 }
141165 }
142- wc_Sha256Final (& sha256 , hash );
143- #else
144- len = XFREAD (hash , 1 , TPM_SHA256_DIGEST_SIZE , fp );
145- if (len != TPM_SHA256_DIGEST_SIZE ) {
146- printf ("Error while reading SHA256 digest from file.\n" );
147- goto exit ;
148- }
149- #endif
166+ wc_HashFinal (& dig , hashType , hash );
167+
150168 XMEMCPY (cmdIn .pcrExtend .digests .digests [0 ].digest .H ,
151- hash , TPM_SHA256_DIGEST_SIZE );
169+ hash , hashSz );
152170 }
153171 else
154172#endif /* !WOLFTPM2_NO_WOLFCRYPT && !NO_FILESYSTEM */
155173 {
156174 printf ("Error loading file %s, using test data\n" , filename );
157- for (i = 0 ; i < TPM_SHA256_DIGEST_SIZE ; i ++ ) {
175+ for (i = 0 ; i < hashSz ; i ++ ) {
158176 cmdIn .pcrExtend .digests .digests [0 ].digest .H [i ] = i ;
159177 }
160178 }
161179
162180 printf ("Hash to be used for measurement:\n" );
163- for (i = 0 ; i < TPM_SHA256_DIGEST_SIZE ; i ++ )
181+ for (i = 0 ; i < hashSz ; i ++ )
164182 printf ("%02X" , cmdIn .pcrExtend .digests .digests [0 ].digest .H [i ]);
165183 printf ("\n" );
166184
@@ -173,7 +191,7 @@ int TPM2_PCR_Extend_Test(void* userCtx, int argc, char *argv[])
173191 printf ("TPM2_PCR_Extend success\n" );
174192
175193 XMEMSET (& cmdIn .pcrRead , 0 , sizeof (cmdIn .pcrRead ));
176- TPM2_SetupPCRSel (& cmdIn .pcrRead .pcrSelectionIn , TEST_WRAP_DIGEST , pcrIndex );
194+ TPM2_SetupPCRSel (& cmdIn .pcrRead .pcrSelectionIn , alg , pcrIndex );
177195 rc = TPM2_PCR_Read (& cmdIn .pcrRead , & cmdOut .pcrRead );
178196 if (rc != TPM_RC_SUCCESS ) {
179197 printf ("TPM2_PCR_Read failed 0x%x: %s\n" , rc , TPM2_GetRCString (rc ));
0 commit comments