85 SecureBuffer(
const std::array<uint8_t, N>& other) { std::memcpy(data_.data(), other.data(), N); }
98 std::memcpy(data_.data(), other.data_.data(), N);
105 if (
this != &other) {
106 std::memcpy(data_.data(), other.data_.data(), N);
113 uint8_t*
data() noexcept {
return data_.data(); }
116 const uint8_t*
data() const noexcept {
return data_.data(); }
119 std::size_t
size() const noexcept {
return N; }
122 uint8_t*
begin() noexcept {
return data_.data(); }
125 uint8_t*
end() noexcept {
return data_.data() + N; }
128 const uint8_t*
begin() const noexcept {
return data_.data(); }
131 const uint8_t*
end() const noexcept {
return data_.data() + N; }
134 uint8_t&
operator[](std::size_t index)
noexcept {
return data_[index]; }
137 const uint8_t&
operator[](std::size_t index)
const noexcept {
return data_[index]; }
140 void fill(uint8_t value)
noexcept { data_.fill(value); }
145 std::memcpy(copy.data_.data(), data_.data(), N);
150 void Wipe() noexcept {
secure_zero(data_.data(), data_.size()); }
152 std::array<uint8_t, N> data_ = {{0}};
199 uint8_t*
data() noexcept {
return data_; }
202 const uint8_t*
data() const noexcept {
return data_; }
205 std::size_t
size() const noexcept {
return size_; }
208 bool empty() const noexcept {
return size_ == 0; }
211 uint8_t*
begin() noexcept {
return data_; }
214 uint8_t*
end() noexcept {
return data_ + size_; }
217 const uint8_t*
begin() const noexcept {
return data_; }
220 const uint8_t*
end() const noexcept {
return data_ + size_; }
223 uint8_t&
operator[](std::size_t index)
noexcept {
return data_[index]; }
226 const uint8_t&
operator[](std::size_t index)
const noexcept {
return data_[index]; }
229 void Assign(
const uint8_t* data, std::size_t size);
232 void Assign(
const std::vector<uint8_t>& data);
244 bool operator!=(const
SecureBytes& other)
const {
return !(*
this == other); }
247 uint8_t* data_ =
nullptr;
248 std::size_t size_ = 0;
301 const char*
data() const noexcept {
return data_; }
304 char*
data() noexcept {
return data_; }
307 char*
begin() noexcept {
return data_; }
310 const char*
begin() const noexcept {
return data_; }
313 char*
end() noexcept {
return data_ + size_; }
316 const char*
end() const noexcept {
return data_ + size_; }
319 std::size_t
size() const noexcept {
return size_; }
322 std::size_t
length() const noexcept {
return size_; }
325 bool empty() const noexcept {
return size_ == 0; }
328 const char*
c_str() const noexcept {
return data_; }
343 void assign(
const char* data, std::size_t size);
346 void Allocate(
const char* data, std::size_t size);
347 void Release() noexcept;
349 char* data_ =
nullptr;
350 std::
size_t size_ = 0;
Fixed-size byte buffer that is zeroized on destruction.
Definition secure.hh:79
std::size_t size() const noexcept
Returns the size of the buffer in bytes.
Definition secure.hh:119
uint8_t & operator[](std::size_t index) noexcept
Returns the byte at the given index.
Definition secure.hh:134
uint8_t * data() noexcept
Returns a pointer to the raw bytes.
Definition secure.hh:113
SecureBuffer(const std::array< uint8_t, N > &other)
Constructs a buffer by copying the given fixed-size byte array.
Definition secure.hh:85
SecureBuffer(const SecureBuffer &)=delete
No implicit copies of key material.
~SecureBuffer()
Zeroizes the stored bytes.
Definition secure.hh:88
SecureBuffer Clone() const
Returns an explicit deep copy of the buffer.
Definition secure.hh:143
uint8_t * begin() noexcept
Returns an iterator to the first byte.
Definition secure.hh:122
SecureBuffer & operator=(SecureBuffer &&other) noexcept
Definition secure.hh:104
const uint8_t * end() const noexcept
Returns a const iterator past the last byte.
Definition secure.hh:131
const uint8_t * data() const noexcept
Returns a const pointer to the raw bytes.
Definition secure.hh:116
void fill(uint8_t value) noexcept
Fills the buffer with the given byte value.
Definition secure.hh:140
SecureBuffer(SecureBuffer &&other) noexcept
Move-constructs by copying the bytes and wiping the source.
Definition secure.hh:97
uint8_t * end() noexcept
Returns an iterator past the last byte.
Definition secure.hh:125
SecureBuffer()=default
Default-constructs a zero-filled buffer.
SecureBuffer & operator=(const SecureBuffer &)=delete
No implicit copies of key material.
const uint8_t * begin() const noexcept
Returns a const iterator to the first byte.
Definition secure.hh:128
const uint8_t & operator[](std::size_t index) const noexcept
Returns the byte at the given index.
Definition secure.hh:137
Dynamic byte buffer that is zeroized in best-effort locked memory.
Definition secure.hh:164
SecureBytes & operator=(const SecureBytes &)=delete
No implicit copies of key material.
std::size_t size() const noexcept
Returns the size of the buffer in bytes.
Definition secure.hh:205
bool empty() const noexcept
Returns whether the buffer is empty.
Definition secure.hh:208
const uint8_t & operator[](std::size_t index) const noexcept
Returns the byte at the given index.
Definition secure.hh:226
void Clear() noexcept
Erases and releases the stored bytes.
uint8_t * begin() noexcept
Returns an iterator to the first byte.
Definition secure.hh:211
SecureBytes(const std::vector< uint8_t > &data)
Constructs a buffer by copying the given byte vector.
uint8_t & operator[](std::size_t index) noexcept
Returns the byte at the given index.
Definition secure.hh:223
SecureBytes()=default
Default-constructs an empty buffer.
~SecureBytes()
Zeroizes and releases the stored bytes.
void Assign(const std::vector< uint8_t > &data)
Replaces the content with a copy of the given byte vector.
void Assign(const uint8_t *data, std::size_t size)
Replaces the content with a copy of the given bytes.
SecureBytes(const SecureBytes &)=delete
No implicit copies of key material.
SecureBytes & operator=(const std::vector< uint8_t > &data)
Replaces the content with a copy of the given byte vector.
SecureBytes(const uint8_t *data, std::size_t size)
Constructs a buffer by copying the given bytes.
const uint8_t * end() const noexcept
Returns a const iterator past the last byte.
Definition secure.hh:220
uint8_t * data() noexcept
Returns a pointer to the raw bytes.
Definition secure.hh:199
const uint8_t * data() const noexcept
Returns a const pointer to the raw bytes.
Definition secure.hh:202
SecureBytes & operator=(SecureBytes &&other) noexcept
const uint8_t * begin() const noexcept
Returns a const iterator to the first byte.
Definition secure.hh:217
uint8_t * end() noexcept
Returns an iterator past the last byte.
Definition secure.hh:214
SecureBytes(SecureBytes &&other) noexcept
Move-constructs by transferring the bytes and wiping the source.
String that is stored in wiped and best-effort locked memory.
Definition secure.hh:262
char * data() noexcept
Returns a pointer to the character data for in-place modification.
Definition secure.hh:304
secure_string(const char *str)
Constructs a string from a NUL-terminated C string.
std::size_t length() const noexcept
Returns the number of characters.
Definition secure.hh:322
void assign(const char *data, std::size_t size)
Replaces the value with a byte range of explicitly known length.
~secure_string()
Zeroizes and releases the stored memory.
const char * data() const noexcept
Returns a pointer to the (NUL-terminated) character data.
Definition secure.hh:301
secure_string & operator=(secure_string &&other) noexcept
Move-assigns, wiping the previous content and the moved-from string.
const char * begin() const noexcept
Returns an iterator to the first character.
Definition secure.hh:310
void clear()
Erases the content, wiping the memory.
const char * c_str() const noexcept
Returns a NUL-terminated pointer to the character data.
Definition secure.hh:328
std::size_t size() const noexcept
Returns the number of characters (excluding the trailing NUL).
Definition secure.hh:319
std::string str() const
Returns an explicit std::string copy of the value.
secure_string & operator=(const secure_string &other)
Copy-assigns by allocating a fresh wiped buffer.
bool empty() const noexcept
Returns whether the string is empty.
Definition secure.hh:325
secure_string(const std::string &str)
Constructs a string from a std::string.
secure_string(const secure_string &other)
Copy-constructs by allocating a fresh wiped buffer.
secure_string & operator=(const char *str)
Assigns from a NUL-terminated C string.
Definition secure.hh:286
void assign(const char *str)
Replaces the value with a NUL-terminated C string.
char * end() noexcept
Returns an iterator past the last character.
Definition secure.hh:313
secure_string()
Constructs an empty string.
void assign(const std::string &str)
Replaces the value with a std::string.
secure_string(secure_string &&other) noexcept
Move-constructs, wiping the moved-from string.
const char * end() const noexcept
Returns an iterator past the last character.
Definition secure.hh:316
secure_string & operator=(const std::string &str)
Assigns from a std::string.
Definition secure.hh:292
char * begin() noexcept
Returns an iterator to the first character.
Definition secure.hh:307
Visibility/export macro for the shared library.
#define LIBKEEPASS_API
Marks a class, function or variable as part of the public ABI.
Definition export.hh:43
LIBKEEPASS_API void secure_zero(void *data, std::size_t size) noexcept
Securely erases a memory region by overwriting it with zero bytes.
LIBKEEPASS_API void secure_free(void *data, std::size_t size) noexcept
Erases and releases memory obtained from secure_alloc().
LIBKEEPASS_API void * secure_alloc(std::size_t size) noexcept
Allocates a memory region that is best-effort locked in RAM.