Skip to content

Commit 74ea78d

Browse files
authored
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 <[email protected]> Reviewed-By: Benjamin Gruenbaum <[email protected]>
1 parent c00ea01 commit 74ea78d

File tree

2 files changed

+26
-35
lines changed

2 files changed

+26
-35
lines changed

src/crypto/crypto_aes.cc

Lines changed: 13 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -471,12 +471,9 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
471471
params->variant =
472472
static_cast<AESKeyVariant>(args[offset].As<Uint32>()->Value());
473473

474-
AESCipherMode cipher_op_mode;
475474
int cipher_nid;
476-
477-
#define V(name, _, mode, nid) \
475+
#define V(name, _, nid) \
478476
case kKeyVariantAES_##name: { \
479-
cipher_op_mode = mode; \
480477
cipher_nid = nid; \
481478
break; \
482479
}
@@ -487,15 +484,22 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
487484
}
488485
#undef V
489486

490-
if (cipher_op_mode != AESCipherMode::KW) {
487+
params->cipher = EVP_get_cipherbynid(cipher_nid);
488+
if (params->cipher == nullptr) {
489+
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
490+
return Nothing<bool>();
491+
}
492+
493+
int cipher_op_mode = EVP_CIPHER_mode(params->cipher);
494+
if (cipher_op_mode != EVP_CIPH_WRAP_MODE) {
491495
if (!ValidateIV(env, mode, args[offset + 1], params)) {
492496
return Nothing<bool>();
493497
}
494-
if (cipher_op_mode == AESCipherMode::CTR) {
498+
if (cipher_op_mode == EVP_CIPH_CTR_MODE) {
495499
if (!ValidateCounter(env, args[offset + 2], params)) {
496500
return Nothing<bool>();
497501
}
498-
} else if (cipher_op_mode == AESCipherMode::GCM) {
502+
} else if (cipher_op_mode == EVP_CIPH_GCM_MODE) {
499503
if (!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
500504
!ValidateAdditionalData(env, mode, args[offset + 3], params)) {
501505
return Nothing<bool>();
@@ -505,12 +509,6 @@ Maybe<bool> AESCipherTraits::AdditionalConfig(
505509
UseDefaultIV(params);
506510
}
507511

508-
params->cipher = EVP_get_cipherbynid(cipher_nid);
509-
if (params->cipher == nullptr) {
510-
THROW_ERR_CRYPTO_UNKNOWN_CIPHER(env);
511-
return Nothing<bool>();
512-
}
513-
514512
if (params->iv.size() <
515513
static_cast<size_t>(EVP_CIPHER_iv_length(params->cipher))) {
516514
THROW_ERR_CRYPTO_INVALID_IV(env);
@@ -527,7 +525,7 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(
527525
const AESCipherConfig& params,
528526
const ByteSource& in,
529527
ByteSource* out) {
530-
#define V(name, fn, _, __) \
528+
#define V(name, fn, _) \
531529
case kKeyVariantAES_##name: \
532530
return fn(env, key_data.get(), cipher_mode, params, in, out);
533531
switch (params.variant) {
@@ -541,7 +539,7 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(
541539
void AES::Initialize(Environment* env, Local<Object> target) {
542540
AESCryptoJob::Initialize(env, target);
543541

544-
#define V(name, _, __, ___) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
542+
#define V(name, _, __) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
545543
VARIANTS(V)
546544
#undef V
547545
}

src/crypto/crypto_aes.h

Lines changed: 13 additions & 20 deletions
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)