StormByte-Crypto 2.0.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
48
49#include <span>
50
55namespace StormByte {
60 namespace Crypto {
65 namespace Crypter {
70 enum class Type {
71 AES_GCM,
72 AES,
73 Camellia,
75 ECC,
76 Serpent,
77 RSA,
78 TwoFish
79 };
80
86 public:
95 Generic(const Generic& other) = default;
96
101 Generic(Generic&& other) noexcept = default;
102
106 virtual ~Generic() noexcept;
107
113 Generic& operator=(const Generic& other) = default;
114
120 Generic& operator=(Generic&& other) noexcept = default;
133 inline bool Encrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
134 return DoEncrypt(input, output);
135 }
136
144 template<StormByte::Type::ByteInputRange Range>
145 bool Encrypt(const Range& input, Buffer::WriteOnly& output) const {
147 for (const auto value: input) {
148 data.emplace_back(static_cast<std::byte>(value));
149 }
150 return Encrypt(std::span<const std::byte>(data), output);
151 }
152
159 inline bool Encrypt(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
160 return DoEncrypt(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
161 }
162
169 inline bool Encrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
170 return DoEncrypt(input, output, ReadMode::Move);
171 }
172
179 inline Buffer::Consumer Encrypt(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
180 return DoEncrypt(consumer, mode);
181 }
194 inline bool Decrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
195 return DoDecrypt(input, output);
196 }
197
205 template<StormByte::Type::ByteInputRange Range>
206 bool Decrypt(const Range& input, Buffer::WriteOnly& output) const {
208 for (const auto value: input) {
209 data.emplace_back(static_cast<std::byte>(value));
210 }
211 return Decrypt(std::span<const std::byte>(data), output);
212 }
213
220 inline bool Decrypt(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
221 return DoDecrypt(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
222 }
223
230 inline bool Decrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
231 return DoDecrypt(input, output, ReadMode::Move);
232 }
233
240 inline Buffer::Consumer Decrypt(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
241 return DoDecrypt(consumer, mode);
242 }
249 inline enum Type Type() const noexcept {
250 return m_type;
251 }
252
253 protected:
255
260 inline Generic(enum Type type):
261 m_type(type) {}
262
269 virtual bool DoEncrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
270
277 virtual Buffer::Consumer DoEncrypt(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
278
285 virtual bool DoDecrypt(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
286
293 virtual Buffer::Consumer DoDecrypt(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
294
295 private:
303 bool DoEncrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
304
312 bool DoDecrypt(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
313 };
314 }
315 }
316}
reference emplace_back(T &&value)
AES-GCM crypter.
Definition aes_gcm.hxx:64
AES-CBC crypter.
Definition aes.hxx:64
Camellia-CBC crypter.
Definition camellia.hxx:64
ChaCha20-Poly1305 crypter.
Definition chachapoly.hxx:64
Elliptic-curve crypter.
Definition ecc.hxx:65
Abstract crypter.
Definition generic.hxx:85
bool Decrypt(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Decrypt a byte span.
Definition generic.hxx:194
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:145
Buffer::Consumer Encrypt(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Encrypt a Consumer into another Consumer.
Definition generic.hxx:179
bool Encrypt(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Encrypt a read-only buffer (copy).
Definition generic.hxx:159
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:230
bool Decrypt(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Decrypt a read-only buffer (copy).
Definition generic.hxx:220
bool Encrypt(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Encrypt a buffer, consuming it.
Definition generic.hxx:169
Generic(const Generic &other)=default
Copy constructor.
enum Type m_type
Cipher.
Definition generic.hxx:254
Generic(Generic &&other) noexcept=default
Move constructor.
enum Type Type() const noexcept
Cipher of this crypter.
Definition generic.hxx:249
Generic(enum Type type)
Construct with a cipher.
Definition generic.hxx:260
Buffer::Consumer Decrypt(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Decrypt a Consumer into another Consumer.
Definition generic.hxx:240
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:206
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:65
Serpent-CBC crypter.
Definition serpent.hxx:64
Twofish-CBC crypter.
Definition twofish.hxx:64
ReadMode
How a source buffer is consumed.
Definition typedefs.hxx:57
Root namespace of the StormByte suite.
STL namespace.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53