Skip to content

Commit 9981220

Browse files
tniessentargos
authored andcommitted
crypto: fix behavior of createCipher in wrap mode
The old implementation silently failed in EVP_CipherInit_ex in EVP_CIPH_WRAP_MODE, this commit should fix that. PR-URL: #21287 Reviewed-By: Ujjwal Sharma <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9353093 commit 9981220

File tree

1 file changed

+5
-4
lines changed

1 file changed

+5
-4
lines changed

src/node_crypto.cc

+5-4
Original file line numberDiff line numberDiff line change
@@ -2611,10 +2611,14 @@ void CipherBase::Init(const char* cipher_type,
26112611
iv);
26122612

26132613
ctx_.reset(EVP_CIPHER_CTX_new());
2614+
2615+
const int mode = EVP_CIPHER_mode(cipher);
2616+
if (mode == EVP_CIPH_WRAP_MODE)
2617+
EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2618+
26142619
const bool encrypt = (kind_ == kCipher);
26152620
EVP_CipherInit_ex(ctx_.get(), cipher, nullptr, nullptr, nullptr, encrypt);
26162621

2617-
int mode = EVP_CIPHER_CTX_mode(ctx_.get());
26182622
if (encrypt && (mode == EVP_CIPH_CTR_MODE || mode == EVP_CIPH_GCM_MODE ||
26192623
mode == EVP_CIPH_CCM_MODE)) {
26202624
// Ignore the return value (i.e. possible exception) because we are
@@ -2624,9 +2628,6 @@ void CipherBase::Init(const char* cipher_type,
26242628
cipher_type);
26252629
}
26262630

2627-
if (mode == EVP_CIPH_WRAP_MODE)
2628-
EVP_CIPHER_CTX_set_flags(ctx_.get(), EVP_CIPHER_CTX_FLAG_WRAP_ALLOW);
2629-
26302631
if (IsAuthenticatedMode()) {
26312632
if (!InitAuthenticated(cipher_type, EVP_CIPHER_iv_length(cipher),
26322633
auth_tag_len))

0 commit comments

Comments
 (0)