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 }
@@ -1391,7 +1392,7 @@ static int _HandleHkdf(whServerContext* ctx, uint16_t magic, int devId,
13911392 whNvmFlags flags = 0 ;
13921393 uint8_t * label = 0 ;
13931394 uint16_t label_size = WH_NVM_LABEL_LEN ;
1394- uint32_t available = inSize - sizeof ( whMessageCrypto_HkdfRequest ) ;
1395+ uint32_t available = 0 ;
13951396
13961397 const uint8_t * inKey = NULL ;
13971398 const uint8_t * salt = NULL ;
@@ -1427,6 +1428,7 @@ static int _HandleHkdf(whServerContext* ctx, uint16_t magic, int devId,
14271428 WH_KEYTYPE_CRYPTO , ctx -> comm -> client_id , req .keyIdIn );
14281429
14291430 /* Validate variable-length fields fit within input buffer */
1431+ available = inSize - sizeof (whMessageCrypto_HkdfRequest );
14301432 if (inKeySz > available ) {
14311433 return WH_ERROR_BADARGS ;
14321434 }
@@ -1542,7 +1544,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic, int devId,
15421544 whNvmFlags flags = WH_NVM_FLAGS_NONE ;
15431545 uint8_t * label = NULL ;
15441546 uint16_t label_size = WH_NVM_LABEL_LEN ;
1545- uint32_t available = inSize - sizeof ( whMessageCrypto_CmacKdfRequest ) ;
1547+ uint32_t available = 0 ;
15461548
15471549 const uint8_t * salt = NULL ;
15481550 const uint8_t * z = NULL ;
@@ -1582,6 +1584,7 @@ static int _HandleCmacKdf(whServerContext* ctx, uint16_t magic, int devId,
15821584
15831585
15841586 /* Validate variable-length fields fit within input buffer */
1587+ available = inSize - sizeof (whMessageCrypto_CmacKdfRequest );
15851588 if (saltSz > available ) {
15861589 return WH_ERROR_BADARGS ;
15871590 }
@@ -1976,7 +1979,7 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic, int devId,
19761979 whMessageCrypto_Ed25519SignRequest req ;
19771980 uint8_t sig [ED25519_SIG_SIZE ];
19781981 word32 sig_len = sizeof (sig );
1979- uint32_t available = inSize - sizeof ( req ) ;
1982+ uint32_t available = 0 ;
19801983 whKeyId key_id = WH_KEYID_ERASED ;
19811984 uint32_t msg_len = 0 ;
19821985 uint8_t * req_msg = NULL ;
@@ -1994,6 +1997,8 @@ static int _HandleEd25519Sign(whServerContext* ctx, uint16_t magic, int devId,
19941997 return ret ;
19951998 }
19961999
2000+ /* Validate variable-length fields fit within input buffer */
2001+ available = inSize - sizeof (whMessageCrypto_Ed25519SignRequest );
19972002 if (req .msgSz > available ) {
19982003 return WH_ERROR_BADARGS ;
19992004 }
@@ -2078,7 +2083,7 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic, int devId,
20782083 ed25519_key key [1 ];
20792084 whMessageCrypto_Ed25519VerifyRequest req ;
20802085 whMessageCrypto_Ed25519VerifyResponse res ;
2081- uint32_t available = inSize - sizeof ( req ) ;
2086+ uint32_t available = 0 ;
20822087 whKeyId key_id = WH_KEYID_ERASED ;
20832088 uint32_t sig_len = 0 ;
20842089 uint32_t msg_len = 0 ;
@@ -2098,6 +2103,8 @@ static int _HandleEd25519Verify(whServerContext* ctx, uint16_t magic, int devId,
20982103 return ret ;
20992104 }
21002105
2106+ /* Validate variable-length fields fit within input buffer */
2107+ available = inSize - sizeof (whMessageCrypto_Ed25519VerifyRequest );
21012108 if (req .sigSz > available ) {
21022109 return WH_ERROR_BADARGS ;
21032110 }
@@ -2176,7 +2183,7 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic,
21762183 whMessageCrypto_Ed25519SignDmaRequest req ;
21772184 whMessageCrypto_Ed25519SignDmaResponse res ;
21782185 word32 sigLen = 0 ;
2179- uint32_t available = inSize - sizeof ( req ) ;
2186+ uint32_t available = 0 ;
21802187 uint8_t * req_ctx = NULL ;
21812188 whKeyId key_id = WH_KEYID_ERASED ;
21822189 int evict = 0 ;
@@ -2192,6 +2199,8 @@ static int _HandleEd25519SignDma(whServerContext* ctx, uint16_t magic,
21922199 return ret ;
21932200 }
21942201
2202+ /* Validate variable-length fields fit within input buffer */
2203+ available = inSize - sizeof (whMessageCrypto_Ed25519SignDmaRequest );
21952204 if (req .ctxSz > available ) {
21962205 return WH_ERROR_BADARGS ;
21972206 }
@@ -2286,7 +2295,7 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic,
22862295 void * sigAddr = NULL ;
22872296 whMessageCrypto_Ed25519VerifyDmaRequest req ;
22882297 whMessageCrypto_Ed25519VerifyDmaResponse res ;
2289- uint32_t available = inSize - sizeof ( req ) ;
2298+ uint32_t available = 0 ;
22902299 uint8_t * req_ctx = NULL ;
22912300 whKeyId key_id = WH_KEYID_ERASED ;
22922301 int evict = 0 ;
@@ -2302,6 +2311,8 @@ static int _HandleEd25519VerifyDma(whServerContext* ctx, uint16_t magic,
23022311 return ret ;
23032312 }
23042313
2314+ /* Validate variable-length fields fit within input buffer */
2315+ available = inSize - sizeof (whMessageCrypto_Ed25519VerifyDmaRequest );
23052316 if (req .ctxSz > available ) {
23062317 return WH_ERROR_BADARGS ;
23072318 }
0 commit comments