libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
secure_io.hh
Go to the documentation of this file.
1/*
2 * libkeepass - KeePass key database importer/exporter
3 * Copyright (C) 2014 Christian Kindahl
4 * Copyright (C) 2024 Dominik Krümpelmann
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program. If not, see <http://www.gnu.org/licenses/>.
18 */
19
25#pragma once
26#include <cstddef>
27#include <sstream>
28
29#include "libkeepass/secure.hh"
30
31namespace keepass {
32namespace detail {
33
37inline void WipeStream(std::stringstream& stream) {
38 std::streambuf* buffer = stream.rdbuf();
39 std::streamsize size =
40 buffer->pubseekoff(0, std::ios_base::end, std::ios_base::in | std::ios_base::out);
41 buffer->pubseekoff(0, std::ios_base::beg, std::ios_base::in | std::ios_base::out);
42
43 static constexpr std::streamsize kChunkSize = 4096;
44 char zeros[kChunkSize] = {};
45 while (size > 0) {
46 std::streamsize chunk = size < kChunkSize ? size : kChunkSize;
47 if (buffer->sputn(zeros, chunk) != chunk)
48 return;
49 size -= chunk;
50 }
51}
52
57template <typename Container> inline void WipeBuffer(Container* buffer) {
58 if (buffer != nullptr && !buffer->empty()) {
59 secure_zero(const_cast<typename Container::value_type*>(buffer->data()),
60 buffer->size() * sizeof(typename Container::value_type));
61 }
62}
63
64} // namespace detail
65} // namespace keepass
Secure memory handling: zeroization, locked allocations and sensitive value containers.
LIBKEEPASS_API void secure_zero(void *data, std::size_t size) noexcept
Securely erases a memory region by overwriting it with zero bytes.
void WipeStream(std::stringstream &stream)
Definition secure_io.hh:37
void WipeBuffer(Container *buffer)
Definition secure_io.hh:57