Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
godotengine
GitHub Repository: godotengine/godot
Path: blob/master/core/crypto/crypto_core.h
9973 views
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
#pragma once
32
33
#include "core/object/ref_counted.h"
34
35
class CryptoCore {
36
public:
37
class RandomGenerator {
38
private:
39
void *entropy = nullptr;
40
void *ctx = nullptr;
41
42
static int _entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len);
43
44
public:
45
RandomGenerator();
46
~RandomGenerator();
47
48
Error init();
49
Error get_random_bytes(uint8_t *r_buffer, size_t p_bytes);
50
};
51
52
class MD5Context {
53
private:
54
void *ctx = nullptr;
55
56
public:
57
MD5Context();
58
~MD5Context();
59
60
Error start();
61
Error update(const uint8_t *p_src, size_t p_len);
62
Error finish(unsigned char r_hash[16]);
63
};
64
65
class SHA1Context {
66
private:
67
void *ctx = nullptr;
68
69
public:
70
SHA1Context();
71
~SHA1Context();
72
73
Error start();
74
Error update(const uint8_t *p_src, size_t p_len);
75
Error finish(unsigned char r_hash[20]);
76
};
77
78
class SHA256Context {
79
private:
80
void *ctx = nullptr;
81
82
public:
83
SHA256Context();
84
~SHA256Context();
85
86
Error start();
87
Error update(const uint8_t *p_src, size_t p_len);
88
Error finish(unsigned char r_hash[32]);
89
};
90
91
class AESContext {
92
private:
93
void *ctx = nullptr;
94
95
public:
96
AESContext();
97
~AESContext();
98
99
Error set_encode_key(const uint8_t *p_key, size_t p_bits);
100
Error set_decode_key(const uint8_t *p_key, size_t p_bits);
101
Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
102
Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
103
Error encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
104
Error decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
105
Error encrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst);
106
Error decrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst);
107
};
108
109
static String b64_encode_str(const uint8_t *p_src, size_t p_src_len);
110
static Error b64_encode(uint8_t *r_dst, size_t p_dst_len, size_t *r_len, const uint8_t *p_src, size_t p_src_len);
111
static Error b64_decode(uint8_t *r_dst, size_t p_dst_len, size_t *r_len, const uint8_t *p_src, size_t p_src_len);
112
113
static Error md5(const uint8_t *p_src, size_t p_src_len, unsigned char r_hash[16]);
114
static Error sha1(const uint8_t *p_src, size_t p_src_len, unsigned char r_hash[20]);
115
static Error sha256(const uint8_t *p_src, size_t p_src_len, unsigned char r_hash[32]);
116
};
117
118