1 /*************************************************************************/
2 /*  crypto_core.cpp                                                      */
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 #include "crypto_core.h"
32 
33 #include <mbedtls/aes.h>
34 #include <mbedtls/base64.h>
35 #include <mbedtls/md5.h>
36 #include <mbedtls/sha1.h>
37 #include <mbedtls/sha256.h>
38 
39 // MD5
MD5Context()40 CryptoCore::MD5Context::MD5Context() {
41 	ctx = memalloc(sizeof(mbedtls_md5_context));
42 	mbedtls_md5_init((mbedtls_md5_context *)ctx);
43 }
44 
~MD5Context()45 CryptoCore::MD5Context::~MD5Context() {
46 	mbedtls_md5_free((mbedtls_md5_context *)ctx);
47 	memfree((mbedtls_md5_context *)ctx);
48 }
49 
start()50 Error CryptoCore::MD5Context::start() {
51 	int ret = mbedtls_md5_starts_ret((mbedtls_md5_context *)ctx);
52 	return ret ? FAILED : OK;
53 }
54 
update(const uint8_t * p_src,size_t p_len)55 Error CryptoCore::MD5Context::update(const uint8_t *p_src, size_t p_len) {
56 	int ret = mbedtls_md5_update_ret((mbedtls_md5_context *)ctx, p_src, p_len);
57 	return ret ? FAILED : OK;
58 }
59 
finish(unsigned char r_hash[16])60 Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) {
61 	int ret = mbedtls_md5_finish_ret((mbedtls_md5_context *)ctx, r_hash);
62 	return ret ? FAILED : OK;
63 }
64 
65 // SHA1
SHA1Context()66 CryptoCore::SHA1Context::SHA1Context() {
67 	ctx = memalloc(sizeof(mbedtls_sha1_context));
68 	mbedtls_sha1_init((mbedtls_sha1_context *)ctx);
69 }
70 
~SHA1Context()71 CryptoCore::SHA1Context::~SHA1Context() {
72 	mbedtls_sha1_free((mbedtls_sha1_context *)ctx);
73 	memfree((mbedtls_sha1_context *)ctx);
74 }
75 
start()76 Error CryptoCore::SHA1Context::start() {
77 	int ret = mbedtls_sha1_starts_ret((mbedtls_sha1_context *)ctx);
78 	return ret ? FAILED : OK;
79 }
80 
update(const uint8_t * p_src,size_t p_len)81 Error CryptoCore::SHA1Context::update(const uint8_t *p_src, size_t p_len) {
82 	int ret = mbedtls_sha1_update_ret((mbedtls_sha1_context *)ctx, p_src, p_len);
83 	return ret ? FAILED : OK;
84 }
85 
finish(unsigned char r_hash[20])86 Error CryptoCore::SHA1Context::finish(unsigned char r_hash[20]) {
87 	int ret = mbedtls_sha1_finish_ret((mbedtls_sha1_context *)ctx, r_hash);
88 	return ret ? FAILED : OK;
89 }
90 
91 // SHA256
SHA256Context()92 CryptoCore::SHA256Context::SHA256Context() {
93 	ctx = memalloc(sizeof(mbedtls_sha256_context));
94 	mbedtls_sha256_init((mbedtls_sha256_context *)ctx);
95 }
96 
~SHA256Context()97 CryptoCore::SHA256Context::~SHA256Context() {
98 	mbedtls_sha256_free((mbedtls_sha256_context *)ctx);
99 	memfree((mbedtls_sha256_context *)ctx);
100 }
101 
start()102 Error CryptoCore::SHA256Context::start() {
103 	int ret = mbedtls_sha256_starts_ret((mbedtls_sha256_context *)ctx, 0);
104 	return ret ? FAILED : OK;
105 }
106 
update(const uint8_t * p_src,size_t p_len)107 Error CryptoCore::SHA256Context::update(const uint8_t *p_src, size_t p_len) {
108 	int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len);
109 	return ret ? FAILED : OK;
110 }
111 
finish(unsigned char r_hash[32])112 Error CryptoCore::SHA256Context::finish(unsigned char r_hash[32]) {
113 	int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash);
114 	return ret ? FAILED : OK;
115 }
116 
117 // AES256
AESContext()118 CryptoCore::AESContext::AESContext() {
119 	ctx = memalloc(sizeof(mbedtls_aes_context));
120 	mbedtls_aes_init((mbedtls_aes_context *)ctx);
121 }
122 
~AESContext()123 CryptoCore::AESContext::~AESContext() {
124 	mbedtls_aes_free((mbedtls_aes_context *)ctx);
125 	memfree((mbedtls_aes_context *)ctx);
126 }
127 
set_encode_key(const uint8_t * p_key,size_t p_bits)128 Error CryptoCore::AESContext::set_encode_key(const uint8_t *p_key, size_t p_bits) {
129 	int ret = mbedtls_aes_setkey_enc((mbedtls_aes_context *)ctx, p_key, p_bits);
130 	return ret ? FAILED : OK;
131 }
132 
set_decode_key(const uint8_t * p_key,size_t p_bits)133 Error CryptoCore::AESContext::set_decode_key(const uint8_t *p_key, size_t p_bits) {
134 	int ret = mbedtls_aes_setkey_dec((mbedtls_aes_context *)ctx, p_key, p_bits);
135 	return ret ? FAILED : OK;
136 }
137 
encrypt_ecb(const uint8_t p_src[16],uint8_t r_dst[16])138 Error CryptoCore::AESContext::encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
139 	int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_src, r_dst);
140 	return ret ? FAILED : OK;
141 }
142 
decrypt_ecb(const uint8_t p_src[16],uint8_t r_dst[16])143 Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
144 	int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst);
145 	return ret ? FAILED : OK;
146 }
147 
148 // CryptoCore
b64_encode_str(const uint8_t * p_src,int p_src_len)149 String CryptoCore::b64_encode_str(const uint8_t *p_src, int p_src_len) {
150 	int b64len = p_src_len / 3 * 4 + 4 + 1;
151 	PoolVector<uint8_t> b64buff;
152 	b64buff.resize(b64len);
153 	PoolVector<uint8_t>::Write w64 = b64buff.write();
154 	size_t strlen = 0;
155 	int ret = b64_encode(&w64[0], b64len, &strlen, p_src, p_src_len);
156 	w64[strlen] = 0;
157 	return ret ? String() : (const char *)&w64[0];
158 }
159 
b64_encode(uint8_t * r_dst,int p_dst_len,size_t * r_len,const uint8_t * p_src,int p_src_len)160 Error CryptoCore::b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len) {
161 	int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len);
162 	return ret ? FAILED : OK;
163 }
164 
b64_decode(uint8_t * r_dst,int p_dst_len,size_t * r_len,const uint8_t * p_src,int p_src_len)165 Error CryptoCore::b64_decode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len) {
166 	int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len);
167 	return ret ? FAILED : OK;
168 }
169 
md5(const uint8_t * p_src,int p_src_len,unsigned char r_hash[16])170 Error CryptoCore::md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]) {
171 	int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash);
172 	return ret ? FAILED : OK;
173 }
174 
sha1(const uint8_t * p_src,int p_src_len,unsigned char r_hash[20])175 Error CryptoCore::sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]) {
176 	int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash);
177 	return ret ? FAILED : OK;
178 }
179 
sha256(const uint8_t * p_src,int p_src_len,unsigned char r_hash[32])180 Error CryptoCore::sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]) {
181 	int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0);
182 	return ret ? FAILED : OK;
183 }
184