Skip to content

Commit 4de6b72

Browse files
committed
Implement request/response functions for aes cbc. Update crypto affinity test to new aes cbc functions
1 parent 9766c37 commit 4de6b72

File tree

5 files changed

+328
-45
lines changed

5 files changed

+328
-45
lines changed

src/wh_client_crypto.c

Lines changed: 111 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -482,7 +482,7 @@ int wh_Client_AesCtrDma(whClientContext* ctx, Aes* aes, int enc,
482482

483483
/* Setup generic header and get pointer to request data */
484484
req = (whMessageCrypto_AesCtrDmaRequest*)_createCryptoRequest(
485-
dataPtr, WC_CIPHER_AES_CTR);
485+
dataPtr, WC_CIPHER_AES_CTR, ctx->cryptoAffinity);
486486
uint8_t* req_iv = (uint8_t*)req + sizeof(whMessageCrypto_AesCtrDmaRequest);
487487
uint8_t* req_tmp = req_iv + AES_IV_SIZE;
488488
uint8_t* req_key = req_tmp + AES_BLOCK_SIZE;
@@ -713,7 +713,7 @@ int wh_Client_AesEcbDma(whClientContext* ctx, Aes* aes, int enc,
713713

714714
/* Setup generic header and get pointer to request data */
715715
req = (whMessageCrypto_AesEcbDmaRequest*)_createCryptoRequest(
716-
dataPtr, WC_CIPHER_AES_ECB);
716+
dataPtr, WC_CIPHER_AES_ECB, ctx->cryptoAffinity);
717717
uint8_t* req_key = (uint8_t*)req + sizeof(whMessageCrypto_AesEcbDmaRequest);
718718
uint32_t req_len = sizeof(whMessageCrypto_GenericRequestHeader) +
719719
sizeof(*req);
@@ -840,7 +840,7 @@ int wh_Client_AesCbc(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
840840
}
841841
/* Setup generic header and get pointer to request data */
842842
req = (whMessageCrypto_AesCbcRequest*)_createCryptoRequest(
843-
dataPtr, WC_CIPHER_AES_CBC);
843+
dataPtr, WC_CIPHER_AES_CBC, ctx->cryptoAffinity);
844844
uint8_t* req_in = (uint8_t*)(req + 1);
845845
uint8_t* req_key = req_in + len;
846846
uint8_t* req_iv = req_key + key_len;
@@ -1034,6 +1034,114 @@ int wh_Client_AesCbcDma(whClientContext* ctx, Aes* aes, int enc,
10341034
return ret;
10351035
}
10361036
#endif /* WOLFHSM_CFG_DMA */
1037+
1038+
int wh_Client_AesCbcRequest(whClientContext* ctx, Aes* aes, int enc,
1039+
const uint8_t* in, uint32_t len)
1040+
{
1041+
whMessageCrypto_AesCbcRequest* req;
1042+
uint8_t* dataPtr;
1043+
1044+
if ((ctx == NULL) || (aes == NULL) || ((len % AES_BLOCK_SIZE) != 0) ||
1045+
(in == NULL)) {
1046+
return WH_ERROR_BADARGS;
1047+
}
1048+
1049+
uint16_t blocks = len / AES_BLOCK_SIZE;
1050+
1051+
if (blocks == 0) {
1052+
return WH_ERROR_OK;
1053+
}
1054+
1055+
uint32_t key_len = aes->keylen;
1056+
const uint8_t* key = (const uint8_t*)(aes->devKey);
1057+
whKeyId key_id = WH_DEVCTX_TO_KEYID(aes->devCtx);
1058+
uint8_t* iv = (uint8_t*)aes->reg;
1059+
uint32_t iv_len = AES_IV_SIZE;
1060+
1061+
dataPtr = wh_CommClient_GetDataPtr(ctx->comm);
1062+
if (dataPtr == NULL) {
1063+
return WH_ERROR_BADARGS;
1064+
}
1065+
1066+
req = (whMessageCrypto_AesCbcRequest*)_createCryptoRequest(
1067+
dataPtr, WC_CIPHER_AES_CBC, ctx->cryptoAffinity);
1068+
uint8_t* req_in = (uint8_t*)(req + 1);
1069+
uint8_t* req_key = req_in + len;
1070+
uint8_t* req_iv = req_key + key_len;
1071+
uint32_t req_len = sizeof(whMessageCrypto_GenericRequestHeader) +
1072+
sizeof(*req) + len + key_len + iv_len;
1073+
1074+
if (req_len > WOLFHSM_CFG_COMM_DATA_LEN) {
1075+
return WH_ERROR_BADARGS;
1076+
}
1077+
1078+
req->enc = enc;
1079+
req->keyLen = key_len;
1080+
req->sz = len;
1081+
req->keyId = key_id;
1082+
if ((in != NULL) && (len > 0)) {
1083+
memcpy(req_in, in, len);
1084+
}
1085+
if ((key != NULL) && (key_len > 0)) {
1086+
memcpy(req_key, key, key_len);
1087+
}
1088+
if ((iv != NULL) && (iv_len > 0)) {
1089+
memcpy(req_iv, iv, iv_len);
1090+
}
1091+
1092+
/* For decryption, update the IV with the last ciphertext block before
1093+
* sending, since the input buffer may be reused for output (in-place) */
1094+
if (enc == 0) {
1095+
uint32_t last_offset = (blocks - 1) * AES_BLOCK_SIZE;
1096+
memcpy(iv, in + last_offset, iv_len);
1097+
}
1098+
1099+
return wh_Client_SendRequest(ctx, WH_MESSAGE_GROUP_CRYPTO,
1100+
WC_ALGO_TYPE_CIPHER, req_len, dataPtr);
1101+
}
1102+
1103+
int wh_Client_AesCbcResponse(whClientContext* ctx, Aes* aes, uint8_t* out,
1104+
uint32_t* out_size)
1105+
{
1106+
int ret;
1107+
uint8_t* dataPtr;
1108+
uint16_t group = 0;
1109+
uint16_t action = 0;
1110+
uint16_t res_len = 0;
1111+
whMessageCrypto_AesCbcResponse* res;
1112+
1113+
if ((ctx == NULL) || (aes == NULL)) {
1114+
return WH_ERROR_BADARGS;
1115+
}
1116+
1117+
dataPtr = wh_CommClient_GetDataPtr(ctx->comm);
1118+
if (dataPtr == NULL) {
1119+
return WH_ERROR_BADARGS;
1120+
}
1121+
1122+
ret = wh_Client_RecvResponse(ctx, &group, &action, &res_len, dataPtr);
1123+
if (ret == WH_ERROR_OK) {
1124+
ret = _getCryptoResponse(dataPtr, WC_CIPHER_AES_CBC, (uint8_t**)&res);
1125+
if (ret == WH_ERROR_OK) {
1126+
uint8_t* res_out = (uint8_t*)(res + 1);
1127+
if (out != NULL) {
1128+
memcpy(out, res_out, res->sz);
1129+
/* For encryption, update the IV with the last ciphertext
1130+
* block for CBC chaining */
1131+
if (res->sz >= AES_BLOCK_SIZE) {
1132+
uint32_t last_offset =
1133+
((res->sz / AES_BLOCK_SIZE) - 1) * AES_BLOCK_SIZE;
1134+
memcpy((uint8_t*)aes->reg, out + last_offset, AES_IV_SIZE);
1135+
}
1136+
}
1137+
if (out_size != NULL) {
1138+
*out_size = res->sz;
1139+
}
1140+
}
1141+
}
1142+
1143+
return ret;
1144+
}
10371145
#endif /* HAVE_AES_CBC */
10381146

10391147
#ifdef HAVE_AESGCM

src/wh_server_crypto.c

Lines changed: 24 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -2388,9 +2388,10 @@ static int _HandleAesCtr(whServerContext* ctx, uint16_t magic, int devId,
23882388

23892389

23902390
#ifdef WOLFHSM_CFG_DMA
2391-
static int _HandleAesCtrDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
2392-
const void* cryptoDataIn, uint16_t inSize,
2393-
void* cryptoDataOut, uint16_t* outSize)
2391+
static int _HandleAesCtrDma(whServerContext* ctx, uint16_t magic, int devId,
2392+
uint16_t seq, const void* cryptoDataIn,
2393+
uint16_t inSize, void* cryptoDataOut,
2394+
uint16_t* outSize)
23942395
{
23952396
int ret = WH_ERROR_OK;
23962397
whMessageCrypto_AesCtrDmaRequest req;
@@ -2500,7 +2501,7 @@ static int _HandleAesCtrDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
25002501
}
25012502

25022503
if (ret == WH_ERROR_OK) {
2503-
ret = wc_AesInit(aes, NULL, ctx->crypto->devId);
2504+
ret = wc_AesInit(aes, NULL, devId);
25042505
}
25052506

25062507
if (ret == WH_ERROR_OK) {
@@ -2687,9 +2688,10 @@ static int _HandleAesEcb(whServerContext* ctx, uint16_t magic, int devId,
26872688
}
26882689

26892690
#ifdef WOLFHSM_CFG_DMA
2690-
static int _HandleAesEcbDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
2691-
const void* cryptoDataIn, uint16_t inSize,
2692-
void* cryptoDataOut, uint16_t* outSize)
2691+
static int _HandleAesEcbDma(whServerContext* ctx, uint16_t magic, int devId,
2692+
uint16_t seq, const void* cryptoDataIn,
2693+
uint16_t inSize, void* cryptoDataOut,
2694+
uint16_t* outSize)
26932695
{
26942696
int ret = WH_ERROR_OK;
26952697
whMessageCrypto_AesEcbDmaRequest req;
@@ -2786,7 +2788,7 @@ static int _HandleAesEcbDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
27862788
}
27872789

27882790
if (ret == WH_ERROR_OK) {
2789-
ret = wc_AesInit(aes, NULL, ctx->crypto->devId);
2791+
ret = wc_AesInit(aes, NULL, devId);
27902792
}
27912793

27922794
if (ret == WH_ERROR_OK) {
@@ -2971,9 +2973,10 @@ static int _HandleAesCbc(whServerContext* ctx, uint16_t magic, int devId,
29712973
}
29722974

29732975
#ifdef WOLFHSM_CFG_DMA
2974-
static int _HandleAesCbcDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
2975-
const void* cryptoDataIn, uint16_t inSize,
2976-
void* cryptoDataOut, uint16_t* outSize)
2976+
static int _HandleAesCbcDma(whServerContext* ctx, uint16_t magic, int devId,
2977+
uint16_t seq, const void* cryptoDataIn,
2978+
uint16_t inSize, void* cryptoDataOut,
2979+
uint16_t* outSize)
29772980
{
29782981
int ret = WH_ERROR_OK;
29792982
whMessageCrypto_AesCbcDmaRequest req;
@@ -3077,7 +3080,7 @@ static int _HandleAesCbcDma(whServerContext* ctx, uint16_t magic, uint16_t seq,
30773080
}
30783081

30793082
if (ret == WH_ERROR_OK) {
3080-
ret = wc_AesInit(aes, NULL, ctx->crypto->devId);
3083+
ret = wc_AesInit(aes, NULL, devId);
30813084
}
30823085

30833086
if (ret == WH_ERROR_OK) {
@@ -5961,23 +5964,23 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
59615964
#endif /* HAVE_AESGCM */
59625965
#ifdef WOLFSSL_AES_COUNTER
59635966
case WC_CIPHER_AES_CTR:
5964-
ret = _HandleAesCtrDma(ctx, magic, seq, cryptoDataIn,
5965-
cryptoInSize, cryptoDataOut,
5966-
&cryptoOutSize);
5967+
ret = _HandleAesCtrDma(ctx, magic, devId, seq,
5968+
cryptoDataIn, cryptoInSize,
5969+
cryptoDataOut, &cryptoOutSize);
59675970
break;
59685971
#endif /* WOLFSSL_AES_COUNTER */
59695972
#ifdef HAVE_AES_CBC
59705973
case WC_CIPHER_AES_CBC:
5971-
ret = _HandleAesCbcDma(ctx, magic, seq, cryptoDataIn,
5972-
cryptoInSize, cryptoDataOut,
5973-
&cryptoOutSize);
5974+
ret = _HandleAesCbcDma(ctx, magic, devId, seq,
5975+
cryptoDataIn, cryptoInSize,
5976+
cryptoDataOut, &cryptoOutSize);
59745977
break;
59755978
#endif /* HAVE_AES_CBC */
59765979
#ifdef HAVE_AES_ECB
59775980
case WC_CIPHER_AES_ECB:
5978-
ret = _HandleAesEcbDma(ctx, magic, seq, cryptoDataIn,
5979-
cryptoInSize, cryptoDataOut,
5980-
&cryptoOutSize);
5981+
ret = _HandleAesEcbDma(ctx, magic, devId, seq,
5982+
cryptoDataIn, cryptoInSize,
5983+
cryptoDataOut, &cryptoOutSize);
59815984
break;
59825985
#endif /* HAVE_AES_ECB */
59835986
default:

test/wh_test_clientserver.c

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,9 @@
4242

4343
#ifdef WOLFHSM_CFG_ENABLE_CLIENT
4444
#include "wolfhsm/wh_client.h"
45+
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
46+
#include "wolfhsm/wh_client_crypto.h"
47+
#endif
4548
#include "wh_test_nvmflags.h"
4649
#endif
4750

@@ -504,6 +507,69 @@ static int _clientServerSequentialTestConnectCb(void* context,
504507
connected);
505508
}
506509

510+
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
511+
#ifdef HAVE_AES_CBC
512+
static int _testAesCbcRequestResponse(whClientContext* client,
513+
whServerContext* server)
514+
{
515+
Aes aes[1];
516+
uint8_t key[AES_BLOCK_SIZE] = {0xAA, 0xBB, 0xCC, 0xDD, 0xEE, 0xFF,
517+
0x11, 0x22, 0x33, 0x44, 0x55, 0x66,
518+
0x77, 0x88, 0x99, 0x00};
519+
uint8_t iv[AES_BLOCK_SIZE] = {0};
520+
uint8_t plainIn[AES_BLOCK_SIZE] = {0x77, 0x6F, 0x6C, 0x66, 0x48, 0x53,
521+
0x4D, 0x20, 0x41, 0x45, 0x53, 0x20,
522+
0x74, 0x65, 0x73, 0x74};
523+
uint8_t cipherOut[AES_BLOCK_SIZE] = {0};
524+
uint8_t plainOut[AES_BLOCK_SIZE] = {0};
525+
uint32_t outSize = 0;
526+
int rc;
527+
528+
WH_TEST_PRINT(" Testing AES CBC request/response...\n");
529+
530+
/* Encrypt */
531+
WH_TEST_RETURN_ON_FAIL(wc_AesInit(aes, NULL, INVALID_DEVID));
532+
WH_TEST_RETURN_ON_FAIL(
533+
wc_AesSetKey(aes, key, sizeof(key), iv, AES_ENCRYPTION));
534+
535+
WH_TEST_RETURN_ON_FAIL(
536+
wh_Client_AesCbcRequest(client, aes, 1, plainIn, sizeof(plainIn)));
537+
538+
/* Response should not be ready before server processes */
539+
rc = wh_Client_AesCbcResponse(client, aes, cipherOut, &outSize);
540+
WH_TEST_ASSERT_RETURN(rc == WH_ERROR_NOTREADY);
541+
542+
/* Server processes the request */
543+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
544+
545+
/* Now response should be available */
546+
WH_TEST_RETURN_ON_FAIL(
547+
wh_Client_AesCbcResponse(client, aes, cipherOut, &outSize));
548+
WH_TEST_ASSERT_RETURN(outSize == sizeof(plainIn));
549+
/* Ciphertext should differ from plaintext */
550+
WH_TEST_ASSERT_RETURN(memcmp(cipherOut, plainIn, sizeof(plainIn)) != 0);
551+
wc_AesFree(aes);
552+
553+
/* Decrypt */
554+
WH_TEST_RETURN_ON_FAIL(wc_AesInit(aes, NULL, INVALID_DEVID));
555+
WH_TEST_RETURN_ON_FAIL(
556+
wc_AesSetKey(aes, key, sizeof(key), iv, AES_DECRYPTION));
557+
558+
WH_TEST_RETURN_ON_FAIL(
559+
wh_Client_AesCbcRequest(client, aes, 0, cipherOut, sizeof(cipherOut)));
560+
WH_TEST_RETURN_ON_FAIL(wh_Server_HandleRequestMessage(server));
561+
WH_TEST_RETURN_ON_FAIL(
562+
wh_Client_AesCbcResponse(client, aes, plainOut, &outSize));
563+
WH_TEST_ASSERT_RETURN(outSize == sizeof(cipherOut));
564+
/* Decrypted output should match original plaintext */
565+
WH_TEST_ASSERT_RETURN(memcmp(plainOut, plainIn, sizeof(plainIn)) == 0);
566+
wc_AesFree(aes);
567+
568+
return WH_ERROR_OK;
569+
}
570+
#endif /* HAVE_AES_CBC */
571+
#endif /* !WOLFHSM_CFG_NO_CRYPTO */
572+
507573
static int _testOutOfBoundsNvmReads(whClientContext* client,
508574
whServerContext* server, whNvmId id)
509575
{
@@ -1105,6 +1171,12 @@ int whTest_ClientServerSequential(whTestNvmBackendType nvmType)
11051171
WH_TEST_RETURN_ON_FAIL(_testDma(server, client));
11061172
#endif /* WOLFHSM_CFG_DMA */
11071173

1174+
#if !defined(WOLFHSM_CFG_NO_CRYPTO) && defined(HAVE_AES_CBC)
1175+
/* Test split AES CBC request/response */
1176+
WH_TEST_RETURN_ON_FAIL(
1177+
_testAesCbcRequestResponse(client, server));
1178+
#endif /* !WOLFHSM_CFG_NO_CRYPTO && HAVE_AES_CBC */
1179+
11081180
/* Check that we are still connected */
11091181
WH_TEST_RETURN_ON_FAIL(wh_Server_GetConnected(server, &server_connected));
11101182
WH_TEST_ASSERT_RETURN(server_connected == WH_COMM_CONNECTED);

0 commit comments

Comments
 (0)