1818 */
1919
2020#include "wolfhsm/wh_settings.h"
21- #ifdef WOLFHSM_CFG_WRAPKEY
2221#include <stdint.h>
2322#include <stdio.h>
2423#include <string.h>
2726#include "wolfhsm/wh_error.h"
2827#include "wolfhsm/wh_client.h"
2928#include "wolfhsm/wh_client_crypto.h"
30- #include "wolfhsm/wh_client_wrapkey.h"
3129
3230#include "wolfssl/wolfcrypt/settings.h"
3331#include "wolfssl/wolfcrypt/aes.h"
3432#include "wolfssl/wolfcrypt/random.h"
3533
36- #include "wh_demo_client_wrapkey .h"
34+ #include "wh_demo_client_keywrap .h"
3735
3836#ifndef NO_AES
3937#define HAVE_AESGCM
4846 sizeof(whNvmMetadata))
4947#define WH_TEST_WRAPKEY_ID 8
5048
51- int wh_DemoClient_AesGcmWrapKeyBasic (whClientContext * ctx , WC_RNG * rng )
49+ int wh_DemoClient_AesGcmKeyWrapBasic (whClientContext * ctx , WC_RNG * rng )
5250{
5351 int ret = 0 ;
54- uint8_t iv [AES_BLOCK_SIZE ];
55- uint8_t key [WH_TEST_AES_KEYSIZE ];
56- uint8_t plainKey [WH_TEST_AES_KEYSIZE ];
57- uint8_t tmpPlainKey [WH_TEST_AES_KEYSIZE ];
52+ uint8_t kek [WH_TEST_AES_KEYSIZE ];
53+ uint8_t clientKey [WH_TEST_AES_KEYSIZE ];
54+ uint8_t tmpClientKey [WH_TEST_AES_KEYSIZE ];
5855 uint8_t wrappedKey [WH_TEST_AES_WRAPPED_KEYSIZE ];
5956 uint8_t label [WH_NVM_LABEL_LEN ] = "Server AES Key Label" ;
6057 whKeyId serverKeyId ;
@@ -65,61 +62,61 @@ int wh_DemoClient_AesGcmWrapKeyBasic(whClientContext* ctx, WC_RNG* rng)
6562 .len = WH_TEST_AES_KEYSIZE };
6663 whNvmMetadata tmpMetadata ;
6764
68- /* Randomize inputs */
69- ret = wc_RNG_GenerateBlock (rng , key , sizeof (key ));
65+ /* Generate a random KEK to encrypt the client key */
66+ ret = wc_RNG_GenerateBlock (rng , kek , sizeof (kek ));
7067 if (ret != 0 ) {
7168 printf ("Failed to wc_RNG_GenerateBlock for key %d\n" , ret );
7269 return ret ;
7370 }
74-
75- ret = wc_RNG_GenerateBlock (rng , plainKey , sizeof (plainKey ));
71+
72+ /* Generate a random client key */
73+ ret = wc_RNG_GenerateBlock (rng , clientKey , sizeof (clientKey ));
7674 if (ret != 0 ) {
7775 printf ("Failed to wc_RNG_GenerateBlock for key data %d\n" , ret );
7876 return ret ;
7977 }
8078
81- ret = wc_RNG_GenerateBlock (rng , iv , sizeof (iv ));
82- if (ret != 0 ) {
83- printf ("Failed to wc_RNG_GenerateBlock for IV %d\n" , ret );
84- return ret ;
85- }
86-
87- /* Initialize the AES GCM Server key */
88- ret = wh_Client_KeyCache (ctx , 0 , label , sizeof (label ), key , sizeof (key ),
79+ /* Request the server to cache the KEK and give us back a key ID*/
80+ ret = wh_Client_KeyCache (ctx , 0 , label , sizeof (label ), kek , sizeof (kek ),
8981 & serverKeyId );
9082 if (ret != 0 ) {
9183 printf ("Failed to wh_Client_KeyCache %d\n" , ret );
9284 return ret ;
9385 }
9486
95- ret = wh_Client_WrapKey (ctx , WC_CIPHER_AES_GCM , serverKeyId , plainKey , sizeof (plainKey ),
87+ /* Request the server to wrap the client key using the KEK we just cached */
88+ ret = wh_Client_KeyWrap (ctx , WC_CIPHER_AES_GCM , serverKeyId , clientKey , sizeof (clientKey ),
9689 & metadata , wrappedKey , sizeof (wrappedKey ));
9790 if (ret != 0 ) {
98- printf ("Failed to wh_Client_WrapKey %d\n" , ret );
91+ printf ("Failed to wh_Client_KeyWrap %d\n" , ret );
9992 return ret ;
10093 }
10194
102- ret = wh_Client_UnwrapKeyCache (ctx , WC_CIPHER_AES_GCM , serverKeyId , wrappedKey ,
95+ /* Request the server to unwrap and cache the wrapped key we just created */
96+ ret = wh_Client_KeyUnwrapAndCache (ctx , WC_CIPHER_AES_GCM , serverKeyId , wrappedKey ,
10397 sizeof (wrappedKey ), & wrappedKeyId );
10498 if (ret != 0 ) {
105- printf ("Failed to wh_Client_UnwrapKeyCache %d\n" , ret );
99+ printf ("Failed to wh_Client_KeyUnwrapAndCache %d\n" , ret );
106100 return ret ;
107101 }
108102
109- ret = wh_Client_UnwrapKeyExport (ctx , WC_CIPHER_AES_GCM , serverKeyId , wrappedKey ,
103+ /* Request the server to unwrap and export the wrapped key we created */
104+ ret = wh_Client_KeyUnwrapAndExport (ctx , WC_CIPHER_AES_GCM , serverKeyId , wrappedKey ,
110105 sizeof (wrappedKey ), & tmpMetadata ,
111- tmpPlainKey , sizeof (tmpPlainKey ));
106+ tmpClientKey , sizeof (tmpClientKey ));
112107 if (ret != 0 ) {
113- printf ("Failed to wh_Client_UnwrapKeyCache %d\n" , ret );
108+ printf ("Failed to wh_Client_KeyUnwrapAndCache %d\n" , ret );
114109 return ret ;
115110 }
116111
117112
118- if (memcmp (plainKey , tmpPlainKey , sizeof (plainKey )) != 0 ) {
113+ /* Compare the exported key to the client key we requested to wrap */
114+ if (memcmp (clientKey , tmpClientKey , sizeof (clientKey )) != 0 ) {
119115 printf ("AES GCM wrap/unwrap key failed to match\n" );
120116 return ret ;
121117 }
122118
119+ /* Compare the exported metadata to the metadata we requested to wrap */
123120 if (memcmp (& metadata , & tmpMetadata , sizeof (metadata )) != 0 ) {
124121 printf ("AES GCM wrap/unwrap metadata failed to match\n" );
125122 return ret ;
@@ -130,19 +127,19 @@ int wh_DemoClient_AesGcmWrapKeyBasic(whClientContext* ctx, WC_RNG* rng)
130127
131128#endif /* HAVE_AESGCM */
132129
133- int wh_DemoClient_AesWrapKeyBasic (whClientContext * clientContext , WC_RNG * rng )
130+ int wh_DemoClient_AesKeyWrapBasic (whClientContext * clientContext , WC_RNG * rng )
134131{
135132 int ret = WH_ERROR_OK ;
136133
137134#ifdef HAVE_AESGCM
138- ret = wh_DemoClient_AesGcmWrapKeyBasic (clientContext , rng );
135+ ret = wh_DemoClient_AesGcmKeyWrapBasic (clientContext , rng );
139136#endif
140137
141138 return ret ;
142139}
143140
144141#endif /* !NO_AES */
145- int wh_DemoClient_WrapKeyBasic (whClientContext * clientContext )
142+ int wh_DemoClient_KeyWrapBasic (whClientContext * clientContext )
146143{
147144
148145 int ret ;
@@ -155,11 +152,9 @@ int wh_DemoClient_WrapKeyBasic(whClientContext* clientContext)
155152 }
156153
157154#ifndef NO_AES
158- ret = wh_DemoClient_AesWrapKeyBasic (clientContext , rng );
155+ ret = wh_DemoClient_AesKeyWrapBasic (clientContext , rng );
159156#endif
160157
161158 wc_FreeRng (rng );
162159 return ret ;
163160}
164-
165- #endif /* WOLFHSM_CFG_WRAPKEY */
0 commit comments