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