summaryrefslogtreecommitdiffstats
path: root/core/io/file_access_encrypted.cpp
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/io/file_access_encrypted.cpp
parent0268a4869ded42079d3f4c255406711c726e3df4 (diff)
downloadredot-engine-564d93ff10b19dd15df6ea049bd7c9a9c99680c6.tar.gz
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/io/file_access_encrypted.cpp')
-rw-r--r--core/io/file_access_encrypted.cpp36
1 files changed, 13 insertions, 23 deletions
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());