2222
2323/* Provisioning process:
2424 * 1. Generate a server keypair into key cache as keyId 27
25- * 2. Commit the server keypair to server NVM
25+ * 2. Commit the server keypair to server NVM
2626 * 3. Map a file into memory and ask server to hash it using SHA256
2727 * 4. Sign the hash using the server keypair
28- * 5. Store the signature to server NVM as object 29
28+ * 5. Store the signature to server NVM as object 29
2929 * 6. Hexdump hash, public key, and signature
3030 * Note: Provisioning can also be done offline using the whnvmtool
31- *
31+ *
3232 * SecBoot process:
3333 * 1. Load the signature from server NVM as object 29
3434 * 2. Map a file into memory and ask server to hash it using SHA256
3535 * 3. Verify the signature using server keyId 27
3636 * 4. Hexdump hash, public key, and signature
37- *
37+ *
3838 * Zeroization process:
3939 * 1. Destroy keyId 27
4040 * 2. Destroy nvmId 29
@@ -52,13 +52,12 @@ static int _showNvm(whClientContext* clientContext);
5252
5353static int _provisionMakeCommitKey (whClientContext * clientContext );
5454static int _sha256File (const char * file_to_measure , uint8_t * hash );
55- static int _signHash ( const uint8_t * hash , size_t hash_len ,
56- uint8_t * sig , uint16_t * sig_len );
57- static int _verifyHash ( const uint8_t * hash , size_t hash_len ,
58- const uint8_t * sig , uint16_t sig_len ,
59- int32_t * rc );
55+ static int _signHash (const uint8_t * hash , size_t hash_len , uint8_t * sig ,
56+ uint16_t * sig_len );
57+ static int _verifyHash (const uint8_t * hash , size_t hash_len , const uint8_t * sig ,
58+ uint16_t sig_len , int32_t * rc );
6059
61- static int _showNvm (whClientContext * clientContext )
60+ static int _showNvm (whClientContext * clientContext )
6261{
6362 int ret = 0 ;
6463 whNvmAccess access = WH_NVM_ACCESS_ANY ;
@@ -68,52 +67,47 @@ static int _showNvm(whClientContext* clientContext)
6867
6968 printf ("NVM Contents:\n" );
7069 do {
71- ret = wh_Client_NvmList ( clientContext ,
72- access , flags ,
73- id , NULL ,
74- & count , & id );
70+ ret = wh_Client_NvmList (clientContext , access , flags , id , NULL , & count ,
71+ & id );
7572 if (ret != WH_ERROR_OK ) {
7673 printf ("wh_Client_NvmList failed with ret:%d\n" , ret );
7774 break ;
7875 }
79- printf ( "NVM List: count=%u, id=%u\n" ,
80- ( unsigned int ) count , (unsigned int )id );
76+ printf ("NVM List: count=%u, id=%u\n" , ( unsigned int ) count ,
77+ (unsigned int )id );
8178
8279 if (count > 0 ) {
8380 whNvmSize data_len = 0 ;
8481 uint8_t label [WH_NVM_LABEL_LEN ] = {0 };
85- ret = wh_Client_NvmGetMetadata ( clientContext , id ,
86- NULL , NULL ,
87- NULL , NULL ,
88- & data_len ,
89- sizeof (label ), label );
82+ ret =
83+ wh_Client_NvmGetMetadata (clientContext , id , NULL , NULL , NULL ,
84+ NULL , & data_len , sizeof (label ), label );
9085 if (ret != WH_ERROR_OK ) {
9186 printf ("wh_Client_NvmGetMetadata failed with ret:%d\n" , ret );
9287 break ;
9388 }
9489
95- printf ( "NVM Object ID %u has label '%-*s' and size:%u\n" ,
96- (unsigned int )id ,
97- (int )sizeof (label ), label ,
98- (unsigned int )data_len );
90+ printf ("NVM Object ID %u has label '%-*s' and size:%u\n" ,
91+ (unsigned int )id , (int )sizeof (label ), label ,
92+ (unsigned int )data_len );
9993 }
10094 } while (count > 0 );
10195 printf ("End of NVM Contents\n" );
10296 return ret ;
10397}
10498
105- static int _provisionMakeCommitKey (whClientContext * clientContext )
99+ static int _provisionMakeCommitKey (whClientContext * clientContext )
106100{
107101 int ret ;
108-
102+
109103 /* Use the default ECC curve for 32 byte key, likely P256r1 */
110104 whKeyId keyId = prov_keyId ;
111105 uint8_t keyLabel [WH_NVM_LABEL_LEN ] = {0 };
112106 memcpy (keyLabel , prov_keyLabel , sizeof (prov_keyLabel ));
113107
114- ret = wh_Client_EccMakeCacheKey (clientContext , 32 , ECC_CURVE_DEF ,
115- & keyId , WH_NVM_FLAGS_NONE ,
116- sizeof ( prov_keyLabel ), keyLabel );
108+ ret = wh_Client_EccMakeCacheKey (clientContext , 32 , ECC_CURVE_DEF , & keyId ,
109+ WH_NVM_FLAGS_NONE , sizeof ( prov_keyLabel ),
110+ keyLabel );
117111 if (ret == WH_ERROR_OK ) {
118112 ret = wh_Client_KeyCommit (clientContext , prov_keyId );
119113 }
@@ -132,8 +126,8 @@ static int _sha256File(const char* file_to_measure, uint8_t* hash)
132126 close (fd );
133127
134128 if (ptr != (void * )-1 ) {
135- printf ("Generating SHA256 of %s over %u bytes at %p\n" ,
136- file_to_measure , (unsigned int )size , ptr );
129+ printf ("Generating SHA256 of %s over %u bytes at %p\n" ,
130+ file_to_measure , (unsigned int )size , ptr );
137131 wc_Sha256 sha256 [1 ];
138132 ret = wc_InitSha256_ex (sha256 , NULL , WH_DEV_ID );
139133 if (ret == 0 ) {
@@ -157,8 +151,8 @@ static int _sha256File(const char* file_to_measure, uint8_t* hash)
157151 return ret ;
158152}
159153
160- static int _signHash ( const uint8_t * hash , size_t hash_len ,
161- uint8_t * sig , uint16_t * sig_len )
154+ static int _signHash (const uint8_t * hash , size_t hash_len , uint8_t * sig ,
155+ uint16_t * sig_len )
162156{
163157 ecc_key key [1 ];
164158 int ret = wc_ecc_init_ex (key , NULL , WH_DEV_ID );
@@ -177,19 +171,17 @@ static int _signHash( const uint8_t* hash, size_t hash_len,
177171 return ret ;
178172}
179173
180- static int _verifyHash ( const uint8_t * hash , size_t hash_len ,
181- const uint8_t * sig , uint16_t sig_len ,
182- int32_t * rc )
174+ static int _verifyHash (const uint8_t * hash , size_t hash_len , const uint8_t * sig ,
175+ uint16_t sig_len , int32_t * rc )
183176{
184177 ecc_key key [1 ];
185178 int ret = wc_ecc_init_ex (key , NULL , WH_DEV_ID );
186179 if (ret == 0 ) {
187180 ret = wh_Client_EccSetKeyId (key , prov_keyId );
188181 if (ret == 0 ) {
189182 int res = 0 ;
190- ret = wc_ecc_verify_hash ( sig , (word32 )sig_len ,
191- hash , (word32 )hash_len ,
192- & res , key );
183+ ret = wc_ecc_verify_hash (sig , (word32 )sig_len , hash ,
184+ (word32 )hash_len , & res , key );
193185 if (ret == 0 ) {
194186 * rc = res ;
195187 }
@@ -208,7 +200,7 @@ int wh_DemoClient_SecBoot_Provision(whClientContext* clientContext)
208200 if (clientContext == NULL ) {
209201 return WH_ERROR_BADARGS ;
210202 }
211-
203+
212204 ret = wh_Client_CommInit (clientContext , & client_id , & server_id );
213205 if (ret == WH_ERROR_OK ) {
214206 printf ("Provision client connected to server id %u with client id %u\n" ,
@@ -227,8 +219,7 @@ int wh_DemoClient_SecBoot_Provision(whClientContext* clientContext)
227219 uint16_t siglen = sizeof (sig );
228220
229221 printf ("Signing hash...\n" );
230- ret = _signHash ( hash , sizeof (hash ),
231- sig , & siglen );
222+ ret = _signHash (hash , sizeof (hash ), sig , & siglen );
232223 if (ret == WH_ERROR_OK ) {
233224 int32_t rc = 0 ;
234225 uint8_t sigLabel [WH_NVM_LABEL_LEN ] = {0 };
@@ -237,11 +228,10 @@ int wh_DemoClient_SecBoot_Provision(whClientContext* clientContext)
237228 wh_Utils_Hexdump ("Signature:\n" , sig , siglen );
238229 printf ("Storing the signature in NVM as nvmId %u\n" ,
239230 sig_nvmId );
240- ret = wh_Client_NvmAddObject (clientContext , sig_nvmId ,
241- WH_NVM_ACCESS_NONE , WH_NVM_FLAGS_NONE ,
242- sizeof (sig_nvmLabel ), sigLabel ,
243- siglen , sig ,
244- & rc );
231+ ret = wh_Client_NvmAddObject (
232+ clientContext , sig_nvmId , WH_NVM_ACCESS_NONE ,
233+ WH_NVM_FLAGS_NONE , sizeof (sig_nvmLabel ), sigLabel ,
234+ siglen , sig , & rc );
245235 printf ("Stored signature with ret:%d and rc:%d\n" , ret , rc );
246236 }
247237 }
@@ -266,7 +256,7 @@ int wh_DemoClient_SecBoot_Boot(whClientContext* clientContext)
266256 if (ret == WH_ERROR_OK ) {
267257 printf ("SecBoot Client connected to server id %u with client id %u\n" ,
268258 server_id , client_id );
269-
259+
270260 _showNvm (clientContext );
271261
272262 uint8_t sig [ECC_MAX_SIG_SIZE ] = {0 };
@@ -276,10 +266,19 @@ int wh_DemoClient_SecBoot_Boot(whClientContext* clientContext)
276266 sig_nvmId );
277267 ret = wh_Client_NvmGetMetadata (clientContext ,
278268 sig_nvmId , & rc , NULL , NULL , NULL , & siglen , 0 , NULL );
269+ if (ret != WH_ERROR_OK ) {
270+ printf ("wh_Client_NvmGetMetadata failed with ret:%d\n" , ret );
271+ return ret ;
272+ }
279273 printf ("SecBoot got siglen %d with ret:%d rc:%d\n" , siglen , ret , rc );
280- ret = wh_Client_NvmRead (clientContext , sig_nvmId ,
281- 0 , siglen , & rc ,
282- NULL , sig );
274+ ret = wh_Client_NvmRead (clientContext , sig_nvmId , 0 , siglen , & rc , NULL ,
275+ sig );
276+ if (ret != WH_ERROR_OK || rc != 0 ) {
277+ printf ("Read Object %d failed with error code: %d, server error "
278+ "code: %d\n" ,
279+ sig_nvmId , ret , rc );
280+ return (ret != WH_ERROR_OK ) ? ret : rc ;
281+ }
283282 wh_Utils_Hexdump ("Signature:\n" , sig , siglen );
284283
285284
@@ -289,9 +288,7 @@ int wh_DemoClient_SecBoot_Boot(whClientContext* clientContext)
289288 if (ret == WH_ERROR_OK ) {
290289
291290 printf ("SecBoot Client Verifying signature using keyId %u\n" , prov_keyId );
292- ret = _verifyHash ( hash , sizeof (hash ),
293- sig , siglen ,
294- & rc );
291+ ret = _verifyHash (hash , sizeof (hash ), sig , siglen , & rc );
295292 printf ("ecc_verify:%d rc:%d\n" , ret , rc );
296293
297294 if ((ret == 0 ) && (rc == 1 )) {
@@ -326,8 +323,8 @@ int wh_DemoClient_SecBoot_Zeroize(whClientContext* clientContext)
326323 printf ("Zeroize Client erased keyId:%u ret:%d\n" , prov_keyId , ret );
327324
328325 ret = wh_Client_NvmDestroyObjects (clientContext , 1 , & sig_nvmId , & rc );
329- printf ("Zeroize Client destroyed NVM object:%u ret:%d with rc:%d\n" ,
330- sig_nvmId , ret , rc );
326+ printf ("Zeroize Client destroyed NVM object:%u ret:%d with rc:%d\n" ,
327+ sig_nvmId , ret , rc );
331328
332329 _showNvm (clientContext );
333330 }
0 commit comments