11#include <openssl/evp.h>
12#include <openssl/rand.h>
13#include <openssl/sha.h>
14#include <openssl/x509.h>
25 void md_ctx_destroy(EVP_MD_CTX *);
27 using sha256_t = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>;
29 using aes_t = std::vector<std::uint8_t>;
45 sha256_t
hash(
const std::string_view &plaintext);
47 aes_t gen_aes_key(
const std::array<uint8_t, 16> &salt,
const std::string_view &pin);
48 x509_t x509(
const std::string_view &x);
49 pkey_t pkey(
const std::string_view &k);
50 std::string pem(
x509_t &x509);
51 std::string pem(
pkey_t &pkey);
53 std::vector<uint8_t> sign256(
const pkey_t &pkey,
const std::string_view &data);
54 bool verify256(
const x509_t &x509,
const std::string_view &data,
const std::string_view &signature);
56 creds_t gen_creds(
const std::string_view &cn, std::uint32_t key_bits);
58 std::string_view signature(
const x509_t &x);
60 std::string rand(std::size_t bytes);
61 std::string rand_alphabet(std::size_t bytes,
const std::string_view &alphabet = std::string_view {
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!%&()=-"});
71 const char *
verify(x509_t::element_type *cert);
74 std::vector<std::pair<x509_t, x509_store_t>> _certs;
79 constexpr std::size_t tag_size = 16;
81 constexpr std::size_t round_to_pkcs7_padded(std::size_t size) {
82 return ((size + 15) / 16) * 16;
101 ecb_t(
const aes_t &key,
bool padding =
true);
103 int encrypt(
const std::string_view &plaintext, std::vector<std::uint8_t> &cipher);
104 int decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext);
111 gcm_t &operator=(
gcm_t &&)
noexcept =
default;
113 gcm_t(
const crypto::aes_t &key,
bool padding =
true);
123 int encrypt(
const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv);
133 int encrypt(
const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
135 int decrypt(
const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
142 cbc_t &operator=(
cbc_t &&)
noexcept =
default;
144 cbc_t(
const crypto::aes_t &key,
bool padding =
true);
154 int encrypt(
const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv);
const char * verify(x509_t::element_type *cert)
Verify the certificate chain. When certificates from two or more instances of Moonlight have been add...
Definition crypto.cpp:59
int encrypt(const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv)
Encrypts the plaintext using AES CBC mode. length of cipher must be at least: round_to_pkcs7_padded(p...
Definition crypto.cpp:282
int encrypt(const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv)
Encrypts the plaintext using AES GCM mode.
Definition crypto.cpp:187
sha256_t hash(const std::string_view &plaintext)
Hashes the given plaintext using SHA-256.
Definition crypto.cpp:337
Declarations for utility functions.