Skip to content

Commit 7c1c8b1

Browse files
authored
Merge pull request #75 from billphipps/doc_updates
Updates to correct documentation bug and minor API tweaks.
2 parents 3f537d4 + 469ba75 commit 7c1c8b1

File tree

5 files changed

+65
-174
lines changed

5 files changed

+65
-174
lines changed

src/wh_client.c

Lines changed: 0 additions & 157 deletions
Original file line numberDiff line numberDiff line change
@@ -1188,160 +1188,3 @@ int wh_Client_CounterDestroy(whClientContext* c, whNvmId counterId)
11881188
}
11891189
return ret;
11901190
}
1191-
1192-
#if 0
1193-
#ifndef WOLFHSM_CFG_NO_CRYPTO
1194-
1195-
#ifdef HAVE_CURVE25519
1196-
int wh_Client_SetKeyIdCurve25519(curve25519_key* key, whNvmId keyId)
1197-
{
1198-
if (key == NULL)
1199-
return WH_ERROR_BADARGS;
1200-
key->devCtx = (void*)((intptr_t)keyId);
1201-
key->pubSet = 1;
1202-
key->privSet = 1;
1203-
return WH_ERROR_OK;
1204-
}
1205-
1206-
int wh_Client_GetKeyIdCurve25519(curve25519_key* key, whNvmId* outId)
1207-
{
1208-
if (key == NULL || outId == NULL)
1209-
return WH_ERROR_BADARGS;
1210-
*outId = (intptr_t)key->devCtx;
1211-
return WH_ERROR_OK;
1212-
}
1213-
#endif /* HAVE_CURVE25519 */
1214-
1215-
#ifndef NO_RSA
1216-
int wh_Client_SetKeyIdRsa(RsaKey* key, whNvmId keyId)
1217-
{
1218-
if (key == NULL)
1219-
return WH_ERROR_BADARGS;
1220-
key->devCtx = (void*)((intptr_t)keyId);
1221-
return WH_ERROR_OK;
1222-
}
1223-
1224-
int wh_Client_GetKeyIdRsa(RsaKey* key, whNvmId* outId)
1225-
{
1226-
if (key == NULL || outId == NULL)
1227-
return WH_ERROR_BADARGS;
1228-
*outId = (intptr_t)key->devCtx;
1229-
return WH_ERROR_OK;
1230-
}
1231-
#endif
1232-
1233-
#ifndef NO_AES
1234-
int wh_Client_SetKeyIdAes(Aes* key, whNvmId keyId)
1235-
{
1236-
if (key == NULL)
1237-
return WH_ERROR_BADARGS;
1238-
key->devCtx = (void*)((intptr_t)keyId);
1239-
return WH_ERROR_OK;
1240-
}
1241-
1242-
int wh_Client_GetKeyIdAes(Aes* key, whNvmId* outId)
1243-
{
1244-
if (key == NULL || outId == NULL)
1245-
return WH_ERROR_BADARGS;
1246-
*outId = (intptr_t)key->devCtx;
1247-
return WH_ERROR_OK;
1248-
}
1249-
#endif
1250-
1251-
#ifdef WOLFSSL_CMAC
1252-
int wh_Client_SetKeyIdCmac(Cmac* key, whNvmId keyId)
1253-
{
1254-
if (key == NULL)
1255-
return WH_ERROR_BADARGS;
1256-
key->devCtx = (void*)((intptr_t)keyId);
1257-
return WH_ERROR_OK;
1258-
}
1259-
1260-
int wh_Client_GetKeyIdCmac(Cmac* key, whNvmId* outId)
1261-
{
1262-
if (key == NULL || outId == NULL)
1263-
return WH_ERROR_BADARGS;
1264-
*outId = (intptr_t)key->devCtx;
1265-
return WH_ERROR_OK;
1266-
}
1267-
1268-
int wh_Client_AesCmacGenerate(Cmac* cmac, byte* out, word32* outSz,
1269-
const byte* in, word32 inSz, whNvmId keyId, void* heap)
1270-
{
1271-
int ret;
1272-
ret = wc_InitCmac_ex(cmac, NULL, 0, WC_CMAC_AES, NULL, heap,
1273-
WH_DEV_ID);
1274-
/* set keyId */
1275-
if (ret == 0)
1276-
ret = wh_Client_SetKeyIdCmac(cmac, keyId);
1277-
if (ret == 0)
1278-
ret = wc_CmacUpdate(cmac, in, inSz);
1279-
if (ret == 0)
1280-
ret = wc_CmacFinal(cmac, out, outSz);
1281-
return ret;
1282-
}
1283-
1284-
int wh_Client_AesCmacVerify(Cmac* cmac, const byte* check, word32 checkSz,
1285-
const byte* in, word32 inSz, whNvmId keyId, void* heap)
1286-
{
1287-
int ret;
1288-
word32 outSz = AES_BLOCK_SIZE;
1289-
byte out[AES_BLOCK_SIZE];
1290-
ret = wc_InitCmac_ex(cmac, NULL, 0, WC_CMAC_AES, NULL, heap,
1291-
WH_DEV_ID);
1292-
/* set keyId */
1293-
if (ret == 0)
1294-
ret = wh_Client_SetKeyIdCmac(cmac, keyId);
1295-
if (ret == 0)
1296-
ret = wc_CmacUpdate(cmac, in, inSz);
1297-
if (ret == 0)
1298-
ret = wc_CmacFinal(cmac, out, &outSz);
1299-
if (ret == 0)
1300-
ret = memcmp(out, check, outSz) == 0 ? 0 : 1;
1301-
return ret;
1302-
}
1303-
1304-
int wh_Client_CmacCancelableResponse(whClientContext* c, Cmac* cmac,
1305-
uint8_t* out, uint16_t* outSz)
1306-
{
1307-
whPacket* packet;
1308-
uint8_t* packOut;
1309-
int ret;
1310-
uint16_t group;
1311-
uint16_t action;
1312-
uint16_t dataSz;
1313-
if (c == NULL || cmac == NULL)
1314-
return WH_ERROR_BADARGS;
1315-
packet = (whPacket*)wh_CommClient_GetDataPtr(c->comm);
1316-
/* out is after the fixed size fields */
1317-
packOut = (uint8_t*)(&packet->cmacRes + 1);
1318-
do {
1319-
ret = wh_Client_RecvResponse(c, &group, &action, &dataSz,
1320-
(uint8_t*)packet);
1321-
} while (ret == WH_ERROR_NOTREADY);
1322-
/* check for out of sequence action */
1323-
if (ret == 0 && (group != WH_MESSAGE_GROUP_CRYPTO ||
1324-
action != WC_ALGO_TYPE_CMAC)) {
1325-
ret = WH_ERROR_ABORTED;
1326-
}
1327-
if (ret == 0) {
1328-
if (packet->rc != 0)
1329-
ret = packet->rc;
1330-
/* read keyId and out */
1331-
else {
1332-
cmac->devCtx = (void*)((intptr_t)packet->cmacRes.keyId);
1333-
if (out != NULL) {
1334-
if (packet->cmacRes.outSz > *outSz)
1335-
ret = WH_ERROR_BADARGS;
1336-
else {
1337-
XMEMCPY(out, packOut, packet->cmacRes.outSz);
1338-
*outSz = packet->cmacRes.outSz;
1339-
}
1340-
}
1341-
}
1342-
}
1343-
return ret;
1344-
}
1345-
#endif /* WOLFSSL_CMAC */
1346-
#endif /* !WOLFHSM_CFG_NO_CRYPTO */
1347-
#endif /*0*/

src/wh_client_crypto.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static int _EccMakeKey(whClientContext* ctx,
7878
static int _Curve25519MakeKey(whClientContext* ctx,
7979
uint16_t size,
8080
whKeyId *inout_key_id, whNvmFlags flags,
81-
uint16_t label_len, uint8_t* label,
81+
const uint8_t* label, uint16_t label_len,
8282
curve25519_key* key);
8383
#endif /* HAVE_CURVE25519 */
8484

@@ -1230,7 +1230,7 @@ int wh_Client_Curve25519ExportKey(whClientContext* ctx, whKeyId keyId,
12301230
static int _Curve25519MakeKey(whClientContext* ctx,
12311231
uint16_t size,
12321232
whKeyId *inout_key_id, whNvmFlags flags,
1233-
uint16_t label_len, uint8_t* label,
1233+
const uint8_t* label, uint16_t label_len,
12341234
curve25519_key* key)
12351235
{
12361236
int ret = 0;
@@ -1329,7 +1329,7 @@ static int _Curve25519MakeKey(whClientContext* ctx,
13291329
int wh_Client_Curve25519MakeCacheKey(whClientContext* ctx,
13301330
uint16_t size,
13311331
whKeyId *inout_key_id, whNvmFlags flags,
1332-
uint16_t label_len, uint8_t* label)
1332+
const uint8_t* label, uint16_t label_len)
13331333
{
13341334
/* Valid keyid ptr is required in this form */
13351335
if (inout_key_id == NULL) {
@@ -1339,7 +1339,7 @@ int wh_Client_Curve25519MakeCacheKey(whClientContext* ctx,
13391339
return _Curve25519MakeKey(ctx,
13401340
size,
13411341
inout_key_id, flags,
1342-
label_len, label,
1342+
label, label_len,
13431343
NULL);
13441344
}
13451345

@@ -1354,7 +1354,7 @@ int wh_Client_Curve25519MakeExportKey(whClientContext* ctx,
13541354
return _Curve25519MakeKey(ctx,
13551355
size,
13561356
NULL, WH_NVM_FLAGS_EPHEMERAL,
1357-
0, NULL,
1357+
NULL, 0,
13581358
key);
13591359
}
13601360

test/wh_test_crypto.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -503,13 +503,13 @@ static int whTest_CryptoCurve25519(whClientContext* ctx, int devId, WC_RNG* rng)
503503
WH_ERROR_PRINT("Failed to wc_curve25519_init_ex %d\n", ret);
504504
} else {
505505
ret = wh_Client_Curve25519MakeCacheKey(ctx, key_size,
506-
&key_id_a, flags, sizeof(label_a), label_a);
506+
&key_id_a, flags, label_a, sizeof(label_a));
507507
if (ret != 0) {
508508
WH_ERROR_PRINT("Failed to make cached key %d\n", ret);
509509
}
510510
if (ret == 0) {
511511
ret = wh_Client_Curve25519MakeCacheKey(ctx, key_size,
512-
&key_id_b, flags, sizeof(label_b), label_b);
512+
&key_id_b, flags, label_b, sizeof(label_b));
513513
if (ret != 0) {
514514
WH_ERROR_PRINT("Failed to make cached key %d\n", ret);
515515
}

wolfhsm/wh_client.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@
5454
#ifndef WOLFHSM_CFG_NO_CRYPTO
5555

5656
/* Device Id to be registered and passed to wolfCrypt functions */
57-
enum {
57+
enum WH_CLIENT_DEVID_ENUM {
5858
WH_DEV_ID = 0x5748534D, /* "WHSM" */
5959
#ifdef WOLFHSM_CFG_DMA
6060
WH_DEV_ID_DMA = 0x57444D41, /* "WDMA" */
@@ -226,7 +226,7 @@ int wh_Client_CommInfoRequest(whClientContext* c);
226226
* size of each key in server RAM
227227
* @param[out] out_cfg_keycache_bigcount Pointer to store the server's number of
228228
* big keys in the server RAM
229-
* @param[out] out_cfg_keycache_big bufsize Pointer to store the server's
229+
* @param[out] out_cfg_keycache_bigbufsize bufsize Pointer to store the server's
230230
* maximum size of each big key in server RAM
231231
* @param[out] out_cfg_customcb_count Pointer to store the server's number of
232232
* custom callbacks
@@ -274,6 +274,10 @@ int wh_Client_CommInfoResponse(whClientContext* c,
274274
* keys in the server RAM
275275
* @param[out] out_cfg_keycache_bufsize Pointer to store the server's maximum
276276
* size of each key in server RAM
277+
* @param[out] out_cfg_keycache_bigcount Pointer to store the server's number of
278+
* keys in the server RAM
279+
* @param[out] out_cfg_keycache_bigbufsize Pointer to store the server's maximum
280+
* size of each key in server RAM
277281
* @param[out] out_cfg_customcb_count Pointer to store the server's number of
278282
* custom callbacks
279283
* @param[out] out_cfg_dmaaddr_count Pointer to store the server's number of
@@ -1699,5 +1703,4 @@ int wh_Client_CustomCbCheckRegisteredResponse(whClientContext* c,
16991703
int wh_Client_CustomCbCheckRegistered(whClientContext* c, uint16_t id,
17001704
int* responseError);
17011705

1702-
17031706
#endif /* !WOLFHSM_WH_CLIENT_H_ */

wolfhsm/wh_client_crypto.h

Lines changed: 52 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -130,18 +130,63 @@ int wh_Client_Curve25519ImportKey(whClientContext* ctx, curve25519_key* key,
130130
int wh_Client_Curve25519ExportKey(whClientContext* ctx, whKeyId keyId,
131131
curve25519_key* key, uint16_t label_len, uint8_t* label);
132132

133-
/* TODO: Generate a Curve25519 key on the server and put it in the server keycache */
133+
/**
134+
* @brief Generate a Curve25519 key in the server key cache
135+
*
136+
* This function requests the server to generate a new Curve25519 key and insert
137+
* it into the server's key cache.
138+
*
139+
* @param[in] ctx Pointer to the client context
140+
* @param[in] size Size of the key to generate in bytes, normally set to
141+
* CURVE25519_KEY_SIZE.
142+
* @param[in,out] inout_key_id. Set to WH_KEYID_ERASED to have the server
143+
* select a unique id for this key.
144+
* @param[in] flags Optional flags to be associated with the key while in the
145+
* key cache or after being committed. Set to WH_NVM_FLAGS_NONE
146+
* if not used.
147+
* @param[in] label Optional label to be associated with the key while in the
148+
* key cache or after being committed. Set to NULL if not used.
149+
* @param[in] label_len Size of the label up to WH_NVM_LABEL_SIZE. Set to 0 if
150+
* not used.
151+
* @return int Returns 0 on success or a negative error code on failure.
152+
*/
134153
int wh_Client_Curve25519MakeCacheKey(whClientContext* ctx,
135154
uint16_t size,
136-
whKeyId *inout_key_Id, whNvmFlags flags,
137-
uint16_t label_len, uint8_t* label);
155+
whKeyId *inout_key_id, whNvmFlags flags,
156+
const uint8_t* label, uint16_t label_len);
138157

139-
/* TODO: Generate a Curve25519 key on the server and export it inta a local struct */
158+
/**
159+
* @brief Generate a Curve25519 key by the server and export to the client
160+
*
161+
* This function requests the server to generate a new Curve25519 key pair and
162+
* export it to the client, without using any key cache or additional resources
163+
*
164+
* @param[in] ctx Pointer to the client context
165+
* @param[in] size Size of the key to generate in bytes, normally set to
166+
* CURVE25519_KEY_SIZE.
167+
* @param[in] key Pointer to a wolfCrypt key structure, which will be
168+
* initialized to the new key pair when successful
169+
* @return int Returns 0 on success or a negative error code on failure.
170+
*/
140171
int wh_Client_Curve25519MakeExportKey(whClientContext* ctx,
141-
uint16_t size,
142-
curve25519_key* key);
172+
uint16_t size, curve25519_key* key);
143173

144-
/* TODO: Compute an X25519 shared secret */
174+
/**
175+
* @brief Compute an X25519 shared secret using a public and private key
176+
*
177+
* This function requests the server compute the shared secret using the
178+
* provided wolfCrypt private and public keys. Note, the client will
179+
* temporarily import any missing key material to the server as required.
180+
*
181+
* @param[in] ctx Pointer to the client context
182+
* @param[in] priv_key Pointer to a wolfCrypt key structure that holds the
183+
* private key
184+
* @param[in] pub_key Pointer to a wolfCrypt key structure that holds the
185+
* public key
186+
* @param[in] endian Endianness of the values. EC25519_BIG_ENDIAN (typical) or
187+
* EC25519_LITTLE_ENDIAN
188+
* @return int Returns 0 on success or a negative error code on failure.
189+
*/
145190
int wh_Client_Curve25519SharedSecret(whClientContext* ctx,
146191
curve25519_key* priv_key, curve25519_key* pub_key,
147192
int endian, uint8_t* out, uint16_t *out_size);

0 commit comments

Comments
 (0)