Sunshine v2025.118.151840
Self-hosted game stream host for Moonlight.
crypto.h
Go to the documentation of this file.
1
5#pragma once
6
7#include <array>
8#include <openssl/evp.h>
9#include <openssl/rand.h>
10#include <openssl/sha.h>
11#include <openssl/x509.h>
12
13#include "utility.h"
14
15namespace crypto {
16 struct creds_t {
17 std::string x509;
18 std::string pkey;
19 };
20
21 void
22 md_ctx_destroy(EVP_MD_CTX *);
23
24 using sha256_t = std::array<std::uint8_t, SHA256_DIGEST_LENGTH>;
25
26 using aes_t = std::vector<std::uint8_t>;
36
42 sha256_t
43 hash(const std::string_view &plaintext);
44
45 aes_t
46 gen_aes_key(const std::array<uint8_t, 16> &salt, const std::string_view &pin);
47
48 x509_t
49 x509(const std::string_view &x);
50 pkey_t
51 pkey(const std::string_view &k);
52 std::string
53 pem(x509_t &x509);
54 std::string
55 pem(pkey_t &pkey);
56
57 std::vector<uint8_t>
58 sign256(const pkey_t &pkey, const std::string_view &data);
59 bool
60 verify256(const x509_t &x509, const std::string_view &data, const std::string_view &signature);
61
63 gen_creds(const std::string_view &cn, std::uint32_t key_bits);
64
65 std::string_view
66 signature(const x509_t &x);
67
68 std::string
69 rand(std::size_t bytes);
70 std::string
71 rand_alphabet(std::size_t bytes,
72 const std::string_view &alphabet = std::string_view { "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!%&()=-" });
73
75 public:
76 KITTY_DECL_CONSTR(cert_chain_t)
77
78 void
79 add(x509_t &&cert);
80
81 void
82 clear();
83
84 const char *
85 verify(x509_t::element_type *cert);
86
87 private:
88 std::vector<std::pair<x509_t, x509_store_t>> _certs;
89 x509_store_ctx_t _cert_ctx;
90 };
91
92 namespace cipher {
93 constexpr std::size_t tag_size = 16;
94 constexpr std::size_t
95 round_to_pkcs7_padded(std::size_t size) {
96 return ((size + 15) / 16) * 16;
97 }
98
99 class cipher_t {
100 public:
101 cipher_ctx_t decrypt_ctx;
102 cipher_ctx_t encrypt_ctx;
103
104 aes_t key;
105
106 bool padding;
107 };
108
109 class ecb_t: public cipher_t {
110 public:
111 ecb_t() = default;
112 ecb_t(ecb_t &&) noexcept = default;
113 ecb_t &
114 operator=(ecb_t &&) noexcept = default;
115
116 ecb_t(const aes_t &key, bool padding = true);
117
118 int
119 encrypt(const std::string_view &plaintext, std::vector<std::uint8_t> &cipher);
120 int
121 decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext);
122 };
123
124 class gcm_t: public cipher_t {
125 public:
126 gcm_t() = default;
127 gcm_t(gcm_t &&) noexcept = default;
128 gcm_t &
129 operator=(gcm_t &&) noexcept = default;
130
131 gcm_t(const crypto::aes_t &key, bool padding = true);
132
141 int
142 encrypt(const std::string_view &plaintext, std::uint8_t *tag, std::uint8_t *ciphertext, aes_t *iv);
143
152 int
153 encrypt(const std::string_view &plaintext, std::uint8_t *tagged_cipher, aes_t *iv);
154
155 int
156 decrypt(const std::string_view &cipher, std::vector<std::uint8_t> &plaintext, aes_t *iv);
157 };
158
159 class cbc_t: public cipher_t {
160 public:
161 cbc_t() = default;
162 cbc_t(cbc_t &&) noexcept = default;
163 cbc_t &
164 operator=(cbc_t &&) noexcept = default;
165
166 cbc_t(const crypto::aes_t &key, bool padding = true);
167
176 int
177 encrypt(const std::string_view &plaintext, std::uint8_t *cipher, aes_t *iv);
178 };
179 } // namespace cipher
180} // namespace crypto
Definition crypto.h:74
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:56
Definition crypto.h:159
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:288
Definition crypto.h:99
Definition crypto.h:109
Definition crypto.h:124
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:189
Definition utility.h:496
sha256_t hash(const std::string_view &plaintext)
Hashes the given plaintext using SHA-256.
Definition crypto.cpp:342
Definition crypto.h:16
Declarations for utility functions.