Skip to content

Commit 8422064

Browse files
tniessentargos
authored andcommittedOct 2, 2024
src: remove redundant AESCipherMode
For each supported variant of AES, we already have OpenSSL's associated NID, so we can simply retrieve the block cipher mode of operation from the NID. PR-URL: #54438 Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Benjamin Gruenbaum <benjamingr@gmail.com>
1 parent 84bf93b commit 8422064

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed
 

‎src/crypto/crypto_aes.cc

+13-15
Original file line numberDiff line numberDiff line change
@@ -476,12 +476,9 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
476476
params->variant =
477477
static_cast<AESKeyVariant>(args[offset].As<Uint32>()->Value());
478478

479-
AESCipherMode cipher_op_mode;
480479
int cipher_nid;
481-
482-
#define V(name, _, mode, nid) \
480+
#define V(name, _, nid) \
483481
case kKeyVariantAES_##name: { \
484-
cipher_op_mode = mode; \
485482
cipher_nid = nid; \
486483
break; \
487484
}
@@ -492,15 +489,22 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
492489
}
493490
#undef V
494491

495-
if (cipher_op_mode != AESCipherMode::KW) {
492+
params->cipher = EVP_get_cipherbynid(cipher_nid);
493+
if (params->cipher == nullptr) {
494+
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
495+
return Nothing<bool>();
496+
}
497+
498+
int cipher_op_mode = EVP_CIPHER_mode(params->cipher);
499+
if (cipher_op_mode != EVP_CIPH_WRAP_MODE) {
496500
if (!ValidateIV(env, mode, args[offset + 1], params)) {
497501
return Nothing<bool>();
498502
}
499-
if (cipher_op_mode == AESCipherMode::CTR) {
503+
if (cipher_op_mode == EVP_CIPH_CTR_MODE) {
500504
if (!ValidateCounter(env, args[offset + 2], params)) {
501505
return Nothing<bool>();
502506
}
503-
} else if (cipher_op_mode == AESCipherMode::GCM) {
507+
} else if (cipher_op_mode == EVP_CIPH_GCM_MODE) {
504508
if (!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
505509
!ValidateAdditionalData(env, mode, args[offset + 3], params)) {
506510
return Nothing<bool>();
@@ -510,12 +514,6 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
510514
UseDefaultIV(params);
511515
}
512516

513-
params->cipher = EVP_get_cipherbynid(cipher_nid);
514-
if (params->cipher == nullptr) {
515-
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
516-
return Nothing<bool>();
517-
}
518-
519517
if (params->iv.size() <
520518
static_cast<size_t>(EVP_CIPHER_iv_length(params->cipher))) {
521519
THROW_ERR_CRYPTO_INVALID_IV(env);
@@ -532,7 +530,7 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(
532530
const AESCipherConfig& params,
533531
const ByteSource& in,
534532
ByteSource* out) {
535-
#define V(name, fn, _, __) \
533+
#define V(name, fn, _) \
536534
case kKeyVariantAES_##name: \
537535
return fn(env, key_data.get(), cipher_mode, params, in, out);
538536
switch (params.variant) {
@@ -546,7 +544,7 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(
546544
void AES::Initialize(Environment* env, Local<Object> target) {
547545
AESCryptoJob::Initialize(env, target);
548546

549-
#define V(name, _, __, ___) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
547+
#define V(name, _, __) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
550548
VARIANTS(V)
551549
#undef V
552550
}

‎src/crypto/crypto_aes.h

+13-20
Original file line numberDiff line numberDiff line change
@@ -15,29 +15,22 @@ constexpr size_t kAesBlockSize = 16;
1515
constexpr unsigned kNoAuthTagLength = static_cast<unsigned>(-1);
1616
constexpr const char* kDefaultWrapIV = "\xa6\xa6\xa6\xa6\xa6\xa6\xa6\xa6";
1717

18-
enum class AESCipherMode {
19-
CTR,
20-
CBC,
21-
GCM,
22-
KW,
23-
};
24-
2518
#define VARIANTS(V) \
26-
V(CTR_128, AES_CTR_Cipher, AESCipherMode::CTR, NID_aes_128_ctr) \
27-
V(CTR_192, AES_CTR_Cipher, AESCipherMode::CTR, NID_aes_192_ctr) \
28-
V(CTR_256, AES_CTR_Cipher, AESCipherMode::CTR, NID_aes_256_ctr) \
29-
V(CBC_128, AES_Cipher, AESCipherMode::CBC, NID_aes_128_cbc) \
30-
V(CBC_192, AES_Cipher, AESCipherMode::CBC, NID_aes_192_cbc) \
31-
V(CBC_256, AES_Cipher, AESCipherMode::CBC, NID_aes_256_cbc) \
32-
V(GCM_128, AES_Cipher, AESCipherMode::GCM, NID_aes_128_gcm) \
33-
V(GCM_192, AES_Cipher, AESCipherMode::GCM, NID_aes_192_gcm) \
34-
V(GCM_256, AES_Cipher, AESCipherMode::GCM, NID_aes_256_gcm) \
35-
V(KW_128, AES_Cipher, AESCipherMode::KW, NID_id_aes128_wrap) \
36-
V(KW_192, AES_Cipher, AESCipherMode::KW, NID_id_aes192_wrap) \
37-
V(KW_256, AES_Cipher, AESCipherMode::KW, NID_id_aes256_wrap)
19+
V(CTR_128, AES_CTR_Cipher, NID_aes_128_ctr) \
20+
V(CTR_192, AES_CTR_Cipher, NID_aes_192_ctr) \
21+
V(CTR_256, AES_CTR_Cipher, NID_aes_256_ctr) \
22+
V(CBC_128, AES_Cipher, NID_aes_128_cbc) \
23+
V(CBC_192, AES_Cipher, NID_aes_192_cbc) \
24+
V(CBC_256, AES_Cipher, NID_aes_256_cbc) \
25+
V(GCM_128, AES_Cipher, NID_aes_128_gcm) \
26+
V(GCM_192, AES_Cipher, NID_aes_192_gcm) \
27+
V(GCM_256, AES_Cipher, NID_aes_256_gcm) \
28+
V(KW_128, AES_Cipher, NID_id_aes128_wrap) \
29+
V(KW_192, AES_Cipher, NID_id_aes192_wrap) \
30+
V(KW_256, AES_Cipher, NID_id_aes256_wrap)
3831

3932
enum AESKeyVariant {
40-
#define V(name, _, __, ___) kKeyVariantAES_##name,
33+
#define V(name, _, __) kKeyVariantAES_##name,
4134
VARIANTS(V)
4235
#undef V
4336
};

0 commit comments

Comments
 (0)
Please sign in to comment.