Skip to content

Commit 6f1e98a

Browse files
authored
Merge pull request #187 from jackctj117/CMAC-cancellation-fix
Add WOLFHSM_CFG_CANCEL_API for opt-in cancellation support
2 parents 33ec360 + 077066d commit 6f1e98a

File tree

10 files changed

+132
-50
lines changed

10 files changed

+132
-50
lines changed

.github/workflows/clang-format-check.yml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
trap 'rm -f "$DIFF_FILE"' EXIT
3939
4040
# Run git-clang-format against the PR base commit and capture status safely under set -e
41-
if git-clang-format-15 --diff "$BASE_REF" --binary clang-format-15 > "$DIFF_FILE"; then
41+
if git-clang-format-15 "$BASE_REF" > "$DIFF_FILE"; then
4242
status=0
4343
else
4444
status=$?
@@ -55,7 +55,7 @@ jobs:
5555
echo "=================================================="
5656
echo ""
5757
echo "Please run the following command locally on your feature branch and commit the changes:"
58-
echo " git-clang-format-15 --binary clang-format-15 $BASE_REF"
58+
echo " git-clang-format-15 $BASE_REF"
5959
exit 1
6060
else
6161
echo "❌ git-clang-format-15 failed with exit code $status"

src/wh_client.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,10 @@ int wh_Client_Init(whClientContext* c, const whClientConfig* config)
7676
}
7777

7878
memset(c, 0, sizeof(*c));
79+
#ifdef WOLFHSM_CFG_CANCEL_API
7980
/* register the cancel callback */
8081
c->cancelCb = config->cancelCb;
82+
#endif
8183

8284
rc = wh_CommClient_Init(c->comm, config->comm);
8385

@@ -470,6 +472,7 @@ int wh_Client_CommClose(whClientContext* c)
470472
return rc;
471473
}
472474

475+
#ifdef WOLFHSM_CFG_CANCEL_API
473476
int wh_Client_EnableCancel(whClientContext* c)
474477
{
475478
if (c == NULL)
@@ -535,6 +538,7 @@ int wh_Client_Cancel(whClientContext* c)
535538
}
536539
return ret;
537540
}
541+
#endif /* WOLFHSM_CFG_CANCEL_API */
538542

539543
int wh_Client_EchoRequest(whClientContext* c, uint16_t size, const void* data)
540544
{

src/wh_client_crypto.c

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2394,11 +2394,13 @@ int wh_Client_Cmac(whClientContext* ctx, Cmac* cmac, CmacType type,
23942394
if (ret == WH_ERROR_OK) {
23952395
/* Update the local type since call succeeded */
23962396
cmac->type = type;
2397+
#ifdef WOLFHSM_CFG_CANCEL_API
23972398
/* if the client marked they may want to cancel, handle the
23982399
* response in a separate call */
23992400
if (ctx->cancelable) {
24002401
return ret;
24012402
}
2403+
#endif
24022404

24032405
uint16_t res_len = 0;
24042406
do {
@@ -2434,6 +2436,7 @@ int wh_Client_Cmac(whClientContext* ctx, Cmac* cmac, CmacType type,
24342436

24352437
#endif /* !NO_AES */
24362438

2439+
#ifdef WOLFHSM_CFG_CANCEL_API
24372440
int wh_Client_CmacCancelableResponse(whClientContext* c, Cmac* cmac,
24382441
uint8_t* out, uint16_t* outSz)
24392442
{
@@ -2485,6 +2488,7 @@ int wh_Client_CmacCancelableResponse(whClientContext* c, Cmac* cmac,
24852488
}
24862489
return ret;
24872490
}
2491+
#endif /* WOLFHSM_CFG_CANCEL_API */
24882492

24892493
#ifdef WOLFHSM_CFG_DMA
24902494
int wh_Client_CmacDma(whClientContext* ctx, Cmac* cmac, CmacType type,

src/wh_server.c

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,7 @@ int wh_Server_GetConnected(whServerContext *server,
150150
return WH_ERROR_OK;
151151
}
152152

153+
#ifdef WOLFHSM_CFG_CANCEL_API
153154
int wh_Server_GetCanceledSequence(whServerContext* server, uint16_t* outSeq)
154155
{
155156
if (server == NULL || outSeq == NULL)
@@ -167,6 +168,7 @@ int wh_Server_SetCanceledSequence(whServerContext* server, uint16_t cancelSeq)
167168
server->cancelSeq = cancelSeq;
168169
return WH_ERROR_OK;
169170
}
171+
#endif /* WOLFHSM_CFG_CANCEL_API */
170172

171173
static int _wh_Server_HandleCommRequest(whServerContext* server,
172174
uint16_t magic, uint16_t action, uint16_t seq,
@@ -376,13 +378,17 @@ int wh_Server_HandleRequestMessage(whServerContext* server)
376378

377379
/* Send a response */
378380
/* TODO: Respond with ErrorResponse if handler returns an error */
379-
if (rc == 0 || rc == WH_ERROR_CANCEL) {
381+
#ifdef WOLFHSM_CFG_CANCEL_API
382+
if (rc == WH_ERROR_CANCEL) {
380383
/* notify the client that their request was canceled */
381-
if (rc == WH_ERROR_CANCEL) {
382-
kind = WH_MESSAGE_KIND(WH_MESSAGE_GROUP_CANCEL, 0);
383-
size = 0;
384-
data = NULL;
385-
}
384+
kind = WH_MESSAGE_KIND(WH_MESSAGE_GROUP_CANCEL, 0);
385+
size = 0;
386+
data = NULL;
387+
/* reset RC so the cancellation response is sent */
388+
rc = 0;
389+
}
390+
#endif
391+
if (rc == 0) {
386392
do {
387393
rc = wh_CommServer_SendResponse(server->comm, magic, kind, seq,
388394
size, data);

src/wh_server_crypto.c

Lines changed: 64 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,25 @@
5454

5555
#include "wolfhsm/wh_message_crypto.h"
5656

57+
/** Helper functions */
58+
#ifdef WOLFHSM_CFG_CANCEL_API
59+
/**
60+
* Check if the current operation should be canceled
61+
* @param ctx Server context
62+
* @param seq Sequence number to check against
63+
* @return WH_ERROR_CANCEL if canceled, 0 if not canceled, or error code
64+
*/
65+
static int _CheckCancellation(whServerContext* ctx, uint16_t seq)
66+
{
67+
uint16_t cancelSeq;
68+
int ret = wh_Server_GetCanceledSequence(ctx, &cancelSeq);
69+
if (ret == 0 && cancelSeq == seq) {
70+
return WH_ERROR_CANCEL;
71+
}
72+
return ret;
73+
}
74+
#endif
75+
5776
/** Forward declarations */
5877
#ifndef NO_RSA
5978
#ifdef WOLFSSL_KEY_GEN
@@ -1798,8 +1817,7 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq,
17981817
}
17991818

18001819
uint32_t i;
1801-
word32 len;
1802-
uint16_t cancelSeq;
1820+
word32 len;
18031821
whKeyId keyId = WH_KEYID_ERASED;
18041822

18051823
/* Setup fixed size fields */
@@ -1890,49 +1908,60 @@ static int _HandleCmac(whServerContext* ctx, uint16_t magic, uint16_t seq,
18901908
}
18911909
/* Handle CMAC update, checking for cancellation */
18921910
if (ret == 0 && req.inSz != 0) {
1911+
#ifndef WOLFHSM_CFG_CANCEL_API
1912+
(void)seq;
1913+
#endif
18931914
for (i = 0; ret == 0 && i < req.inSz; i += AES_BLOCK_SIZE) {
18941915
if (i + AES_BLOCK_SIZE > req.inSz) {
18951916
blockSz = req.inSz - i;
18961917
}
18971918
ret = wc_CmacUpdate(ctx->crypto->algoCtx.cmac, in + i,
18981919
blockSz);
1920+
#ifdef WOLFHSM_CFG_CANCEL_API
18991921
if (ret == 0) {
1900-
ret = wh_Server_GetCanceledSequence(ctx, &cancelSeq);
1901-
if (ret == 0 && cancelSeq == seq) {
1902-
ret = WH_ERROR_CANCEL;
1903-
}
1922+
ret = _CheckCancellation(ctx, seq);
19041923
}
1924+
#endif
19051925
}
19061926
#ifdef DEBUG_CRYPTOCB_VERBOSE
19071927
printf("[server] cmac update done. ret:%d\n", ret);
19081928
#endif
19091929
}
1910-
/* do final and evict the struct if outSz is set, otherwise cache the
1911-
* struct for a future call */
1912-
if ((ret == 0 && req.outSz != 0) || ret == WH_ERROR_CANCEL) {
1913-
if (ret != WH_ERROR_CANCEL) {
1914-
keyId = req.keyId;
1915-
len = req.outSz;
1930+
1931+
/* Check if we should finalize and evict, or cache for future calls
1932+
*/
1933+
if (ret == 0 && req.outSz != 0) {
1934+
/* Finalize CMAC operation */
1935+
keyId = req.keyId;
1936+
len = req.outSz;
19161937
#ifdef DEBUG_CRYPTOCB_VERBOSE
1917-
printf("[server] cmac final keyId:%x len:%d\n",keyId, len);
1938+
printf("[server] cmac final keyId:%x len:%d\n", keyId, len);
19181939
#endif
1919-
ret = wc_CmacFinal(ctx->crypto->algoCtx.cmac, out, &len);
1920-
res.outSz = len;
1921-
res.keyId = WH_KEYID_ERASED;
1922-
}
1923-
/* evict the key, canceling means abandoning the current state */
1924-
if (ret == 0 || ret == WH_ERROR_CANCEL) {
1925-
if (!WH_KEYID_ISERASED(keyId)) {
1926-
/* Don't override return value except on failure */
1927-
int tmpRet = wh_Server_KeystoreEvictKey(
1928-
ctx, WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO,
1929-
ctx->comm->client_id, keyId));
1930-
if (tmpRet != 0) {
1931-
ret = tmpRet;
1932-
}
1940+
ret = wc_CmacFinal(ctx->crypto->algoCtx.cmac, out, &len);
1941+
res.outSz = len;
1942+
res.keyId = WH_KEYID_ERASED;
1943+
1944+
/* Evict the key from cache */
1945+
if (!WH_KEYID_ISERASED(keyId)) {
1946+
/* Don't override return value except on failure */
1947+
int tmpRet = wh_Server_KeystoreEvictKey(
1948+
ctx, WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO,
1949+
ctx->comm->client_id, keyId));
1950+
if (tmpRet != 0) {
1951+
ret = tmpRet;
19331952
}
19341953
}
19351954
}
1955+
#ifdef WOLFHSM_CFG_CANCEL_API
1956+
else if (ret == WH_ERROR_CANCEL) {
1957+
/* Handle cancellation - evict key and abandon state */
1958+
if (!WH_KEYID_ISERASED(req.keyId)) {
1959+
wh_Server_KeystoreEvictKey(
1960+
ctx, WH_MAKE_KEYID(WH_KEYTYPE_CRYPTO,
1961+
ctx->comm->client_id, req.keyId));
1962+
}
1963+
}
1964+
#endif
19361965
/* Cache the CMAC struct for a future update call */
19371966
else if (ret == 0) {
19381967
/* cache/re-cache updated struct */
@@ -2930,9 +2959,13 @@ int wh_Server_HandleCryptoRequest(whServerContext* ctx, uint16_t magic,
29302959
/* Since crypto error codes are propagated to the client in the response
29312960
* packet, return success to the caller unless a cancellation has occurred
29322961
*/
2962+
#ifdef WOLFHSM_CFG_CANCEL_API
29332963
if (ret != WH_ERROR_CANCEL) {
29342964
ret = WH_ERROR_OK;
29352965
}
2966+
#else
2967+
ret = WH_ERROR_OK;
2968+
#endif
29362969
return ret;
29372970
}
29382971

@@ -4303,9 +4336,13 @@ int wh_Server_HandleCryptoDmaRequest(whServerContext* ctx, uint16_t magic,
43034336
/* Since crypto error codes are propagated to the client in the response
43044337
* packet, return success to the caller unless a cancellation has occurred
43054338
*/
4339+
#ifdef WOLFHSM_CFG_CANCEL_API
43064340
if (ret != WH_ERROR_CANCEL) {
43074341
ret = WH_ERROR_OK;
43084342
}
4343+
#else
4344+
ret = WH_ERROR_OK;
4345+
#endif
43094346
return ret;
43104347
}
43114348
#endif /* WOLFHSM_CFG_DMA */

test/config/wolfhsm_cfg.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,4 +48,10 @@
4848

4949
#define WOLFHSM_CFG_KEYWRAP
5050

51+
/* Only enable cancellation tests in POSIX test harness if using the
52+
* instrumented tests server. Otherwise CMAC is too fast to test cancellation */
53+
#ifdef WOLFHSM_CFG_IS_TEST_SERVER
54+
#define WOLFHSM_CFG_CANCEL_API
55+
#endif
56+
5157
#endif /* WOLFHSM_CFG_H_ */

0 commit comments

Comments
 (0)