-
Notifications
You must be signed in to change notification settings - Fork 30k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
crypto: allow to restrict valid GCM tag length #20039
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2806,6 +2806,10 @@ void CipherBase::InitIv(const FunctionCallbackInfo<Value>& args) { | |
} | ||
|
||
|
||
static bool IsValidGCMTagLength(unsigned int tag_len) { | ||
return tag_len == 4 || tag_len == 8 || tag_len >= 12 && tag_len <= 16; | ||
} | ||
|
||
bool CipherBase::InitAuthenticated(const char *cipher_type, int iv_len, | ||
int auth_tag_len) { | ||
CHECK(IsAuthenticatedMode()); | ||
|
@@ -2818,7 +2822,8 @@ bool CipherBase::InitAuthenticated(const char *cipher_type, int iv_len, | |
return false; | ||
} | ||
|
||
if (EVP_CIPHER_CTX_mode(ctx_) == EVP_CIPH_CCM_MODE) { | ||
const int mode = EVP_CIPHER_CTX_mode(ctx_); | ||
if (mode == EVP_CIPH_CCM_MODE) { | ||
if (auth_tag_len < 0) { | ||
char msg[128]; | ||
snprintf(msg, sizeof(msg), "authTagLength required for %s", cipher_type); | ||
|
@@ -2851,6 +2856,21 @@ bool CipherBase::InitAuthenticated(const char *cipher_type, int iv_len, | |
} else { | ||
max_message_size_ = INT_MAX; | ||
} | ||
} else { | ||
CHECK_EQ(mode, EVP_CIPH_GCM_MODE); | ||
|
||
if (auth_tag_len >= 0) { | ||
if (!IsValidGCMTagLength(auth_tag_len)) { | ||
char msg[50]; | ||
snprintf(msg, sizeof(msg), | ||
"Invalid GCM authentication tag length: %u", auth_tag_len); | ||
env()->ThrowError(msg); | ||
return false; | ||
} | ||
|
||
// Remember the given authentication tag length for later. | ||
auth_tag_len_ = auth_tag_len; | ||
} | ||
} | ||
|
||
return true; | ||
|
@@ -2886,7 +2906,7 @@ void CipherBase::GetAuthTag(const FunctionCallbackInfo<Value>& args) { | |
// Only callable after Final and if encrypting. | ||
if (cipher->ctx_ != nullptr || | ||
cipher->kind_ != kCipher || | ||
cipher->auth_tag_len_ == 0) { | ||
cipher->auth_tag_len_ == kNoAuthTagLength) { | ||
return args.GetReturnValue().SetUndefined(); | ||
} | ||
|
||
|
@@ -2911,7 +2931,9 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) { | |
unsigned int tag_len = Buffer::Length(args[0]); | ||
const int mode = EVP_CIPHER_CTX_mode(cipher->ctx_); | ||
if (mode == EVP_CIPH_GCM_MODE) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tniessen I know your change is for
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes! |
||
if (tag_len > 16 || (tag_len < 12 && tag_len != 8 && tag_len != 4)) { | ||
if (cipher->auth_tag_len_ != kNoAuthTagLength && | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. compiler warning:
|
||
cipher->auth_tag_len_ != tag_len || | ||
!IsValidGCMTagLength(tag_len)) { | ||
char msg[50]; | ||
snprintf(msg, sizeof(msg), | ||
"Invalid GCM authentication tag length: %u", tag_len); | ||
|
@@ -2947,7 +2969,8 @@ bool CipherBase::SetAAD(const char* data, unsigned int len, int plaintext_len) { | |
if (!CheckCCMMessageLength(plaintext_len)) | ||
return false; | ||
|
||
if (kind_ == kDecipher && !auth_tag_set_ && auth_tag_len_ > 0) { | ||
if (kind_ == kDecipher && !auth_tag_set_ && auth_tag_len_ > 0 && | ||
auth_tag_len_ != kNoAuthTagLength) { | ||
if (!EVP_CIPHER_CTX_ctrl(ctx_, | ||
EVP_CTRL_CCM_SET_TAG, | ||
auth_tag_len_, | ||
|
@@ -3000,7 +3023,7 @@ CipherBase::UpdateResult CipherBase::Update(const char* data, | |
|
||
// on first update: | ||
if (kind_ == kDecipher && IsAuthenticatedMode() && auth_tag_len_ > 0 && | ||
!auth_tag_set_) { | ||
auth_tag_len_ != kNoAuthTagLength && !auth_tag_set_) { | ||
EVP_CIPHER_CTX_ctrl(ctx_, | ||
EVP_CTRL_GCM_SET_TAG, | ||
auth_tag_len_, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@tniessen I just saw compiler warning today:
Typically, how do we fix this? Create another PR to fix this?