3#if __has_include(<openssl/aes.h>)
4#define MAL_PACKET_WEAVER_HAS_OPENSSL
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>
19#pragma message("WARNING: mal-packet-weaver: OpenSSL wasn't found. mal_packet_weaver::crypto is disabled.")
31 using ByteArray::ByteArray;
32 using ByteArray::operator=;
33 using ByteArray::operator[];
43 using ByteView::ByteView;
44 using ByteView::operator=;
45 using ByteView::operator[];
53#ifdef MAL_PACKET_WEAVER_HAS_OPENSSL
67 static constexpr const char *SHA256_NAME =
"SHA256";
68 static constexpr const char *SHA384_NAME =
"SHA384";
69 static constexpr const char *SHA512_NAME =
"SHA512";
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;
80 Hash(
const ByteArray hash_value,
const HashType hash) : hash_type{ hash }, hash_value{ hash_value } {}
86 [[nodiscard]] uint32_t size()
const {
return static_cast<uint32_t
>(hash_value.size()); }
92 [[nodiscard]]
auto data()
const {
return hash_value.data(); }
98 [[nodiscard]]
auto type()
const {
return hash_type; }
105 template <
typename T>
106 [[nodiscard]]
auto *as()
const
108 return reinterpret_cast<const T *
>(hash_value.data());
115 [[nodiscard]]
const uint8_t *as_uint8()
const {
return as<uint8_t>(); }
117 const HashType hash_type;
118 const ByteArray hash_value;
150#ifdef MAL_PACKET_WEAVER_HAS_OPENSSL
156 template <
typename T>
157 struct OPENSSL_OBJECT_WRAPPER;
165 struct OPENSSL_OBJECT_WRAPPER<EVP_PKEY_CTX>
167 void operator()(EVP_PKEY_CTX *ptr)
const { EVP_PKEY_CTX_free(ptr); }
176 struct OPENSSL_OBJECT_WRAPPER<EVP_PKEY>
178 void operator()(EVP_PKEY *ptr)
const { EVP_PKEY_free(ptr); }
187 struct OPENSSL_OBJECT_WRAPPER<BIO>
189 void operator()(BIO *ptr)
const { BIO_free_all(ptr); }
198 struct OPENSSL_OBJECT_WRAPPER<EVP_CIPHER_CTX>
200 void operator()(EVP_CIPHER_CTX *ptr)
const { EVP_CIPHER_CTX_free(ptr); }
207 using EVP_PKEY_CTX_WRAPPER = std::unique_ptr<EVP_PKEY_CTX, OPENSSL_OBJECT_WRAPPER<EVP_PKEY_CTX>>;
213 using EVP_PKEY_WRAPPER = std::unique_ptr<EVP_PKEY, OPENSSL_OBJECT_WRAPPER<EVP_PKEY>>;
219 using BIO_WRAPPER = std::unique_ptr<BIO, OPENSSL_OBJECT_WRAPPER<BIO>>;
225 using EVP_CIPHER_CTX_WRAPPER = std::unique_ptr<EVP_CIPHER_CTX, OPENSSL_OBJECT_WRAPPER<EVP_CIPHER_CTX>>;
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.