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/safe/clonable.hxx>
47#include <StormByte/safe/optional.hxx>
48#include <StormByte/safe/secure_binary.hxx>
49#include <StormByte/safe/string.hxx>
50
51#include <filesystem>
52#include <string_view>
53
58namespace StormByte {
63 namespace Crypto {
68 namespace KeyPair {
82
87 enum class StorageFormat {
90 };
91
98 using PathView = std::basic_string_view<std::filesystem::path::value_type>;
99
114 public:
123 Generic(const Generic& other);
124
129 Generic(Generic&& other) noexcept;
130
134 virtual ~Generic() noexcept;
135
141 Generic& operator=(const Generic& other);
142
148 Generic& operator=(Generic&& other) noexcept;
150
155 inline enum Type Type() const noexcept {
156 return m_type;
157 }
158
163 inline const StormByte::Safe::String& PublicKey() const noexcept {
164 return m_public_key;
165 }
166
171 inline bool HasPrivateKey() const noexcept {
172 return m_private_key.has_value();
173 }
174
182 return m_private_key;
183 }
184
196
203
211
219 bool Save(PathView directory, std::string_view baseName, StorageFormat format = StorageFormat::PEM) const noexcept;
220
229 template<typename Compatibility = void>
230 STORMBYTE_FORCE_INLINE bool Save(const std::filesystem::path& directory, std::string_view baseName, StorageFormat format = StorageFormat::PEM) const noexcept {
231 return Save(PathView{directory.native()}, baseName, format);
232 }
233
242 bool Save(PathView directory, std::string_view baseName, const Secure::Password& encryptPassword, StorageFormat format = StorageFormat::PEM) const noexcept;
243
253 template<typename Compatibility = void>
254 STORMBYTE_FORCE_INLINE bool Save(const std::filesystem::path& directory, std::string_view baseName, const Secure::Password& encryptPassword, StorageFormat format = StorageFormat::PEM) const noexcept {
255 return Save(PathView{directory.native()}, baseName, encryptPassword, format);
256 }
257
264 bool SavePublic(PathView filePath, StorageFormat format = StorageFormat::PEM) const noexcept;
265
273 template<typename Compatibility = void>
274 STORMBYTE_FORCE_INLINE bool SavePublic(const std::filesystem::path& filePath, StorageFormat format = StorageFormat::PEM) const noexcept {
275 return SavePublic(PathView{filePath.native()}, format);
276 }
277
284 bool SavePrivate(PathView filePath, StorageFormat format = StorageFormat::PEM) const noexcept;
285
293 template<typename Compatibility = void>
294 STORMBYTE_FORCE_INLINE bool SavePrivate(const std::filesystem::path& filePath, StorageFormat format = StorageFormat::PEM) const noexcept {
295 return SavePrivate(PathView{filePath.native()}, format);
296 }
297
305 bool SavePrivate(PathView filePath, const Secure::Password& encryptPassword, StorageFormat format = StorageFormat::PEM) const noexcept;
306
315 template<typename Compatibility = void>
316 STORMBYTE_FORCE_INLINE bool SavePrivate(const std::filesystem::path& filePath, const Secure::Password& encryptPassword, StorageFormat format = StorageFormat::PEM) const noexcept {
317 return SavePrivate(PathView{filePath.native()}, encryptPassword, format);
318 }
319
320
321 protected:
325
332 Generic(enum Type type, StormByte::Safe::String public_key, StormByte::Safe::Optional<Secure::Password> private_key = std::nullopt);
333 };
334
341 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Create(Type type, unsigned short bits) noexcept;
342
349
357
365
374
381 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Load(PathView publicKeyPath, PathView privateKeyPath) noexcept;
382
390 template<typename Compatibility = void>
391 STORMBYTE_FORCE_INLINE Generic::PointerType Load(const std::filesystem::path& publicKeyPath, const std::filesystem::path& privateKeyPath) noexcept {
392 return Load(PathView{publicKeyPath.native()}, PathView{privateKeyPath.native()});
393 }
394
402 STORMBYTE_CRYPTO_PUBLIC Generic::PointerType Load(PathView publicKeyPath, PathView privateKeyPath, const Secure::Password& password) noexcept;
403
412 template<typename Compatibility = void>
413 STORMBYTE_FORCE_INLINE Generic::PointerType Load(const std::filesystem::path& publicKeyPath, const std::filesystem::path& privateKeyPath, const Secure::Password& password) noexcept {
414 return Load(PathView{publicKeyPath.native()}, PathView{privateKeyPath.native()}, password);
415 }
416
423
430 template<typename Compatibility = void>
431 STORMBYTE_FORCE_INLINE Generic::PointerType Load(const std::filesystem::path& path) noexcept {
432 return Load(PathView{path.native()});
433 }
434
442
450 template<typename Compatibility = void>
451 STORMBYTE_FORCE_INLINE Generic::PointerType Load(const std::filesystem::path& path, const Secure::Password& password) noexcept {
452 return Load(PathView{path.native()}, password);
453 }
454 }
455 }
456}
457
DSA keypair.
Definition dsa.hxx:71
ECC keypair.
Definition ecc.hxx:71
ECDH keypair.
Definition ecdh.hxx:71
ECDSA keypair.
Definition ecdsa.hxx:71
Ed25519 keypair.
Definition ed25519.hxx:71
Abstract keypair.
Definition generic.hxx:113
bool SavePublic(const std::filesystem::path &filePath, StorageFormat format=StorageFormat::PEM) const noexcept
Caller-side filesystem path adapter for SavePublic.
Definition generic.hxx:274
bool SavePublic(PathView filePath, StorageFormat format=StorageFormat::PEM) const noexcept
Write only the public key.
enum Type m_type
Algorithm.
Definition generic.hxx:322
Generic(enum Type type, StormByte::Safe::String public_key, StormByte::Safe::Optional< Secure::Password > private_key=std::nullopt)
Construct with algorithm and material.
bool SavePrivate(PathView filePath, const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Write only the private key, encrypted (prefer PEM).
bool SavePrivate(const std::filesystem::path &filePath, StorageFormat format=StorageFormat::PEM) const noexcept
Caller-side filesystem path adapter for unencrypted SavePrivate.
Definition generic.hxx:294
bool Save(PathView directory, std::string_view baseName, const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Write public and private files.
StormByte::Safe::SecureBinary SavePrivate(StorageFormat format=StorageFormat::PEM) const noexcept
Encode the private key in memory, unencrypted.
Generic(Generic &&other) noexcept
Move constructor.
StormByte::Safe::Optional< Secure::Password > m_private_key
Crypto-owned optional private material.
Definition generic.hxx:324
bool Save(const std::filesystem::path &directory, std::string_view baseName, StorageFormat format=StorageFormat::PEM) const noexcept
Caller-side filesystem path adapter for unencrypted Save.
Definition generic.hxx:230
StormByte::Safe::SecureBinary SavePrivate(const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Encode the private key in memory as encrypted PKCS#8 (prefer PEM).
bool SavePrivate(PathView filePath, StorageFormat format=StorageFormat::PEM) const noexcept
Write only the private key, unencrypted.
const StormByte::Safe::Optional< Secure::Password > & PrivateKey() const noexcept
Private key, if any.
Definition generic.hxx:181
bool Save(PathView directory, std::string_view baseName, StorageFormat format=StorageFormat::PEM) const noexcept
Write public and private files under a directory.
bool Save(const std::filesystem::path &directory, std::string_view baseName, const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Caller-side filesystem path adapter for encrypted Save.
Definition generic.hxx:254
bool HasPrivateKey() const noexcept
Whether a private key is stored.
Definition generic.hxx:171
const StormByte::Safe::String & PublicKey() const noexcept
Public key string.
Definition generic.hxx:163
Generic(const Generic &other)
Copy constructor.
virtual ~Generic() noexcept
Destructor.
StormByte::Safe::String m_public_key
Public material.
Definition generic.hxx:323
StormByte::Safe::SecureBinary SavePublic(StorageFormat format=StorageFormat::PEM) const noexcept
Encode the public key in memory.
bool SavePrivate(const std::filesystem::path &filePath, const Secure::Password &encryptPassword, StorageFormat format=StorageFormat::PEM) const noexcept
Caller-side filesystem path adapter for encrypted SavePrivate.
Definition generic.hxx:316
RSA keypair.
Definition rsa.hxx:71
X25519 keypair.
Definition x25519.hxx:71
Shared, wiped container for passwords and raw key material.
Definition password.hxx:108
STORMBYTE_DECLARE_MAYBE_SAFE(class StormByte::System::Device::Access)
Keypairs of the Crypto module.
StorageFormat
On-disk encoding.
Definition generic.hxx:87
@ PEM
OpenSSL PEM.
Definition generic.hxx:88
@ DER
Binary DER.
Definition generic.hxx:89
Generic::PointerType Load(const StormByte::Safe::SecureBinary &data) noexcept
Load from one in-memory key (public, private, or concatenated PEM).
Generic::PointerType Create(Type type, unsigned short bits) noexcept
Generate a keypair.
std::basic_string_view< std::filesystem::path::value_type > PathView
Borrowed native path characters for allocation-free DLL arguments.
Definition generic.hxx:98
Crypto module of the StormByte suite.
Definition aead.hxx:53
Root namespace of the StormByte suite.
#define STORMBYTE_FORCE_INLINE
#define STORMBYTE_CRYPTO_PUBLIC
Definition visibility.h:53