diff options
Diffstat (limited to 'core/crypto')
-rw-r--r-- | core/crypto/SCsub | 3 | ||||
-rw-r--r-- | core/crypto/aes_context.cpp | 4 | ||||
-rw-r--r-- | core/crypto/aes_context.h | 4 | ||||
-rw-r--r-- | core/crypto/crypto.cpp | 4 | ||||
-rw-r--r-- | core/crypto/crypto.h | 4 | ||||
-rw-r--r-- | core/crypto/crypto_core.cpp | 47 | ||||
-rw-r--r-- | core/crypto/crypto_core.h | 27 | ||||
-rw-r--r-- | core/crypto/hashing_context.cpp | 4 | ||||
-rw-r--r-- | core/crypto/hashing_context.h | 4 |
9 files changed, 81 insertions, 20 deletions
diff --git a/core/crypto/SCsub b/core/crypto/SCsub index 4f3104d84b..9b7953fdc5 100644 --- a/core/crypto/SCsub +++ b/core/crypto/SCsub @@ -30,6 +30,9 @@ if not has_module: thirdparty_mbedtls_sources = [ "aes.c", "base64.c", + "constant_time.c", + "ctr_drbg.c", + "entropy.c", "md5.c", "sha1.c", "sha256.c", diff --git a/core/crypto/aes_context.cpp b/core/crypto/aes_context.cpp index b387aeb27d..8ff4f6a34c 100644 --- a/core/crypto/aes_context.cpp +++ b/core/crypto/aes_context.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/crypto/aes_context.h b/core/crypto/aes_context.h index 2f8422f537..2121adfcc6 100644 --- a/core/crypto/aes_context.h +++ b/core/crypto/aes_context.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/crypto/crypto.cpp b/core/crypto/crypto.cpp index fe913549c9..f62d2cce1f 100644 --- a/core/crypto/crypto.cpp +++ b/core/crypto/crypto.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/crypto/crypto.h b/core/crypto/crypto.h index a46f42949d..9de2c16fbc 100644 --- a/core/crypto/crypto.h +++ b/core/crypto/crypto.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/crypto/crypto_core.cpp b/core/crypto/crypto_core.cpp index f90092056e..3cf7b6c310 100644 --- a/core/crypto/crypto_core.cpp +++ b/core/crypto/crypto_core.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -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 7a2f4df589..eacef268cc 100644 --- a/core/crypto/crypto_core.h +++ b/core/crypto/crypto_core.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ @@ -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/crypto/hashing_context.cpp b/core/crypto/hashing_context.cpp index 070d2d4dd7..2ff540f798 100644 --- a/core/crypto/hashing_context.cpp +++ b/core/crypto/hashing_context.cpp @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ diff --git a/core/crypto/hashing_context.h b/core/crypto/hashing_context.h index 31521a147c..1a7cabf0f1 100644 --- a/core/crypto/hashing_context.h +++ b/core/crypto/hashing_context.h @@ -5,8 +5,8 @@ /* GODOT ENGINE */ /* https://godotengine.org */ /*************************************************************************/ -/* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */ -/* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */ +/* Copyright (c) 2007-2022 Juan Linietsky, Ariel Manzur. */ +/* Copyright (c) 2014-2022 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 */ |