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
49
50#include <span>
51#include <string_view>
52
57namespace StormByte {
62 namespace Crypto {
67 namespace Signer {
72 enum class Type {
73 DSA,
74 ECDSA,
75 ED25519,
76 RSA
77 };
78
84 public:
93 Generic(const Generic& other) = default;
94
99 Generic(Generic&& other) noexcept = default;
100
104 virtual ~Generic() noexcept;
105
111 Generic& operator=(const Generic& other) = default;
112
118 Generic& operator=(Generic&& other) noexcept = default;
125 KeyPair::Generic::PointerType KeyPair() const noexcept {
126 return m_keypair;
127 }
128
139 inline bool Sign(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
140 return DoSign(input, output);
141 }
142
150 template<StormByte::Type::ByteInputRange Range>
151 bool Sign(const Range& input, Buffer::WriteOnly& output) const {
153 for (const auto value: input) {
154 data.emplace_back(static_cast<std::byte>(value));
155 }
156 return Sign(std::span<const std::byte>(data), output);
157 }
158
165 inline bool Sign(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
166 return DoSign(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
167 }
168
175 inline bool Sign(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
176 return DoSign(input, output, ReadMode::Move);
177 }
178
185 inline Buffer::Consumer Sign(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
186 return DoSign(consumer, mode);
187 }
200 inline bool Verify(std::span<const std::byte> input, std::string_view signature) const noexcept {
201 return DoVerify(input, signature);
202 }
203
211 template<StormByte::Type::ByteInputRange Range>
212 bool Verify(const Range& input, std::string_view signature) const {
214 for (const auto value: input) {
215 data.emplace_back(static_cast<std::byte>(value));
216 }
217 return Verify(std::span<const std::byte>(data), signature);
218 }
219
226 inline bool Verify(const Buffer::ReadOnly& input, std::string_view signature) const noexcept {
227 return DoVerify(const_cast<Buffer::ReadOnly&>(input), signature, ReadMode::Copy);
228 }
229
236 inline bool Verify(Buffer::ReadOnly& input, std::string_view signature) const noexcept {
237 return DoVerify(input, signature, ReadMode::Move);
238 }
239
247 inline bool Verify(Buffer::Consumer consumer, std::string_view signature, ReadMode mode = ReadMode::Move) const noexcept {
248 return DoVerify(consumer, signature, mode);
249 }
252 protected:
255
261 inline Generic(enum Type type, KeyPair::Generic::PointerType keypair):
262 m_type(type), m_keypair(keypair) {}
263
269 inline Generic(enum Type type, const KeyPair::Generic& keypair):
270 m_type(type), m_keypair(keypair.Clone()) {}
271
277 inline Generic(enum Type type, KeyPair::Generic&& keypair):
278 m_type(type), m_keypair(keypair.Move()) {}
279
280 private:
288 bool DoSign(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
289
296 virtual bool DoSign(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
297
304 virtual Buffer::Consumer DoSign(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
305
313 bool DoVerify(Buffer::ReadOnly& input, std::string_view signature, ReadMode mode) const noexcept;
314
321 virtual bool DoVerify(std::span<const std::byte> input, std::string_view signature) const noexcept = 0;
322
330 virtual bool DoVerify(Buffer::Consumer consumer, std::string_view signature, ReadMode mode) const noexcept = 0;
331 };
332
340
348
356 }
357 }
358}
reference emplace_back(T &&value)
Abstract keypair.
Definition generic.hxx:99
DSA signer.
Definition dsa.hxx:67
ECDSA signer.
Definition ecdsa.hxx:67
Ed25519 signer.
Definition ed25519.hxx:67
Abstract signer.
Definition generic.hxx:83
virtual ~Generic() noexcept
Destructor.
Generic(enum Type type, KeyPair::Generic &&keypair)
Construct by moving a keypair.
Definition generic.hxx:277
bool Verify(const Buffer::ReadOnly &input, std::string_view signature) const noexcept
Verify a ReadOnly buffer (copy).
Definition generic.hxx:226
enum Type m_type
Algorithm.
Definition generic.hxx:253
bool Sign(const Range &input, Buffer::WriteOnly &output) const
Sign an input range of byte-convertible values.
Definition generic.hxx:151
bool Verify(Buffer::Consumer consumer, std::string_view signature, ReadMode mode=ReadMode::Move) const noexcept
Verify a Consumer.
Definition generic.hxx:247
Generic(const Generic &other)=default
Copy constructor.
KeyPair::Generic::PointerType m_keypair
Keypair.
Definition generic.hxx:254
bool Verify(std::span< const std::byte > input, std::string_view signature) const noexcept
Verify a byte span against a signature.
Definition generic.hxx:200
Generic(enum Type type, KeyPair::Generic::PointerType keypair)
Construct from a keypair pointer.
Definition generic.hxx:261
Buffer::Consumer Sign(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Sign a Consumer.
Definition generic.hxx:185
bool Sign(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Sign a ReadOnly buffer (copy).
Definition generic.hxx:165
Generic(Generic &&other) noexcept=default
Move constructor.
bool Sign(std::span< const std::byte > input, Buffer::WriteOnly &output) const noexcept
Sign a byte span.
Definition generic.hxx:139
Generic(enum Type type, const KeyPair::Generic &keypair)
Construct by cloning a keypair.
Definition generic.hxx:269
bool Verify(const Range &input, std::string_view signature) const
Verify an input range of byte-convertible values against a signature.
Definition generic.hxx:212
bool Sign(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Sign a ReadOnly buffer (move).
Definition generic.hxx:175
bool Verify(Buffer::ReadOnly &input, std::string_view signature) const noexcept
Verify a ReadOnly buffer (move).
Definition generic.hxx:236
RSA signer.
Definition rsa.hxx:67
Generic::PointerType Create(enum Type type, KeyPair::Generic::PointerType keypair) noexcept
Create a signer from a keypair pointer.
ReadMode
How a source buffer is consumed.
Definition typedefs.hxx:57
@ Move
Consume the input.
Root namespace of the StormByte suite.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53