@@ -7671,35 +7671,19 @@ int wh_Client_MlKemImportKey(whClientContext* ctx, MlKemKey* key,
76717671{
76727672 int ret = WH_ERROR_OK ;
76737673 whKeyId key_id = WH_KEYID_ERASED ;
7674- byte * buffer = NULL ;
7675- uint16_t buffer_len = 0 ;
7676- word32 allocSz = 0 ;
7674+ byte buffer [WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ];
7675+ uint16_t buffer_len = sizeof (buffer );
76777676
76787677 if ((ctx == NULL ) || (key == NULL ) ||
76797678 ((label_len != 0 ) && (label == NULL ))) {
76807679 return WH_ERROR_BADARGS ;
76817680 }
76827681
7683- /* Use exact key size based on level to avoid over-allocation */
7684- ret = wc_MlKemKey_PrivateKeySize (key , & allocSz );
7685- if (ret != 0 ) {
7686- /* Fall back to public key size if no private key */
7687- ret = wc_MlKemKey_PublicKeySize (key , & allocSz );
7688- }
7689- if (ret != 0 || allocSz == 0 ) {
7690- return WH_ERROR_BADARGS ;
7691- }
7692-
7693- buffer = (byte * )XMALLOC (allocSz , NULL , DYNAMIC_TYPE_TMP_BUFFER );
7694- if (buffer == NULL ) {
7695- return WH_ERROR_ABORTED ;
7696- }
7697-
76987682 if (inout_keyId != NULL ) {
76997683 key_id = * inout_keyId ;
77007684 }
77017685
7702- ret = wh_Crypto_MlKemSerializeKey (key , (uint16_t )allocSz , buffer ,
7686+ ret = wh_Crypto_MlKemSerializeKey (key , (uint16_t )buffer_len , buffer ,
77037687 & buffer_len );
77047688 WH_DEBUG_CLIENT_VERBOSE ("MlKemImportKey: serialize ret:%d, len:%u\n" ,
77057689 ret , (unsigned int )buffer_len );
@@ -7712,28 +7696,21 @@ int wh_Client_MlKemImportKey(whClientContext* ctx, MlKemKey* key,
77127696 }
77137697 WH_DEBUG_CLIENT_VERBOSE ("MlKemImportKey: ret:%d keyId:%u\n" , ret , key_id );
77147698
7715- wc_ForceZero (buffer , allocSz );
7716- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
7699+ wc_ForceZero (buffer , buffer_len );
77177700 return ret ;
77187701}
77197702
77207703int wh_Client_MlKemExportKey (whClientContext * ctx , whKeyId keyId , MlKemKey * key ,
77217704 uint16_t label_len , uint8_t * label )
77227705{
77237706 int ret = WH_ERROR_OK ;
7724- byte * buffer = NULL ;
7725- uint16_t buffer_len = WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ;
7707+ byte buffer [ WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ] ;
7708+ uint16_t buffer_len = sizeof ( buffer ) ;
77267709
77277710 if ((ctx == NULL ) || WH_KEYID_ISERASED (keyId ) || (key == NULL )) {
77287711 return WH_ERROR_BADARGS ;
77297712 }
77307713
7731- buffer = (byte * )XMALLOC (WC_ML_KEM_MAX_PRIVATE_KEY_SIZE , NULL ,
7732- DYNAMIC_TYPE_TMP_BUFFER );
7733- if (buffer == NULL ) {
7734- return WH_ERROR_ABORTED ;
7735- }
7736-
77377714 ret =
77387715 wh_Client_KeyExport (ctx , keyId , label , label_len , buffer , & buffer_len );
77397716 WH_DEBUG_CLIENT_VERBOSE ("MlKemExportKey: export ret:%d, len:%u\n" ,
@@ -7743,8 +7720,7 @@ int wh_Client_MlKemExportKey(whClientContext* ctx, whKeyId keyId, MlKemKey* key,
77437720 }
77447721 WH_DEBUG_CLIENT_VERBOSE ("MlKemExportKey: keyId:%x ret:%d\n" , keyId , ret );
77457722
7746- wc_ForceZero (buffer , WC_ML_KEM_MAX_PRIVATE_KEY_SIZE );
7747- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
7723+ wc_ForceZero (buffer , buffer_len );
77487724 return ret ;
77497725}
77507726
@@ -8110,34 +8086,19 @@ int wh_Client_MlKemImportKeyDma(whClientContext* ctx, MlKemKey* key,
81108086{
81118087 int ret = WH_ERROR_OK ;
81128088 whKeyId key_id = WH_KEYID_ERASED ;
8113- byte * buffer = NULL ;
8114- uint16_t buffer_len = 0 ;
8115- word32 allocSz = 0 ;
8089+ byte buffer [WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ];
8090+ uint16_t buffer_len = sizeof (buffer );
81168091
81178092 if ((ctx == NULL ) || (key == NULL ) ||
81188093 ((label_len != 0 ) && (label == NULL ))) {
81198094 return WH_ERROR_BADARGS ;
81208095 }
81218096
8122- /* Use exact key size based on level to avoid over-allocation */
8123- ret = wc_MlKemKey_PrivateKeySize (key , & allocSz );
8124- if (ret != 0 ) {
8125- ret = wc_MlKemKey_PublicKeySize (key , & allocSz );
8126- }
8127- if (ret != 0 || allocSz == 0 ) {
8128- return WH_ERROR_BADARGS ;
8129- }
8130-
8131- buffer = (byte * )XMALLOC (allocSz , NULL , DYNAMIC_TYPE_TMP_BUFFER );
8132- if (buffer == NULL ) {
8133- return WH_ERROR_ABORTED ;
8134- }
8135-
81368097 if (inout_keyId != NULL ) {
81378098 key_id = * inout_keyId ;
81388099 }
81398100
8140- ret = wh_Crypto_MlKemSerializeKey (key , (uint16_t )allocSz , buffer ,
8101+ ret = wh_Crypto_MlKemSerializeKey (key , (uint16_t )buffer_len , buffer ,
81418102 & buffer_len );
81428103 WH_DEBUG_CLIENT_VERBOSE ("MlKemImportKeyDma: serialize ret:%d, len:%u\n" ,
81438104 ret , (unsigned int )buffer_len );
@@ -8151,8 +8112,7 @@ int wh_Client_MlKemImportKeyDma(whClientContext* ctx, MlKemKey* key,
81518112 WH_DEBUG_CLIENT_VERBOSE ("MlKemImportKeyDma: ret:%d keyId:%u\n" ,
81528113 ret , key_id );
81538114
8154- wc_ForceZero (buffer , allocSz );
8155- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
8115+ wc_ForceZero (buffer , buffer_len );
81568116 return ret ;
81578117}
81588118
@@ -8161,20 +8121,13 @@ int wh_Client_MlKemExportKeyDma(whClientContext* ctx, whKeyId keyId,
81618121 uint8_t * label )
81628122{
81638123 int ret = WH_ERROR_OK ;
8164- byte * buffer = NULL ;
8165- uint16_t buffer_len = WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ;
8124+ byte buffer [ WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ] = { 0 } ;
8125+ uint16_t buffer_len = sizeof ( buffer ) ;
81668126
81678127 if ((ctx == NULL ) || WH_KEYID_ISERASED (keyId ) || (key == NULL )) {
81688128 return WH_ERROR_BADARGS ;
81698129 }
81708130
8171- buffer = (byte * )XMALLOC (WC_ML_KEM_MAX_PRIVATE_KEY_SIZE , NULL ,
8172- DYNAMIC_TYPE_TMP_BUFFER );
8173- if (buffer == NULL ) {
8174- return WH_ERROR_ABORTED ;
8175- }
8176- memset (buffer , 0 , WC_ML_KEM_MAX_PRIVATE_KEY_SIZE );
8177-
81788131 ret = wh_Client_KeyExportDma (ctx , keyId , buffer , buffer_len , label ,
81798132 label_len , & buffer_len );
81808133 WH_DEBUG_CLIENT_VERBOSE ("MlKemExportKeyDma: export ret:%d, len:%u\n" ,
@@ -8185,8 +8138,7 @@ int wh_Client_MlKemExportKeyDma(whClientContext* ctx, whKeyId keyId,
81858138 WH_DEBUG_CLIENT_VERBOSE ("MlKemExportKeyDma: keyId:%x ret:%d\n" ,
81868139 keyId , ret );
81878140
8188- wc_ForceZero (buffer , WC_ML_KEM_MAX_PRIVATE_KEY_SIZE );
8189- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
8141+ wc_ForceZero (buffer , buffer_len );
81908142 return ret ;
81918143}
81928144
@@ -8196,31 +8148,20 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level,
81968148{
81978149 int ret = WH_ERROR_OK ;
81988150 whKeyId key_id = WH_KEYID_ERASED ;
8199- byte * buffer = NULL ;
82008151 uint8_t * dataPtr = NULL ;
82018152 whMessageCrypto_MlKemKeyGenDmaRequest * req = NULL ;
82028153 whMessageCrypto_MlKemKeyGenDmaResponse * res = NULL ;
82038154 uintptr_t keyAddr = 0 ;
8204- uint32_t allocSz = 0 ;
82058155
8206- if ((ctx == NULL ) || (key == NULL )) {
8207- return WH_ERROR_BADARGS ;
8208- }
8156+ byte buffer [WC_ML_KEM_MAX_PRIVATE_KEY_SIZE ];
8157+ uint16_t buffer_len = sizeof (buffer );
82098158
8210- ret = wc_MlKemKey_PrivateKeySize (key , & allocSz );
8211- if (ret != 0 ) {
8159+ if ((ctx == NULL ) || (key == NULL )) {
82128160 return WH_ERROR_BADARGS ;
82138161 }
8214- else {
8215- buffer = (byte * )XMALLOC (allocSz , NULL , DYNAMIC_TYPE_TMP_BUFFER );
8216- if (buffer == NULL ) {
8217- return WH_ERROR_ABORTED ;
8218- }
8219- }
82208162
82218163 dataPtr = (uint8_t * )wh_CommClient_GetDataPtr (ctx -> comm );
82228164 if (dataPtr == NULL ) {
8223- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
82248165 return WH_ERROR_BADARGS ;
82258166 }
82268167
@@ -8238,7 +8179,6 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level,
82388179 sizeof (whMessageCrypto_GenericRequestHeader ) + sizeof (* req );
82398180
82408181 if (req_len > WOLFHSM_CFG_COMM_DATA_LEN ) {
8241- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
82428182 return WH_ERROR_BADARGS ;
82438183 }
82448184
@@ -8247,10 +8187,10 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level,
82478187 req -> flags = flags ;
82488188 req -> keyId = key_id ;
82498189 req -> access = WH_NVM_ACCESS_ANY ;
8250- req -> key .sz = allocSz ;
8190+ req -> key .sz = buffer_len ;
82518191
82528192 ret = wh_Client_DmaProcessClientAddress (
8253- ctx , (uintptr_t )buffer , (void * * )& keyAddr , allocSz ,
8193+ ctx , (uintptr_t )buffer , (void * * )& keyAddr , buffer_len ,
82548194 WH_DMA_OPER_CLIENT_WRITE_PRE , (whDmaFlags ){0 });
82558195 if (ret == WH_ERROR_OK ) {
82568196 req -> key .addr = (uint64_t )(uintptr_t )keyAddr ;
@@ -8276,7 +8216,7 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level,
82768216 }
82778217
82788218 (void )wh_Client_DmaProcessClientAddress (
8279- ctx , (uintptr_t )buffer , (void * * )& keyAddr , allocSz ,
8219+ ctx , (uintptr_t )buffer , (void * * )& keyAddr , buffer_len ,
82808220 WH_DMA_OPER_CLIENT_WRITE_POST , (whDmaFlags ){0 });
82818221
82828222 if (ret == WH_ERROR_OK ) {
@@ -8290,7 +8230,7 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level,
82908230 if (key != NULL ) {
82918231 wh_Client_MlKemSetKeyId (key , key_id );
82928232 if ((flags & WH_NVM_FLAGS_EPHEMERAL ) != 0 ) {
8293- if (res -> keySize > allocSz ) {
8233+ if (res -> keySize > buffer_len ) {
82948234 ret = WH_ERROR_BADARGS ;
82958235 }
82968236 else {
@@ -8302,8 +8242,7 @@ static int _MlKemMakeKeyDma(whClientContext* ctx, int level,
83028242 }
83038243 }
83048244
8305- wc_ForceZero (buffer , allocSz );
8306- XFREE (buffer , NULL , DYNAMIC_TYPE_TMP_BUFFER );
8245+ wc_ForceZero (buffer , buffer_len );
83078246 return ret ;
83088247}
83098248
0 commit comments