@@ -3332,16 +3332,16 @@ void CipherBase::Init(const char* cipher_type,
3332
3332
}
3333
3333
#endif // NODE_FIPS_MODE
3334
3334
3335
- CHECK_EQ (cipher_, nullptr );
3336
- cipher_ = EVP_get_cipherbyname (cipher_type);
3337
- if (cipher_ == nullptr ) {
3335
+ CHECK_EQ (initialised_, false );
3336
+ const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
3337
+ if (cipher == nullptr ) {
3338
3338
return env ()->ThrowError (" Unknown cipher" );
3339
3339
}
3340
3340
3341
3341
unsigned char key[EVP_MAX_KEY_LENGTH];
3342
3342
unsigned char iv[EVP_MAX_IV_LENGTH];
3343
3343
3344
- int key_len = EVP_BytesToKey (cipher_ ,
3344
+ int key_len = EVP_BytesToKey (cipher ,
3345
3345
EVP_md5 (),
3346
3346
nullptr ,
3347
3347
reinterpret_cast <const unsigned char *>(key_buf),
@@ -3352,7 +3352,7 @@ void CipherBase::Init(const char* cipher_type,
3352
3352
3353
3353
EVP_CIPHER_CTX_init (&ctx_);
3354
3354
const bool encrypt = (kind_ == kCipher );
3355
- EVP_CipherInit_ex (&ctx_, cipher_ , nullptr , nullptr , nullptr , encrypt );
3355
+ EVP_CipherInit_ex (&ctx_, cipher , nullptr , nullptr , nullptr , encrypt );
3356
3356
if (!EVP_CIPHER_CTX_set_key_length (&ctx_, key_len)) {
3357
3357
EVP_CIPHER_CTX_cleanup (&ctx_);
3358
3358
return env ()->ThrowError (" Invalid key length" );
@@ -3394,21 +3394,21 @@ void CipherBase::InitIv(const char* cipher_type,
3394
3394
int iv_len) {
3395
3395
HandleScope scope (env ()->isolate ());
3396
3396
3397
- cipher_ = EVP_get_cipherbyname (cipher_type);
3398
- if (cipher_ == nullptr ) {
3397
+ const EVP_CIPHER* const cipher = EVP_get_cipherbyname (cipher_type);
3398
+ if (cipher == nullptr ) {
3399
3399
return env ()->ThrowError (" Unknown cipher" );
3400
3400
}
3401
3401
3402
- const int expected_iv_len = EVP_CIPHER_iv_length (cipher_ );
3403
- const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == EVP_CIPHER_mode (cipher_ ));
3402
+ const int expected_iv_len = EVP_CIPHER_iv_length (cipher );
3403
+ const bool is_gcm_mode = (EVP_CIPH_GCM_MODE == EVP_CIPHER_mode (cipher ));
3404
3404
3405
3405
if (is_gcm_mode == false && iv_len != expected_iv_len) {
3406
3406
return env ()->ThrowError (" Invalid IV length" );
3407
3407
}
3408
3408
3409
3409
EVP_CIPHER_CTX_init (&ctx_);
3410
3410
const bool encrypt = (kind_ == kCipher );
3411
- EVP_CipherInit_ex (&ctx_, cipher_ , nullptr , nullptr , nullptr , encrypt );
3411
+ EVP_CipherInit_ex (&ctx_, cipher , nullptr , nullptr , nullptr , encrypt );
3412
3412
3413
3413
if (is_gcm_mode &&
3414
3414
!EVP_CIPHER_CTX_ctrl (&ctx_, EVP_CTRL_GCM_SET_IVLEN, iv_len, nullptr )) {
@@ -3454,10 +3454,10 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
3454
3454
3455
3455
3456
3456
bool CipherBase::IsAuthenticatedMode () const {
3457
- // check if this cipher operates in an AEAD mode that we support.
3458
- if (!cipher_)
3459
- return false ;
3460
- int mode = EVP_CIPHER_mode (cipher_ );
3457
+ // Check if this cipher operates in an AEAD mode that we support.
3458
+ CHECK_EQ (initialised_, true );
3459
+ const EVP_CIPHER* const cipher = EVP_CIPHER_CTX_cipher (&ctx_) ;
3460
+ int mode = EVP_CIPHER_mode (cipher );
3461
3461
return mode == EVP_CIPH_GCM_MODE;
3462
3462
}
3463
3463
@@ -3644,19 +3644,22 @@ void CipherBase::Final(const FunctionCallbackInfo<Value>& args) {
3644
3644
3645
3645
CipherBase* cipher;
3646
3646
ASSIGN_OR_RETURN_UNWRAP (&cipher, args.Holder ());
3647
+ if (!cipher->initialised_ ) return env->ThrowError (" Unsupported state" );
3647
3648
3648
3649
unsigned char * out_value = nullptr ;
3649
3650
int out_len = -1 ;
3650
3651
Local<Value> outString;
3651
3652
3653
+ // Check IsAuthenticatedMode() first, Final() destroys the EVP_CIPHER_CTX.
3654
+ const bool is_auth_mode = cipher->IsAuthenticatedMode ();
3652
3655
bool r = cipher->Final (&out_value, &out_len);
3653
3656
3654
3657
if (out_len <= 0 || !r) {
3655
3658
free (out_value);
3656
3659
out_value = nullptr ;
3657
3660
out_len = 0 ;
3658
3661
if (!r) {
3659
- const char * msg = cipher-> IsAuthenticatedMode () ?
3662
+ const char * msg = is_auth_mode ?
3660
3663
" Unsupported state or unable to authenticate data" :
3661
3664
" Unsupported state" ;
3662
3665
0 commit comments