Skip to content

Commit 189268d

Browse files
committed
Add DMA support for RNG, add support for seed gen
1 parent 6449396 commit 189268d

File tree

7 files changed

+227
-4
lines changed

7 files changed

+227
-4
lines changed

src/wh_client_crypto.c

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -257,6 +257,73 @@ int wh_Client_RngGenerate(whClientContext* ctx, uint8_t* out, uint32_t size)
257257
return ret;
258258
}
259259

260+
#ifdef WOLFHSM_CFG_DMA
261+
int wh_Client_RngGenerateDma(whClientContext* ctx, uint8_t* out, uint32_t size)
262+
{
263+
int ret = WH_ERROR_OK;
264+
uint8_t* dataPtr = NULL;
265+
whMessageCrypto_RngDmaRequest* req = NULL;
266+
whMessageCrypto_RngDmaResponse* resp = NULL;
267+
uint16_t respSz = 0;
268+
uintptr_t outAddr = 0;
269+
270+
if ((ctx == NULL) || (out == NULL) || (size == 0)) {
271+
return WH_ERROR_BADARGS;
272+
}
273+
274+
/* Get data pointer from the context to use as request/response storage */
275+
dataPtr = (uint8_t*)wh_CommClient_GetDataPtr(ctx->comm);
276+
if (dataPtr == NULL) {
277+
return WH_ERROR_BADARGS;
278+
}
279+
280+
/* Setup generic header and get pointer to request data */
281+
req = (whMessageCrypto_RngDmaRequest*)_createCryptoRequest(
282+
dataPtr, WC_ALGO_TYPE_RNG);
283+
284+
/* Set up output buffer address and size */
285+
req->output.sz = size;
286+
287+
/* Perform address translation for output buffer (PRE operation) */
288+
ret = wh_Client_DmaProcessClientAddress(
289+
ctx, (uintptr_t)out, (void**)&outAddr, req->output.sz,
290+
WH_DMA_OPER_CLIENT_WRITE_PRE, (whDmaFlags){0});
291+
req->output.addr = outAddr;
292+
293+
if (ret == WH_ERROR_OK) {
294+
/* Send the request to the server */
295+
ret = wh_Client_SendRequest(
296+
ctx, WH_MESSAGE_GROUP_CRYPTO_DMA, WC_ALGO_TYPE_RNG,
297+
sizeof(whMessageCrypto_GenericRequestHeader) + sizeof(*req),
298+
(uint8_t*)dataPtr);
299+
}
300+
301+
if (ret == WH_ERROR_OK) {
302+
/* Wait for and receive the response */
303+
do {
304+
ret = wh_Client_RecvResponse(ctx, NULL, NULL, &respSz,
305+
(uint8_t*)dataPtr);
306+
} while (ret == WH_ERROR_NOTREADY);
307+
}
308+
309+
if (ret == WH_ERROR_OK) {
310+
/* Get response structure pointer, validates generic header rc */
311+
ret = _getCryptoResponse(dataPtr, WC_ALGO_TYPE_RNG, (uint8_t**)&resp);
312+
/* Nothing more to do on success, as server will have written random
313+
* bytes directly to client memory */
314+
}
315+
316+
/* Perform address translation cleanup (POST operation)
317+
* This is called regardless of successful operation to give the callback a
318+
* chance for cleanup */
319+
(void)wh_Client_DmaProcessClientAddress(
320+
ctx, (uintptr_t)out, (void**)&outAddr, size,
321+
WH_DMA_OPER_CLIENT_WRITE_POST, (whDmaFlags){0});
322+
323+
return ret;
324+
}
325+
#endif /* WOLFHSM_CFG_DMA */
326+
260327
#ifndef NO_AES
261328

262329
#ifdef WOLFSSL_AES_COUNTER

src/wh_client_cryptocb.c

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -395,6 +395,13 @@ int wh_Client_CryptoCb(int devId, wc_CryptoInfo* info, void* inCtx)
395395

396396
ret = wh_Client_RngGenerate(ctx, out, size);
397397
} break;
398+
case WC_ALGO_TYPE_SEED: {
399+
/* Extract info parameters */
400+
uint8_t* seed = info->seed.seed;
401+
uint32_t size = info->seed.sz;
402+
403+
ret = wh_Client_RngGenerate(ctx, seed, size);
404+
} break;
398405
#endif /* !WC_NO_RNG */
399406

400407
#ifdef WOLFSSL_CMAC
@@ -846,6 +853,23 @@ int wh_Client_CryptoCbDma(int devId, wc_CryptoInfo* info, void* inCtx)
846853
break;
847854
#endif /* !NO_AES || !NO_DES */
848855

856+
#ifndef WC_NO_RNG
857+
case WC_ALGO_TYPE_RNG: {
858+
/* Extract info parameters */
859+
uint8_t* out = info->rng.out;
860+
uint32_t size = info->rng.sz;
861+
862+
ret = wh_Client_RngGenerateDma(ctx, out, size);
863+
} break;
864+
case WC_ALGO_TYPE_SEED: {
865+
/* Extract info parameters */
866+
uint8_t* seed = info->seed.seed;
867+
uint32_t size = info->seed.sz;
868+
869+
ret = wh_Client_RngGenerateDma(ctx, seed, size);
870+
} break;
871+
#endif /* !WC_NO_RNG */
872+
849873
case WC_ALGO_TYPE_NONE:
850874
default:
851875
ret = CRYPTOCB_UNAVAILABLE;

src/wh_message_crypto.c

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1045,3 +1045,29 @@ int wh_MessageCrypto_TranslateAesDmaResponse(
10451045
WH_T32(magic, dest, src, outSz);
10461046
return 0;
10471047
}
1048+
1049+
/* RNG DMA Request translation */
1050+
int wh_MessageCrypto_TranslateRngDmaRequest(
1051+
uint16_t magic, const whMessageCrypto_RngDmaRequest* src,
1052+
whMessageCrypto_RngDmaRequest* dest)
1053+
{
1054+
if ((src == NULL) || (dest == NULL)) {
1055+
return WH_ERROR_BADARGS;
1056+
}
1057+
1058+
return wh_MessageCrypto_TranslateDmaBuffer(magic, &src->output,
1059+
&dest->output);
1060+
}
1061+
1062+
/* RNG DMA Response translation */
1063+
int wh_MessageCrypto_TranslateRngDmaResponse(
1064+
uint16_t magic, const whMessageCrypto_RngDmaResponse* src,
1065+
whMessageCrypto_RngDmaResponse* dest)
1066+
{
1067+
if ((src == NULL) || (dest == NULL)) {
1068+
return WH_ERROR_BADARGS;
1069+
}
1070+
1071+
return wh_MessageCrypto_TranslateDmaAddrStatus(magic, &src->dmaAddrStatus,
1072+
&dest->dmaAddrStatus);
1073+
}

src/wh_server_crypto.c

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4536,7 +4536,66 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
45364536
/* return value populates rc in response message */
45374537
return ret;
45384538
}
4539-
#endif /* WOLFHSM_CFG_DMA */
4539+
#endif /* WOLFSSL_CMAC */
4540+
4541+
#ifndef WC_NO_RNG
4542+
static int _HandleRngDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
4543+
const void* cryptoDataIn, uint16_t inSize,
4544+
void* cryptoDataOut, uint16_t* outSize)
4545+
{
4546+
(void)seq;
4547+
(void)inSize;
4548+
4549+
int ret = 0;
4550+
whMessageCrypto_RngDmaRequest req;
4551+
whMessageCrypto_RngDmaResponse res;
4552+
void* outAddr = NULL;
4553+
4554+
/* Translate the request */
4555+
ret = wh_MessageCrypto_TranslateRngDmaRequest(
4556+
magic, (whMessageCrypto_RngDmaRequest*)cryptoDataIn, &req);
4557+
if (ret != WH_ERROR_OK) {
4558+
return ret;
4559+
}
4560+
4561+
/* Process the output address (PRE operation) */
4562+
if (ret == WH_ERROR_OK) {
4563+
ret = wh_Server_DmaProcessClientAddress(
4564+
ctx, req.output.addr, &outAddr, req.output.sz,
4565+
WH_DMA_OPER_CLIENT_WRITE_PRE, (whServerDmaFlags){0});
4566+
if (ret == WH_ERROR_ACCESS) {
4567+
res.dmaAddrStatus.badAddr = req.output;
4568+
}
4569+
}
4570+
4571+
/* Generate random bytes directly into client memory */
4572+
if (ret == WH_ERROR_OK) {
4573+
#ifdef DEBUG_CRYPTOCB_VERBOSE
4574+
printf("[server] RNG DMA: generating %llu bytes to addr=%p\n",
4575+
(long long unsigned int)req.output.sz, outAddr);
4576+
#endif
4577+
ret = wc_RNG_GenerateBlock(ctx->crypto->rng, outAddr, req.output.sz);
4578+
}
4579+
4580+
/* Process the output address (POST operation) */
4581+
if (ret == WH_ERROR_OK) {
4582+
ret = wh_Server_DmaProcessClientAddress(
4583+
ctx, req.output.addr, &outAddr, req.output.sz,
4584+
WH_DMA_OPER_CLIENT_WRITE_POST, (whServerDmaFlags){0});
4585+
if (ret == WH_ERROR_ACCESS) {
4586+
res.dmaAddrStatus.badAddr = req.output;
4587+
}
4588+
}
4589+
4590+
/* Translate the response */
4591+
(void)wh_MessageCrypto_TranslateRngDmaResponse(
4592+
magic, &res, (whMessageCrypto_RngDmaResponse*)cryptoDataOut);
4593+
*outSize = sizeof(res);
4594+
4595+
/* return value populates rc in response message */
4596+
return ret;
4597+
}
4598+
#endif /* !WC_NO_RNG */
45404599

45414600
int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
45424601
uint16_t action, uint16_t seq,
@@ -4657,6 +4716,13 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
46574716
break;
46584717
#endif /* WOLFSSL_CMAC */
46594718

4719+
#ifndef WC_NO_RNG
4720+
case WC_ALGO_TYPE_RNG:
4721+
ret = _HandleRngDma(ctx, magic, seq, cryptoDataIn, cryptoInSize,
4722+
cryptoDataOut, &cryptoOutSize);
4723+
break;
4724+
#endif /* !WC_NO_RNG */
4725+
46604726
case WC_ALGO_TYPE_NONE:
46614727
default:
46624728
ret = NOT_COMPILED_IN;

test/wh_test_crypto.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,7 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
155155
}
156156
}
157157
if (ret == 0) {
158-
printf("RNG SUCCESS\n");
158+
printf("RNG DEVID=0x%X SUCCESS\n", devId);
159159
}
160160
return ret;
161161
}
@@ -3561,8 +3561,13 @@ int whTest_CryptoClientConfig(whClientConfig* config)
35613561
}
35623562
#endif /* WOLFHSM_CFG_TEST_VERBOSE */
35633563

3564-
if (ret == 0) {
3565-
ret = whTest_CryptoRng(client, WH_DEV_ID, rng);
3564+
i = 0;
3565+
while ((ret == WH_ERROR_OK) && (i < WH_NUM_DEVIDS)) {
3566+
ret = whTest_CryptoRng(client, WH_DEV_IDS_ARRAY[i], rng);
3567+
if (ret == WH_ERROR_OK) {
3568+
wc_FreeRng(rng);
3569+
i++;
3570+
}
35663571
}
35673572

35683573
if (ret == 0) {

wolfhsm/wh_client_crypto.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,22 @@
6464
*/
6565
int wh_Client_RngGenerate(whClientContext* ctx, uint8_t* out, uint32_t size);
6666

67+
#ifdef WOLFHSM_CFG_DMA
68+
/**
69+
* @brief Generate random bytes using DMA
70+
*
71+
* This function requests the server to generate random bytes directly into
72+
* client memory using DMA, eliminating the need for chunking and copying
73+
* through the communication buffer.
74+
*
75+
* @param[in] ctx Pointer to the client context
76+
* @param[out] out Pointer to where the bytes are to be placed
77+
* @param[in] size Number of bytes to generate
78+
* @return int Returns 0 on success or a negative error code on failure.
79+
*/
80+
int wh_Client_RngGenerateDma(whClientContext* ctx, uint8_t* out, uint32_t size);
81+
#endif /* WOLFHSM_CFG_DMA */
82+
6783
#ifdef HAVE_CURVE25519
6884
/**
6985
* @brief Associates a Curve25519 key with a specific key ID.

wolfhsm/wh_message_crypto.h

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -974,4 +974,23 @@ int wh_MessageCrypto_TranslateMlDsaVerifyDmaResponse(
974974
uint16_t magic, const whMessageCrypto_MlDsaVerifyDmaResponse* src,
975975
whMessageCrypto_MlDsaVerifyDmaResponse* dest);
976976

977+
/* RNG DMA Request */
978+
typedef struct {
979+
whMessageCrypto_DmaBuffer output; /* Output buffer for random bytes */
980+
} whMessageCrypto_RngDmaRequest;
981+
982+
/* RNG DMA Response */
983+
typedef struct {
984+
whMessageCrypto_DmaAddrStatus dmaAddrStatus;
985+
} whMessageCrypto_RngDmaResponse;
986+
987+
/* RNG DMA translation functions */
988+
int wh_MessageCrypto_TranslateRngDmaRequest(
989+
uint16_t magic, const whMessageCrypto_RngDmaRequest* src,
990+
whMessageCrypto_RngDmaRequest* dest);
991+
992+
int wh_MessageCrypto_TranslateRngDmaResponse(
993+
uint16_t magic, const whMessageCrypto_RngDmaResponse* src,
994+
whMessageCrypto_RngDmaResponse* dest);
995+
977996
#endif /* !WOLFHSM_WH_MESSAGE_CRYPTO_H_ */

0 commit comments

Comments
 (0)