Skip to content
This repository was archived by the owner on Apr 17, 2024. It is now read-only.

Commit 62da0f3

Browse files
happyCoder92copybara-github
authored andcommitted
Add CallWithCoreDumpProtection to AES CMAC
PiperOrigin-RevId: 623403538
1 parent 59d6a2b commit 62da0f3

File tree

3 files changed

+23
-13
lines changed

3 files changed

+23
-13
lines changed

cc/subtle/BUILD.bazel

+1
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,7 @@ cc_library(
189189
":subtle_util",
190190
"//:mac",
191191
"//internal:aes_util",
192+
"//internal:call_with_core_dump_protection",
192193
"//internal:fips_utils",
193194
"//internal:ssl_unique_ptr",
194195
"//internal:util",

cc/subtle/CMakeLists.txt

+1
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@ tink_cc_library(
180180
crypto
181181
tink::core::mac
182182
tink::internal::aes_util
183+
tink::internal::call_with_core_dump_protection
183184
tink::internal::fips_utils
184185
tink::internal::ssl_unique_ptr
185186
tink::internal::util

cc/subtle/aes_cmac_boringssl.cc

+21-13
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
#include "openssl/cmac.h"
2929
#include "openssl/evp.h"
3030
#include "tink/internal/aes_util.h"
31+
#include "tink/internal/call_with_core_dump_protection.h"
3132
#include "tink/internal/fips_utils.h"
3233
#include "tink/internal/ssl_unique_ptr.h"
3334
#include "tink/internal/util.h"
@@ -84,16 +85,21 @@ util::StatusOr<std::string> AesCmacBoringSsl::ComputeMac(
8485
return cipher.status();
8586
}
8687
size_t len = 0;
87-
const uint8_t* key_ptr = reinterpret_cast<const uint8_t*>(&key_[0]);
8888
const uint8_t* data_ptr = reinterpret_cast<const uint8_t*>(data.data());
8989
uint8_t* result_ptr = reinterpret_cast<uint8_t*>(&result[0]);
90-
if (CMAC_Init(context.get(), key_ptr, key_.size(), *cipher, nullptr) <= 0 ||
91-
CMAC_Update(context.get(), data_ptr, data.size()) <= 0 ||
92-
CMAC_Final(context.get(), result_ptr, &len) == 0) {
90+
bool res = internal::CallWithCoreDumpProtection([&]() {
91+
if (CMAC_Init(context.get(), key_.data(), key_.size(), *cipher, nullptr) <=
92+
0 ||
93+
CMAC_Update(context.get(), data_ptr, data.size()) <= 0 ||
94+
CMAC_Final(context.get(), result_ptr, &len) == 0) {
95+
return false;
96+
}
97+
result.resize(tag_size_);
98+
return true;
99+
});
100+
if (!res) {
93101
return util::Status(absl::StatusCode::kInternal, "Failed to compute CMAC");
94102
}
95-
96-
result.resize(tag_size_);
97103
return result;
98104
}
99105

@@ -104,13 +110,15 @@ util::Status AesCmacBoringSsl::VerifyMac(absl::string_view mac,
104110
"Incorrect tag size: expected %d, found %d", tag_size_,
105111
mac.size());
106112
}
107-
util::StatusOr<std::string> computed_mac = ComputeMac(data);
108-
if (!computed_mac.ok()) return computed_mac.status();
109-
if (CRYPTO_memcmp(computed_mac->data(), mac.data(), tag_size_) != 0) {
110-
return util::Status(absl::StatusCode::kInvalidArgument,
111-
"CMAC verification failed");
112-
}
113-
return util::OkStatus();
113+
return internal::CallWithCoreDumpProtection([&]() {
114+
util::StatusOr<std::string> computed_mac = ComputeMac(data);
115+
if (!computed_mac.ok()) return computed_mac.status();
116+
if (CRYPTO_memcmp(computed_mac->data(), mac.data(), tag_size_) != 0) {
117+
return util::Status(absl::StatusCode::kInvalidArgument,
118+
"CMAC verification failed");
119+
}
120+
return util::OkStatus();
121+
});
114122
}
115123

116124
} // namespace subtle

0 commit comments

Comments
 (0)