3737
3838#ifdef WOLFHSM_CFG_KEYWRAP
3939
40- #define WH_TEST_KEKID 1
40+ #define WH_DEMO_KEYWRAP_KEKID 1
4141static int _InitServerKek (whClientContext * ctx )
4242{
4343 /* IMPORTANT NOTE: Server KEK is typically intrinsic or set during
4444 * provisioning. Uploading the KEK via the client is for testing purposes
4545 * only and not intended as a recommendation */
46- whKeyId serverKeyId = WH_TEST_KEKID ;
46+ whKeyId serverKeyId = WH_DEMO_KEYWRAP_KEKID ;
4747 whNvmFlags flags = WH_NVM_FLAGS_NONEXPORTABLE ;
4848 uint8_t label [WH_NVM_LABEL_LEN ] = "Server KEK key" ;
4949 uint8_t kek [] = {0x03 , 0x03 , 0x0d , 0xd9 , 0xeb , 0x18 , 0x17 , 0x2e ,
@@ -57,43 +57,44 @@ static int _InitServerKek(whClientContext* ctx)
5757
5858static int _CleanupServerKek (whClientContext * ctx )
5959{
60- return wh_Client_KeyErase (ctx , WH_TEST_KEKID );
60+ return wh_Client_KeyErase (ctx , WH_DEMO_KEYWRAP_KEKID );
6161}
6262
6363#ifndef NO_AES
6464#ifdef HAVE_AESGCM
6565
66- #define WH_TEST_AES_KEYSIZE 16
67- #define WH_TEST_AES_TEXTSIZE 16
68- #define WH_TEST_AES_IVSIZE 12
69- #define WH_TEST_AES_TAGSIZE 16
70- #define WH_TEST_AES_WRAPPED_KEYSIZE \
71- (WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_AES_KEYSIZE + \
72- sizeof(whNvmMetadata))
73- #define WH_TEST_AESGCM_WRAPKEY_ID 8
66+ #define WH_DEMO_KEYWRAP_AES_KEYSIZE 16
67+ #define WH_DEMO_KEYWRAP_AES_TEXTSIZE 16
68+ #define WH_DEMO_KEYWRAP_AES_IVSIZE 12
69+ #define WH_DEMO_KEYWRAP_AES_TAGSIZE 16
70+ #define WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE \
71+ (WH_DEMO_KEYWRAP_AES_IVSIZE + WH_DEMO_KEYWRAP_AES_TAGSIZE + \
72+ WH_DEMO_KEYWRAP_AES_KEYSIZE + sizeof(whNvmMetadata))
73+ #define WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID 8
7474
7575int wh_DemoClient_AesGcmKeyWrap (whClientContext * client )
7676{
7777 int ret = 0 ;
7878 Aes aes [1 ];
7979 WC_RNG rng [1 ];
80- uint8_t key [WH_TEST_AES_KEYSIZE ];
81- uint8_t exportedKey [WH_TEST_AES_KEYSIZE ];
80+ uint8_t key [WH_DEMO_KEYWRAP_AES_KEYSIZE ];
81+ uint8_t exportedKey [WH_DEMO_KEYWRAP_AES_KEYSIZE ];
8282 whNvmMetadata metadata = {
83- .id = WH_MAKE_KEYID (WH_KEYTYPE_CRYPTO , 0 , WH_TEST_AESGCM_WRAPKEY_ID ),
84- .label = "AES Key Label" ,
83+ .id = WH_CLIENT_KEYID_MAKE_WRAPPED_META (
84+ client -> comm -> client_id , WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID ),
85+ .label = "AES Key Label" ,
8586 .access = WH_NVM_ACCESS_ANY ,
86- .len = WH_TEST_AES_KEYSIZE };
87+ .len = WH_DEMO_KEYWRAP_AES_KEYSIZE };
8788 whNvmMetadata exportedMetadata ;
88- uint8_t wrappedKey [WH_TEST_AES_WRAPPED_KEYSIZE ];
89+ uint8_t wrappedKey [WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE ];
8990 whKeyId wrappedKeyId ;
9091
9192 const uint8_t plaintext [] = "hello, wolfSSL AES-GCM!" ;
9293 uint8_t ciphertext [sizeof (plaintext )];
9394 uint8_t decrypted [sizeof (plaintext )];
9495
95- uint8_t tag [WH_TEST_AES_TAGSIZE ];
96- uint8_t iv [WH_TEST_AES_IVSIZE ];
96+ uint8_t tag [WH_DEMO_KEYWRAP_AES_TAGSIZE ];
97+ uint8_t iv [WH_DEMO_KEYWRAP_AES_IVSIZE ];
9798 const uint8_t aad [] = {0xfe , 0xed , 0xfa , 0xce , 0xde , 0xad , 0xbe ,
9899 0xef , 0xfe , 0xed , 0xfa , 0xce , 0xde , 0xad ,
99100 0xbe , 0xef , 0xab , 0xad , 0xda , 0xd2 };
@@ -129,8 +130,8 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
129130
130131 /* Now we request the server to wrap the key using the KEK we
131132 * establish above in the first step. */
132- ret = wh_Client_KeyWrap (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID , key ,
133- sizeof (key ), & metadata , wrappedKey ,
133+ ret = wh_Client_KeyWrap (client , WC_CIPHER_AES_GCM , WH_DEMO_KEYWRAP_KEKID ,
134+ key , sizeof (key ), & metadata , wrappedKey ,
134135 sizeof (wrappedKey ));
135136 if (ret != 0 ) {
136137 printf ("Failed to wh_Client_KeyWrap %d\n" , ret );
@@ -146,9 +147,9 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
146147 /* Request the server to unwrap and cache the wrapped key we just created.
147148 * This will provide us back a key ID that the client can use to do crypto
148149 * operations */
149- ret = wh_Client_KeyUnwrapAndCache (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
150- wrappedKey , sizeof ( wrappedKey ) ,
151- & wrappedKeyId );
150+ ret = wh_Client_KeyUnwrapAndCache (client , WC_CIPHER_AES_GCM ,
151+ WH_DEMO_KEYWRAP_KEKID , wrappedKey ,
152+ sizeof ( wrappedKey ), & wrappedKeyId );
152153 if (ret != 0 ) {
153154 printf ("Failed to wh_Client_KeyUnwrapAndCache %d\n" , ret );
154155 goto cleanup_rng ;
@@ -163,7 +164,8 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
163164
164165 /* Set the key id for this AES context to the wrapped key ID that the server
165166 * provided us */
166- ret = wh_Client_AesSetKeyId (aes , wrappedKeyId );
167+ ret =
168+ wh_Client_AesSetKeyId (aes , WH_CLIENT_KEYID_MAKE_WRAPPED (wrappedKeyId ));
167169 if (ret != 0 ) {
168170 printf ("Failed to wh_Client_AesSetKeyId %d\n" , ret );
169171 goto cleanup_aes ;
@@ -209,12 +211,12 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
209211 /* Exporting a wrapped key */
210212
211213 /* Request the server to unwrap and export the wrapped key we created */
212- ret = wh_Client_KeyUnwrapAndExport (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
213- wrappedKey , sizeof ( wrappedKey ) ,
214- & exportedMetadata , exportedKey ,
215- sizeof (exportedKey ));
214+ ret = wh_Client_KeyUnwrapAndExport (client , WC_CIPHER_AES_GCM ,
215+ WH_DEMO_KEYWRAP_KEKID , wrappedKey ,
216+ sizeof ( wrappedKey ), & exportedMetadata ,
217+ exportedKey , sizeof (exportedKey ));
216218 if (ret != 0 ) {
217- printf ("Failed to wh_Client_KeyUnwrapAndCache %d\n" , ret );
219+ printf ("Failed to wh_Client_KeyUnwrapAndExport %d\n" , ret );
218220 goto cleanup_aes ;
219221 }
220222
0 commit comments