Sunshine latest
Self-hosted game stream host for Moonlight.
crypto.h
Go to the documentation of this file.
1
5#pragma once
6
7// standard includes
8#include <array>
9
10// lib includes
11#include <openssl/evp.h>
12#include <openssl/rand.h>
13#include <openssl/sha.h>
14#include <openssl/x509.h>
15
16// local includes
17#include "utility.h"
18
19namespace crypto {
20 struct creds_t {
21 std::string x509;
22 std::string pkey;
23 };
24
25 void md_ctx_destroy(EVP_MD_CTX *);
26
27 using sha256_t = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>;
28
29 using aes_t = std::vector<std::uint8_t>;
39
45 sha256_t hash(const std::string_view &plaintext);
46
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);
52
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);
55
56 creds_t gen_creds(const std::string_view &cn, std::uint32_t key_bits);
57
58 std::string_view signature(const x509_t &x);
59
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!%&()=-"});
62
64 public:
65 KITTY_DECL_CONSTR(cert_chain_t)
66
67 void add(x509_t &&cert);
68
69 void clear();
70
71 const char *verify(x509_t::element_type *cert);
72
73 private:
74 std::vector<std::pair<x509_t, x509_store_t>> _certs;
75 x509_store_ctx_t _cert_ctx;
76 };
77
78 namespace cipher {
79 constexpr std::size_t tag_size = 16;
80
81 constexpr std::size_t round_to_pkcs7_padded(std::size_t size) {
82 return ((size + 15) / 16) * 16;
83 }
84
85 class cipher_t {
86 public:
87 cipher_ctx_t decrypt_ctx;
88 cipher_ctx_t encrypt_ctx;
89
90 aes_t key;
91
92 bool padding;
93 };
94
95 class ecb_t: public cipher_t {
96 public:
97 ecb_t() = default;
98 ecb_t(ecb_t &&) noexcept = default;
99 ecb_t &operator=(ecb_t &&) noexcept = default;
100
101 ecb_t(const aes_t &key, bool padding = true);
102
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);
105 };
106
107 class gcm_t: public cipher_t {
108 public:
109 gcm_t() = default;
110 gcm_t(gcm_t &&) noexcept = default;
111 gcm_t &operator=(gcm_t &&) noexcept = default;
112
113 gcm_t(const crypto::aes_t &key, bool padding = true);
114
123 int encrypt(const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv);
124
133 int encrypt(const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
134
135 int decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
136 };
137
138 class cbc_t: public cipher_t {
139 public:
140 cbc_t() = default;
141 cbc_t(cbc_t &&) noexcept = default;
142 cbc_t &operator=(cbc_t &&) noexcept = default;
143
144 cbc_t(const crypto::aes_t &key, bool padding = true);
145
154 int encrypt(const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv);
155 };
156 } // namespace cipher
157} // namespace crypto
Definition crypto.h:63
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
Definition crypto.h:138
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
Definition crypto.h:85
Definition crypto.h:95
Definition crypto.h:107
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
Definition utility.h:530
sha256_t hash(const std::string_view &plaintext)
Hashes the given plaintext using SHA-256.
Definition crypto.cpp:337
Definition crypto.h:20
Declarations for utility functions.