Skip to content

Commit

Permalink
Add SecretBuffer::substr.
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 718877508
Change-Id: I1482c12f89d1cfc1b28651251cef6ce94ae45002
  • Loading branch information
tholenst authored and copybara-github committed Jan 23, 2025
1 parent 89910a8 commit c2d6fce
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 0 deletions.
1 change: 1 addition & 0 deletions tink/internal/BUILD.bazel
Original file line number Diff line number Diff line change
Expand Up @@ -1976,6 +1976,7 @@ cc_test(
deps = [
":secret_buffer",
"//tink/util:test_util",
"@com_google_absl//absl/strings:string_view",
"@com_google_googletest//:gtest_main",
],
)
Expand Down
1 change: 1 addition & 0 deletions tink/internal/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1904,6 +1904,7 @@ tink_cc_test(
DEPS
tink::internal::secret_buffer
gmock
absl::string_view
tink::util::test_util
)

Expand Down
26 changes: 26 additions & 0 deletions tink/internal/secret_buffer.h
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
#ifndef TINK_INTERNAL_SECRET_BUFFER_H_
#define TINK_INTERNAL_SECRET_BUFFER_H_

#include <algorithm>
#include <cstddef>
#include <cstdint>
#include <cstring>
Expand Down Expand Up @@ -136,6 +137,31 @@ class SecretBuffer {
swap(capacity_, other.capacity_);
}

SecretBuffer substr(
size_t pos, size_t count = std::numeric_limits<size_t>::max()) const& {
CHECK(pos <= size());
count = std::min(count, size() - pos);
SecretBuffer result;
result.reserve(count);
SafeMemCopy(result.data_, data_ + pos, count);
result.size_ = count;
return result;
}

SecretBuffer substr(size_t pos,
size_t count = std::numeric_limits<size_t>::max()) && {
CHECK(pos <= size());
count = std::min(count, size() - pos);
SecretBuffer result;
result.swap(*this);
if (pos != 0) {
crypto::tink::internal::SafeMemMove(result.data(), result.data() + pos,
count);
}
result.resize(count);
return result;
}

bool operator==(const SecretBuffer& other) const {
if (size() != other.size()) {
return false;
Expand Down
26 changes: 26 additions & 0 deletions tink/internal/secret_buffer_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

#include "gmock/gmock.h"
#include "gtest/gtest.h"
#include "absl/strings/string_view.h"
#include "tink/util/test_util.h"

namespace crypto {
Expand Down Expand Up @@ -157,6 +158,31 @@ TEST(SecretBufferTest, SwapWithEmpty) {
EXPECT_THAT(buffer2.AsStringView(), Eq(""));
}

TEST(SecretBufferTest, SubstrConstRef) {
constexpr absl::string_view kData = "Some arbitrary data";
SecretBuffer buffer(kData);
for (int start = 0; start <= kData.size(); ++start) {
for (int num = 0; num <= kData.size(); ++num) {
EXPECT_THAT(buffer.substr(start, num),
Eq(SecretBuffer(kData.substr(start, num))))
<< "substr(" << start << ", " << num << ")";
}
}
}

TEST(SecretBufferTest, SubstrRvalueRef) {
constexpr absl::string_view kData = "Some arbitrary data";
SecretBuffer buffer(kData);
for (int start = 0; start <= kData.size(); ++start) {
for (int num = 0; num <= kData.size(); ++num) {
SecretBuffer tmp_buffer = buffer;
EXPECT_THAT(std::move(tmp_buffer).substr(start, num),
Eq(SecretBuffer(kData.substr(start, num))))
<< "substr(" << start << ", " << num << ")";
}
}
}

} // namespace
} // namespace internal
} // namespace tink
Expand Down

0 comments on commit c2d6fce

Please sign in to comment.