1/**************************************************************************/
2/* crypto_core.h */
3/**************************************************************************/
4/* This file is part of: */
5/* GODOT ENGINE */
6/* https://godotengine.org */
7/**************************************************************************/
8/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
9/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
10/* */
11/* Permission is hereby granted, free of charge, to any person obtaining */
12/* a copy of this software and associated documentation files (the */
13/* "Software"), to deal in the Software without restriction, including */
14/* without limitation the rights to use, copy, modify, merge, publish, */
15/* distribute, sublicense, and/or sell copies of the Software, and to */
16/* permit persons to whom the Software is furnished to do so, subject to */
17/* the following conditions: */
18/* */
19/* The above copyright notice and this permission notice shall be */
20/* included in all copies or substantial portions of the Software. */
21/* */
22/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
23/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
24/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
25/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
26/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
27/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
28/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
29/**************************************************************************/
30
31#ifndef CRYPTO_CORE_H
32#define CRYPTO_CORE_H
33
34#include "core/object/ref_counted.h"
35
36class CryptoCore {
37public:
38 class RandomGenerator {
39 private:
40 void *entropy = nullptr;
41 void *ctx = nullptr;
42
43 static int _entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len);
44
45 public:
46 RandomGenerator();
47 ~RandomGenerator();
48
49 Error init();
50 Error get_random_bytes(uint8_t *r_buffer, size_t p_bytes);
51 };
52
53 class MD5Context {
54 private:
55 void *ctx = nullptr;
56
57 public:
58 MD5Context();
59 ~MD5Context();
60
61 Error start();
62 Error update(const uint8_t *p_src, size_t p_len);
63 Error finish(unsigned char r_hash[16]);
64 };
65
66 class SHA1Context {
67 private:
68 void *ctx = nullptr;
69
70 public:
71 SHA1Context();
72 ~SHA1Context();
73
74 Error start();
75 Error update(const uint8_t *p_src, size_t p_len);
76 Error finish(unsigned char r_hash[20]);
77 };
78
79 class SHA256Context {
80 private:
81 void *ctx = nullptr;
82
83 public:
84 SHA256Context();
85 ~SHA256Context();
86
87 Error start();
88 Error update(const uint8_t *p_src, size_t p_len);
89 Error finish(unsigned char r_hash[32]);
90 };
91
92 class AESContext {
93 private:
94 void *ctx = nullptr;
95
96 public:
97 AESContext();
98 ~AESContext();
99
100 Error set_encode_key(const uint8_t *p_key, size_t p_bits);
101 Error set_decode_key(const uint8_t *p_key, size_t p_bits);
102 Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
103 Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
104 Error encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
105 Error decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
106 Error encrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst);
107 Error decrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst);
108 };
109
110 static String b64_encode_str(const uint8_t *p_src, int p_src_len);
111 static Error b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len);
112 static Error b64_decode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len);
113
114 static Error md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]);
115 static Error sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]);
116 static Error sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]);
117};
118
119#endif // CRYPTO_CORE_H
120