Mineserver
A rewrite of Minecraft 1.8.9 in C++ !
Loading...
Searching...
No Matches
crypto.h
Go to the documentation of this file.
1
12#ifndef MINESERVER_CRYPTO_H
13#define MINESERVER_CRYPTO_H
14
15#include <memory>
16#include <string>
17#include <cstddef>
18#include <openssl/rsa.h>
19#include <openssl/evp.h>
20#include <zlib.h>
21
28namespace crypto
29{
37 constexpr int RSA_KEY_LENGTH = 1024;
38
47 bool init();
55 void cleanup();
56
70 std::unique_ptr<std::byte[]> rsaEncrypt(const std::byte *data, size_t len, size_t *outLen);
84 std::unique_ptr<std::byte[]> rsaDecrypt(const std::byte *data, size_t len, size_t *outLen);
95 std::unique_ptr<std::byte[]> getPublicRSAKey(int *outLen);
96
106 std::unique_ptr<std::byte[]> randomSecure(size_t len);
107
117 std::string md5Digest(const std::string &data);
118
129 {
130 private:
131 EVP_MD_CTX *ctx;
132
133 public:
147
154 void update(const std::string &s);
162 std::string finalize();
163 };
164
191
205 {
206 private:
207 EVP_CIPHER_CTX *ctx;
208 const CipherState state;
209
210 public:
221 AES128CFB8Cipher(CipherState state, const std::byte *key, const std::byte *iv);
229
242 int update(const std::byte *data, size_t len, std::byte *out);
251 int finalize(std::byte *out);
260 size_t calculateBufferSize(size_t len);
261 };
262
271 {
272 private:
273 int compressionLevel;
274
275 public:
290 ~ZLibCompressor() = default;
291
305 int compress(const std::byte *data, size_t len, std::byte *out, size_t outLen);
306
325 int uncompress(const std::byte *data, size_t len, std::byte *out, size_t outLen);
326 };
327}
328
329#endif
AES/CFB8 128bit cipher class.
Definition crypto.h:205
int update(const std::byte *data, size_t len, std::byte *out)
Updates, encrypting or decrypting depending on the state.
Definition crypto.cpp:217
int finalize(std::byte *out)
Finalizes cipher, encrypting / decrypting the rest of the bytes.
Definition crypto.cpp:228
~AES128CFB8Cipher()
Destroy the AES/CFB8 128bit cipher object.
Definition crypto.cpp:212
size_t calculateBufferSize(size_t len)
Calculates minimal buffer size for the cipher.
Definition crypto.cpp:238
AES128CFB8Cipher(CipherState state, const std::byte *key, const std::byte *iv)
Construct a new AES/CFB8 128bit cipher object.
Definition crypto.cpp:201
The Minecraft Hashing class.
Definition crypto.h:129
MinecraftHash()
Construct a new Minecraft Hash object.
Definition crypto.cpp:144
~MinecraftHash()
Destroy the Minecraft Hash object.
Definition crypto.cpp:150
void update(const std::string &s)
Updates the hash.
Definition crypto.cpp:155
std::string finalize()
Finalizes the hash.
Definition crypto.cpp:160
ZLib compressor.
Definition crypto.h:271
ZLibCompressor(int level)
Construct a new Zlib Compressor object.
Definition crypto.cpp:249
~ZLibCompressor()=default
Destroy the Zlib Compressor object.
int uncompress(const std::byte *data, size_t len, std::byte *out, size_t outLen)
Uncompresses data (inflate)
Definition crypto.cpp:295
int compress(const std::byte *data, size_t len, std::byte *out, size_t outLen)
Compresses data (deflate)
Definition crypto.cpp:253
constexpr std::string_view type_name()
Gets the name of the type paramater.
Definition event.h:56
The crypto namespace.
Definition crypto.h:29
std::unique_ptr< std::byte[]> rsaDecrypt(const std::byte *data, size_t len, size_t *outLen)
Decrypts data using the server's RSA keypair.
Definition crypto.cpp:78
std::unique_ptr< std::byte[]> rsaEncrypt(const std::byte *data, size_t len, size_t *outLen)
Encrypts data using the server's RSA keypair.
Definition crypto.cpp:52
bool init()
Inits Crypto.
Definition crypto.cpp:27
std::unique_ptr< std::byte[]> randomSecure(size_t len)
Generates randoms bytes securely.
Definition crypto.cpp:117
CipherState
Possible cipher states.
Definition crypto.h:173
@ DECRYPT
Decrypt Cipher state.
Definition crypto.h:181
@ ENCRYPT
ENCRYPT Cipher state.
Definition crypto.h:189
constexpr int RSA_KEY_LENGTH
The length of the rsa keys.
Definition crypto.h:37
std::string md5Digest(const std::string &data)
MD5 digests a string.
Definition crypto.cpp:126
void cleanup()
Cleanups Crypto.
Definition crypto.cpp:47
std::unique_ptr< std::byte[]> getPublicRSAKey(int *outLen)
Get the Public RSA Public Key.
Definition crypto.cpp:104