@@ -834,7 +834,7 @@ static int we_digest_update(EVP_MD_CTX *ctx, const void *data, size_t len)
834834 }
835835 if (ret == 1 ) {
836836 /* Allocate new, aligned buffer. */
837- tmp = (byte * )XMALLOC (len - add , NULL , DYNAMIC_TYPE_TMP_BUFFER );
837+ tmp = (byte * )XMALLOC (WC_SHA512_BLOCK_SIZE , NULL , DYNAMIC_TYPE_TMP_BUFFER );
838838 if (tmp == NULL ) {
839839 WOLFENGINE_ERROR_FUNC_NULL (WE_LOG_DIGEST , "XMALLOC" ,
840840 tmp );
@@ -843,13 +843,22 @@ static int we_digest_update(EVP_MD_CTX *ctx, const void *data, size_t len)
843843 }
844844 if (ret == 1 ) {
845845 /* Copy remaining data from the unaligned buffer to the aligned one
846- * and update the hash. */
847- XMEMCPY (tmp , (byte * )data + add , len - add );
848- rc = wc_HashUpdate (& digest -> hash , digest -> hashType ,
849- (const byte * )tmp , len - add );
850- if (rc != 0 ) {
851- WOLFENGINE_ERROR_FUNC (WE_LOG_DIGEST , "wc_HashUpdate" , rc );
852- ret = 0 ;
846+ * and update the hash iteratively, one block's worth of data at a
847+ * time. */
848+ byte * nextData = (byte * )data + add ;
849+ for (size_t remaining = len - add ; remaining > 0 ;) {
850+ size_t nextLen = (remaining <= WC_SHA512_BLOCK_SIZE ) ?
851+ remaining : WC_SHA512_BLOCK_SIZE ;
852+ XMEMCPY (tmp , nextData , nextLen );
853+ rc = wc_HashUpdate (& digest -> hash , digest -> hashType ,
854+ (const byte * )tmp , nextLen );
855+ if (rc != 0 ) {
856+ WOLFENGINE_ERROR_FUNC (WE_LOG_DIGEST , "wc_HashUpdate" , rc );
857+ ret = 0 ;
858+ break ;
859+ }
860+ nextData += nextLen ;
861+ remaining -= nextLen ;
853862 }
854863 }
855864
0 commit comments