Skip to content

Commit 5de9c72

Browse files
committed
Address review feedback
1 parent 568732a commit 5de9c72

4 files changed

Lines changed: 30 additions & 15 deletions

File tree

examples/posix/wh_posix_server/wh_posix_server.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -301,19 +301,19 @@ int main(int argc, char** argv)
301301
WH_NVM_FLAGS_USAGE_ANY; /* Default flags if none provided */
302302
uint8_t memory[WH_POSIX_FLASH_RAM_SIZE] = {0};
303303
whServerConfig s_conf[1];
304-
WC_RNG rng[1];
305-
uint8_t buffer[128] = {0};
306304

307305
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
308306
/* Crypto context */
309307
whServerCryptoContext crypto[1] = {{
310308
.devId = INVALID_DEVID,
311309
}};
310+
WC_RNG rng[1];
311+
uint8_t buffer[128] = {0};
312312

313313
#if defined(WOLFHSM_CFG_SHE_EXTENSION)
314314
whServerSheContext she[1] = {{0}};
315315
#endif
316-
316+
#endif /* !defined(WOLFHSM_CFG_NO_CRYPTO) */
317317

318318
WOLFHSM_CFG_PRINTF("Example wolfHSM POSIX server ");
319319
#ifndef WOLFHSM_CFG_NO_CRYPTO
@@ -341,9 +341,10 @@ int main(int argc, char** argv)
341341
}
342342
else if (strcmp(argv[i], "--flags") == 0 && i + 1 < argc) {
343343
char* end;
344-
unsigned long val = strtoul(argv[i + 1], &end, 0);
345-
errno = 0;
344+
unsigned long val;
346345

346+
errno = 0;
347+
val = strtoul(argv[i + 1], &end, 0);
347348
if (errno || *end || val > 0xFFFF) {
348349
WOLFHSM_CFG_PRINTF("Invalid --flags value: %s\n", argv[i + 1]);
349350
return -1;
@@ -428,6 +429,7 @@ int main(int argc, char** argv)
428429
return rc;
429430
}
430431

432+
#if !defined(WOLFHSM_CFG_NO_CRYPTO)
431433
s_conf->crypto = crypto;
432434
s_conf->devId = INVALID_DEVID;
433435
#if defined(WOLFHSM_CFG_SHE_EXTENSION)

src/wh_client_crypto.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -604,7 +604,7 @@ int wh_Client_AesEcb(whClientContext* ctx, Aes* aes, int enc, const uint8_t* in,
604604
whKeyId key_id = WH_DEVCTX_TO_KEYID(aes->devCtx);
605605
uint8_t* req_in = NULL;
606606
uint8_t* req_key = NULL;
607-
uint16_t req_len = 0;
607+
uint64_t req_len = 0;
608608

609609
uint16_t group = WH_MESSAGE_GROUP_CRYPTO;
610610
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,
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,
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
}
@@ -1427,7 +1428,7 @@ static int _HandleHkdf(whServerContext* ctx, uint16_t magic,
14271428
whNvmFlags flags = 0;
14281429
uint8_t* label = 0;
14291430
uint16_t label_size = WH_NVM_LABEL_LEN;
1430-
uint32_t available = inSize - sizeof(whMessageCrypto_HkdfRequest);
1431+
uint32_t available = 0;
14311432

14321433
const uint8_t* inKey = NULL;
14331434
const uint8_t* salt = NULL;
@@ -1463,6 +1464,7 @@ static int _HandleHkdf(whServerContext* ctx, uint16_t magic,
14631464
WH_KEYTYPE_CRYPTO, ctx->comm->client_id, req.keyIdIn);
14641465

14651466
/* Validate variable-length fields fit within input buffer */
1467+
available = inSize - sizeof(whMessageCrypto_HkdfRequest);
14661468
if (inKeySz > available) {
14671469
return WH_ERROR_BADARGS;
14681470
}
@@ -1578,7 +1580,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic,
15781580
whNvmFlags flags = WH_NVM_FLAGS_NONE;
15791581
uint8_t* label = NULL;
15801582
uint16_t label_size = WH_NVM_LABEL_LEN;
1581-
uint32_t available = inSize - sizeof(whMessageCrypto_CmacKdfRequest);
1583+
uint32_t available = 0;
15821584

15831585
const uint8_t* salt = NULL;
15841586
const uint8_t* z = NULL;
@@ -1618,6 +1620,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic,
16181620

16191621

16201622
/* Validate variable-length fields fit within input buffer */
1623+
available = inSize - sizeof(whMessageCrypto_CmacKdfRequest);
16211624
if (saltSz > available) {
16221625
return WH_ERROR_BADARGS;
16231626
}
@@ -2011,7 +2014,7 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic,
20112014
whMessageCrypto_Ed25519SignRequest req;
20122015
uint8_t sig[ED25519_SIG_SIZE];
20132016
word32 sig_len = sizeof(sig);
2014-
uint32_t available = inSize - sizeof(req);
2017+
uint32_t available = 0;
20152018
whKeyId key_id = WH_KEYID_ERASED;
20162019
uint32_t msg_len = 0;
20172020
uint8_t* req_msg = NULL;
@@ -2029,6 +2032,8 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic,
20292032
return ret;
20302033
}
20312034

2035+
/* Validate variable-length fields fit within input buffer */
2036+
available = inSize - sizeof(whMessageCrypto_Ed25519SignRequest);
20322037
if (req.msgSz > available) {
20332038
return WH_ERROR_BADARGS;
20342039
}
@@ -2113,7 +2118,7 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic,
21132118
ed25519_key key[1];
21142119
whMessageCrypto_Ed25519VerifyRequest req;
21152120
whMessageCrypto_Ed25519VerifyResponse res;
2116-
uint32_t available = inSize - sizeof(req);
2121+
uint32_t available = 0;
21172122
whKeyId key_id = WH_KEYID_ERASED;
21182123
uint32_t sig_len = 0;
21192124
uint32_t msg_len = 0;
@@ -2133,6 +2138,8 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic,
21332138
return ret;
21342139
}
21352140

2141+
/* Validate variable-length fields fit within input buffer */
2142+
available = inSize - sizeof(whMessageCrypto_Ed25519VerifyRequest);
21362143
if (req.sigSz > available) {
21372144
return WH_ERROR_BADARGS;
21382145
}
@@ -2210,7 +2217,7 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic,
22102217
whMessageCrypto_Ed25519SignDmaRequest req;
22112218
whMessageCrypto_Ed25519SignDmaResponse res;
22122219
word32 sigLen = 0;
2213-
uint32_t available = inSize - sizeof(req);
2220+
uint32_t available = 0;
22142221
uint8_t* req_ctx = NULL;
22152222
whKeyId key_id = WH_KEYID_ERASED;
22162223
int evict = 0;
@@ -2226,6 +2233,8 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic,
22262233
return ret;
22272234
}
22282235

2236+
/* Validate variable-length fields fit within input buffer */
2237+
available = inSize - sizeof(whMessageCrypto_Ed25519SignDmaRequest);
22292238
if (req.ctxSz > available) {
22302239
return WH_ERROR_BADARGS;
22312240
}
@@ -2319,7 +2328,7 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic,
23192328
void* sigAddr = NULL;
23202329
whMessageCrypto_Ed25519VerifyDmaRequest req;
23212330
whMessageCrypto_Ed25519VerifyDmaResponse res;
2322-
uint32_t available = inSize - sizeof(req);
2331+
uint32_t available = 0;
23232332
uint8_t* req_ctx = NULL;
23242333
whKeyId key_id = WH_KEYID_ERASED;
23252334
int evict = 0;
@@ -2335,6 +2344,8 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic,
23352344
return ret;
23362345
}
23372346

2347+
/* Validate variable-length fields fit within input buffer */
2348+
available = inSize - sizeof(whMessageCrypto_Ed25519VerifyDmaRequest);
23382349
if (req.ctxSz > available) {
23392350
return WH_ERROR_BADARGS;
23402351
}

0 commit comments

Comments
 (0)