mal-packet-weaver
C++20 packet serialization/deserialization library.
Loading...
Searching...
No Matches
crypto-common.hpp
Go to the documentation of this file.
1#pragma once
2#include "../../common.hpp"
3#if __has_include(<openssl/aes.h>)
4#define MAL_PACKET_WEAVER_HAS_OPENSSL
5#endif
6
7#ifdef MAL_PACKET_WEAVER_HAS_OPENSSL
8#include <openssl/aes.h>
9#include <openssl/bio.h>
10#include <openssl/dh.h>
11#include <openssl/ec.h>
12#include <openssl/ecdh.h>
13#include <openssl/ecdsa.h>
14#include <openssl/evp.h>
15#include <openssl/kdf.h>
16#include <openssl/pem.h>
17#include <openssl/sha.h>
18#else
19#pragma message("WARNING: mal-packet-weaver: OpenSSL wasn't found. mal_packet_weaver::crypto is disabled.")
20#endif
21
23{
28 class Key : public ByteArray
29 {
30 public:
31 using ByteArray::ByteArray;
32 using ByteArray::operator=;
33 using ByteArray::operator[];
34 };
35
40 class KeyView : public ByteView
41 {
42 public:
43 using ByteView::ByteView;
44 using ByteView::operator=;
45 using ByteView::operator[];
46 };
47
53#ifdef MAL_PACKET_WEAVER_HAS_OPENSSL
54 struct Hash
55 {
60 enum class HashType
61 {
62 SHA256,
63 SHA384,
64 SHA512
65 };
66
67 static constexpr const char *SHA256_NAME = "SHA256";
68 static constexpr const char *SHA384_NAME = "SHA384";
69 static constexpr const char *SHA512_NAME = "SHA512";
70
71 static constexpr uint32_t SHA256_SIZE = SHA256_DIGEST_LENGTH;
72 static constexpr uint32_t SHA384_SIZE = SHA384_DIGEST_LENGTH;
73 static constexpr uint32_t SHA512_SIZE = SHA512_DIGEST_LENGTH;
74
80 Hash(const ByteArray hash_value, const HashType hash) : hash_type{ hash }, hash_value{ hash_value } {}
81
86 [[nodiscard]] uint32_t size() const { return static_cast<uint32_t>(hash_value.size()); }
87
92 [[nodiscard]] auto data() const { return hash_value.data(); }
93
98 [[nodiscard]] auto type() const { return hash_type; }
99
105 template <typename T>
106 [[nodiscard]] auto *as() const
107 {
108 return reinterpret_cast<const T *>(hash_value.data());
109 }
110
115 [[nodiscard]] const uint8_t *as_uint8() const { return as<uint8_t>(); }
116
117 const HashType hash_type;
118 const ByteArray hash_value;
119 };
120#endif
125 struct KeyPair
126 {
133
138 [[nodiscard]] auto get_public_key_view() const { return KeyView{ public_key.data(), public_key.size() }; }
139
144 [[nodiscard]] auto get_private_key_view() const { return KeyView{ private_key.data(), private_key.size() }; }
145
148 };
149
150#ifdef MAL_PACKET_WEAVER_HAS_OPENSSL
156 template <typename T>
157 struct OPENSSL_OBJECT_WRAPPER;
158
164 template <>
165 struct OPENSSL_OBJECT_WRAPPER<EVP_PKEY_CTX>
166 {
167 void operator()(EVP_PKEY_CTX *ptr) const { EVP_PKEY_CTX_free(ptr); }
168 };
169
175 template <>
176 struct OPENSSL_OBJECT_WRAPPER<EVP_PKEY>
177 {
178 void operator()(EVP_PKEY *ptr) const { EVP_PKEY_free(ptr); }
179 };
180
186 template <>
187 struct OPENSSL_OBJECT_WRAPPER<BIO>
188 {
189 void operator()(BIO *ptr) const { BIO_free_all(ptr); }
190 };
191
197 template <>
198 struct OPENSSL_OBJECT_WRAPPER<EVP_CIPHER_CTX>
199 {
200 void operator()(EVP_CIPHER_CTX *ptr) const { EVP_CIPHER_CTX_free(ptr); }
201 };
202
207 using EVP_PKEY_CTX_WRAPPER = std::unique_ptr<EVP_PKEY_CTX, OPENSSL_OBJECT_WRAPPER<EVP_PKEY_CTX>>;
208
213 using EVP_PKEY_WRAPPER = std::unique_ptr<EVP_PKEY, OPENSSL_OBJECT_WRAPPER<EVP_PKEY>>;
214
219 using BIO_WRAPPER = std::unique_ptr<BIO, OPENSSL_OBJECT_WRAPPER<BIO>>;
220
225 using EVP_CIPHER_CTX_WRAPPER = std::unique_ptr<EVP_CIPHER_CTX, OPENSSL_OBJECT_WRAPPER<EVP_CIPHER_CTX>>;
226#endif
227} // namespace mal_packet_weaver::crypto
Represents a cryptographic key as a byte array.
Represents a view of a cryptographic key as a byte view.
Represents a cryptographic hash value along with its type.
Represents a pair of cryptographic keys (public and private keys).
auto get_public_key_view() const
Returns a view of the public key.
auto get_private_key_view() const
Returns a view of the private key.
KeyPair(const Key private_key, const Key public_key)
Constructs a KeyPair object with the given private and public keys.
A dynamically resizable array of bytes.
A lightweight view over a sequence of bytes.