@@ -2931,6 +2931,20 @@ void CipherBase::SetAuthTag(const FunctionCallbackInfo<Value>& args) { |
2931 | 2931 | } |
2932 | 2932 |
|
2933 | 2933 |
|
| 2934 | +bool CipherBase::MaybePassAuthTagToOpenSSL() { |
| 2935 | + if (!auth_tag_set_ && auth_tag_len_ != kNoAuthTagLength) { |
| 2936 | + if (!EVP_CIPHER_CTX_ctrl(ctx_.get(), |
| 2937 | + EVP_CTRL_AEAD_SET_TAG, |
| 2938 | + auth_tag_len_, |
| 2939 | + reinterpret_cast<unsigned char*>(auth_tag_))) { |
| 2940 | + return false; |
| 2941 | + } |
| 2942 | + auth_tag_set_ = true; |
| 2943 | + } |
| 2944 | + return true; |
| 2945 | +} |
| 2946 | + |
| 2947 | + |
2934 | 2948 | bool CipherBase::SetAAD(const char* data, unsigned int len, int plaintext_len) { |
2935 | 2949 | if (!ctx_ || !IsAuthenticatedMode()) |
2936 | 2950 | return false; |
@@ -2950,15 +2964,9 @@ bool CipherBase::SetAAD(const char* data, unsigned int len, int plaintext_len) { |
2950 | 2964 | if (!CheckCCMMessageLength(plaintext_len)) |
2951 | 2965 | return false; |
2952 | 2966 |
|
2953 | | - if (kind_ == kDecipher && !auth_tag_set_ && auth_tag_len_ > 0 && |
2954 | | - auth_tag_len_ != kNoAuthTagLength) { |
2955 | | - if (!EVP_CIPHER_CTX_ctrl(ctx_.get(), |
2956 | | - EVP_CTRL_CCM_SET_TAG, |
2957 | | - auth_tag_len_, |
2958 | | - reinterpret_cast<unsigned char*>(auth_tag_))) { |
| 2967 | + if (kind_ == kDecipher) { |
| 2968 | + if (!MaybePassAuthTagToOpenSSL()) |
2959 | 2969 | return false; |
2960 | | - } |
2961 | | - auth_tag_set_ = true; |
2962 | 2970 | } |
2963 | 2971 |
|
2964 | 2972 | // Specify the plaintext length. |
@@ -3003,14 +3011,10 @@ CipherBase::UpdateResult CipherBase::Update(const char* data, |
3003 | 3011 | return kErrorMessageSize; |
3004 | 3012 | } |
3005 | 3013 |
|
3006 | | - // on first update: |
3007 | | - if (kind_ == kDecipher && IsAuthenticatedMode() && auth_tag_len_ > 0 && |
3008 | | - auth_tag_len_ != kNoAuthTagLength && !auth_tag_set_) { |
3009 | | - CHECK(EVP_CIPHER_CTX_ctrl(ctx_.get(), |
3010 | | - EVP_CTRL_AEAD_SET_TAG, |
3011 | | - auth_tag_len_, |
3012 | | - reinterpret_cast<unsigned char*>(auth_tag_))); |
3013 | | - auth_tag_set_ = true; |
| 3014 | + // Pass the authentication tag to OpenSSL if possible. This will only happen |
| 3015 | + // once, usually on the first update. |
| 3016 | + if (kind_ == kDecipher && IsAuthenticatedMode()) { |
| 3017 | + CHECK(MaybePassAuthTagToOpenSSL()); |
3014 | 3018 | } |
3015 | 3019 |
|
3016 | 3020 | *out_len = 0; |
@@ -3110,6 +3114,10 @@ bool CipherBase::Final(unsigned char** out, int* out_len) { |
3110 | 3114 | *out = Malloc<unsigned char>( |
3111 | 3115 | static_cast<size_t>(EVP_CIPHER_CTX_block_size(ctx_.get()))); |
3112 | 3116 |
|
| 3117 | + if (kind_ == kDecipher && IsSupportedAuthenticatedMode(mode)) { |
| 3118 | + MaybePassAuthTagToOpenSSL(); |
| 3119 | + } |
| 3120 | + |
3113 | 3121 | // In CCM mode, final() only checks whether authentication failed in update(). |
3114 | 3122 | // EVP_CipherFinal_ex must not be called and will fail. |
3115 | 3123 | bool ok; |
|