40template <std::
size_t N>
class Cipher;
51template <std::
size_t N>
54 std::array<uint8_t, N>& buffer_;
57 std::streampos seekoff(std::streamoff off, std::ios_base::seekdir way,
58 std::ios_base::openmode which)
override {
60 return {std::streamoff(-1)};
62 off = clamp<std::streamoff>(0,
static_cast<long long>(buffer_.size()), off);
64 std::streamoff lin_off = 0;
66 case std::ios_base::beg:
67 lin_off = clamp<std::streamoff>(0,
static_cast<long long>(buffer_.size()), off);
69 case std::ios_base::cur:
70 lin_off = clamp<std::streamoff>(0,
static_cast<long long>(buffer_.size()),
71 (gptr() - eback()) + off);
73 case std::ios_base::end:
74 lin_off = clamp<std::streamoff>(0,
static_cast<std::streamoff
>(buffer_.size()),
75 static_cast<std::streamoff
>(buffer_.size()) - off);
82 if (which & std::ios_base::in) {
83 char* buffer_ptr =
reinterpret_cast<char*
>(buffer_.data());
84 setg(buffer_ptr, buffer_ptr + lin_off, buffer_ptr + buffer_.size());
90 std::streampos seekpos(std::streampos sp, std::ios_base::openmode which)
override {
91 if (which == 0 || sp < 0 || sp >=
static_cast<std::streamoff
>(buffer_.size())) {
92 return {std::streamoff(-1)};
96 if (which & std::ios_base::in) {
97 char* buffer_ptr =
reinterpret_cast<char*
>(buffer_.data());
98 setg(buffer_ptr, buffer_ptr + sp, buffer_ptr + buffer_.size());
111 char* buffer_ptr =
reinterpret_cast<char*
>(buffer.data());
112 setg(buffer_ptr, buffer_ptr, buffer_ptr + buffer.size());
113 setp(buffer_ptr, buffer_ptr + buffer.size());
129 uint32_t block_index{};
130 std::array<uint8_t, 32> block_hash{};
131 uint32_t block_size = 0;
134 uint32_t block_index_ = 0;
135 std::vector<char> block_;
154 public std::basic_streambuf<char, std::char_traits<char>> {
160 uint64_t decoded_bytes_ = 0;
165 bool budget_exceeded_ =
false;
185 : src_(src), limits_(limits) {}
200 public std::basic_streambuf<char, std::char_traits<char>> {
202 static constexpr uint32_t kDefaultBlockSize = 1024 * 1024;
205 const uint32_t block_size_;
224 hashed_ostreambuf(std::ostream& dst, uint32_t block_size) : dst_(dst), block_size_(block_size) {}
241 :
public std::basic_streambuf<char, std::char_traits<char>> {
244 std::array<uint8_t, 64> hmac_key_ = {{0}};
247 uint64_t block_index_ = 0;
248 std::vector<char> block_;
251 uint64_t decoded_bytes_ = 0;
254 bool budget_exceeded_ =
false;
257 std::array<uint8_t, 64> GetCurrentHmacKey()
const;
267 if (hmac_key !=
nullptr)
268 std::memcpy(hmac_key_.data(), hmac_key, hmac_key_.size());
282 : src_(src), limits_(limits) {
283 if (hmac_key !=
nullptr)
284 std::memcpy(hmac_key_.data(), hmac_key, hmac_key_.size());
302 :
public std::basic_streambuf<char, std::char_traits<char>> {
304 static constexpr uint32_t kDefaultBlockSize = 1024 * 1024;
307 std::array<uint8_t, 64> hmac_key_ = {{0}};
308 const uint32_t block_size_;
310 uint64_t block_index_ = 0;
311 std::vector<char> block_;
314 std::array<uint8_t, 64> GetCurrentHmacKey()
const;
327 : dst_(dst), block_size_(kDefaultBlockSize) {
328 if (hmac_key !=
nullptr)
329 std::memcpy(hmac_key_.data(), hmac_key, hmac_key_.size());
340 : dst_(dst), block_size_(block_size) {
341 if (hmac_key !=
nullptr)
342 std::memcpy(hmac_key_.data(), hmac_key, hmac_key_.size());
362 :
public std::basic_streambuf<char, std::char_traits<char>> {
364 static const std::size_t kBufferSize = 16384;
368 z_stream z_stream_{};
371 std::array<char, kBufferSize> input_ = {{0}};
373 std::array<char, kBufferSize> output_ = {{0}};
376 uint64_t decoded_bytes_ = 0;
379 bool budget_exceeded_ =
false;
401 : src_(src), limits_(limits) {
422 :
public std::basic_streambuf<char, std::char_traits<char>> {
424 static const std::size_t kBufferSize = 16384;
427 z_stream z_stream_{};
429 std::vector<char> buffer_;
437 bool WriteOutput(
bool flush);
469 :
public std::basic_streambuf<char, std::char_traits<char>> {
471 static constexpr std::size_t kChaChaBlockSize = 64;
477 std::array<uint8_t, 64> pending_{};
478 std::size_t pending_n_ = 0;
479 std::array<uint8_t, 16> prev_block_{};
480 bool finalized_ =
false;
509 std::streamsize
xsputn(
const char* s, std::streamsize count)
override;
ChaCha20 stream cipher implementation (RFC 8439, 32-bit counter).
Definition cipher.hh:326
Abstract base class for 16-byte block ciphers.
Definition cipher.hh:113
A std::streambuf backed by a fixed-size std::array.
Definition stream.hh:52
array_iostreambuf(std::array< uint8_t, N > &buffer)
Constructs a streambuf over the given array.
Definition stream.hh:110
Input streambuf that encrypts on the fly (streaming counterpart of the whole-buffer encrypt_cbc / Cha...
Definition stream.hh:469
~encrypt_ostreambuf() override
Zeroizes the transient plaintext and chaining state.
std::streamsize xsputn(const char *s, std::streamsize count) override
Writes several characters, applying cipher respectively.
encrypt_ostreambuf(std::ostream &dst, ChaCha20Cipher &cipher)
Constructs a ChaCha20 encrypting streambuf.
int overflow(int c) override
Buffers a character, encrypting one block when it reaches capacity.
encrypt_ostreambuf(std::ostream &dst, Cipher< 16 > &cipher)
Constructs a CBC-mode encrypting streambuf.
int sync() override
Finalizes the stream (PKCS #7 padding for CBC, partial block for ChaCha20).
Input streambuf that decompresses gzip-compressed data on the fly.
Definition stream.hh:362
int underflow() override
Inflates compressed data into the output buffer when the get area is exhausted.
~gzip_istreambuf() override
Destroys the streambuf and releases the zlib inflate context.
gzip_istreambuf(std::istream &src, const detail::ResourceLimits &limits)
Constructs a gzip-decompressing streambuf with resource limits.
Definition stream.hh:400
gzip_istreambuf(std::istream &src)
Constructs a gzip-decompressing streambuf.
Definition stream.hh:391
bool BudgetExceeded() const
Returns true if the decompressed size budget was exceeded while reading.
Definition stream.hh:409
Output streambuf that compresses data with gzip on the fly.
Definition stream.hh:422
int overflow(int c) override
Buffers a character; deflates and writes the buffer when it reaches capacity.
gzip_ostreambuf(std::ostream &dst)
Constructs a gzip-compressing streambuf.
~gzip_ostreambuf() override
Destroys the streambuf, finalizing the gzip stream and releasing the zlib context.
int sync() override
Flushes any buffered data and finalizes the gzip stream.
Base class providing SHA-256 hashing for block-oriented stream buffers.
Definition stream.hh:123
virtual ~hashed_basic_streambuf()
Destroys the streambuf, zeroizing any buffered block bytes.
std::array< uint8_t, 32 > GetBlockHash() const
Computes the SHA-256 hash of the current block.
Input streambuf that reads SHA-256-hashed, block-structured data.
Definition stream.hh:154
bool BudgetExceeded() const
Returns true if a resource budget was exceeded while reading.
Definition stream.hh:176
int underflow() override
Reads and verifies the next block when the get area is exhausted.
hashed_istreambuf(std::istream &src, const detail::ResourceLimits &limits)
Constructs an input streambuf with explicit resource limits.
Definition stream.hh:184
hashed_istreambuf(std::istream &src)
Constructs an input streambuf that reads from the given stream.
Definition stream.hh:173
Output streambuf that writes SHA-256-hashed, block-structured data.
Definition stream.hh:200
hashed_ostreambuf(std::ostream &dst, uint32_t block_size)
Constructs an output streambuf with a custom block size.
Definition stream.hh:224
int overflow(int c) override
Buffers a character; flushes the current block when it reaches capacity.
hashed_ostreambuf(std::ostream &dst)
Constructs an output streambuf with the default 1 MiB block size.
Definition stream.hh:216
int sync() override
Flushes any buffered data and writes the trailing empty end-of-stream block.
Input streambuf that reads HMAC-verified, block-structured data (KDBX4 format).
Definition stream.hh:241
hmac_istreambuf(std::istream &src, const uint8_t *hmac_key)
Constructs an HMAC-verifying input streambuf.
Definition stream.hh:266
~hmac_istreambuf()
Zeroizes the stored master HMAC key.
int underflow() override
Reads and HMAC-verifies the next block when the get area is exhausted.
bool BudgetExceeded() const
Returns true if a resource budget was exceeded while reading.
Definition stream.hh:272
hmac_istreambuf(std::istream &src, const uint8_t *hmac_key, const detail::ResourceLimits &limits)
Constructs an HMAC-verifying input streambuf with resource limits.
Definition stream.hh:281
Output streambuf that writes HMAC-authenticated, block-structured data (KDBX4 format).
Definition stream.hh:302
hmac_ostreambuf(std::ostream &dst, const uint8_t *hmac_key)
Constructs an HMAC-signing output streambuf with the default 1 MiB block size.
Definition stream.hh:326
int sync() override
Flushes any buffered data and writes the trailing empty end-of-stream block.
int overflow(int c) override
Buffers a character; flushes the current block when it reaches capacity.
~hmac_ostreambuf()
Zeroizes the stored master HMAC key.
hmac_ostreambuf(std::ostream &dst, const uint8_t *hmac_key, uint32_t block_size)
Constructs an HMAC-signing output streambuf with a custom block size.
Definition stream.hh:339
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
Resource budgets enforced while importing (and parsing) databases.
Configurable resource budgets for database import and XML parsing.
Definition limits.hh:45
Utility functions for clamp, comparison, traversal, and time formatting.