StormByte-Crypto 3.1.0
C++26 cryptography module of the StormByte suite
Loading...
Searching...
No Matches
generic.hxx
Go to the documentation of this file.
1/*
2 * Copyright (C) 2024-2026 David C. Manuelda (StormBytePP)
3 *
4 * This file is part of StormByte-Crypto.
5 *
6 * StormByte-Crypto original source is dual-licensed:
7 *
8 * 1. GNU Lesser General Public License v3.0 (or later)
9 * You may redistribute and/or modify this file under the terms of the
10 * GNU Lesser General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option)
12 * any later version.
13 *
14 * 2. Commercial license
15 * Alternatively, this file may be used under the terms of a commercial
16 * license agreement with the copyright holder
17 * (David C. Manuelda <StormByte@gmail.com>).
18 *
19 * Both licenses apply only to original StormByte-Crypto source in this
20 * repository. They do not cover other StormByte modules or any third-party
21 * material shipped with this repository (including everything under
22 * thirdparty/, in particular Crypto++, bundled libbzip2, the StormByte-Buffer
23 * tree and the StormByte suite it vendors), which remains under its own license.
24 *
25 * Neither license grants any patent rights. Any patent licenses required
26 * to use this software or third-party components must be obtained separately
27 * from the patent holders.
28 *
29 * StormByte-Crypto is distributed in the hope that it will be useful,
30 * but WITHOUT ANY WARRANTY; without even the implied warranty of
31 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
32 * GNU Lesser General Public License for more details.
33 *
34 * You should have received a copy of the GNU Lesser General Public License
35 * version 3 along with StormByte-Crypto. If not, see
36 * <https://www.gnu.org/licenses/lgpl-3.0.html>.
37 *
38 * SPDX-License-Identifier: LGPL-3.0-or-later OR LicenseRef-StormByte-Commercial
39 */
40
41#pragma once
42
43#include <StormByte/buffer/consumer.hxx>
46#include <StormByte/safe/binary.hxx>
47#include <StormByte/safe/clonable.hxx>
48#include <StormByte/type_traits.hxx>
49#include <StormByte/type_traits/safe.hxx>
50
51#include <span>
52
57namespace StormByte {
62 namespace Crypto {
67 namespace Crypter {
82
95 public:
104 Generic(const Generic& other) = default;
105
110 Generic(Generic&& other) noexcept = default;
111
115 virtual ~Generic() noexcept;
116
122 Generic& operator=(const Generic& other) = default;
123
129 Generic& operator=(Generic&& other) noexcept = default;
131
142 inline bool Encrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
143 return DoEncrypt(input, output);
144 }
145
153 template<StormByte::Type::ByteInputRange Range>
154 bool Encrypt(const Range& input, Buffer::WriteOnly& output) const {
156 for (const auto value: input) {
157 data.emplace_back(static_cast<std::byte>(value));
158 }
159 return Encrypt(std::span<const std::byte>(data.span()), output);
160 }
161
168 inline bool Encrypt(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
169 return DoEncrypt(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
170 }
171
178 inline bool Encrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
179 return DoEncrypt(input, output, ReadMode::Move);
180 }
181
188 inline Buffer::Consumer Encrypt(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
189 return DoEncrypt(consumer, mode);
190 }
191
192
203 inline bool Decrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
204 return DoDecrypt(input, output);
205 }
206
214 template<StormByte::Type::ByteInputRange Range>
215 bool Decrypt(const Range& input, Buffer::WriteOnly& output) const {
217 for (const auto value: input) {
218 data.emplace_back(static_cast<std::byte>(value));
219 }
220 return Decrypt(std::span<const std::byte>(data.span()), output);
221 }
222
229 inline bool Decrypt(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
230 return DoDecrypt(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
231 }
232
239 inline bool Decrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
240 return DoDecrypt(input, output, ReadMode::Move);
241 }
242
249 inline Buffer::Consumer Decrypt(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
250 return DoDecrypt(consumer, mode);
251 }
252
253
258 inline enum Type Type() const noexcept {
259 return m_type;
260 }
261
262 protected:
264
269 inline Generic(enum Type type):
270 m_type(type) {}
271
278 virtual bool DoEncrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
279
286 virtual Buffer::Consumer DoEncrypt(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
287
294 virtual bool DoDecrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
295
302 virtual Buffer::Consumer DoDecrypt(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
303
304 private:
312 bool DoEncrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
313
321 bool DoDecrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
322 };
323 }
324 }
325}
326
AES-GCM crypter.
Definition aes_gcm.hxx:66
AES-CBC crypter.
Definition aes.hxx:66
Camellia-CBC crypter.
Definition camellia.hxx:66
ChaCha20-Poly1305 crypter.
Definition chachapoly.hxx:66
Elliptic-curve crypter.
Definition ecc.hxx:67
Abstract crypter.
Definition generic.hxx:94
bool Decrypt(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Decrypt a byte span.
Definition generic.hxx:203
virtual Buffer::Consumer DoEncrypt(Buffer::Consumer consumer, ReadMode mode) const noexcept=0
Encrypt a Consumer.
bool Encrypt(const Range &input, Buffer::WriteOnly &output) const
Encrypt an input range of byte-convertible values.
Definition generic.hxx:154
Buffer::Consumer Encrypt(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Encrypt a Consumer into another Consumer.
Definition generic.hxx:188
bool Encrypt(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Encrypt a read-only buffer (copy).
Definition generic.hxx:168
virtual bool DoDecrypt(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept=0
Decrypt a byte span.
bool Decrypt(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decrypt a buffer, consuming it.
Definition generic.hxx:239
bool Decrypt(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decrypt a read-only buffer (copy).
Definition generic.hxx:229
bool Encrypt(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Encrypt a buffer, consuming it.
Definition generic.hxx:178
Generic(const Generic &other)=default
Copy constructor.
enum Type m_type
Cipher.
Definition generic.hxx:263
Generic(Generic &&other) noexcept=default
Move constructor.
enum Type Type() const noexcept
Cipher of this crypter.
Definition generic.hxx:258
Generic(enum Type type)
Construct with a cipher.
Definition generic.hxx:269
Buffer::Consumer Decrypt(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Decrypt a Consumer into another Consumer.
Definition generic.hxx:249
virtual Buffer::Consumer DoDecrypt(Buffer::Consumer consumer, ReadMode mode) const noexcept=0
Decrypt a Consumer.
bool Decrypt(const Range &input, Buffer::WriteOnly &output) const
Decrypt an input range of byte-convertible values.
Definition generic.hxx:215
bool Encrypt(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Encrypt a byte span.
Definition generic.hxx:142
virtual bool DoEncrypt(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept=0
Encrypt a byte span.
virtual ~Generic() noexcept
Destructor.
RSA crypter.
Definition rsa.hxx:67
Serpent-CBC crypter.
Definition serpent.hxx:66
Twofish-CBC crypter.
Definition twofish.hxx:66
reference emplace_back(T &&value)
STORMBYTE_FORCE_INLINE std::span< std::byte > span() noexcept
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Ciphers of the Crypto module.
Crypto module of the StormByte suite.
Definition aead.hxx:53
ReadMode
How a source buffer is consumed.
Definition typedefs.hxx:57
@ Copy
Copy the input.
Definition typedefs.hxx:58
@ Move
Consume the input.
Definition typedefs.hxx:59
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53