Skip to content

Commit 694a273

Browse files
panvanpaun
authored andcommitted
crypto: add AES-OCB Web Cryptography algorithm
PR-URL: nodejs/node#59539 Reviewed-By: Tobias Nießen <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 9718666 commit 694a273

File tree

2 files changed

+113
-0
lines changed

2 files changed

+113
-0
lines changed

include/ncrypto.h

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -297,6 +297,35 @@ class Cipher final {
297297
static const Cipher FromNid(int nid);
298298
static const Cipher FromCtx(const CipherCtxPointer& ctx);
299299

300+
using CipherNameCallback = std::function<void(const char* name)>;
301+
302+
// Iterates the known ciphers if the underlying implementation
303+
// is able to do so.
304+
static void ForEach(CipherNameCallback callback);
305+
306+
// Utilities to get various ciphers by type. If the underlying
307+
// implementation does not support the requested cipher, then
308+
// the result will be an empty Cipher object whose bool operator
309+
// will return false.
310+
311+
static const Cipher EMPTY;
312+
static const Cipher AES_128_CBC;
313+
static const Cipher AES_192_CBC;
314+
static const Cipher AES_256_CBC;
315+
static const Cipher AES_128_CTR;
316+
static const Cipher AES_192_CTR;
317+
static const Cipher AES_256_CTR;
318+
static const Cipher AES_128_GCM;
319+
static const Cipher AES_192_GCM;
320+
static const Cipher AES_256_GCM;
321+
static const Cipher AES_128_KW;
322+
static const Cipher AES_192_KW;
323+
static const Cipher AES_256_KW;
324+
static const Cipher AES_128_OCB;
325+
static const Cipher AES_192_OCB;
326+
static const Cipher AES_256_OCB;
327+
static const Cipher CHACHA20_POLY1305;
328+
300329
struct CipherParams {
301330
int padding;
302331
const EVP_MD* digest;
@@ -638,6 +667,12 @@ class CipherCtxPointer final {
638667
int getMode() const;
639668
int getNid() const;
640669

670+
bool isGcmMode() const;
671+
bool isOcbMode() const;
672+
bool isCcmMode() const;
673+
bool isWrapMode() const;
674+
bool isChaCha20Poly1305() const;
675+
641676
bool update(const Buffer<const unsigned char>& in,
642677
unsigned char* out,
643678
int* out_len,

src/ncrypto.cpp

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3159,6 +3159,59 @@ const Cipher Cipher::FromCtx(const CipherCtxPointer& ctx) {
31593159
return Cipher(EVP_CIPHER_CTX_cipher(ctx.get()));
31603160
}
31613161

3162+
const Cipher Cipher::EMPTY = Cipher();
3163+
const Cipher Cipher::AES_128_CBC = Cipher::FromNid(NID_aes_128_cbc);
3164+
const Cipher Cipher::AES_192_CBC = Cipher::FromNid(NID_aes_192_cbc);
3165+
const Cipher Cipher::AES_256_CBC = Cipher::FromNid(NID_aes_256_cbc);
3166+
const Cipher Cipher::AES_128_CTR = Cipher::FromNid(NID_aes_128_ctr);
3167+
const Cipher Cipher::AES_192_CTR = Cipher::FromNid(NID_aes_192_ctr);
3168+
const Cipher Cipher::AES_256_CTR = Cipher::FromNid(NID_aes_256_ctr);
3169+
const Cipher Cipher::AES_128_GCM = Cipher::FromNid(NID_aes_128_gcm);
3170+
const Cipher Cipher::AES_192_GCM = Cipher::FromNid(NID_aes_192_gcm);
3171+
const Cipher Cipher::AES_256_GCM = Cipher::FromNid(NID_aes_256_gcm);
3172+
const Cipher Cipher::AES_128_KW = Cipher::FromNid(NID_id_aes128_wrap);
3173+
const Cipher Cipher::AES_192_KW = Cipher::FromNid(NID_id_aes192_wrap);
3174+
const Cipher Cipher::AES_256_KW = Cipher::FromNid(NID_id_aes256_wrap);
3175+
const Cipher Cipher::AES_128_OCB = Cipher::FromNid(NID_aes_128_ocb);
3176+
const Cipher Cipher::AES_192_OCB = Cipher::FromNid(NID_aes_192_ocb);
3177+
const Cipher Cipher::AES_256_OCB = Cipher::FromNid(NID_aes_256_ocb);
3178+
const Cipher Cipher::CHACHA20_POLY1305 = Cipher::FromNid(NID_chacha20_poly1305);
3179+
3180+
bool Cipher::isGcmMode() const {
3181+
if (!cipher_) return false;
3182+
return getMode() == EVP_CIPH_GCM_MODE;
3183+
}
3184+
3185+
bool Cipher::isWrapMode() const {
3186+
if (!cipher_) return false;
3187+
return getMode() == EVP_CIPH_WRAP_MODE;
3188+
}
3189+
3190+
bool Cipher::isCtrMode() const {
3191+
if (!cipher_) return false;
3192+
return getMode() == EVP_CIPH_CTR_MODE;
3193+
}
3194+
3195+
bool Cipher::isCcmMode() const {
3196+
if (!cipher_) return false;
3197+
return getMode() == EVP_CIPH_CCM_MODE;
3198+
}
3199+
3200+
bool Cipher::isOcbMode() const {
3201+
if (!cipher_) return false;
3202+
return getMode() == EVP_CIPH_OCB_MODE;
3203+
}
3204+
3205+
bool Cipher::isStreamMode() const {
3206+
if (!cipher_) return false;
3207+
return getMode() == EVP_CIPH_STREAM_CIPHER;
3208+
}
3209+
3210+
bool Cipher::isChaCha20Poly1305() const {
3211+
if (!cipher_) return false;
3212+
return getNid() == NID_chacha20_poly1305;
3213+
}
3214+
31623215
int Cipher::getMode() const {
31633216
if (!cipher_) return 0;
31643217
return EVP_CIPHER_mode(cipher_);
@@ -3311,6 +3364,31 @@ int CipherCtxPointer::getMode() const {
33113364
return EVP_CIPHER_CTX_mode(ctx_.get());
33123365
}
33133366

3367+
bool CipherCtxPointer::isGcmMode() const {
3368+
if (!ctx_) return false;
3369+
return getMode() == EVP_CIPH_GCM_MODE;
3370+
}
3371+
3372+
bool CipherCtxPointer::isOcbMode() const {
3373+
if (!ctx_) return false;
3374+
return getMode() == EVP_CIPH_OCB_MODE;
3375+
}
3376+
3377+
bool CipherCtxPointer::isCcmMode() const {
3378+
if (!ctx_) return false;
3379+
return getMode() == EVP_CIPH_CCM_MODE;
3380+
}
3381+
3382+
bool CipherCtxPointer::isWrapMode() const {
3383+
if (!ctx_) return false;
3384+
return getMode() == EVP_CIPH_WRAP_MODE;
3385+
}
3386+
3387+
bool CipherCtxPointer::isChaCha20Poly1305() const {
3388+
if (!ctx_) return false;
3389+
return getNid() == NID_chacha20_poly1305;
3390+
}
3391+
33143392
int CipherCtxPointer::getNid() const {
33153393
if (!ctx_) return 0;
33163394
return EVP_CIPHER_CTX_nid(ctx_.get());

0 commit comments

Comments
 (0)