@@ -3461,7 +3461,7 @@ void CipherBase::Init(const char* cipher_type,
3461
3461
}
3462
3462
#endif // NODE_FIPS_MODE
3463
3463
3464
- CHECK_EQ(initialised_, false );
3464
+ CHECK_EQ(ctx_, nullptr );
3465
3465
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
3466
3466
if (cipher == nullptr) {
3467
3467
return env()->ThrowError("Unknown cipher");
@@ -3479,29 +3479,28 @@ void CipherBase::Init(const char* cipher_type,
3479
3479
key,
3480
3480
iv);
3481
3481
3482
- EVP_CIPHER_CTX_init(& ctx_);
3482
+ ctx_ = EVP_CIPHER_CTX_new( );
3483
3483
const bool encrypt = (kind_ == kCipher);
3484
- EVP_CipherInit_ex(& ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
3484
+ EVP_CipherInit_ex(ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
3485
3485
3486
- int mode = EVP_CIPHER_CTX_mode(& ctx_);
3486
+ int mode = EVP_CIPHER_CTX_mode(ctx_);
3487
3487
if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
3488
3488
mode == EVP_CIPH_CCM_MODE)) {
3489
3489
ProcessEmitWarning(env(), "Use Cipheriv for counter mode of %s",
3490
3490
cipher_type);
3491
3491
}
3492
3492
3493
3493
if (mode == EVP_CIPH_WRAP_MODE)
3494
- EVP_CIPHER_CTX_set_flags(& ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3494
+ EVP_CIPHER_CTX_set_flags(ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3495
3495
3496
- CHECK_EQ(1, EVP_CIPHER_CTX_set_key_length(& ctx_, key_len));
3496
+ CHECK_EQ(1, EVP_CIPHER_CTX_set_key_length(ctx_, key_len));
3497
3497
3498
- EVP_CipherInit_ex(& ctx_,
3498
+ EVP_CipherInit_ex(ctx_,
3499
3499
nullptr,
3500
3500
nullptr,
3501
3501
reinterpret_cast<unsigned char*>(key),
3502
3502
reinterpret_cast<unsigned char*>(iv),
3503
3503
kind_ == kCipher);
3504
- initialised_ = true;
3505
3504
}
3506
3505
3507
3506
@@ -3544,32 +3543,33 @@ void CipherBase::InitIv(const char* cipher_type,
3544
3543
return env()->ThrowError("Invalid IV length");
3545
3544
}
3546
3545
3547
- EVP_CIPHER_CTX_init(& ctx_);
3546
+ ctx_ = EVP_CIPHER_CTX_new( );
3548
3547
3549
3548
if (mode == EVP_CIPH_WRAP_MODE)
3550
- EVP_CIPHER_CTX_set_flags(& ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3549
+ EVP_CIPHER_CTX_set_flags(ctx_, EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
3551
3550
3552
3551
const bool encrypt = (kind_ == kCipher);
3553
- EVP_CipherInit_ex(& ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
3552
+ EVP_CipherInit_ex(ctx_, cipher, nullptr, nullptr, nullptr, encrypt);
3554
3553
3555
3554
if (is_gcm_mode &&
3556
- !EVP_CIPHER_CTX_ctrl(&ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) {
3557
- EVP_CIPHER_CTX_cleanup(&ctx_);
3555
+ !EVP_CIPHER_CTX_ctrl(ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr)) {
3556
+ EVP_CIPHER_CTX_free(ctx_);
3557
+ ctx_ = nullptr;
3558
3558
return env()->ThrowError("Invalid IV length");
3559
3559
}
3560
3560
3561
- if (!EVP_CIPHER_CTX_set_key_length(&ctx_, key_len)) {
3562
- EVP_CIPHER_CTX_cleanup(&ctx_);
3561
+ if (!EVP_CIPHER_CTX_set_key_length(ctx_, key_len)) {
3562
+ EVP_CIPHER_CTX_free(ctx_);
3563
+ ctx_ = nullptr;
3563
3564
return env()->ThrowError("Invalid key length");
3564
3565
}
3565
3566
3566
- EVP_CipherInit_ex(& ctx_,
3567
+ EVP_CipherInit_ex(ctx_,
3567
3568
nullptr,
3568
3569
nullptr,
3569
3570
reinterpret_cast<const unsigned char*>(key),
3570
3571
reinterpret_cast<const unsigned char*>(iv),
3571
3572
kind_ == kCipher);
3572
- initialised_ = true;
3573
3573
}
3574
3574
3575
3575
@@ -3597,8 +3597,8 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
3597
3597
3598
3598
bool CipherBase::IsAuthenticatedMode() const {
3599
3599
// Check if this cipher operates in an AEAD mode that we support.
3600
- CHECK_EQ(initialised_, true );
3601
- const EVP_CIPHER* const cipher = EVP_CIPHER_CTX_cipher(& ctx_);
3600
+ CHECK_NE(ctx_, nullptr );
3601
+ const EVP_CIPHER* const cipher = EVP_CIPHER_CTX_cipher(ctx_);
3602
3602
int mode = EVP_CIPHER_mode(cipher);
3603
3603
return mode == EVP_CIPH_GCM_MODE;
3604
3604
}
@@ -3610,7 +3610,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) {
3610
3610
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
3611
3611
3612
3612
// Only callable after Final and if encrypting.
3613
- if (cipher->initialised_ ||
3613
+ if (cipher->ctx_ != nullptr ||
3614
3614
cipher->kind_ != kCipher ||
3615
3615
cipher->auth_tag_len_ == 0) {
3616
3616
return env->ThrowError("Attempting to get auth tag in unsupported state");
@@ -3631,7 +3631,7 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
3631
3631
CipherBase* cipher;
3632
3632
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
3633
3633
3634
- if (! cipher->initialised_ ||
3634
+ if (cipher->ctx_ == nullptr ||
3635
3635
!cipher->IsAuthenticatedMode() ||
3636
3636
cipher->kind_ != kDecipher) {
3637
3637
return env->ThrowError("Attempting to set auth tag in unsupported state");
@@ -3649,10 +3649,10 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) {
3649
3649
3650
3650
3651
3651
bool CipherBase::SetAAD(const char* data, unsigned int len) {
3652
- if (!initialised_ || !IsAuthenticatedMode())
3652
+ if (ctx_ == nullptr || !IsAuthenticatedMode())
3653
3653
return false;
3654
3654
int outlen;
3655
- if (!EVP_CipherUpdate(& ctx_,
3655
+ if (!EVP_CipherUpdate(ctx_,
3656
3656
nullptr,
3657
3657
&outlen,
3658
3658
reinterpret_cast<const unsigned char*>(data),
@@ -3680,21 +3680,21 @@ bool CipherBase::Update(const char* data,
3680
3680
int len,
3681
3681
unsigned char** out,
3682
3682
int* out_len) {
3683
- if (!initialised_ )
3683
+ if (ctx_ == nullptr )
3684
3684
return 0;
3685
3685
3686
3686
// on first update:
3687
3687
if (kind_ == kDecipher && IsAuthenticatedMode() && auth_tag_len_ > 0) {
3688
- EVP_CIPHER_CTX_ctrl(& ctx_,
3688
+ EVP_CIPHER_CTX_ctrl(ctx_,
3689
3689
EVP_CTRL_GCM_SET_TAG,
3690
3690
auth_tag_len_,
3691
3691
reinterpret_cast<unsigned char*>(auth_tag_));
3692
3692
auth_tag_len_ = 0;
3693
3693
}
3694
3694
3695
- *out_len = len + EVP_CIPHER_CTX_block_size(& ctx_);
3695
+ *out_len = len + EVP_CIPHER_CTX_block_size(ctx_);
3696
3696
*out = Malloc<unsigned char>(static_cast<size_t>(*out_len));
3697
- return EVP_CipherUpdate(& ctx_,
3697
+ return EVP_CipherUpdate(ctx_,
3698
3698
*out,
3699
3699
out_len,
3700
3700
reinterpret_cast<const unsigned char*>(data),
@@ -3742,9 +3742,9 @@ void CipherBase::Update(const FunctionCallbackInfo<Value>& args) {
3742
3742
3743
3743
3744
3744
bool CipherBase::SetAutoPadding(bool auto_padding) {
3745
- if (!initialised_ )
3745
+ if (ctx_ == nullptr )
3746
3746
return false;
3747
- return EVP_CIPHER_CTX_set_padding(& ctx_, auto_padding);
3747
+ return EVP_CIPHER_CTX_set_padding(ctx_, auto_padding);
3748
3748
}
3749
3749
3750
3750
@@ -3760,22 +3760,22 @@ void CipherBase::SetAutoPadding(const FunctionCallbackInfo<Value>& args) {
3760
3760
3761
3761
3762
3762
bool CipherBase::Final(unsigned char** out, int *out_len) {
3763
- if (!initialised_ )
3763
+ if (ctx_ == nullptr )
3764
3764
return false;
3765
3765
3766
3766
*out = Malloc<unsigned char>(
3767
- static_cast<size_t>(EVP_CIPHER_CTX_block_size(& ctx_)));
3768
- int r = EVP_CipherFinal_ex(& ctx_, *out, out_len);
3767
+ static_cast<size_t>(EVP_CIPHER_CTX_block_size(ctx_)));
3768
+ int r = EVP_CipherFinal_ex(ctx_, *out, out_len);
3769
3769
3770
3770
if (r == 1 && kind_ == kCipher && IsAuthenticatedMode()) {
3771
3771
auth_tag_len_ = sizeof(auth_tag_);
3772
- r = EVP_CIPHER_CTX_ctrl(& ctx_, EVP_CTRL_GCM_GET_TAG, auth_tag_len_,
3772
+ r = EVP_CIPHER_CTX_ctrl(ctx_, EVP_CTRL_GCM_GET_TAG, auth_tag_len_,
3773
3773
reinterpret_cast<unsigned char*>(auth_tag_));
3774
3774
CHECK_EQ(r, 1);
3775
3775
}
3776
3776
3777
- EVP_CIPHER_CTX_cleanup(& ctx_);
3778
- initialised_ = false ;
3777
+ EVP_CIPHER_CTX_free( ctx_);
3778
+ ctx_ = nullptr ;
3779
3779
3780
3780
return r == 1;
3781
3781
}
@@ -3786,7 +3786,7 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3786
3786
3787
3787
CipherBase* cipher;
3788
3788
ASSIGN_OR_RETURN_UNWRAP(&cipher, args.Holder());
3789
- if (! cipher->initialised_ ) return env->ThrowError("Unsupported state");
3789
+ if (cipher->ctx_ == nullptr ) return env->ThrowError("Unsupported state");
3790
3790
3791
3791
unsigned char* out_value = nullptr;
3792
3792
int out_len = -1;
0 commit comments