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