Skip to content

Commit 4809db9

Browse files
committed
crypto: use kNoAuthTagLength in InitAuthenticated
PR-URL: #20225 Refs: #20039 Reviewed-By: James M Snell <[email protected]> Reviewed-By: Daniel Bevenius <[email protected]> Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 3152b7c commit 4809db9

File tree

2 files changed

+25
-12
lines changed

2 files changed

+25
-12
lines changed

src/node_crypto.cc

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2629,7 +2629,7 @@ void CipherBase::New(const FunctionCallbackInfo<Value>& args) {
26292629
void CipherBase::Init(const char* cipher_type,
26302630
const char* key_buf,
26312631
int key_buf_len,
2632-
int auth_tag_len) {
2632+
unsigned int auth_tag_len) {
26332633
HandleScope scope(env()->isolate());
26342634

26352635
#ifdef NODE_FIPS_MODE
@@ -2700,10 +2700,16 @@ void CipherBase::Init(const FunctionCallbackInfo<Value>& args) {
27002700
const node::Utf8Value cipher_type(args.GetIsolate(), args[0]);
27012701
const char* key_buf = Buffer::Data(args[1]);
27022702
ssize_t key_buf_len = Buffer::Length(args[1]);
2703-
CHECK(args[2]->IsInt32());
2703+
27042704
// Don't assign to cipher->auth_tag_len_ directly; the value might not
27052705
// represent a valid length at this point.
2706-
int auth_tag_len = args[2].As<v8::Int32>()->Value();
2706+
unsigned int auth_tag_len;
2707+
if (args[2]->IsUint32()) {
2708+
auth_tag_len = args[2].As<v8::Uint32>()->Value();
2709+
} else {
2710+
CHECK(args[2]->IsInt32() && args[2].As<v8::Int32>()->Value() == -1);
2711+
auth_tag_len = kNoAuthTagLength;
2712+
}
27072713

27082714
cipher->Init(*cipher_type, key_buf, key_buf_len, auth_tag_len);
27092715
}
@@ -2714,7 +2720,7 @@ void CipherBase::InitIv(const char* cipher_type,
27142720
int key_len,
27152721
const char* iv,
27162722
int iv_len,
2717-
int auth_tag_len) {
2723+
unsigned int auth_tag_len) {
27182724
HandleScope scope(env()->isolate());
27192725

27202726
const EVP_CIPHER* const cipher = EVP_get_cipherbyname(cipher_type);
@@ -2788,10 +2794,16 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) {
27882794
iv_buf = Buffer::Data(args[2]);
27892795
iv_len = Buffer::Length(args[2]);
27902796
}
2791-
CHECK(args[3]->IsInt32());
2797+
27922798
// Don't assign to cipher->auth_tag_len_ directly; the value might not
27932799
// represent a valid length at this point.
2794-
int auth_tag_len = args[3].As<v8::Int32>()->Value();
2800+
unsigned int auth_tag_len;
2801+
if (args[3]->IsUint32()) {
2802+
auth_tag_len = args[3].As<v8::Uint32>()->Value();
2803+
} else {
2804+
CHECK(args[3]->IsInt32() && args[3].As<v8::Int32>()->Value() == -1);
2805+
auth_tag_len = kNoAuthTagLength;
2806+
}
27952807

27962808
cipher->InitIv(*cipher_type, key_buf, key_len, iv_buf, iv_len, auth_tag_len);
27972809
}
@@ -2802,7 +2814,7 @@ static bool IsValidGCMTagLength(unsigned int tag_len) {
28022814
}
28032815

28042816
bool CipherBase::InitAuthenticated(const char *cipher_type, int iv_len,
2805-
int auth_tag_len) {
2817+
unsigned int auth_tag_len) {
28062818
CHECK(IsAuthenticatedMode());
28072819

28082820
// TODO(tniessen) Use EVP_CTRL_AEAD_SET_IVLEN when migrating to OpenSSL 1.1.0
@@ -2815,7 +2827,7 @@ bool CipherBase::InitAuthenticated(const char *cipher_type, int iv_len,
28152827

28162828
const int mode = EVP_CIPHER_CTX_mode(ctx_);
28172829
if (mode == EVP_CIPH_CCM_MODE) {
2818-
if (auth_tag_len < 0) {
2830+
if (auth_tag_len == kNoAuthTagLength) {
28192831
char msg[128];
28202832
snprintf(msg, sizeof(msg), "authTagLength required for %s", cipher_type);
28212833
env()->ThrowError(msg);
@@ -2850,7 +2862,7 @@ bool CipherBase::InitAuthenticated(const char *cipher_type, int iv_len,
28502862
} else {
28512863
CHECK_EQ(mode, EVP_CIPH_GCM_MODE);
28522864

2853-
if (auth_tag_len >= 0) {
2865+
if (auth_tag_len != kNoAuthTagLength) {
28542866
if (!IsValidGCMTagLength(auth_tag_len)) {
28552867
char msg[50];
28562868
snprintf(msg, sizeof(msg),

src/node_crypto.h

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -364,14 +364,15 @@ class CipherBase : public BaseObject {
364364
void Init(const char* cipher_type,
365365
const char* key_buf,
366366
int key_buf_len,
367-
int auth_tag_len);
367+
unsigned int auth_tag_len);
368368
void InitIv(const char* cipher_type,
369369
const char* key,
370370
int key_len,
371371
const char* iv,
372372
int iv_len,
373-
int auth_tag_len);
374-
bool InitAuthenticated(const char *cipher_type, int iv_len, int auth_tag_len);
373+
unsigned int auth_tag_len);
374+
bool InitAuthenticated(const char *cipher_type, int iv_len,
375+
unsigned int auth_tag_len);
375376
bool CheckCCMMessageLength(int message_len);
376377
UpdateResult Update(const char* data, int len, unsigned char** out,
377378
int* out_len);

0 commit comments

Comments
 (0)