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

Commit

Permalink
Add CallWithCoreDumpProtection to AES CMAC
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 623403538
  • Loading branch information
happyCoder92 authored and copybara-github committed Apr 10, 2024
1 parent 59d6a2b commit 62da0f3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 13 deletions.
1 change: 1 addition & 0 deletions cc/subtle/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@ cc_library(
":subtle_util",
"//:mac",
"//internal:aes_util",
"//internal:call_with_core_dump_protection",
"//internal:fips_utils",
"//internal:ssl_unique_ptr",
"//internal:util",
Expand Down
1 change: 1 addition & 0 deletions cc/subtle/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,7 @@ tink_cc_library(
crypto
tink::core::mac
tink::internal::aes_util
tink::internal::call_with_core_dump_protection
tink::internal::fips_utils
tink::internal::ssl_unique_ptr
tink::internal::util
Expand Down
34 changes: 21 additions & 13 deletions cc/subtle/aes_cmac_boringssl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "openssl/cmac.h"
#include "openssl/evp.h"
#include "tink/internal/aes_util.h"
#include "tink/internal/call_with_core_dump_protection.h"
#include "tink/internal/fips_utils.h"
#include "tink/internal/ssl_unique_ptr.h"
#include "tink/internal/util.h"
Expand Down Expand Up @@ -84,16 +85,21 @@ util::StatusOr<std::string> AesCmacBoringSsl::ComputeMac(
return cipher.status();
}
size_t len = 0;
const uint8_t* key_ptr = reinterpret_cast<const uint8_t*>(&key_[0]);
const uint8_t* data_ptr = reinterpret_cast<const uint8_t*>(data.data());
uint8_t* result_ptr = reinterpret_cast<uint8_t*>(&result[0]);
if (CMAC_Init(context.get(), key_ptr, key_.size(), *cipher, nullptr) <= 0 ||
CMAC_Update(context.get(), data_ptr, data.size()) <= 0 ||
CMAC_Final(context.get(), result_ptr, &len) == 0) {
bool res = internal::CallWithCoreDumpProtection([&]() {
if (CMAC_Init(context.get(), key_.data(), key_.size(), *cipher, nullptr) <=
0 ||
CMAC_Update(context.get(), data_ptr, data.size()) <= 0 ||
CMAC_Final(context.get(), result_ptr, &len) == 0) {
return false;
}
result.resize(tag_size_);
return true;
});
if (!res) {
return util::Status(absl::StatusCode::kInternal, "Failed to compute CMAC");
}

result.resize(tag_size_);
return result;
}

Expand All @@ -104,13 +110,15 @@ util::Status AesCmacBoringSsl::VerifyMac(absl::string_view mac,
"Incorrect tag size: expected %d, found %d", tag_size_,
mac.size());
}
util::StatusOr<std::string> computed_mac = ComputeMac(data);
if (!computed_mac.ok()) return computed_mac.status();
if (CRYPTO_memcmp(computed_mac->data(), mac.data(), tag_size_) != 0) {
return util::Status(absl::StatusCode::kInvalidArgument,
"CMAC verification failed");
}
return util::OkStatus();
return internal::CallWithCoreDumpProtection([&]() {
util::StatusOr<std::string> computed_mac = ComputeMac(data);
if (!computed_mac.ok()) return computed_mac.status();
if (CRYPTO_memcmp(computed_mac->data(), mac.data(), tag_size_) != 0) {
return util::Status(absl::StatusCode::kInvalidArgument,
"CMAC verification failed");
}
return util::OkStatus();
});
}

} // namespace subtle
Expand Down

0 comments on commit 62da0f3

Please sign in to comment.