Skip to content

Commit 7c39722

Browse files
committed
crypto: enable SIV and GCM-SIV modes in Cipher/Decipher APIs
Closes nodejs#63393 Signed-off-by: Filip Skokan <[email protected]>
1 parent af100d1 commit 7c39722

8 files changed

Lines changed: 422 additions & 65 deletions

File tree

‎deps/ncrypto/ncrypto.cc‎

Lines changed: 131 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3176,12 +3176,47 @@ bool SSLCtxPointer::setCipherSuites(const char* ciphers) {
31763176

31773177
// ============================================================================
31783178

3179+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
3180+
Cipher::Cipher(EVP_CIPHER* cipher)
3181+
: cipher_(cipher), fetched_cipher_(cipher, EVP_CIPHER_free) {}
3182+
#endif
3183+
31793184
const Cipher Cipher::FromName(const char* name) {
3180-
return Cipher(EVP_get_cipherbyname(name));
3185+
const EVP_CIPHER* cipher = EVP_get_cipherbyname(name);
3186+
if (cipher != nullptr) return Cipher(cipher);
3187+
3188+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
3189+
EVP_CIPHER* fetched = EVP_CIPHER_fetch(nullptr, name, nullptr);
3190+
if (fetched == nullptr) return Cipher();
3191+
3192+
const int mode = EVP_CIPHER_mode(fetched);
3193+
const bool is_siv_mode =
3194+
#if OPENSSL_WITH_AES_SIV
3195+
mode == EVP_CIPH_SIV_MODE ||
3196+
#endif
3197+
#if OPENSSL_WITH_AES_GCM_SIV
3198+
mode == EVP_CIPH_GCM_SIV_MODE ||
3199+
#endif
3200+
false;
3201+
if (is_siv_mode) return Cipher(fetched);
3202+
3203+
EVP_CIPHER_free(fetched);
3204+
return Cipher();
3205+
#else
3206+
return Cipher();
3207+
#endif
31813208
}
31823209

31833210
const Cipher Cipher::FromNid(int nid) {
3184-
return Cipher(EVP_get_cipherbynid(nid));
3211+
const EVP_CIPHER* cipher = EVP_get_cipherbynid(nid);
3212+
if (cipher != nullptr) return Cipher(cipher);
3213+
3214+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
3215+
const char* name = OBJ_nid2sn(nid);
3216+
if (name != nullptr) return FromName(name);
3217+
#endif
3218+
3219+
return Cipher();
31853220
}
31863221

31873222
const Cipher Cipher::FromCtx(const CipherCtxPointer& ctx) {
@@ -3235,6 +3270,24 @@ bool Cipher::isOcbMode() const {
32353270
return getMode() == EVP_CIPH_OCB_MODE;
32363271
}
32373272

3273+
bool Cipher::isSivMode() const {
3274+
if (!cipher_) return false;
3275+
#if OPENSSL_WITH_AES_SIV
3276+
return getMode() == EVP_CIPH_SIV_MODE;
3277+
#else
3278+
return false;
3279+
#endif
3280+
}
3281+
3282+
bool Cipher::isGcmSivMode() const {
3283+
if (!cipher_) return false;
3284+
#if OPENSSL_WITH_AES_GCM_SIV
3285+
return getMode() == EVP_CIPH_GCM_SIV_MODE;
3286+
#else
3287+
return false;
3288+
#endif
3289+
}
3290+
32383291
bool Cipher::isStreamMode() const {
32393292
if (!cipher_) return false;
32403293
return getMode() == EVP_CIPH_STREAM_CIPHER;
@@ -3289,6 +3342,14 @@ std::string_view Cipher::getModeLabel() const {
32893342
return "ocb";
32903343
case EVP_CIPH_OFB_MODE:
32913344
return "ofb";
3345+
#if OPENSSL_WITH_AES_SIV
3346+
case EVP_CIPH_SIV_MODE:
3347+
return "siv";
3348+
#endif
3349+
#if OPENSSL_WITH_AES_GCM_SIV
3350+
case EVP_CIPH_GCM_SIV_MODE:
3351+
return "gcm-siv";
3352+
#endif
32923353
case EVP_CIPH_WRAP_MODE:
32933354
return "wrap";
32943355
case EVP_CIPH_XTS_MODE:
@@ -3303,7 +3364,16 @@ const char* Cipher::getName() const {
33033364
if (!cipher_) return {};
33043365
// OBJ_nid2sn(EVP_CIPHER_nid(cipher)) is used here instead of
33053366
// EVP_CIPHER_name(cipher) for compatibility with BoringSSL.
3306-
return OBJ_nid2sn(getNid());
3367+
const int nid = getNid();
3368+
if (nid != NID_undef) {
3369+
const char* name = OBJ_nid2sn(nid);
3370+
if (name != nullptr) return name;
3371+
}
3372+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
3373+
return EVP_CIPHER_get0_name(cipher_);
3374+
#else
3375+
return {};
3376+
#endif
33073377
}
33083378

33093379
bool Cipher::isSupportedAuthenticatedMode() const {
@@ -3312,6 +3382,12 @@ bool Cipher::isSupportedAuthenticatedMode() const {
33123382
case EVP_CIPH_GCM_MODE:
33133383
#ifndef OPENSSL_NO_OCB
33143384
case EVP_CIPH_OCB_MODE:
3385+
#endif
3386+
#if OPENSSL_WITH_AES_SIV
3387+
case EVP_CIPH_SIV_MODE:
3388+
#endif
3389+
#if OPENSSL_WITH_AES_GCM_SIV
3390+
case EVP_CIPH_GCM_SIV_MODE:
33153391
#endif
33163392
return true;
33173393
case EVP_CIPH_STREAM_CIPHER:
@@ -3425,6 +3501,24 @@ bool CipherCtxPointer::isWrapMode() const {
34253501
return getMode() == EVP_CIPH_WRAP_MODE;
34263502
}
34273503

3504+
bool CipherCtxPointer::isSivMode() const {
3505+
if (!ctx_) return false;
3506+
#if OPENSSL_WITH_AES_SIV
3507+
return getMode() == EVP_CIPH_SIV_MODE;
3508+
#else
3509+
return false;
3510+
#endif
3511+
}
3512+
3513+
bool CipherCtxPointer::isGcmSivMode() const {
3514+
if (!ctx_) return false;
3515+
#if OPENSSL_WITH_AES_GCM_SIV
3516+
return getMode() == EVP_CIPH_GCM_SIV_MODE;
3517+
#else
3518+
return false;
3519+
#endif
3520+
}
3521+
34283522
bool CipherCtxPointer::isChaCha20Poly1305() const {
34293523
if (!ctx_) return false;
34303524
return getNid() == NID_chacha20_poly1305;
@@ -4253,6 +4347,22 @@ struct CipherCallbackContext {
42534347
void operator()(const char* name) { cb(name); }
42544348
};
42554349

4350+
#if OPENSSL_WITH_AES_SIV
4351+
constexpr const char* kProviderOnlyAesSivCiphers[] = {
4352+
"aes-128-siv",
4353+
"aes-192-siv",
4354+
"aes-256-siv",
4355+
};
4356+
#endif
4357+
4358+
#if OPENSSL_WITH_AES_GCM_SIV
4359+
constexpr const char* kProviderOnlyAesGcmSivCiphers[] = {
4360+
"aes-128-gcm-siv",
4361+
"aes-192-gcm-siv",
4362+
"aes-256-gcm-siv",
4363+
};
4364+
#endif
4365+
42564366
#if OPENSSL_VERSION_MAJOR >= 3
42574367
template <class TypeName,
42584368
TypeName* fetch_type(OSSL_LIB_CTX*, const char*, const char*),
@@ -4319,6 +4429,24 @@ void Cipher::ForEach(Cipher::CipherNameCallback callback) {
43194429
array_push_back<EVP_CIPHER>,
43204430
#endif
43214431
&context);
4432+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
4433+
auto maybe_push_provider_only_cipher = [&](const char* name) {
4434+
EVP_CIPHER* cipher = EVP_CIPHER_fetch(nullptr, name, nullptr);
4435+
if (cipher == nullptr) return;
4436+
EVP_CIPHER_free(cipher);
4437+
context.cb(name);
4438+
};
4439+
#endif
4440+
#if OPENSSL_WITH_AES_SIV
4441+
for (const char* name : kProviderOnlyAesSivCiphers) {
4442+
maybe_push_provider_only_cipher(name);
4443+
}
4444+
#endif
4445+
#if OPENSSL_WITH_AES_GCM_SIV
4446+
for (const char* name : kProviderOnlyAesGcmSivCiphers) {
4447+
maybe_push_provider_only_cipher(name);
4448+
}
4449+
#endif
43224450
#endif
43234451
}
43244452

‎deps/ncrypto/ncrypto.h‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,18 @@
7070
#define OPENSSL_WITH_EVP_MAC 0
7171
#endif
7272

73+
#if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_PREREQ(3, 0)
74+
#define OPENSSL_WITH_AES_SIV 1
75+
#else
76+
#define OPENSSL_WITH_AES_SIV 0
77+
#endif
78+
79+
#if !defined(OPENSSL_IS_BORINGSSL) && OPENSSL_VERSION_PREREQ(3, 2)
80+
#define OPENSSL_WITH_AES_GCM_SIV 1
81+
#else
82+
#define OPENSSL_WITH_AES_GCM_SIV 0
83+
#endif
84+
7385
#if defined(OPENSSL_IS_BORINGSSL) || OPENSSL_VERSION_PREREQ(3, 2)
7486
#define OPENSSL_WITH_SIGNATURE_CONTEXT_STRING 1
7587
#else
@@ -402,6 +414,9 @@ class Cipher final {
402414
Cipher(const Cipher&) = default;
403415
Cipher& operator=(const Cipher&) = default;
404416
inline Cipher& operator=(const EVP_CIPHER* cipher) {
417+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
418+
fetched_cipher_.reset();
419+
#endif
405420
cipher_ = cipher;
406421
return *this;
407422
}
@@ -424,6 +439,8 @@ class Cipher final {
424439
bool isCtrMode() const;
425440
bool isCcmMode() const;
426441
bool isOcbMode() const;
442+
bool isSivMode() const;
443+
bool isGcmSivMode() const;
427444
bool isStreamMode() const;
428445
bool isChaCha20Poly1305() const;
429446

@@ -494,6 +511,10 @@ class Cipher final {
494511

495512
private:
496513
const EVP_CIPHER* cipher_ = nullptr;
514+
#if OPENSSL_WITH_AES_SIV || OPENSSL_WITH_AES_GCM_SIV
515+
explicit Cipher(EVP_CIPHER* cipher);
516+
std::shared_ptr<EVP_CIPHER> fetched_cipher_;
517+
#endif
497518
};
498519

499520
// ============================================================================
@@ -834,6 +855,8 @@ class CipherCtxPointer final {
834855
bool isOcbMode() const;
835856
bool isCcmMode() const;
836857
bool isWrapMode() const;
858+
bool isSivMode() const;
859+
bool isGcmSivMode() const;
837860
bool isChaCha20Poly1305() const;
838861

839862
bool update(const Buffer<const unsigned char>& in,

0 commit comments

Comments
 (0)