libkeepass 0.2.0
KeePass key database importer/exporter library
Loading...
Searching...
No Matches
random.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 "cipher.hh"
27#include "libkeepass/export.hh"
28#include "libkeepass/secure.hh"
29
30#include <memory>
31#include <random>
32#include <vector>
33
34namespace keepass {
35
41public:
43 enum class Type {
44 kSalsa20,
45 kChaCha20
46 };
47
48private:
49 Type type_ = Type::kSalsa20;
50 Salsa20Cipher salsa_cipher_;
51 ChaCha20Cipher chacha_cipher_;
52
53 std::array<uint8_t, 64> buffer_{};
54 std::size_t buffer_pos_ = 64;
55
56 void FillBuffer();
57 void ProcessBytes(uint8_t* data, std::size_t size);
58
59public:
65 RandomObfuscator(const std::array<uint8_t, 32>& key, const std::array<uint8_t, 8>& init_vec);
66
73 RandomObfuscator(Type type, const std::array<uint8_t, 32>& stream_key);
74
79 RandomObfuscator(Type type, const std::vector<uint8_t>& stream_key);
80
83
85
89 std::vector<uint8_t> Process(const std::vector<uint8_t>& data);
90
92
96 std::string Process(const std::string& data);
97
99
104};
105
107
111template <std::size_t N> std::array<uint8_t, N> random_array() {
112 std::random_device rd;
113 std::mt19937 engine(rd());
114
115 std::uniform_int_distribution<int> uniform_dist(0, 255);
116
117 // Fill block with random values.
118 std::array<uint8_t, N> array;
119 for (std::size_t i = 0; i < N; ++i)
120 array[i] = static_cast<uint8_t>(uniform_dist(engine));
121
122 return array;
123}
124
125} // namespace keepass
Block and stream cipher abstractions for KeePass encryption.
ChaCha20 stream cipher implementation (RFC 8439, 32-bit counter).
Definition cipher.hh:326
Definition random.hh:40
~RandomObfuscator()
Wipes the buffered keystream and destroys the underlying ciphers.
RandomObfuscator(const std::array< uint8_t, 32 > &key, const std::array< uint8_t, 8 > &init_vec)
Constructs a random stream cipher from a raw key and initialization vector.
RandomObfuscator(Type type, const std::array< uint8_t, 32 > &stream_key)
std::vector< uint8_t > Process(const std::vector< uint8_t > &data)
Obfuscates or deobfuscates a byte vector using the random stream.
Type
Definition random.hh:43
RandomObfuscator(Type type, const std::vector< uint8_t > &stream_key)
std::string Process(const std::string &data)
Obfuscates or deobfuscates a string using the random stream.
secure_string Process(const secure_string &data)
Obfuscates or deobfuscates a sensitive string using the random stream.
Salsa20 stream cipher implementation.
Definition cipher.hh:276
String that is stored in wiped and best-effort locked memory.
Definition secure.hh:262
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
std::array< uint8_t, N > random_array()
Generates an array of N cryptographically random bytes.
Definition random.hh:111
Secure memory handling: zeroization, locked allocations and sensitive value containers.