3535
3636#ifdef WOLFHSM_CFG_KEYWRAP
3737
38- #define WH_TEST_KEKID 1
38+ #define WH_DEMO_KEYWRAP_KEKID 1
3939static int _InitServerKek (whClientContext * ctx )
4040{
4141 /* IMPORTANT NOTE: Server KEK is typically intrinsic or set during
4242 * provisioning. Uploading the KEK via the client is for testing purposes
4343 * only and not intended as a recommendation */
44- whKeyId serverKeyId = WH_TEST_KEKID ;
44+ whKeyId serverKeyId = WH_DEMO_KEYWRAP_KEKID ;
4545 whNvmFlags flags = WH_NVM_FLAGS_NONEXPORTABLE ;
4646 uint8_t label [WH_NVM_LABEL_LEN ] = "Server KEK key" ;
4747 uint8_t kek [] = {0x03 , 0x03 , 0x0d , 0xd9 , 0xeb , 0x18 , 0x17 , 0x2e ,
@@ -55,43 +55,44 @@ static int _InitServerKek(whClientContext* ctx)
5555
5656static int _CleanupServerKek (whClientContext * ctx )
5757{
58- return wh_Client_KeyErase (ctx , WH_TEST_KEKID );
58+ return wh_Client_KeyErase (ctx , WH_DEMO_KEYWRAP_KEKID );
5959}
6060
6161#ifndef NO_AES
6262#ifdef HAVE_AESGCM
6363
64- #define WH_TEST_AES_KEYSIZE 16
65- #define WH_TEST_AES_TEXTSIZE 16
66- #define WH_TEST_AES_IVSIZE 12
67- #define WH_TEST_AES_TAGSIZE 16
68- #define WH_TEST_AES_WRAPPED_KEYSIZE \
69- (WH_TEST_AES_IVSIZE + WH_TEST_AES_TAGSIZE + WH_TEST_AES_KEYSIZE + \
70- sizeof(whNvmMetadata))
71- #define WH_TEST_AESGCM_WRAPKEY_ID 8
64+ #define WH_DEMO_KEYWRAP_AES_KEYSIZE 16
65+ #define WH_DEMO_KEYWRAP_AES_TEXTSIZE 16
66+ #define WH_DEMO_KEYWRAP_AES_IVSIZE 12
67+ #define WH_DEMO_KEYWRAP_AES_TAGSIZE 16
68+ #define WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE \
69+ (WH_DEMO_KEYWRAP_AES_IVSIZE + WH_DEMO_KEYWRAP_AES_TAGSIZE + \
70+ WH_DEMO_KEYWRAP_AES_KEYSIZE + sizeof(whNvmMetadata))
71+ #define WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID 8
7272
7373int wh_DemoClient_AesGcmKeyWrap (whClientContext * client )
7474{
7575 int ret = 0 ;
7676 Aes aes [1 ];
7777 WC_RNG rng [1 ];
78- uint8_t key [WH_TEST_AES_KEYSIZE ];
79- uint8_t exportedKey [WH_TEST_AES_KEYSIZE ];
78+ uint8_t key [WH_DEMO_KEYWRAP_AES_KEYSIZE ];
79+ uint8_t exportedKey [WH_DEMO_KEYWRAP_AES_KEYSIZE ];
8080 whNvmMetadata metadata = {
81- .id = WH_MAKE_KEYID (WH_KEYTYPE_CRYPTO , 0 , WH_TEST_AESGCM_WRAPKEY_ID ),
82- .label = "AES Key Label" ,
81+ .id = WH_CLIENT_KEYID_MAKE_WRAPPED_META (
82+ client -> comm -> client_id , WH_DEMO_KEYWRAP_AESGCM_WRAPKEY_ID ),
83+ .label = "AES Key Label" ,
8384 .access = WH_NVM_ACCESS_ANY ,
84- .len = WH_TEST_AES_KEYSIZE };
85+ .len = WH_DEMO_KEYWRAP_AES_KEYSIZE };
8586 whNvmMetadata exportedMetadata ;
86- uint8_t wrappedKey [WH_TEST_AES_WRAPPED_KEYSIZE ];
87+ uint8_t wrappedKey [WH_DEMO_KEYWRAP_AES_WRAPPED_KEYSIZE ];
8788 whKeyId wrappedKeyId ;
8889
8990 const uint8_t plaintext [] = "hello, wolfSSL AES-GCM!" ;
9091 uint8_t ciphertext [sizeof (plaintext )];
9192 uint8_t decrypted [sizeof (plaintext )];
9293
93- uint8_t tag [WH_TEST_AES_TAGSIZE ];
94- uint8_t iv [WH_TEST_AES_IVSIZE ];
94+ uint8_t tag [WH_DEMO_KEYWRAP_AES_TAGSIZE ];
95+ uint8_t iv [WH_DEMO_KEYWRAP_AES_IVSIZE ];
9596 const uint8_t aad [] = {0xfe , 0xed , 0xfa , 0xce , 0xde , 0xad , 0xbe ,
9697 0xef , 0xfe , 0xed , 0xfa , 0xce , 0xde , 0xad ,
9798 0xbe , 0xef , 0xab , 0xad , 0xda , 0xd2 };
@@ -127,8 +128,8 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
127128
128129 /* Now we request the server to wrap the key using the KEK we
129130 * establish above in the first step. */
130- ret = wh_Client_KeyWrap (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID , key ,
131- sizeof (key ), & metadata , wrappedKey ,
131+ ret = wh_Client_KeyWrap (client , WC_CIPHER_AES_GCM , WH_DEMO_KEYWRAP_KEKID ,
132+ key , sizeof (key ), & metadata , wrappedKey ,
132133 sizeof (wrappedKey ));
133134 if (ret != 0 ) {
134135 printf ("Failed to wh_Client_KeyWrap %d\n" , ret );
@@ -144,9 +145,9 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
144145 /* Request the server to unwrap and cache the wrapped key we just created.
145146 * This will provide us back a key ID that the client can use to do crypto
146147 * operations */
147- ret = wh_Client_KeyUnwrapAndCache (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
148- wrappedKey , sizeof ( wrappedKey ) ,
149- & wrappedKeyId );
148+ ret = wh_Client_KeyUnwrapAndCache (client , WC_CIPHER_AES_GCM ,
149+ WH_DEMO_KEYWRAP_KEKID , wrappedKey ,
150+ sizeof ( wrappedKey ), & wrappedKeyId );
150151 if (ret != 0 ) {
151152 printf ("Failed to wh_Client_KeyUnwrapAndCache %d\n" , ret );
152153 goto cleanup_rng ;
@@ -161,7 +162,8 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
161162
162163 /* Set the key id for this AES context to the wrapped key ID that the server
163164 * provided us */
164- ret = wh_Client_AesSetKeyId (aes , wrappedKeyId );
165+ ret =
166+ wh_Client_AesSetKeyId (aes , WH_CLIENT_KEYID_MAKE_WRAPPED (wrappedKeyId ));
165167 if (ret != 0 ) {
166168 printf ("Failed to wh_Client_AesSetKeyId %d\n" , ret );
167169 goto cleanup_aes ;
@@ -207,12 +209,12 @@ int wh_DemoClient_AesGcmKeyWrap(whClientContext* client)
207209 /* Exporting a wrapped key */
208210
209211 /* Request the server to unwrap and export the wrapped key we created */
210- ret = wh_Client_KeyUnwrapAndExport (client , WC_CIPHER_AES_GCM , WH_TEST_KEKID ,
211- wrappedKey , sizeof ( wrappedKey ) ,
212- & exportedMetadata , exportedKey ,
213- sizeof (exportedKey ));
212+ ret = wh_Client_KeyUnwrapAndExport (client , WC_CIPHER_AES_GCM ,
213+ WH_DEMO_KEYWRAP_KEKID , wrappedKey ,
214+ sizeof ( wrappedKey ), & exportedMetadata ,
215+ exportedKey , sizeof (exportedKey ));
214216 if (ret != 0 ) {
215- printf ("Failed to wh_Client_KeyUnwrapAndCache %d\n" , ret );
217+ printf ("Failed to wh_Client_KeyUnwrapAndExport %d\n" , ret );
216218 goto cleanup_aes ;
217219 }
218220
0 commit comments