summaryrefslogtreecommitdiff
path: root/core
diff options
context:
space:
mode:
Diffstat (limited to 'core')
-rw-r--r--core/crypto/SCsub2
-rw-r--r--core/crypto/crypto_core.cpp43
-rw-r--r--core/crypto/crypto_core.h23
-rw-r--r--core/io/resource_uid.cpp23
-rw-r--r--core/io/resource_uid.h5
-rw-r--r--core/os/os.h2
6 files changed, 78 insertions, 20 deletions
diff --git a/core/crypto/SCsub b/core/crypto/SCsub
index 1fe2fa5b23..9b7953fdc5 100644
--- a/core/crypto/SCsub
+++ b/core/crypto/SCsub
@@ -31,6 +31,8 @@ if not has_module:
"aes.c",
"base64.c",
"constant_time.c",
+ "ctr_drbg.c",
+ "entropy.c",
"md5.c",
"sha1.c",
"sha256.c",
diff --git a/core/crypto/crypto_core.cpp b/core/crypto/crypto_core.cpp
index 9f000c5aeb..3cf7b6c310 100644
--- a/core/crypto/crypto_core.cpp
+++ b/core/crypto/crypto_core.cpp
@@ -30,12 +30,55 @@
#include "crypto_core.h"
+#include "core/os/os.h"
+
#include <mbedtls/aes.h>
#include <mbedtls/base64.h>
+#include <mbedtls/ctr_drbg.h>
+#include <mbedtls/entropy.h>
#include <mbedtls/md5.h>
#include <mbedtls/sha1.h>
#include <mbedtls/sha256.h>
+// RandomGenerator
+CryptoCore::RandomGenerator::RandomGenerator() {
+ entropy = memalloc(sizeof(mbedtls_entropy_context));
+ mbedtls_entropy_init((mbedtls_entropy_context *)entropy);
+ mbedtls_entropy_add_source((mbedtls_entropy_context *)entropy, &CryptoCore::RandomGenerator::_entropy_poll, nullptr, 256, MBEDTLS_ENTROPY_SOURCE_STRONG);
+ ctx = memalloc(sizeof(mbedtls_ctr_drbg_context));
+ mbedtls_ctr_drbg_init((mbedtls_ctr_drbg_context *)ctx);
+}
+
+CryptoCore::RandomGenerator::~RandomGenerator() {
+ mbedtls_ctr_drbg_free((mbedtls_ctr_drbg_context *)ctx);
+ memfree(ctx);
+ mbedtls_entropy_free((mbedtls_entropy_context *)entropy);
+ memfree(entropy);
+}
+
+int CryptoCore::RandomGenerator::_entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len) {
+ *r_len = 0;
+ Error err = OS::get_singleton()->get_entropy(r_buffer, p_len);
+ ERR_FAIL_COND_V(err, MBEDTLS_ERR_ENTROPY_SOURCE_FAILED);
+ *r_len = p_len;
+ return 0;
+}
+
+Error CryptoCore::RandomGenerator::init() {
+ int ret = mbedtls_ctr_drbg_seed((mbedtls_ctr_drbg_context *)ctx, mbedtls_entropy_func, (mbedtls_entropy_context *)entropy, nullptr, 0);
+ if (ret) {
+ ERR_FAIL_COND_V_MSG(ret, FAILED, " failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret));
+ }
+ return OK;
+}
+
+Error CryptoCore::RandomGenerator::get_random_bytes(uint8_t *r_buffer, size_t p_bytes) {
+ ERR_FAIL_COND_V(!ctx, ERR_UNCONFIGURED);
+ int ret = mbedtls_ctr_drbg_random((mbedtls_ctr_drbg_context *)ctx, r_buffer, p_bytes);
+ ERR_FAIL_COND_V_MSG(ret, FAILED, " failed\n ! mbedtls_ctr_drbg_seed returned an error" + itos(ret));
+ return OK;
+}
+
// MD5
CryptoCore::MD5Context::MD5Context() {
ctx = memalloc(sizeof(mbedtls_md5_context));
diff --git a/core/crypto/crypto_core.h b/core/crypto/crypto_core.h
index 355f4a2404..eacef268cc 100644
--- a/core/crypto/crypto_core.h
+++ b/core/crypto/crypto_core.h
@@ -35,9 +35,24 @@
class CryptoCore {
public:
+ class RandomGenerator {
+ private:
+ void *entropy = nullptr;
+ void *ctx = nullptr;
+
+ static int _entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len);
+
+ public:
+ RandomGenerator();
+ ~RandomGenerator();
+
+ Error init();
+ Error get_random_bytes(uint8_t *r_buffer, size_t p_bytes);
+ };
+
class MD5Context {
private:
- void *ctx = nullptr; // To include, or not to include...
+ void *ctx = nullptr;
public:
MD5Context();
@@ -50,7 +65,7 @@ public:
class SHA1Context {
private:
- void *ctx = nullptr; // To include, or not to include...
+ void *ctx = nullptr;
public:
SHA1Context();
@@ -63,7 +78,7 @@ public:
class SHA256Context {
private:
- void *ctx = nullptr; // To include, or not to include...
+ void *ctx = nullptr;
public:
SHA256Context();
@@ -76,7 +91,7 @@ public:
class AESContext {
private:
- void *ctx = nullptr; // To include, or not to include...
+ void *ctx = nullptr;
public:
AESContext();
diff --git a/core/io/resource_uid.cpp b/core/io/resource_uid.cpp
index 776756e64e..d0335bed3a 100644
--- a/core/io/resource_uid.cpp
+++ b/core/io/resource_uid.cpp
@@ -31,7 +31,7 @@
#include "resource_uid.h"
#include "core/config/project_settings.h"
-#include "core/crypto/crypto.h"
+#include "core/crypto/crypto_core.h"
#include "core/io/dir_access.h"
#include "core/io/file_access.h"
@@ -82,20 +82,14 @@ ResourceUID::ID ResourceUID::text_to_id(const String &p_text) const {
return ID(uid & 0x7FFFFFFFFFFFFFFF);
}
-ResourceUID::ID ResourceUID::create_id() const {
- mutex.lock();
- if (crypto.is_null()) {
- crypto = Ref<Crypto>(Crypto::create());
- }
- mutex.unlock();
+ResourceUID::ID ResourceUID::create_id() {
while (true) {
- PackedByteArray bytes = crypto->generate_random_bytes(8);
- ERR_FAIL_COND_V(bytes.size() != 8, INVALID_ID);
- const uint64_t *ptr64 = (const uint64_t *)bytes.ptr();
- ID id = int64_t((*ptr64) & 0x7FFFFFFFFFFFFFFF);
- mutex.lock();
+ ID id = INVALID_ID;
+ MutexLock lock(mutex);
+ Error err = ((CryptoCore::RandomGenerator *)crypto)->get_random_bytes((uint8_t *)&id, sizeof(id));
+ ERR_FAIL_COND_V(err != OK, INVALID_ID);
+ id &= 0x7FFFFFFFFFFFFFFF;
bool exists = unique_ids.has(id);
- mutex.unlock();
if (!exists) {
return id;
}
@@ -261,6 +255,9 @@ ResourceUID *ResourceUID::singleton = nullptr;
ResourceUID::ResourceUID() {
ERR_FAIL_COND(singleton != nullptr);
singleton = this;
+ crypto = memnew(CryptoCore::RandomGenerator);
+ ((CryptoCore::RandomGenerator *)crypto)->init();
}
ResourceUID::~ResourceUID() {
+ memdelete((CryptoCore::RandomGenerator *)crypto);
}
diff --git a/core/io/resource_uid.h b/core/io/resource_uid.h
index 9f2ab5245b..1ea44b9d06 100644
--- a/core/io/resource_uid.h
+++ b/core/io/resource_uid.h
@@ -35,7 +35,6 @@
#include "core/string/string_name.h"
#include "core/templates/ordered_hash_map.h"
-class Crypto;
class ResourceUID : public Object {
GDCLASS(ResourceUID, Object)
public:
@@ -47,7 +46,7 @@ public:
static String get_cache_file();
private:
- mutable Ref<Crypto> crypto;
+ void *crypto; // CryptoCore::RandomGenerator (avoid including crypto_core.h)
Mutex mutex;
struct Cache {
CharString cs;
@@ -67,7 +66,7 @@ public:
String id_to_text(ID p_id) const;
ID text_to_id(const String &p_text) const;
- ID create_id() const;
+ ID create_id();
bool has_id(ID p_id) const;
void add_id(ID p_id, const String &p_path);
void set_id(ID p_id, const String &p_path);
diff --git a/core/os/os.h b/core/os/os.h
index d3d2a868fa..188900a070 100644
--- a/core/os/os.h
+++ b/core/os/os.h
@@ -132,6 +132,8 @@ public:
virtual String get_stdin_string(bool p_block = true) = 0;
+ virtual Error get_entropy(uint8_t *r_buffer, int p_bytes) = 0; // Should return cryptographically-safe random bytes.
+
virtual PackedStringArray get_connected_midi_inputs();
virtual void open_midi_inputs();
virtual void close_midi_inputs();