summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
authorFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-07-02 03:06:52 +0200
committerFabio Alessandrelli <fabio.alessandrelli@gmail.com>2019-07-02 12:36:27 +0200
commit564d93ff10b19dd15df6ea049bd7c9a9c99680c6 (patch)
treeee6523844280ff5874e8d7aff7c95f498ae769e0 /core
parent0268a4869ded42079d3f4c255406711c726e3df4 (diff)
CryptoCore class to access to base crypto utils.
Godot core needs MD5/SHA256/AES/Base64 which used to be provided by separate libraries. Since we bundle mbedtls in most cases, and we can easily only include the needed sources if we so desire, let's use it. To simplify library changes in the future, and better isolate header dependencies all functions have been wrapped around inside a class in `core/math/crypto_base.h`. If the mbedtls module is disabled, we only bundle the needed source files independently of the `builtin_mbedtls` option. If the module is enabled, the `builtin_mbedtls` option works as usual. Also remove some unused headers from StreamPeerMbedTLS which were causing build issues.
Diffstat (limited to 'core')
-rw-r--r--core/SCsub4
-rw-r--r--core/bind/core_bind.cpp25
-rw-r--r--core/io/file_access_encrypted.cpp36
-rw-r--r--core/math/SCsub35
-rw-r--r--core/math/crypto_core.cpp146
-rw-r--r--core/math/crypto_core.h89
-rw-r--r--core/os/file_access.cpp40
-rw-r--r--core/ustring.cpp34
-rw-r--r--core/variant_call.cpp7
9 files changed, 326 insertions, 90 deletions
diff --git a/core/SCsub b/core/SCsub
index 166b7083e4..6389cd176c 100644
--- a/core/SCsub
+++ b/core/SCsub
@@ -47,15 +47,11 @@ env_thirdparty.disable_warnings()
thirdparty_misc_dir = "#thirdparty/misc/"
thirdparty_misc_sources = [
# C sources
- "base64.c",
"fastlz.c",
- "sha256.c",
"smaz.c",
# C++ sources
- "aes256.cpp",
"hq2x.cpp",
- "md5.cpp",
"pcg.cpp",
"triangulator.cpp",
"clipper.cpp",
diff --git a/core/bind/core_bind.cpp b/core/bind/core_bind.cpp
index f9fb7d7695..0230865111 100644
--- a/core/bind/core_bind.cpp
+++ b/core/bind/core_bind.cpp
@@ -34,13 +34,12 @@
#include "core/io/file_access_encrypted.h"
#include "core/io/json.h"
#include "core/io/marshalls.h"
+#include "core/math/crypto_core.h"
#include "core/math/geometry.h"
#include "core/os/keyboard.h"
#include "core/os/os.h"
#include "core/project_settings.h"
-#include "thirdparty/misc/base64.h"
-
/**
* Time constants borrowed from loc_time.h
*/
@@ -2438,7 +2437,8 @@ String _Marshalls::variant_to_base64(const Variant &p_var, bool p_full_objects)
b64buff.resize(b64len);
PoolVector<uint8_t>::Write w64 = b64buff.write();
- int strlen = base64_encode((char *)(&w64[0]), (char *)(&w[0]), len);
+ size_t strlen = 0;
+ ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, &w[0], len) != OK, String());
//OS::get_singleton()->print("len is %i, vector size is %i\n", b64len, strlen);
w64[strlen] = 0;
String ret = (char *)&w64[0];
@@ -2455,7 +2455,8 @@ Variant _Marshalls::base64_to_variant(const String &p_str, bool p_allow_objects)
buf.resize(strlen / 4 * 3 + 1);
PoolVector<uint8_t>::Write w = buf.write();
- int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
+ size_t len = 0;
+ ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, Variant());
Variant v;
Error err = decode_variant(v, &w[0], len, NULL, p_allow_objects);
@@ -2474,7 +2475,8 @@ String _Marshalls::raw_to_base64(const PoolVector<uint8_t> &p_arr) {
b64buff.resize(b64len);
PoolVector<uint8_t>::Write w64 = b64buff.write();
- int strlen = base64_encode((char *)(&w64[0]), (char *)(&r[0]), len);
+ size_t strlen = 0;
+ ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, &r[0], len) != OK, String());
w64[strlen] = 0;
String ret = (char *)&w64[0];
@@ -2486,17 +2488,16 @@ PoolVector<uint8_t> _Marshalls::base64_to_raw(const String &p_str) {
int strlen = p_str.length();
CharString cstr = p_str.ascii();
- int arr_len;
+ size_t arr_len = 0;
PoolVector<uint8_t> buf;
{
buf.resize(strlen / 4 * 3 + 1);
PoolVector<uint8_t>::Write w = buf.write();
- arr_len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
- };
+ ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &arr_len, (unsigned char *)cstr.get_data(), strlen) != OK, PoolVector<uint8_t>());
+ }
buf.resize(arr_len);
- // conversion from PoolVector<uint8_t> to raw array?
return buf;
};
@@ -2510,7 +2511,8 @@ String _Marshalls::utf8_to_base64(const String &p_str) {
b64buff.resize(b64len);
PoolVector<uint8_t>::Write w64 = b64buff.write();
- int strlen = base64_encode((char *)(&w64[0]), (char *)cstr.get_data(), len);
+ size_t strlen = 0;
+ ERR_FAIL_COND_V(CryptoCore::b64_encode(&w64[0], b64len, &strlen, (unsigned char *)cstr.get_data(), len) != OK, String());
w64[strlen] = 0;
String ret = (char *)&w64[0];
@@ -2527,7 +2529,8 @@ String _Marshalls::base64_to_utf8(const String &p_str) {
buf.resize(strlen / 4 * 3 + 1 + 1);
PoolVector<uint8_t>::Write w = buf.write();
- int len = base64_decode((char *)(&w[0]), (char *)cstr.get_data(), strlen);
+ size_t len = 0;
+ ERR_FAIL_COND_V(CryptoCore::b64_decode(&w[0], buf.size(), &len, (unsigned char *)cstr.get_data(), strlen) != OK, String());
w[len] = 0;
String ret = String::utf8((char *)&w[0]);
diff --git a/core/io/file_access_encrypted.cpp b/core/io/file_access_encrypted.cpp
index 7dea749a43..ccee6aeb15 100644
--- a/core/io/file_access_encrypted.cpp
+++ b/core/io/file_access_encrypted.cpp
@@ -30,13 +30,11 @@
#include "file_access_encrypted.h"
+#include "core/math/crypto_core.h"
#include "core/os/copymem.h"
#include "core/print_string.h"
#include "core/variant.h"
-#include "thirdparty/misc/aes256.h"
-#include "thirdparty/misc/md5.h"
-
#include <stdio.h>
#define COMP_MAGIC 0x43454447
@@ -83,25 +81,21 @@ Error FileAccessEncrypted::open_and_parse(FileAccess *p_base, const Vector<uint8
uint32_t blen = p_base->get_buffer(data.ptrw(), ds);
ERR_FAIL_COND_V(blen != ds, ERR_FILE_CORRUPT);
- aes256_context ctx;
- aes256_init(&ctx, key.ptrw());
+ CryptoCore::AESContext ctx;
+ ctx.set_decode_key(key.ptrw(), 256);
for (size_t i = 0; i < ds; i += 16) {
- aes256_decrypt_ecb(&ctx, &data.write[i]);
+ ctx.decrypt_ecb(&data.write[i], &data.write[i]);
}
- aes256_done(&ctx);
-
data.resize(length);
- MD5_CTX md5;
- MD5Init(&md5);
- MD5Update(&md5, (uint8_t *)data.ptr(), data.size());
- MD5Final(&md5);
+ unsigned char hash[16];
+ ERR_FAIL_COND_V(CryptoCore::md5(data.ptr(), data.size(), hash) != OK, ERR_BUG);
ERR_EXPLAIN("The MD5 sum of the decrypted file does not match the expected value. It could be that the file is corrupt, or that the provided decryption key is invalid.");
- ERR_FAIL_COND_V(String::md5(md5.digest) != String::md5(md5d), ERR_FILE_CORRUPT);
+ ERR_FAIL_COND_V(String::md5(hash) != String::md5(md5d), ERR_FILE_CORRUPT);
file = p_base;
}
@@ -140,10 +134,8 @@ void FileAccessEncrypted::close() {
len += 16 - (len % 16);
}
- MD5_CTX md5;
- MD5Init(&md5);
- MD5Update(&md5, (uint8_t *)data.ptr(), data.size());
- MD5Final(&md5);
+ unsigned char hash[16];
+ ERR_FAIL_COND(CryptoCore::md5(data.ptr(), data.size(), hash) != OK); // Bug?
compressed.resize(len);
zeromem(compressed.ptrw(), len);
@@ -151,20 +143,18 @@ void FileAccessEncrypted::close() {
compressed.write[i] = data[i];
}
- aes256_context ctx;
- aes256_init(&ctx, key.ptrw());
+ CryptoCore::AESContext ctx;
+ ctx.set_encode_key(key.ptrw(), 256);
for (size_t i = 0; i < len; i += 16) {
- aes256_encrypt_ecb(&ctx, &compressed.write[i]);
+ ctx.encrypt_ecb(&compressed.write[i], &compressed.write[i]);
}
- aes256_done(&ctx);
-
file->store_32(COMP_MAGIC);
file->store_32(mode);
- file->store_buffer(md5.digest, 16);
+ file->store_buffer(hash, 16);
file->store_64(data.size());
file->store_buffer(compressed.ptr(), compressed.size());
diff --git a/core/math/SCsub b/core/math/SCsub
index 1c5f954470..4e76efceff 100644
--- a/core/math/SCsub
+++ b/core/math/SCsub
@@ -2,4 +2,37 @@
Import('env')
-env.add_source_files(env.core_sources, "*.cpp")
+env_math = env.Clone() # Maybe make one specific for crypto?
+
+is_builtin = env["builtin_mbedtls"]
+has_module = env["module_mbedtls_enabled"]
+
+if is_builtin or not has_module:
+ # Use our headers for builtin or if the module is not going to be compiled.
+ # We decided not to depend on system mbedtls just for these few files that can
+ # be easily extracted.
+ env_math.Prepend(CPPPATH=["#thirdparty/mbedtls/include"])
+
+# MbedTLS core functions (for CryptoCore).
+# If the mbedtls module is compiled we don't need to add the .c files with our
+# custom config since they will be built by the module itself.
+# Only if the module is not enabled, we must compile here the required sources
+# to make a "light" build with only the necessary mbedtls files.
+if not has_module:
+ env_thirdparty = env_math.Clone()
+ env_thirdparty.disable_warnings()
+ # Custom config file
+ env_thirdparty.Append(CPPFLAGS=['-DMBEDTLS_CONFIG_FILE="\\"thirdparty/mbedtls/include/godot_core_mbedtls_config.h\\""'])
+ thirdparty_mbedtls_dir = "#thirdparty/mbedtls/library/"
+ thirdparty_mbedtls_sources = [
+ "aes.c",
+ "base64.c",
+ "md5.c",
+ "sha1.c",
+ "sha256.c",
+ "godot_core_mbedtls_platform.c"
+ ]
+ thirdparty_mbedtls_sources = [thirdparty_mbedtls_dir + file for file in thirdparty_mbedtls_sources]
+ env_thirdparty.add_source_files(env.core_sources, thirdparty_mbedtls_sources)
+
+env_math.add_source_files(env.core_sources, "*.cpp")
diff --git a/core/math/crypto_core.cpp b/core/math/crypto_core.cpp
new file mode 100644
index 0000000000..6449d94db8
--- /dev/null
+++ b/core/math/crypto_core.cpp
@@ -0,0 +1,146 @@
+/*************************************************************************/
+/* crypto_core.cpp */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#include "crypto_core.h"
+
+#include <mbedtls/aes.h>
+#include <mbedtls/base64.h>
+#include <mbedtls/md5.h>
+#include <mbedtls/sha1.h>
+#include <mbedtls/sha256.h>
+
+// MD5
+CryptoCore::MD5Context::MD5Context() {
+ ctx = memalloc(sizeof(mbedtls_md5_context));
+ mbedtls_md5_init((mbedtls_md5_context *)ctx);
+}
+
+CryptoCore::MD5Context::~MD5Context() {
+ mbedtls_md5_free((mbedtls_md5_context *)ctx);
+ memfree((mbedtls_md5_context *)ctx);
+}
+
+Error CryptoCore::MD5Context::start() {
+ int ret = mbedtls_md5_starts_ret((mbedtls_md5_context *)ctx);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::MD5Context::update(uint8_t *p_src, size_t p_len) {
+ int ret = mbedtls_md5_update_ret((mbedtls_md5_context *)ctx, p_src, p_len);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) {
+ int ret = mbedtls_md5_finish_ret((mbedtls_md5_context *)ctx, r_hash);
+ return ret ? FAILED : OK;
+}
+
+// SHA256
+CryptoCore::SHA256Context::SHA256Context() {
+ ctx = memalloc(sizeof(mbedtls_sha256_context));
+ mbedtls_sha256_init((mbedtls_sha256_context *)ctx);
+}
+
+CryptoCore::SHA256Context::~SHA256Context() {
+ mbedtls_sha256_free((mbedtls_sha256_context *)ctx);
+ memfree((mbedtls_sha256_context *)ctx);
+}
+
+Error CryptoCore::SHA256Context::start() {
+ int ret = mbedtls_sha256_starts_ret((mbedtls_sha256_context *)ctx, 0);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::SHA256Context::update(uint8_t *p_src, size_t p_len) {
+ int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::SHA256Context::finish(unsigned char r_hash[16]) {
+ int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash);
+ return ret ? FAILED : OK;
+}
+
+// AES256
+CryptoCore::AESContext::AESContext() {
+ ctx = memalloc(sizeof(mbedtls_aes_context));
+ mbedtls_aes_init((mbedtls_aes_context *)ctx);
+}
+
+CryptoCore::AESContext::~AESContext() {
+ mbedtls_aes_free((mbedtls_aes_context *)ctx);
+ memfree((mbedtls_aes_context *)ctx);
+}
+
+Error CryptoCore::AESContext::set_encode_key(const uint8_t *p_key, size_t p_bits) {
+ int ret = mbedtls_aes_setkey_enc((mbedtls_aes_context *)ctx, p_key, p_bits);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::AESContext::set_decode_key(const uint8_t *p_key, size_t p_bits) {
+ int ret = mbedtls_aes_setkey_dec((mbedtls_aes_context *)ctx, p_key, p_bits);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::AESContext::encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
+ int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_src, r_dst);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
+ int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst);
+ return ret ? FAILED : OK;
+}
+
+// CryptoCore
+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) {
+ int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len);
+ return ret ? FAILED : OK;
+}
+
+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) {
+ int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]) {
+ int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]) {
+ int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash);
+ return ret ? FAILED : OK;
+}
+
+Error CryptoCore::sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]) {
+ int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0);
+ return ret ? FAILED : OK;
+}
diff --git a/core/math/crypto_core.h b/core/math/crypto_core.h
new file mode 100644
index 0000000000..1cb3c86e18
--- /dev/null
+++ b/core/math/crypto_core.h
@@ -0,0 +1,89 @@
+/*************************************************************************/
+/* crypto_core.h */
+/*************************************************************************/
+/* This file is part of: */
+/* GODOT ENGINE */
+/* https://godotengine.org */
+/*************************************************************************/
+/* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
+/* Copyright (c) 2014-2019 Godot Engine contributors (cf. AUTHORS.md) */
+/* */
+/* Permission is hereby granted, free of charge, to any person obtaining */
+/* a copy of this software and associated documentation files (the */
+/* "Software"), to deal in the Software without restriction, including */
+/* without limitation the rights to use, copy, modify, merge, publish, */
+/* distribute, sublicense, and/or sell copies of the Software, and to */
+/* permit persons to whom the Software is furnished to do so, subject to */
+/* the following conditions: */
+/* */
+/* The above copyright notice and this permission notice shall be */
+/* included in all copies or substantial portions of the Software. */
+/* */
+/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
+/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
+/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.*/
+/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
+/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
+/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
+/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
+/*************************************************************************/
+
+#ifndef CRYPTO_CORE_H
+#define CRYPTO_CORE_H
+
+#include "core/reference.h"
+
+class CryptoCore {
+
+public:
+ class MD5Context {
+
+ private:
+ void *ctx; // To include, or not to include...
+
+ public:
+ MD5Context();
+ ~MD5Context();
+
+ Error start();
+ Error update(uint8_t *p_src, size_t p_len);
+ Error finish(unsigned char r_hash[16]);
+ };
+
+ class SHA256Context {
+
+ private:
+ void *ctx; // To include, or not to include...
+
+ public:
+ SHA256Context();
+ ~SHA256Context();
+
+ Error start();
+ Error update(uint8_t *p_src, size_t p_len);
+ Error finish(unsigned char r_hash[16]);
+ };
+
+ class AESContext {
+
+ private:
+ void *ctx; // To include, or not to include...
+
+ public:
+ AESContext();
+ ~AESContext();
+
+ Error set_encode_key(const uint8_t *p_key, size_t p_bits);
+ Error set_decode_key(const uint8_t *p_key, size_t p_bits);
+ Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
+ Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
+ };
+
+ 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);
+ 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);
+
+ static Error md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]);
+ static Error sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]);
+ static Error sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]);
+};
+#endif // CRYPTO_CORE_H
diff --git a/core/os/file_access.cpp b/core/os/file_access.cpp
index b5580b5c6e..7509050b2b 100644
--- a/core/os/file_access.cpp
+++ b/core/os/file_access.cpp
@@ -32,12 +32,10 @@
#include "core/io/file_access_pack.h"
#include "core/io/marshalls.h"
+#include "core/math/crypto_core.h"
#include "core/os/os.h"
#include "core/project_settings.h"
-#include "thirdparty/misc/md5.h"
-#include "thirdparty/misc/sha256.h"
-
FileAccess::CreateFunc FileAccess::create_func[ACCESS_MAX] = { 0, 0 };
FileAccess::FileCloseFailNotify FileAccess::close_fail_notify = NULL;
@@ -637,8 +635,8 @@ String FileAccess::get_md5(const String &p_file) {
if (!f)
return String();
- MD5_CTX md5;
- MD5Init(&md5);
+ CryptoCore::MD5Context ctx;
+ ctx.start();
unsigned char step[32768];
@@ -647,24 +645,24 @@ String FileAccess::get_md5(const String &p_file) {
int br = f->get_buffer(step, 32768);
if (br > 0) {
- MD5Update(&md5, step, br);
+ ctx.update(step, br);
}
if (br < 4096)
break;
}
- MD5Final(&md5);
-
- String ret = String::md5(md5.digest);
+ unsigned char hash[16];
+ ctx.finish(hash);
memdelete(f);
- return ret;
+
+ return String::md5(hash);
}
String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
- MD5_CTX md5;
- MD5Init(&md5);
+ CryptoCore::MD5Context ctx;
+ ctx.start();
for (int i = 0; i < p_file.size(); i++) {
FileAccess *f = FileAccess::open(p_file[i], READ);
@@ -677,7 +675,7 @@ String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
int br = f->get_buffer(step, 32768);
if (br > 0) {
- MD5Update(&md5, step, br);
+ ctx.update(step, br);
}
if (br < 4096)
break;
@@ -685,11 +683,10 @@ String FileAccess::get_multiple_md5(const Vector<String> &p_file) {
memdelete(f);
}
- MD5Final(&md5);
+ unsigned char hash[16];
+ ctx.finish(hash);
- String ret = String::md5(md5.digest);
-
- return ret;
+ return String::md5(hash);
}
String FileAccess::get_sha256(const String &p_file) {
@@ -698,8 +695,8 @@ String FileAccess::get_sha256(const String &p_file) {
if (!f)
return String();
- sha256_context sha256;
- sha256_init(&sha256);
+ CryptoCore::SHA256Context ctx;
+ ctx.start();
unsigned char step[32768];
@@ -708,15 +705,14 @@ String FileAccess::get_sha256(const String &p_file) {
int br = f->get_buffer(step, 32768);
if (br > 0) {
- sha256_hash(&sha256, step, br);
+ ctx.update(step, br);
}
if (br < 4096)
break;
}
unsigned char hash[32];
-
- sha256_done(&sha256, hash);
+ ctx.finish(hash);
memdelete(f);
return String::hex_encode_buffer(hash, 32);
diff --git a/core/ustring.cpp b/core/ustring.cpp
index 18c48b4dad..2b312191e2 100644
--- a/core/ustring.cpp
+++ b/core/ustring.cpp
@@ -31,6 +31,7 @@
#include "ustring.h"
#include "core/color.h"
+#include "core/math/crypto_core.h"
#include "core/math/math_funcs.h"
#include "core/os/memory.h"
#include "core/print_string.h"
@@ -38,9 +39,6 @@
#include "core/ucaps.h"
#include "core/variant.h"
-#include "thirdparty/misc/md5.h"
-#include "thirdparty/misc/sha256.h"
-
#include <wchar.h>
#ifndef NO_USE_STDLIB
@@ -2254,54 +2252,42 @@ uint64_t String::hash64() const {
String String::md5_text() const {
CharString cs = utf8();
- MD5_CTX ctx;
- MD5Init(&ctx);
- MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length());
- MD5Final(&ctx);
- return String::md5(ctx.digest);
+ unsigned char hash[16];
+ CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
+ return String::hex_encode_buffer(hash, 16);
}
String String::sha256_text() const {
CharString cs = utf8();
unsigned char hash[32];
- sha256_context ctx;
- sha256_init(&ctx);
- sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length());
- sha256_done(&ctx, hash);
+ CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
return String::hex_encode_buffer(hash, 32);
}
Vector<uint8_t> String::md5_buffer() const {
CharString cs = utf8();
- MD5_CTX ctx;
- MD5Init(&ctx);
- MD5Update(&ctx, (unsigned char *)cs.ptr(), cs.length());
- MD5Final(&ctx);
+ unsigned char hash[16];
+ CryptoCore::md5((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(16);
for (int i = 0; i < 16; i++) {
- ret.write[i] = ctx.digest[i];
- };
-
+ ret.write[i] = hash[i];
+ }
return ret;
};
Vector<uint8_t> String::sha256_buffer() const {
CharString cs = utf8();
unsigned char hash[32];
- sha256_context ctx;
- sha256_init(&ctx);
- sha256_hash(&ctx, (unsigned char *)cs.ptr(), cs.length());
- sha256_done(&ctx, hash);
+ CryptoCore::sha256((unsigned char *)cs.ptr(), cs.length(), hash);
Vector<uint8_t> ret;
ret.resize(32);
for (int i = 0; i < 32; i++) {
ret.write[i] = hash[i];
}
-
return ret;
}
diff --git a/core/variant_call.cpp b/core/variant_call.cpp
index dc28f1ca02..811008e7c8 100644
--- a/core/variant_call.cpp
+++ b/core/variant_call.cpp
@@ -33,10 +33,10 @@
#include "core/color_names.inc"
#include "core/core_string_names.h"
#include "core/io/compression.h"
+#include "core/math/crypto_core.h"
#include "core/object.h"
#include "core/os/os.h"
#include "core/script_language.h"
-#include "thirdparty/misc/sha256.h"
typedef void (*VariantFunc)(Variant &r_ret, Variant &p_self, const Variant **p_args);
typedef void (*VariantConstructFunc)(Variant &r_ret, const Variant **p_args);
@@ -598,10 +598,7 @@ struct _VariantCall {
PoolByteArray::Read r = ba->read();
String s;
unsigned char hash[32];
- sha256_context sha256;
- sha256_init(&sha256);
- sha256_hash(&sha256, (unsigned char *)r.ptr(), ba->size());
- sha256_done(&sha256, hash);
+ CryptoCore::sha256((unsigned char *)r.ptr(), ba->size(), hash);
s = String::hex_encode_buffer(hash, 32);
r_ret = s;
}