Skip to content

Commit 0109f9c

Browse files
tniessenmarco-ippolito
authored andcommitted
src: simplify AESCipherTraits::AdditionalConfig
Instead of a giant switch statement and a lot of duplicate code, add the NID and the block cipher mode of operation to the VARIANTS list and use those fields to perform configuration appropriately. PR-URL: #53890 Reviewed-By: Yagiz Nizipli <[email protected]>
1 parent 43ede1a commit 0109f9c

File tree

2 files changed

+48
-86
lines changed

2 files changed

+48
-86
lines changed

src/crypto/crypto_aes.cc

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

479+
AESCipherMode cipher_op_mode;
479480
int cipher_nid;
480481

482+
#define V(name, _, mode, nid) \
483+
case kKeyVariantAES_##name: { \
484+
cipher_op_mode = mode; \
485+
cipher_nid = nid; \
486+
break; \
487+
}
481488
switch (params->variant) {
482-
case kKeyVariantAES_CTR_128:
483-
if (!ValidateIV(env, mode, args[offset + 1], params) ||
484-
!ValidateCounter(env, args[offset + 2], params)) {
485-
return Nothing<bool>();
486-
}
487-
cipher_nid = NID_aes_128_ctr;
488-
break;
489-
case kKeyVariantAES_CTR_192:
490-
if (!ValidateIV(env, mode, args[offset + 1], params) ||
491-
!ValidateCounter(env, args[offset + 2], params)) {
492-
return Nothing<bool>();
493-
}
494-
cipher_nid = NID_aes_192_ctr;
495-
break;
496-
case kKeyVariantAES_CTR_256:
497-
if (!ValidateIV(env, mode, args[offset + 1], params) ||
498-
!ValidateCounter(env, args[offset + 2], params)) {
499-
return Nothing<bool>();
500-
}
501-
cipher_nid = NID_aes_256_ctr;
502-
break;
503-
case kKeyVariantAES_CBC_128:
504-
if (!ValidateIV(env, mode, args[offset + 1], params))
505-
return Nothing<bool>();
506-
cipher_nid = NID_aes_128_cbc;
507-
break;
508-
case kKeyVariantAES_CBC_192:
509-
if (!ValidateIV(env, mode, args[offset + 1], params))
510-
return Nothing<bool>();
511-
cipher_nid = NID_aes_192_cbc;
512-
break;
513-
case kKeyVariantAES_CBC_256:
514-
if (!ValidateIV(env, mode, args[offset + 1], params))
515-
return Nothing<bool>();
516-
cipher_nid = NID_aes_256_cbc;
517-
break;
518-
case kKeyVariantAES_KW_128:
519-
UseDefaultIV(params);
520-
cipher_nid = NID_id_aes128_wrap;
521-
break;
522-
case kKeyVariantAES_KW_192:
523-
UseDefaultIV(params);
524-
cipher_nid = NID_id_aes192_wrap;
525-
break;
526-
case kKeyVariantAES_KW_256:
527-
UseDefaultIV(params);
528-
cipher_nid = NID_id_aes256_wrap;
529-
break;
530-
case kKeyVariantAES_GCM_128:
531-
if (!ValidateIV(env, mode, args[offset + 1], params) ||
532-
!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
533-
!ValidateAdditionalData(env, mode, args[offset + 3], params)) {
534-
return Nothing<bool>();
535-
}
536-
cipher_nid = NID_aes_128_gcm;
537-
break;
538-
case kKeyVariantAES_GCM_192:
539-
if (!ValidateIV(env, mode, args[offset + 1], params) ||
540-
!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
541-
!ValidateAdditionalData(env, mode, args[offset + 3], params)) {
489+
VARIANTS(V)
490+
default:
491+
UNREACHABLE();
492+
}
493+
#undef V
494+
495+
if (cipher_op_mode != AESCipherMode::KW) {
496+
if (!ValidateIV(env, mode, args[offset + 1], params)) {
497+
return Nothing<bool>();
498+
}
499+
if (cipher_op_mode == AESCipherMode::CTR) {
500+
if (!ValidateCounter(env, args[offset + 2], params)) {
542501
return Nothing<bool>();
543502
}
544-
cipher_nid = NID_aes_192_gcm;
545-
break;
546-
case kKeyVariantAES_GCM_256:
547-
if (!ValidateIV(env, mode, args[offset + 1], params) ||
548-
!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
503+
} else if (cipher_op_mode == AESCipherMode::GCM) {
504+
if (!ValidateAuthTag(env, mode, cipher_mode, args[offset + 2], params) ||
549505
!ValidateAdditionalData(env, mode, args[offset + 3], params)) {
550506
return Nothing<bool>();
551507
}
552-
cipher_nid = NID_aes_256_gcm;
553-
break;
554-
default:
555-
UNREACHABLE();
508+
}
509+
} else {
510+
UseDefaultIV(params);
556511
}
557512

558513
params->cipher = EVP_get_cipherbynid(cipher_nid);
@@ -577,8 +532,8 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(
577532
const AESCipherConfig& params,
578533
const ByteSource& in,
579534
ByteSource* out) {
580-
#define V(name, fn) \
581-
case kKeyVariantAES_ ## name: \
535+
#define V(name, fn, _, __) \
536+
case kKeyVariantAES_##name: \
582537
return fn(env, key_data.get(), cipher_mode, params, in, out);
583538
switch (params.variant) {
584539
VARIANTS(V)
@@ -591,7 +546,7 @@ WebCryptoCipherStatus AESCipherTraits::DoCipher(
591546
void AES::Initialize(Environment* env, Local<Object> target) {
592547
AESCryptoJob::Initialize(env, target);
593548

594-
#define V(name, _) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_ ## name);
549+
#define V(name, _, __, ___) NODE_DEFINE_CONSTANT(target, kKeyVariantAES_##name);
595550
VARIANTS(V)
596551
#undef V
597552
}

src/crypto/crypto_aes.h

+21-14
Original file line numberDiff line numberDiff line change
@@ -15,22 +15,29 @@ 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-
#define VARIANTS(V) \
19-
V(CTR_128, AES_CTR_Cipher) \
20-
V(CTR_192, AES_CTR_Cipher) \
21-
V(CTR_256, AES_CTR_Cipher) \
22-
V(CBC_128, AES_Cipher) \
23-
V(CBC_192, AES_Cipher) \
24-
V(CBC_256, AES_Cipher) \
25-
V(GCM_128, AES_Cipher) \
26-
V(GCM_192, AES_Cipher) \
27-
V(GCM_256, AES_Cipher) \
28-
V(KW_128, AES_Cipher) \
29-
V(KW_192, AES_Cipher) \
30-
V(KW_256, AES_Cipher)
18+
enum class AESCipherMode {
19+
CTR,
20+
CBC,
21+
GCM,
22+
KW,
23+
};
24+
25+
#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)
3138

3239
enum AESKeyVariant {
33-
#define V(name, _) kKeyVariantAES_ ## name,
40+
#define V(name, _, __, ___) kKeyVariantAES_##name,
3441
VARIANTS(V)
3542
#undef V
3643
};

0 commit comments

Comments
 (0)