@@ -121,20 +121,21 @@ static void wolfBoot_verify_signature(uint8_t key_slot,
121
121
{
122
122
int ret , verify_res = 0 ;
123
123
uint8_t * pubkey = keystore_get_buffer (key_slot );
124
- int point_sz = keystore_get_size (key_slot )/2 ;
125
- #ifdef WOLFBOOT_TPM
124
+ int pubkey_sz = keystore_get_size (key_slot );
125
+ int point_sz = pubkey_sz /2 ;
126
+ #if defined(WOLFBOOT_TPM ) && !defined(WOLFBOOT_TPM_KEYSTORE )
126
127
WOLFTPM2_KEY tpmKey ;
127
128
#else
128
129
ecc_key ecc ;
129
130
mp_int r , s ;
130
131
#endif
131
132
132
- if (pubkey == NULL || point_sz <= 0 )
133
+ if (pubkey == NULL || pubkey_sz <= 0 ) {
133
134
return ;
135
+ }
134
136
135
- #ifdef WOLFBOOT_TPM
136
- /* TODO: Check ECC Root of Trust in TPM */
137
-
137
+ #if defined(WOLFBOOT_TPM ) && !defined(WOLFBOOT_TPM_KEYSTORE )
138
+ /* Use TPM for ECC verify */
138
139
/* Load public key into TPM */
139
140
memset (& tpmKey , 0 , sizeof (tpmKey ));
140
141
ret = wolfTPM2_LoadEccPublicKey (& wolftpm_dev , & tpmKey ,
@@ -276,25 +277,24 @@ static void wolfBoot_verify_signature(uint8_t key_slot,
276
277
{
277
278
int ret ;
278
279
uint8_t output [IMAGE_SIGNATURE_SIZE ];
279
- int output_sz = sizeof (output );
280
+ int output_sz = ( int ) sizeof (output );
280
281
uint8_t * digest_out = NULL ;
281
282
uint8_t * pubkey = keystore_get_buffer (key_slot );
282
283
int pubkey_sz = keystore_get_size (key_slot );
283
284
word32 inOutIdx = 0 ;
284
- #ifdef WOLFBOOT_TPM
285
+ #if defined( WOLFBOOT_TPM ) && !defined( WOLFBOOT_TPM_KEYSTORE )
285
286
WOLFTPM2_KEY tpmKey ;
286
287
const byte * n = NULL , * e = NULL ;
287
288
word32 nSz = 0 , eSz = 0 ;
288
289
#else
289
290
struct RsaKey rsa ;
290
291
#endif
291
292
292
- if (( pubkey_sz < 0 ) || ( pubkey == NULL ))
293
+ if (pubkey == NULL || pubkey_sz < 0 ) {
293
294
return ;
295
+ }
294
296
295
- #ifdef WOLFBOOT_TPM
296
- /* TODO: Check RSA Root of Trust in TPM */
297
-
297
+ #if defined(WOLFBOOT_TPM ) && !defined(WOLFBOOT_TPM_KEYSTORE )
298
298
/* Extract DER RSA key struct */
299
299
memset (& tpmKey , 0 , sizeof (tpmKey ));
300
300
ret = wc_RsaPublicKeyDecode_ex (pubkey , & inOutIdx , pubkey_sz ,
@@ -365,8 +365,9 @@ static void wolfBoot_verify_signature(uint8_t key_slot,
365
365
ret = RsaDecodeSignature (& digest_out , ret );
366
366
}
367
367
#endif
368
- if (ret == WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out )
368
+ if (ret == WOLFBOOT_SHA_DIGEST_SIZE && img && digest_out ) {
369
369
RSA_VERIFY_HASH (img , digest_out );
370
+ }
370
371
}
371
372
#endif /* WOLFBOOT_SIGN_RSA2048 || WOLFBOOT_SIGN_3072 || \
372
373
* WOLFBOOT_SIGN_RSA4096 */
@@ -642,7 +643,8 @@ static void key_sha3_384(uint8_t key_slot, uint8_t *hash)
642
643
#endif /* SHA3-384 */
643
644
644
645
#ifdef WOLFBOOT_TPM
645
- #if defined(WOLFTPM_DEBUG_IO ) || defined(WOLFBOOT_DEBUG_TPM )
646
+ #if defined(DEBUG_WOLFTPM ) || defined(WOLFTPM_DEBUG_IO ) || \
647
+ defined(WOLFBOOT_DEBUG_TPM )
646
648
#define LINE_LEN 16
647
649
static void wolfBoot_PrintBin (const byte * buffer , word32 length )
648
650
{
@@ -698,6 +700,14 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
698
700
byte rxBuf [MAX_SPI_FRAMESIZE + TPM_TIS_HEADER_SZ ];
699
701
int xferSz = TPM_TIS_HEADER_SZ + size ;
700
702
703
+ #ifdef WOLFTPM_DEBUG_IO
704
+ wolfBoot_printf ("TPM2_IoCb (Adv): Read %d, Addr %x, Size %d\n" ,
705
+ isRead ? 1 : 0 , addr , size );
706
+ if (!isRead ) {
707
+ wolfBoot_PrintBin (buf , size );
708
+ }
709
+ #endif
710
+
701
711
/* Build TPM header */
702
712
txBuf [1 ] = (addr >>16 ) & 0xFF ;
703
713
txBuf [2 ] = (addr >>8 ) & 0xFF ;
@@ -711,7 +721,7 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
711
721
memcpy (& txBuf [TPM_TIS_HEADER_SZ ], buf , size );
712
722
}
713
723
memset (rxBuf , 0 , sizeof (rxBuf ));
714
- #endif
724
+ #endif /* WOLFTPM_ADV_IO */
715
725
716
726
#ifdef WOLFTPM_CHECK_WAIT_STATE /* Handle TIS wait states */
717
727
/* Send header - leave CS asserted */
@@ -751,11 +761,20 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
751
761
}
752
762
#else /* Send Entire Message - no wait states */
753
763
ret = spi_xfer (SPI_CS_TPM , txBuf , rxBuf , xferSz , 0 );
754
- #endif
764
+
765
+ #ifdef WOLFTPM_DEBUG_IO
766
+ wolfBoot_printf ("TPM2_IoCb: Ret %d, Sz %d\n" , ret , xferSz );
767
+ wolfBoot_PrintBin (txBuf , xferSz );
768
+ wolfBoot_PrintBin (rxBuf , xferSz );
769
+ #endif
770
+ #endif /* !WOLFTPM_CHECK_WAIT_STATE */
755
771
756
772
#ifdef WOLFTPM_ADV_IO
757
773
if (isRead ) {
758
774
memcpy (buf , & rxBuf [TPM_TIS_HEADER_SZ ], size );
775
+ #ifdef WOLFTPM_DEBUG_IO
776
+ wolfBoot_PrintBin (buf , size );
777
+ #endif
759
778
}
760
779
#endif
761
780
@@ -764,8 +783,8 @@ static int TPM2_IoCb(TPM2_CTX* ctx, const byte* txBuf, byte* rxBuf,
764
783
#endif /* !ARCH_SIM */
765
784
766
785
#if defined(WOLFBOOT_TPM ) && defined(WOLFBOOT_MEASURED_BOOT )
767
- #define measure_boot (hash ) measure_boot_at ((hash), __LINE__)
768
- static int measure_boot_at (uint8_t * hash , int line )
786
+ #define measure_boot (hash ) wolfBoot_tpm2_extend ((hash), __LINE__)
787
+ static int wolfBoot_tpm2_extend (uint8_t * hash , int line )
769
788
{
770
789
int rc ;
771
790
PCR_Extend_In pcrExtend ;
@@ -1178,12 +1197,44 @@ static int keyslot_id_by_sha(const uint8_t *hint)
1178
1197
/* Override global */
1179
1198
uint8_t digest [WOLFBOOT_SHA_DIGEST_SIZE ];
1180
1199
#endif
1200
+
1201
+ #if defined(WOLFBOOT_TPM ) && defined(WOLFBOOT_TPM_KEYSTORE )
1202
+ /* use public key hash (hint) */
1203
+ int rc ;
1204
+ WOLFTPM2_NV nv ;
1205
+ word32 digestSz = (word32 )TPM2_GetHashDigestSize (WOLFBOOT_TPM_HASH_ALG );
1206
+ XMEMSET (& nv , 0 , sizeof (nv ));
1207
+ nv .handle .hndl = WOLFBOOT_TPM_KEYSTORE_NV_INDEX ;
1208
+
1209
+ #if 0 /* TODO: Add auth */
1210
+ nv .handle .auth .size = sizeof (authBuf );
1211
+ XMEMCPY (nv .handle .auth .buffer , authBuf , sizeof (authBuf ));
1212
+ #endif
1213
+
1214
+ rc = wolfTPM2_NVReadAuth (& wolftpm_dev , & nv , WOLFBOOT_TPM_KEYSTORE_NV_INDEX ,
1215
+ digest , & digestSz , 0 );
1216
+ if (rc == 0 && memcmp (digest , hint , WOLFBOOT_SHA_DIGEST_SIZE ) == 0 ) {
1217
+ #ifdef DEBUG_WOLFTPM
1218
+ wolfBoot_printf ("TPM Root of Trust valid\n" );
1219
+ #endif
1220
+ return 0 ;
1221
+ }
1222
+ else {
1223
+ #ifdef DEBUG_WOLFTPM
1224
+ wolfBoot_printf ("TPM Root of Trust failed! %d (%s)\n" ,
1225
+ rc , wolfTPM2_GetRCString (rc ));
1226
+ wolfBoot_printf ("Expected Hash %d\n" , WOLFBOOT_SHA_DIGEST_SIZE );
1227
+ wolfBoot_PrintBin (hint , WOLFBOOT_SHA_DIGEST_SIZE );
1228
+ #endif
1229
+ }
1230
+ #else
1181
1231
int id = 0 ;
1182
1232
for (id = 0 ; id < keystore_num_pubkeys (); id ++ ) {
1183
1233
key_hash (id , digest );
1184
1234
if (memcmp (digest , hint , WOLFBOOT_SHA_DIGEST_SIZE ) == 0 )
1185
1235
return id ;
1186
1236
}
1237
+ #endif
1187
1238
return -1 ;
1188
1239
}
1189
1240
#endif
0 commit comments