@@ -3457,7 +3457,7 @@ void CipherBase::Init(const char* cipher_type,
3457
3457
}
3458
3458
#endif // NODE_FIPS_MODE
3459
3459
3460
- CHECK_EQ (initialised_, false );
3460
+ CHECK_EQ (ctx_, nullptr );
3461
3461
const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
3462
3462
if (cipher == nullptr ) {
3463
3463
return env ()->ThrowError (" Unknown cipher" );
@@ -3475,29 +3475,28 @@ void CipherBase::Init(const char* cipher_type,
3475
3475
key,
3476
3476
iv);
3477
3477
3478
- EVP_CIPHER_CTX_init (& ctx_);
3478
+ ctx_ = EVP_CIPHER_CTX_new ( );
3479
3479
const bool encrypt = (kind_ == kCipher );
3480
- EVP_CipherInit_ex (& ctx_, cipher, nullptr , nullptr , nullptr , encrypt);
3480
+ EVP_CipherInit_ex (ctx_, cipher, nullptr , nullptr , nullptr , encrypt);
3481
3481
3482
- int mode = EVP_CIPHER_CTX_mode (& ctx_);
3482
+ int mode = EVP_CIPHER_CTX_mode (ctx_);
3483
3483
if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
3484
3484
mode == EVP_CIPH_CCM_MODE)) {
3485
3485
ProcessEmitWarning (env (), " Use Cipheriv for counter mode of %s" ,
3486
3486
cipher_type);
3487
3487
}
3488
3488
3489
3489
if (mode == EVP_CIPH_WRAP_MODE)
3490
- EVP_CIPHER_CTX_set_flags (& ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3490
+ EVP_CIPHER_CTX_set_flags (ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3491
3491
3492
- CHECK_EQ (1 , EVP_CIPHER_CTX_set_key_length (& ctx_, key_len));
3492
+ CHECK_EQ (1 , EVP_CIPHER_CTX_set_key_length (ctx_, key_len));
3493
3493
3494
- EVP_CipherInit_ex (& ctx_,
3494
+ EVP_CipherInit_ex (ctx_,
3495
3495
nullptr ,
3496
3496
nullptr ,
3497
3497
reinterpret_cast <unsigned char *>(key),
3498
3498
reinterpret_cast <unsigned char *>(iv),
3499
3499
kind_ == kCipher );
3500
- initialised_ = true ;
3501
3500
}
3502
3501
3503
3502
@@ -3540,32 +3539,33 @@ void CipherBase::InitIv(const char* cipher_type,
3540
3539
return env ()->ThrowError (" Invalid IV length" );
3541
3540
}
3542
3541
3543
- EVP_CIPHER_CTX_init (& ctx_);
3542
+ ctx_ = EVP_CIPHER_CTX_new ( );
3544
3543
3545
3544
if (mode == EVP_CIPH_WRAP_MODE)
3546
- EVP_CIPHER_CTX_set_flags (& ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3545
+ EVP_CIPHER_CTX_set_flags (ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3547
3546
3548
3547
const bool encrypt = (kind_ == kCipher );
3549
- EVP_CipherInit_ex (& ctx_, cipher, nullptr , nullptr , nullptr , encrypt);
3548
+ EVP_CipherInit_ex (ctx_, cipher, nullptr , nullptr , nullptr , encrypt);
3550
3549
3551
3550
if (is_gcm_mode &&
3552
- !EVP_CIPHER_CTX_ctrl (&ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr )) {
3553
- EVP_CIPHER_CTX_cleanup (&ctx_);
3551
+ !EVP_CIPHER_CTX_ctrl (ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr )) {
3552
+ EVP_CIPHER_CTX_free (ctx_);
3553
+ ctx_ = nullptr ;
3554
3554
return env ()->ThrowError (" Invalid IV length" );
3555
3555
}
3556
3556
3557
- if (!EVP_CIPHER_CTX_set_key_length (&ctx_, key_len)) {
3558
- EVP_CIPHER_CTX_cleanup (&ctx_);
3557
+ if (!EVP_CIPHER_CTX_set_key_length (ctx_, key_len)) {
3558
+ EVP_CIPHER_CTX_free (ctx_);
3559
+ ctx_ = nullptr ;
3559
3560
return env ()->ThrowError (" Invalid key length" );
3560
3561
}
3561
3562
3562
- EVP_CipherInit_ex (& ctx_,
3563
+ EVP_CipherInit_ex (ctx_,
3563
3564
nullptr ,
3564
3565
nullptr ,
3565
3566
reinterpret_cast <const unsigned char *>(key),
3566
3567
reinterpret_cast <const unsigned char *>(iv),
3567
3568
kind_ == kCipher );
3568
- initialised_ = true ;
3569
3569
}
3570
3570
3571
3571
@@ -3593,8 +3593,8 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
3593
3593
3594
3594
bool CipherBase::IsAuthenticatedMode () const {
3595
3595
// Check if this cipher operates in an AEAD mode that we support.
3596
- CHECK_EQ (initialised_, true );
3597
- const EVP_CIPHER* const cipher = EVP_CIPHER_CTX_cipher (& ctx_);
3596
+ CHECK_NE (ctx_, nullptr );
3597
+ const EVP_CIPHER* const cipher = EVP_CIPHER_CTX_cipher (ctx_);
3598
3598
int mode = EVP_CIPHER_mode (cipher);
3599
3599
return mode == EVP_CIPH_GCM_MODE;
3600
3600
}
@@ -3606,7 +3606,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {
3606
3606
ASSIGN_OR_RETURN_UNWRAP (&cipher, args.Holder ());
3607
3607
3608
3608
// Only callable after Final and if encrypting.
3609
- if (cipher->initialised_ ||
3609
+ if (cipher->ctx_ != nullptr ||
3610
3610
cipher->kind_ != kCipher ||
3611
3611
cipher->auth_tag_len_ == 0 ) {
3612
3612
return env->ThrowError (" Attempting to get auth tag in unsupported state" );
@@ -3627,7 +3627,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
3627
3627
CipherBase* cipher;
3628
3628
ASSIGN_OR_RETURN_UNWRAP (&cipher, args.Holder ());
3629
3629
3630
- if (! cipher->initialised_ ||
3630
+ if (cipher->ctx_ == nullptr ||
3631
3631
!cipher->IsAuthenticatedMode () ||
3632
3632
cipher->kind_ != kDecipher ) {
3633
3633
return env->ThrowError (" Attempting to set auth tag in unsupported state" );
@@ -3653,10 +3653,10 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
3653
3653
3654
3654
3655
3655
bool CipherBase::SetAAD (const char * data, unsigned int len) {
3656
- if (!initialised_ || !IsAuthenticatedMode ())
3656
+ if (ctx_ == nullptr || !IsAuthenticatedMode ())
3657
3657
return false ;
3658
3658
int outlen;
3659
- if (!EVP_CipherUpdate (& ctx_,
3659
+ if (!EVP_CipherUpdate (ctx_,
3660
3660
nullptr ,
3661
3661
&outlen,
3662
3662
reinterpret_cast <const unsigned char *>(data),
@@ -3684,21 +3684,21 @@ bool CipherBase::Update(const char* data,
3684
3684
int len,
3685
3685
unsigned char ** out,
3686
3686
int * out_len) {
3687
- if (!initialised_ )
3687
+ if (ctx_ == nullptr )
3688
3688
return 0 ;
3689
3689
3690
3690
// on first update:
3691
3691
if (kind_ == kDecipher && IsAuthenticatedMode () && auth_tag_len_ > 0 ) {
3692
- EVP_CIPHER_CTX_ctrl (& ctx_,
3692
+ EVP_CIPHER_CTX_ctrl (ctx_,
3693
3693
EVP_CTRL_GCM_SET_TAG,
3694
3694
auth_tag_len_,
3695
3695
reinterpret_cast <unsigned char *>(auth_tag_));
3696
3696
auth_tag_len_ = 0 ;
3697
3697
}
3698
3698
3699
- *out_len = len + EVP_CIPHER_CTX_block_size (& ctx_);
3699
+ *out_len = len + EVP_CIPHER_CTX_block_size (ctx_);
3700
3700
*out = Malloc<unsigned char >(static_cast <size_t >(*out_len));
3701
- return EVP_CipherUpdate (& ctx_,
3701
+ return EVP_CipherUpdate (ctx_,
3702
3702
*out,
3703
3703
out_len,
3704
3704
reinterpret_cast <const unsigned char *>(data),
@@ -3746,9 +3746,9 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3746
3746
3747
3747
3748
3748
bool CipherBase::SetAutoPadding (bool auto_padding) {
3749
- if (!initialised_ )
3749
+ if (ctx_ == nullptr )
3750
3750
return false ;
3751
- return EVP_CIPHER_CTX_set_padding (& ctx_, auto_padding);
3751
+ return EVP_CIPHER_CTX_set_padding (ctx_, auto_padding);
3752
3752
}
3753
3753
3754
3754
@@ -3764,22 +3764,22 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
3764
3764
3765
3765
3766
3766
bool CipherBase::Final (unsigned char ** out, int *out_len) {
3767
- if (!initialised_ )
3767
+ if (ctx_ == nullptr )
3768
3768
return false ;
3769
3769
3770
3770
*out = Malloc<unsigned char >(
3771
- static_cast <size_t >(EVP_CIPHER_CTX_block_size (& ctx_)));
3772
- int r = EVP_CipherFinal_ex (& ctx_, *out, out_len);
3771
+ static_cast <size_t >(EVP_CIPHER_CTX_block_size (ctx_)));
3772
+ int r = EVP_CipherFinal_ex (ctx_, *out, out_len);
3773
3773
3774
3774
if (r == 1 && kind_ == kCipher && IsAuthenticatedMode ()) {
3775
3775
auth_tag_len_ = sizeof (auth_tag_);
3776
- r = EVP_CIPHER_CTX_ctrl (& ctx_, EVP_CTRL_GCM_GET_TAG, auth_tag_len_,
3776
+ r = EVP_CIPHER_CTX_ctrl (ctx_, EVP_CTRL_GCM_GET_TAG, auth_tag_len_,
3777
3777
reinterpret_cast <unsigned char *>(auth_tag_));
3778
3778
CHECK_EQ (r, 1 );
3779
3779
}
3780
3780
3781
- EVP_CIPHER_CTX_cleanup (& ctx_);
3782
- initialised_ = false ;
3781
+ EVP_CIPHER_CTX_free ( ctx_);
3782
+ ctx_ = nullptr ;
3783
3783
3784
3784
return r == 1 ;
3785
3785
}
@@ -3790,7 +3790,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3790
3790
3791
3791
CipherBase* cipher;
3792
3792
ASSIGN_OR_RETURN_UNWRAP (&cipher, args.Holder ());
3793
- if (! cipher->initialised_ ) return env->ThrowError (" Unsupported state" );
3793
+ if (cipher->ctx_ == nullptr ) return env->ThrowError (" Unsupported state" );
3794
3794
3795
3795
unsigned char * out_value = nullptr ;
3796
3796
int out_len = -1 ;
0 commit comments