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>
47#include <StormByte/safe/binary.hxx>
48#include <StormByte/safe/clonable.hxx>
49#include <StormByte/type_traits.hxx>
50#include <StormByte/type_traits/safe.hxx>
51
52#include <span>
53#include <string_view>
54
59namespace StormByte {
64 namespace Crypto {
69 namespace Signer {
74 enum class Type {
79 };
80
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
136 KeyPair::Generic::PointerType KeyPair() const noexcept {
137 return m_keypair;
138 }
139
150 inline bool Sign(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept {
151 return DoSign(input, output);
152 }
153
161 template<StormByte::Type::ByteInputRange Range>
162 bool Sign(const Range& input, Buffer::WriteOnly& output) const {
163 const StormByte::Safe::Binary data(input);
164 return Sign(data.span(), output);
165 }
166
173 inline bool Sign(const Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
174 return DoSign(const_cast<Buffer::ReadOnly&>(input), output, ReadMode::Copy);
175 }
176
183 inline bool Sign(Buffer::ReadOnly& input, Buffer::WriteOnly& output) const noexcept {
184 return DoSign(input, output, ReadMode::Move);
185 }
186
193 inline Buffer::Consumer Sign(Buffer::Consumer consumer, ReadMode mode = ReadMode::Move) const noexcept {
194 return DoSign(consumer, mode);
195 }
196
197
208 inline bool Verify(std::span<const std::byte> input, std::string_view signature) const noexcept {
209 return DoVerify(input, signature);
210 }
211
219 template<StormByte::Type::ByteInputRange Range>
220 bool Verify(const Range& input, std::string_view signature) const {
221 const StormByte::Safe::Binary data(input);
222 return Verify(data.span(), signature);
223 }
224
231 inline bool Verify(const Buffer::ReadOnly& input, std::string_view signature) const noexcept {
232 return DoVerify(const_cast<Buffer::ReadOnly&>(input), signature, ReadMode::Copy);
233 }
234
241 inline bool Verify(Buffer::ReadOnly& input, std::string_view signature) const noexcept {
242 return DoVerify(input, signature, ReadMode::Move);
243 }
244
252 inline bool Verify(Buffer::Consumer consumer, std::string_view signature, ReadMode mode = ReadMode::Move) const noexcept {
253 return DoVerify(consumer, signature, mode);
254 }
255
256
257 protected:
260
266 inline Generic(enum Type type, KeyPair::Generic::PointerType keypair):
267 m_type(type), m_keypair(keypair) {}
268
274 inline Generic(enum Type type, const KeyPair::Generic& keypair):
275 m_type(type), m_keypair(keypair.Clone()) {}
276
282 inline Generic(enum Type type, KeyPair::Generic&& keypair):
283 m_type(type), m_keypair(keypair.Move()) {}
284
285 private:
293 bool DoSign(Buffer::ReadOnly& input, Buffer::WriteOnly& output, ReadMode mode) const noexcept;
294
301 virtual bool DoSign(std::span<const std::byte> input, Buffer::WriteOnly& output) const noexcept = 0;
302
309 virtual Buffer::Consumer DoSign(Buffer::Consumer consumer, ReadMode mode) const noexcept = 0;
310
318 bool DoVerify(Buffer::ReadOnly& input, std::string_view signature, ReadMode mode) const noexcept;
319
326 virtual bool DoVerify(std::span<const std::byte> input, std::string_view signature) const noexcept = 0;
327
335 virtual bool DoVerify(Buffer::Consumer consumer, std::string_view signature, ReadMode mode) const noexcept = 0;
336 };
337
345
353
361 }
362 }
363}
364
Abstract keypair.
Definition generic.hxx:113
DSA signer.
Definition dsa.hxx:69
ECDSA signer.
Definition ecdsa.hxx:69
Ed25519 signer.
Definition ed25519.hxx:69
Abstract signer.
Definition generic.hxx:94
virtual ~Generic() noexcept
Destructor.
Generic(enum Type type, KeyPair::Generic &&keypair)
Construct by moving a keypair.
Definition generic.hxx:282
bool Verify(const Buffer::ReadOnly &input, std::string_view signature) const noexcept
Verify a ReadOnly buffer (copy).
Definition generic.hxx:231
KeyPair::Generic::PointerType KeyPair() const noexcept
Keypair used to sign and verify.
Definition generic.hxx:136
enum Type m_type
Algorithm.
Definition generic.hxx:258
bool Sign(const Range &input, Buffer::WriteOnly &output) const
Sign an input range of byte-convertible values.
Definition generic.hxx:162
bool Verify(Buffer::Consumer consumer, std::string_view signature, ReadMode mode=ReadMode::Move) const noexcept
Verify a Consumer.
Definition generic.hxx:252
Generic(const Generic &other)=default
Copy constructor.
KeyPair::Generic::PointerType m_keypair
Keypair.
Definition generic.hxx:259
bool Verify(std::span< const std::byte > input, std::string_view signature) const noexcept
Verify a byte span against a signature.
Definition generic.hxx:208
Generic(enum Type type, KeyPair::Generic::PointerType keypair)
Construct from a keypair pointer.
Definition generic.hxx:266
Buffer::Consumer Sign(Buffer::Consumer consumer, ReadMode mode=ReadMode::Move) const noexcept
Sign a Consumer.
Definition generic.hxx:193
bool Sign(const Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Sign a ReadOnly buffer (copy).
Definition generic.hxx:173
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:150
Generic(enum Type type, const KeyPair::Generic &keypair)
Construct by cloning a keypair.
Definition generic.hxx:274
bool Verify(const Range &input, std::string_view signature) const
Verify an input range of byte-convertible values against a signature.
Definition generic.hxx:220
bool Sign(Buffer::ReadOnly &input, Buffer::WriteOnly &output) const noexcept
Sign a ReadOnly buffer (move).
Definition generic.hxx:183
bool Verify(Buffer::ReadOnly &input, std::string_view signature) const noexcept
Verify a ReadOnly buffer (move).
Definition generic.hxx:241
RSA signer.
Definition rsa.hxx:69
STORMBYTE_FORCE_INLINE std::span< std::byte > span() noexcept
virtual PointerType Clone() const =0
virtual PointerType Move()=0
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Signers of the Crypto module.
Generic::PointerType Create(enum Type type, KeyPair::Generic::PointerType keypair) noexcept
Create a signer from a keypair pointer.
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.
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53