@@ -2590,10 +2590,12 @@ void CipherBase::Init(const char* cipher_type,
25902590 1 ,
25912591 key,
25922592 iv);
2593+ CHECK_NE (key_len, 0 );
25932594
25942595 ctx_.reset (EVP_CIPHER_CTX_new ());
25952596 const bool encrypt = (kind_ == kCipher );
2596- EVP_CipherInit_ex (ctx_.get (), cipher, nullptr , nullptr , nullptr , encrypt);
2597+ CHECK (EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2598+ nullptr , nullptr , encrypt));
25972599
25982600 int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
25992601 if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
@@ -2616,12 +2618,12 @@ void CipherBase::Init(const char* cipher_type,
26162618
26172619 CHECK_EQ (1 , EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len));
26182620
2619- EVP_CipherInit_ex (ctx_.get (),
2620- nullptr ,
2621- nullptr ,
2622- reinterpret_cast <unsigned char *>(key),
2623- reinterpret_cast <unsigned char *>(iv),
2624- encrypt);
2621+ CHECK ( EVP_CipherInit_ex (ctx_.get (),
2622+ nullptr ,
2623+ nullptr ,
2624+ reinterpret_cast <unsigned char *>(key),
2625+ reinterpret_cast <unsigned char *>(iv),
2626+ encrypt) );
26252627}
26262628
26272629
@@ -2686,25 +2688,27 @@ void CipherBase::InitIv(const char* cipher_type,
26862688 EVP_CIPHER_CTX_set_flags (ctx_.get (), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
26872689
26882690 const bool encrypt = (kind_ == kCipher );
2689- EVP_CipherInit_ex (ctx_.get (), cipher, nullptr , nullptr , nullptr , encrypt);
2691+ CHECK (EVP_CipherInit_ex (ctx_.get (), cipher, nullptr ,
2692+ nullptr , nullptr , encrypt));
26902693
26912694 if (IsAuthenticatedMode ()) {
26922695 CHECK (has_iv);
26932696 if (!InitAuthenticated (cipher_type, iv_len, auth_tag_len))
26942697 return ;
26952698 }
26962699
2700+ ClearErrorOnReturn clear_error_on_return;
26972701 if (!EVP_CIPHER_CTX_set_key_length (ctx_.get (), key_len)) {
26982702 ctx_.reset ();
26992703 return env ()->ThrowError (" Invalid key length" );
27002704 }
27012705
2702- EVP_CipherInit_ex (ctx_.get (),
2703- nullptr ,
2704- nullptr ,
2705- reinterpret_cast <const unsigned char *>(key),
2706- reinterpret_cast <const unsigned char *>(iv),
2707- encrypt);
2706+ CHECK ( EVP_CipherInit_ex (ctx_.get (),
2707+ nullptr ,
2708+ nullptr ,
2709+ reinterpret_cast <const unsigned char *>(key),
2710+ reinterpret_cast <const unsigned char *>(iv),
2711+ encrypt) );
27082712}
27092713
27102714
@@ -2749,6 +2753,7 @@ static bool IsValidGCMTagLength(unsigned int tag_len) {
27492753bool CipherBase::InitAuthenticated (const char * cipher_type, int iv_len,
27502754 unsigned int auth_tag_len) {
27512755 CHECK (IsAuthenticatedMode ());
2756+ MarkPopErrorOnReturn mark_pop_error_on_return;
27522757
27532758 if (!EVP_CIPHER_CTX_ctrl (ctx_.get (),
27542759 EVP_CTRL_AEAD_SET_IVLEN,
@@ -2893,6 +2898,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
28932898bool CipherBase::SetAAD (const char * data, unsigned int len, int plaintext_len) {
28942899 if (!ctx_ || !IsAuthenticatedMode ())
28952900 return false ;
2901+ ClearErrorOnReturn clear_error_on_return;
28962902
28972903 int outlen;
28982904 const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
@@ -2952,6 +2958,7 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
29522958 int * out_len) {
29532959 if (!ctx_)
29542960 return kErrorState ;
2961+ ClearErrorOnReturn clear_error_on_return;
29552962
29562963 const int mode = EVP_CIPHER_CTX_mode (ctx_.get ());
29572964
@@ -2963,10 +2970,10 @@ CipherBase::UpdateResult CipherBase::Update(const char* data,
29632970 // on first update:
29642971 if (kind_ == kDecipher && IsAuthenticatedMode () && auth_tag_len_ > 0 &&
29652972 auth_tag_len_ != kNoAuthTagLength && !auth_tag_set_) {
2966- EVP_CIPHER_CTX_ctrl (ctx_.get (),
2967- EVP_CTRL_GCM_SET_TAG,
2968- auth_tag_len_,
2969- reinterpret_cast <unsigned char *>(auth_tag_));
2973+ CHECK ( EVP_CIPHER_CTX_ctrl (ctx_.get (),
2974+ EVP_CTRL_GCM_SET_TAG,
2975+ auth_tag_len_,
2976+ reinterpret_cast <unsigned char *>(auth_tag_) ));
29702977 auth_tag_set_ = true ;
29712978 }
29722979
@@ -3044,6 +3051,7 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
30443051bool CipherBase::SetAutoPadding (bool auto_padding) {
30453052 if (!ctx_)
30463053 return false ;
3054+ ClearErrorOnReturn clear_error_on_return;
30473055 return EVP_CIPHER_CTX_set_padding (ctx_.get (), auto_padding);
30483056}
30493057
0 commit comments