@@ -390,6 +390,9 @@ int BSL_AuthCtx_Init(BSL_AuthCtx_t *hmac_ctx, void *keyhandle, BSL_CryptoCipherS
390390 BSL_LOG_ERR ("invalid block size zero, assuming %zu" , hmac_ctx -> block_size );
391391 }
392392
393+ res = BSL_Data_InitBuffer (& hmac_ctx -> in_buf , hmac_ctx -> block_size );
394+ CHK_PROPERTY (!res );
395+
393396 key_info -> stats .stats [BSL_CRYPTO_KEYSTATS_TIMES_USED ]++ ;
394397
395398 return 0 ;
@@ -439,6 +442,7 @@ int BSL_AuthCtx_Finalize(BSL_AuthCtx_t *hmac_ctx, void **hmac, size_t *hmac_len)
439442
440443int BSL_AuthCtx_Deinit (BSL_AuthCtx_t * hmac_ctx )
441444{
445+ BSL_Data_Deinit (& hmac_ctx -> in_buf );
442446 EVP_MD_CTX_free (hmac_ctx -> libhandle );
443447 memset (hmac_ctx , 0 , sizeof (BSL_AuthCtx_t ));
444448 return 0 ;
@@ -490,6 +494,12 @@ int BSL_Cipher_Init(BSL_Cipher_t *cipher_ctx, BSL_CipherMode_e enc, BSL_CryptoCi
490494 res = EVP_CipherInit_ex (cipher_ctx -> libhandle , NULL , NULL , key -> raw .ptr , init_vec , -1 );
491495 CHK_PROPERTY (res == 1 );
492496
497+ res = BSL_Data_InitBuffer (& cipher_ctx -> in_buf , cipher_ctx -> block_size );
498+ CHK_PROPERTY (!res );
499+
500+ res = BSL_Data_InitBuffer (& cipher_ctx -> out_buf , cipher_ctx -> block_size );
501+ CHK_PROPERTY (!res );
502+
493503 key -> stats .stats [BSL_CRYPTO_KEYSTATS_TIMES_USED ]++ ;
494504
495505 return 0 ;
@@ -508,17 +518,17 @@ int BSL_Cipher_AddAAD(BSL_Cipher_t *cipher_ctx, const void *aad, int aad_len)
508518 return 0 ;
509519}
510520
511- int BSL_Cipher_AddData (BSL_Cipher_t * cipher_ctx , BSL_Data_t plaintext , BSL_Data_t ciphertext )
521+ int BSL_Cipher_AddData (BSL_Cipher_t * cipher_ctx , const BSL_Data_t * input , BSL_Data_t * output )
512522{
513523 ASSERT_ARG_NONNULL (cipher_ctx );
514- int cipherlen = (int )ciphertext . len ;
515- if (EVP_CipherUpdate (cipher_ctx -> libhandle , ciphertext . ptr , & cipherlen , plaintext . ptr , (int )plaintext . len ) != 1 )
524+ int cipherlen = (int )output -> len ;
525+ if (EVP_CipherUpdate (cipher_ctx -> libhandle , output -> ptr , & cipherlen , input -> ptr , (int )input -> len ) != 1 )
516526 {
517527 return -1 ;
518528 }
519529
520530 BSL_CryptoKey_t * key = (BSL_CryptoKey_t * )cipher_ctx -> keyhandle ;
521- key -> stats .stats [BSL_CRYPTO_KEYSTATS_BYTES_PROCESSED ] += plaintext . len ;
531+ key -> stats .stats [BSL_CRYPTO_KEYSTATS_BYTES_PROCESSED ] += input -> len ;
522532
523533 return cipherlen ;
524534}
@@ -596,7 +606,7 @@ int BSL_Cipher_FinalizeData(BSL_Cipher_t *cipher_ctx, BSL_Data_t *extra)
596606 CHK_PROPERTY (res == 1 );
597607 BSL_LOG_DEBUG ("extra->len = %zu | got len = %d" , extra -> len , len );
598608 memset (extra -> ptr , 0 , extra -> len );
599- BSL_LOG_INFO ("Completed EVP_CipherFinal_ex" );
609+ BSL_LOG_DEBUG ("Completed EVP_CipherFinal_ex" );
600610 if (len > 0 )
601611 {
602612 memcpy (extra -> ptr , buf , sizeof (buf ));
@@ -610,11 +620,8 @@ int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer)
610620 CHK_ARG_NONNULL (cipher_ctx );
611621 CHK_ARG_NONNULL (writer );
612622
613- // finalize can add 1 cipher block
614- uint8_t buf [cipher_ctx -> block_size ];
615-
616623 int evp_len = 0 ;
617- int res = EVP_CipherFinal_ex (cipher_ctx -> libhandle , buf , & evp_len );
624+ int res = EVP_CipherFinal_ex (cipher_ctx -> libhandle , cipher_ctx -> out_buf . ptr , & evp_len );
618625 if (res != 1 )
619626 {
620627 BSL_LOG_ERR ("EVP_CipherFinal_ex error %s" , ERR_error_string (ERR_get_error (), NULL ));
@@ -624,7 +631,7 @@ int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer)
624631 if (evp_len > 0 )
625632 {
626633 size_t bsl_len = evp_len ;
627- BSL_SeqWriter_Put (writer , buf , bsl_len );
634+ BSL_SeqWriter_Put (writer , cipher_ctx -> out_buf . ptr , bsl_len );
628635 }
629636
630637 return 0 ;
@@ -633,6 +640,8 @@ int BSL_Cipher_FinalizeSeq(BSL_Cipher_t *cipher_ctx, BSL_SeqWriter_t *writer)
633640int BSL_Cipher_Deinit (BSL_Cipher_t * cipher_ctx )
634641{
635642 CHK_ARG_NONNULL (cipher_ctx );
643+ BSL_Data_Deinit (& cipher_ctx -> out_buf );
644+ BSL_Data_Deinit (& cipher_ctx -> in_buf );
636645 EVP_CIPHER_CTX_free (cipher_ctx -> libhandle );
637646 memset (cipher_ctx , 0 , sizeof (* cipher_ctx ));
638647 return BSL_SUCCESS ;
0 commit comments