1 /*************************************************************************/
2 /*  crypto_core.h                                                        */
3 /*************************************************************************/
4 /*                       This file is part of:                           */
5 /*                           GODOT ENGINE                                */
6 /*                      https://godotengine.org                          */
7 /*************************************************************************/
8 /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur.                 */
9 /* Copyright (c) 2014-2020 Godot Engine contributors (cf. AUTHORS.md).   */
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/reference.h"
35 
36 class CryptoCore {
37 
38 public:
39 	class MD5Context {
40 
41 	private:
42 		void *ctx; // To include, or not to include...
43 
44 	public:
45 		MD5Context();
46 		~MD5Context();
47 
48 		Error start();
49 		Error update(const uint8_t *p_src, size_t p_len);
50 		Error finish(unsigned char r_hash[16]);
51 	};
52 
53 	class SHA1Context {
54 
55 	private:
56 		void *ctx; // To include, or not to include...
57 
58 	public:
59 		SHA1Context();
60 		~SHA1Context();
61 
62 		Error start();
63 		Error update(const uint8_t *p_src, size_t p_len);
64 		Error finish(unsigned char r_hash[20]);
65 	};
66 
67 	class SHA256Context {
68 
69 	private:
70 		void *ctx; // To include, or not to include...
71 
72 	public:
73 		SHA256Context();
74 		~SHA256Context();
75 
76 		Error start();
77 		Error update(const uint8_t *p_src, size_t p_len);
78 		Error finish(unsigned char r_hash[32]);
79 	};
80 
81 	class AESContext {
82 
83 	private:
84 		void *ctx; // To include, or not to include...
85 
86 	public:
87 		AESContext();
88 		~AESContext();
89 
90 		Error set_encode_key(const uint8_t *p_key, size_t p_bits);
91 		Error set_decode_key(const uint8_t *p_key, size_t p_bits);
92 		Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
93 		Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
94 	};
95 
96 	static String b64_encode_str(const uint8_t *p_src, int p_src_len);
97 	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);
98 	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);
99 
100 	static Error md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]);
101 	static Error sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]);
102 	static Error sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]);
103 };
104 #endif // CRYPTO_CORE_H
105