|
| 1 | +/**************************************************************************/ |
| 2 | +/* std_mbedtls_platform.cpp */ |
| 3 | +/**************************************************************************/ |
| 4 | +/* This file is part of: */ |
| 5 | +/* GODOT ENGINE */ |
| 6 | +/* https://godotengine.org */ |
| 7 | +/**************************************************************************/ |
| 8 | +/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */ |
| 9 | +/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */ |
| 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 <godot_cpp/classes/os.hpp> |
| 32 | + |
| 33 | +#include <mbedtls/build_info.h> |
| 34 | +#include <mbedtls/threading.h> |
| 35 | +#include <psa/crypto.h> |
| 36 | + |
| 37 | +#include <mutex> |
| 38 | + |
| 39 | +#if !defined(MBEDTLS_THREADING_C) |
| 40 | +#error "Godot Git Plugin require mbedTLS with threading support" |
| 41 | +#endif |
| 42 | + |
| 43 | +using namespace godot; |
| 44 | + |
| 45 | +extern "C" { |
| 46 | +#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) |
| 47 | +int mbedtls_hardware_poll(void *p_data, unsigned char *r_output, size_t p_output_size, size_t *r_output_len) { |
| 48 | + *r_output_len = 0; |
| 49 | + ERR_FAIL_NULL_V(OS::get_singleton(), -1); |
| 50 | + PackedByteArray rnd = OS::get_singleton()->get_entropy(p_output_size); |
| 51 | + ERR_FAIL_COND_V(rnd.size() != p_output_size, -1); |
| 52 | + memcpy(r_output, rnd.ptr(), p_output_size); |
| 53 | + *r_output_len = p_output_size; |
| 54 | + return 0; |
| 55 | +} |
| 56 | + |
| 57 | +#endif |
| 58 | +#if defined(MBEDTLS_THREADING_ALT) |
| 59 | +void std_mbedtls_mutex_init(mbedtls_platform_mutex_t *p_mutex) { |
| 60 | + if (p_mutex == nullptr) { |
| 61 | + return; |
| 62 | + } |
| 63 | + p_mutex->mutex = new std::mutex(); |
| 64 | +} |
| 65 | + |
| 66 | +void std_mbedtls_mutex_free(mbedtls_platform_mutex_t *p_mutex) { |
| 67 | + if (p_mutex == nullptr || p_mutex->mutex == nullptr) { |
| 68 | + return; |
| 69 | + } |
| 70 | + delete ((std::mutex *)p_mutex->mutex); |
| 71 | +} |
| 72 | + |
| 73 | +int std_mbedtls_mutex_lock(mbedtls_platform_mutex_t *p_mutex) { |
| 74 | + if (p_mutex == nullptr || p_mutex->mutex == nullptr) { |
| 75 | + return MBEDTLS_ERR_THREADING_USAGE_ERROR; |
| 76 | + } |
| 77 | + ((std::mutex *)p_mutex->mutex)->lock(); |
| 78 | + return 0; |
| 79 | +} |
| 80 | + |
| 81 | +int std_mbedtls_mutex_unlock(mbedtls_platform_mutex_t *p_mutex) { |
| 82 | + if (p_mutex == nullptr || p_mutex->mutex == nullptr) { |
| 83 | + return MBEDTLS_ERR_THREADING_USAGE_ERROR; |
| 84 | + } |
| 85 | + ((std::mutex *)p_mutex->mutex)->unlock(); |
| 86 | + return 0; |
| 87 | +} |
| 88 | +#endif |
| 89 | +int std_mbedtls_platform_init() { |
| 90 | +#if defined(MBEDTLS_THREADING_ALT) |
| 91 | + mbedtls_threading_set_alt( |
| 92 | + std_mbedtls_mutex_init, |
| 93 | + std_mbedtls_mutex_free, |
| 94 | + std_mbedtls_mutex_lock, |
| 95 | + std_mbedtls_mutex_unlock); |
| 96 | +#endif |
| 97 | + return psa_crypto_init(); |
| 98 | +} |
| 99 | +void std_mbedtls_platform_free() { |
| 100 | +#if defined(MBEDTLS_THREADING_ALT) |
| 101 | + mbedtls_threading_free_alt(); |
| 102 | +#endif |
| 103 | +} |
| 104 | +}; |
0 commit comments