Skip to content

Commit 2640002

Browse files
authored
Add DMA support for RNG, add support for seed gen (#213)
* Add DMA support for RNG, add support for seed gen * rework RNG initialization/free in crypto tests to prevent uninitialized use * fix client printouts
1 parent c3a17b3 commit 2640002

File tree

7 files changed

+304
-46
lines changed

7 files changed

+304
-46
lines changed

src/wh_client_crypto.c

Lines changed: 129 additions & 42 deletions
Large diffs are not rendered by default.

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
@@ -4563,7 +4563,66 @@ static int _HandleCmacDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
45634563
/* return value populates rc in response message */
45644564
return ret;
45654565
}
4566-
#endif /* WOLFHSM_CFG_DMA */
4566+
#endif /* WOLFSSL_CMAC */
4567+
4568+
#ifndef WC_NO_RNG
4569+
static int _HandleRngDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
4570+
const void* cryptoDataIn, uint16_t inSize,
4571+
void* cryptoDataOut, uint16_t* outSize)
4572+
{
4573+
(void)seq;
4574+
(void)inSize;
4575+
4576+
int ret = 0;
4577+
whMessageCrypto_RngDmaRequest req;
4578+
whMessageCrypto_RngDmaResponse res;
4579+
void* outAddr = NULL;
4580+
4581+
/* Translate the request */
4582+
ret = wh_MessageCrypto_TranslateRngDmaRequest(
4583+
magic, (whMessageCrypto_RngDmaRequest*)cryptoDataIn, &req);
4584+
if (ret != WH_ERROR_OK) {
4585+
return ret;
4586+
}
4587+
4588+
/* Process the output address (PRE operation) */
4589+
if (ret == WH_ERROR_OK) {
4590+
ret = wh_Server_DmaProcessClientAddress(
4591+
ctx, req.output.addr, &outAddr, req.output.sz,
4592+
WH_DMA_OPER_CLIENT_WRITE_PRE, (whServerDmaFlags){0});
4593+
if (ret == WH_ERROR_ACCESS) {
4594+
res.dmaAddrStatus.badAddr = req.output;
4595+
}
4596+
}
4597+
4598+
/* Generate random bytes directly into client memory */
4599+
if (ret == WH_ERROR_OK) {
4600+
#ifdef DEBUG_CRYPTOCB_VERBOSE
4601+
printf("[server] RNG DMA: generating %llu bytes to addr=%p\n",
4602+
(long long unsigned int)req.output.sz, outAddr);
4603+
#endif
4604+
ret = wc_RNG_GenerateBlock(ctx->crypto->rng, outAddr, req.output.sz);
4605+
}
4606+
4607+
/* Process the output address (POST operation) */
4608+
if (ret == WH_ERROR_OK) {
4609+
ret = wh_Server_DmaProcessClientAddress(
4610+
ctx, req.output.addr, &outAddr, req.output.sz,
4611+
WH_DMA_OPER_CLIENT_WRITE_POST, (whServerDmaFlags){0});
4612+
if (ret == WH_ERROR_ACCESS) {
4613+
res.dmaAddrStatus.badAddr = req.output;
4614+
}
4615+
}
4616+
4617+
/* Translate the response */
4618+
(void)wh_MessageCrypto_TranslateRngDmaResponse(
4619+
magic, &res, (whMessageCrypto_RngDmaResponse*)cryptoDataOut);
4620+
*outSize = sizeof(res);
4621+
4622+
/* return value populates rc in response message */
4623+
return ret;
4624+
}
4625+
#endif /* !WC_NO_RNG */
45674626

45684627
int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
45694628
uint16_t action, uint16_t seq,
@@ -4684,6 +4743,13 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
46844743
break;
46854744
#endif /* WOLFSSL_CMAC */
46864745

4746+
#ifndef WC_NO_RNG
4747+
case WC_ALGO_TYPE_RNG:
4748+
ret = _HandleRngDma(ctx, magic, seq, cryptoDataIn, cryptoInSize,
4749+
cryptoDataOut, &cryptoOutSize);
4750+
break;
4751+
#endif /* !WC_NO_RNG */
4752+
46874753
case WC_ALGO_TYPE_NONE:
46884754
default:
46894755
ret = NOT_COMPILED_IN;

test/wh_test_crypto.c

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,6 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
134134
uint8_t med[WH_TEST_RNG_MED];
135135
uint8_t big[WH_TEST_RNG_BIG];
136136

137-
/* test rng. Note this rng is used for many tests so is left inited */
138137
ret = wc_InitRng_ex(rng, NULL, devId);
139138
if (ret != 0) {
140139
WH_ERROR_PRINT("Failed to wc_InitRng_ex %d\n", ret);
@@ -152,10 +151,14 @@ static int whTest_CryptoRng(whClientContext* ctx, int devId, WC_RNG* rng)
152151
WH_ERROR_PRINT("Failed to wc_RNG_GenerateBlock %d\n", ret);
153152
}
154153
}
154+
ret = wc_FreeRng(rng);
155+
if (ret != 0) {
156+
WH_ERROR_PRINT("Failed to wc_FreeRng %d\n", ret);
157+
}
155158
}
156159
}
157160
if (ret == 0) {
158-
printf("RNG SUCCESS\n");
161+
printf("RNG DEVID=0x%X SUCCESS\n", devId);
159162
}
160163
return ret;
161164
}
@@ -3561,8 +3564,25 @@ int whTest_CryptoClientConfig(whClientConfig* config)
35613564
}
35623565
#endif /* WOLFHSM_CFG_TEST_VERBOSE */
35633566

3567+
/* First crypto test should be of RNG so we can iterate over and test all
3568+
* devIds before choosing one to run the rest of the tests on */
3569+
i = 0;
3570+
while ((ret == WH_ERROR_OK) && (i < WH_NUM_DEVIDS)) {
3571+
ret = whTest_CryptoRng(client, WH_DEV_IDS_ARRAY[i], rng);
3572+
if (ret == WH_ERROR_OK) {
3573+
wc_FreeRng(rng);
3574+
i++;
3575+
}
3576+
}
3577+
3578+
/* Now that we have tested all RNG devIds, reinitialize the default RNG
3579+
* devId (non-DMA) that will be used by the remainder of the tests for
3580+
* random input generation */
35643581
if (ret == 0) {
3565-
ret = whTest_CryptoRng(client, WH_DEV_ID, rng);
3582+
ret = wc_InitRng_ex(rng, NULL, WH_DEV_ID);
3583+
if (ret != 0) {
3584+
WH_ERROR_PRINT("Failed to reinitialize RNG %d\n", ret);
3585+
}
35663586
}
35673587

35683588
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)