Skip to content

Commit d317239

Browse files
committed
Address review feedback
1 parent cb52966 commit d317239

File tree

4 files changed

+26
-12
lines changed

4 files changed

+26
-12
lines changed

examples/posix/wh_posix_server/wh_posix_server.c

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -339,9 +339,10 @@ int main(int argc, char** argv)
339339
}
340340
else if (strcmp(argv[i], "--flags") == 0 && i + 1 < argc) {
341341
char* end;
342-
unsigned long val = strtoul(argv[i + 1], &end, 0);
343-
errno = 0;
342+
unsigned long val;
344343

344+
errno = 0;
345+
val = strtoul(argv[i + 1], &end, 0);
345346
if (errno || *end || val > 0xFFFF) {
346347
WOLFHSM_CFG_PRINTF("Invalid --flags value: %s\n", argv[i + 1]);
347348
return -1;

src/wh_client_crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -611,7 +611,7 @@ int wh_Client_AesEcb(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
611611
whKeyId key_id = WH_DEVCTX_TO_KEYID(aes->devCtx);
612612
uint8_t* req_in = NULL;
613613
uint8_t* req_key = NULL;
614-
uint16_t req_len = 0;
614+
uint64_t req_len = 0;
615615

616616
uint16_t group = WH_MESSAGE_GROUP_CRYPTO;
617617
uint16_t action = WC_ALGO_TYPE_CIPHER;

src/wh_nvm_flash.c

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1024,10 +1024,12 @@ int wh_NvmFlash_GetAvailable(void* c,
10241024
uint32_t *out_reclaim_size, whNvmId *out_reclaim_objects)
10251025
{
10261026
whNvmFlashContext* context = c;
1027-
nfMemDirectory *d = &context->directory;
1027+
nfMemDirectory *d;
1028+
10281029
if (context == NULL) {
10291030
return WH_ERROR_BADARGS;
10301031
}
1032+
d = &context->directory;
10311033
if (out_avail_size != NULL) {
10321034
*out_avail_size = (context->partition_units -
10331035
NF_PARTITION_DATA_OFFSET - d->next_free_data) *

src/wh_server_crypto.c

Lines changed: 19 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@
2222
*/
2323

2424
/* Pick up compile-time configuration */
25-
#include "wolfhsm/wh_keyid.h"
2625
#include "wolfhsm/wh_settings.h"
2726

2827
#if !defined(WOLFHSM_CFG_NO_CRYPTO) && defined(WOLFHSM_CFG_ENABLE_SERVER)
@@ -53,6 +52,7 @@
5352
#include "wolfhsm/wh_utils.h"
5453
#include "wolfhsm/wh_server_keystore.h"
5554
#include "wolfhsm/wh_server_crypto.h"
55+
#include "wolfhsm/wh_keyid.h"
5656

5757
#include "wolfhsm/wh_server.h"
5858

@@ -1126,7 +1126,7 @@ static int _HandleEccVerify(whServerContext* ctx, uint16_t magic, int devId,
11261126
whMessageCrypto_EccVerifyRequest req;
11271127
whMessageCrypto_EccVerifyResponse res;
11281128

1129-
uint32_t available = inSize - sizeof(whMessageCrypto_EccVerifyRequest);
1129+
uint32_t available = 0;
11301130
uint32_t options = 0;
11311131
whKeyId key_id = WH_KEYID_ERASED;
11321132
uint32_t hash_len = 0;
@@ -1153,6 +1153,7 @@ static int _HandleEccVerify(whServerContext* ctx, uint16_t magic, int devId,
11531153
}
11541154

11551155
/* Validate variable-length fields fit within inSize */
1156+
available = inSize - sizeof(whMessageCrypto_EccVerifyRequest);
11561157
if (req.sigSz > available) {
11571158
return WH_ERROR_BADARGS;
11581159
}
@@ -1390,7 +1391,7 @@ static int _HandleHkdf(whServerContext* ctx, uint16_t magic, int devId,
13901391
whNvmFlags flags = 0;
13911392
uint8_t* label = 0;
13921393
uint16_t label_size = WH_NVM_LABEL_LEN;
1393-
uint32_t available = inSize - sizeof(whMessageCrypto_HkdfRequest);
1394+
uint32_t available = 0;
13941395

13951396
const uint8_t* inKey = NULL;
13961397
const uint8_t* salt = NULL;
@@ -1428,6 +1429,7 @@ static int _HandleHkdf(whServerContext* ctx, uint16_t magic, int devId,
14281429
WH_KEYTYPE_CRYPTO, ctx->comm->client_id, req.keyIdIn);
14291430

14301431
/* Validate variable-length fields fit within input buffer */
1432+
available = inSize - sizeof(whMessageCrypto_HkdfRequest);
14311433
if (inKeySz > available) {
14321434
return WH_ERROR_BADARGS;
14331435
}
@@ -1543,7 +1545,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic, int devId,
15431545
whNvmFlags flags = WH_NVM_FLAGS_NONE;
15441546
uint8_t* label = NULL;
15451547
uint16_t label_size = WH_NVM_LABEL_LEN;
1546-
uint32_t available = inSize - sizeof(whMessageCrypto_CmacKdfRequest);
1548+
uint32_t available = 0;
15471549

15481550
const uint8_t* salt = NULL;
15491551
const uint8_t* z = NULL;
@@ -1583,6 +1585,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic, int devId,
15831585

15841586

15851587
/* Validate variable-length fields fit within input buffer */
1588+
available = inSize - sizeof(whMessageCrypto_CmacKdfRequest);
15861589
if (saltSz > available) {
15871590
return WH_ERROR_BADARGS;
15881591
}
@@ -1977,7 +1980,7 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic, int devId,
19771980
whMessageCrypto_Ed25519SignRequest req;
19781981
uint8_t sig[ED25519_SIG_SIZE];
19791982
word32 sig_len = sizeof(sig);
1980-
uint32_t available = inSize - sizeof(req);
1983+
uint32_t available = 0;
19811984
whKeyId key_id = WH_KEYID_ERASED;
19821985
uint32_t msg_len = 0;
19831986
uint8_t* req_msg = NULL;
@@ -1995,6 +1998,8 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic, int devId,
19951998
return ret;
19961999
}
19972000

2001+
/* Validate variable-length fields fit within input buffer */
2002+
available = inSize - sizeof(whMessageCrypto_Ed25519SignRequest);
19982003
if (req.msgSz > available) {
19992004
return WH_ERROR_BADARGS;
20002005
}
@@ -2079,7 +2084,7 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic, int devId,
20792084
ed25519_key key[1];
20802085
whMessageCrypto_Ed25519VerifyRequest req;
20812086
whMessageCrypto_Ed25519VerifyResponse res;
2082-
uint32_t available = inSize - sizeof(req);
2087+
uint32_t available = 0;
20832088
whKeyId key_id = WH_KEYID_ERASED;
20842089
uint32_t sig_len = 0;
20852090
uint32_t msg_len = 0;
@@ -2099,6 +2104,8 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic, int devId,
20992104
return ret;
21002105
}
21012106

2107+
/* Validate variable-length fields fit within input buffer */
2108+
available = inSize - sizeof(whMessageCrypto_Ed25519VerifyRequest);
21022109
if (req.sigSz > available) {
21032110
return WH_ERROR_BADARGS;
21042111
}
@@ -2177,7 +2184,7 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic,
21772184
whMessageCrypto_Ed25519SignDmaRequest req;
21782185
whMessageCrypto_Ed25519SignDmaResponse res;
21792186
word32 sigLen = 0;
2180-
uint32_t available = inSize - sizeof(req);
2187+
uint32_t available = 0;
21812188
uint8_t* req_ctx = NULL;
21822189
whKeyId key_id = WH_KEYID_ERASED;
21832190
int evict = 0;
@@ -2193,6 +2200,8 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic,
21932200
return ret;
21942201
}
21952202

2203+
/* Validate variable-length fields fit within input buffer */
2204+
available = inSize - sizeof(whMessageCrypto_Ed25519SignDmaRequest);
21962205
if (req.ctxSz > available) {
21972206
return WH_ERROR_BADARGS;
21982207
}
@@ -2287,7 +2296,7 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic,
22872296
void* sigAddr = NULL;
22882297
whMessageCrypto_Ed25519VerifyDmaRequest req;
22892298
whMessageCrypto_Ed25519VerifyDmaResponse res;
2290-
uint32_t available = inSize - sizeof(req);
2299+
uint32_t available = 0;
22912300
uint8_t* req_ctx = NULL;
22922301
whKeyId key_id = WH_KEYID_ERASED;
22932302
int evict = 0;
@@ -2303,6 +2312,8 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic,
23032312
return ret;
23042313
}
23052314

2315+
/* Validate variable-length fields fit within input buffer */
2316+
available = inSize - sizeof(whMessageCrypto_Ed25519VerifyDmaRequest);
23062317
if (req.ctxSz > available) {
23072318
return WH_ERROR_BADARGS;
23082319
}

0 commit comments

Comments
 (0)