| 1 | /**************************************************************************/ |
| 2 | /* crypto_core.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 "crypto_core.h" |
| 32 | |
| 33 | #include "core/os/os.h" |
| 34 | |
| 35 | #include <mbedtls/aes.h> |
| 36 | #include <mbedtls/base64.h> |
| 37 | #include <mbedtls/ctr_drbg.h> |
| 38 | #include <mbedtls/entropy.h> |
| 39 | #include <mbedtls/md5.h> |
| 40 | #include <mbedtls/sha1.h> |
| 41 | #include <mbedtls/sha256.h> |
| 42 | #if MBEDTLS_VERSION_MAJOR >= 3 |
| 43 | #include <mbedtls/compat-2.x.h> |
| 44 | #endif |
| 45 | |
| 46 | // RandomGenerator |
| 47 | CryptoCore::RandomGenerator::RandomGenerator() { |
| 48 | entropy = memalloc(sizeof(mbedtls_entropy_context)); |
| 49 | mbedtls_entropy_init((mbedtls_entropy_context *)entropy); |
| 50 | mbedtls_entropy_add_source((mbedtls_entropy_context *)entropy, &CryptoCore::RandomGenerator::_entropy_poll, nullptr, 256, MBEDTLS_ENTROPY_SOURCE_STRONG); |
| 51 | ctx = memalloc(sizeof(mbedtls_ctr_drbg_context)); |
| 52 | mbedtls_ctr_drbg_init((mbedtls_ctr_drbg_context *)ctx); |
| 53 | } |
| 54 | |
| 55 | CryptoCore::RandomGenerator::~RandomGenerator() { |
| 56 | mbedtls_ctr_drbg_free((mbedtls_ctr_drbg_context *)ctx); |
| 57 | memfree(ctx); |
| 58 | mbedtls_entropy_free((mbedtls_entropy_context *)entropy); |
| 59 | memfree(entropy); |
| 60 | } |
| 61 | |
| 62 | int CryptoCore::RandomGenerator::_entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len) { |
| 63 | *r_len = 0; |
| 64 | Error err = OS::get_singleton()->get_entropy(r_buffer, p_len); |
| 65 | ERR_FAIL_COND_V(err, MBEDTLS_ERR_ENTROPY_SOURCE_FAILED); |
| 66 | *r_len = p_len; |
| 67 | return 0; |
| 68 | } |
| 69 | |
| 70 | Error CryptoCore::RandomGenerator::init() { |
| 71 | int ret = mbedtls_ctr_drbg_seed((mbedtls_ctr_drbg_context *)ctx, mbedtls_entropy_func, (mbedtls_entropy_context *)entropy, nullptr, 0); |
| 72 | if (ret) { |
| 73 | ERR_FAIL_COND_V_MSG(ret, FAILED, vformat(" failed\n ! mbedtls_ctr_drbg_seed returned an error %d.", ret)); |
| 74 | } |
| 75 | return OK; |
| 76 | } |
| 77 | |
| 78 | Error CryptoCore::RandomGenerator::get_random_bytes(uint8_t *r_buffer, size_t p_bytes) { |
| 79 | ERR_FAIL_NULL_V(ctx, ERR_UNCONFIGURED); |
| 80 | int ret = mbedtls_ctr_drbg_random((mbedtls_ctr_drbg_context *)ctx, r_buffer, p_bytes); |
| 81 | ERR_FAIL_COND_V_MSG(ret, FAILED, vformat(" failed\n ! mbedtls_ctr_drbg_seed returned an error %d.", ret)); |
| 82 | return OK; |
| 83 | } |
| 84 | |
| 85 | // MD5 |
| 86 | CryptoCore::MD5Context::MD5Context() { |
| 87 | ctx = memalloc(sizeof(mbedtls_md5_context)); |
| 88 | mbedtls_md5_init((mbedtls_md5_context *)ctx); |
| 89 | } |
| 90 | |
| 91 | CryptoCore::MD5Context::~MD5Context() { |
| 92 | mbedtls_md5_free((mbedtls_md5_context *)ctx); |
| 93 | memfree((mbedtls_md5_context *)ctx); |
| 94 | } |
| 95 | |
| 96 | Error CryptoCore::MD5Context::start() { |
| 97 | int ret = mbedtls_md5_starts_ret((mbedtls_md5_context *)ctx); |
| 98 | return ret ? FAILED : OK; |
| 99 | } |
| 100 | |
| 101 | Error CryptoCore::MD5Context::update(const uint8_t *p_src, size_t p_len) { |
| 102 | int ret = mbedtls_md5_update_ret((mbedtls_md5_context *)ctx, p_src, p_len); |
| 103 | return ret ? FAILED : OK; |
| 104 | } |
| 105 | |
| 106 | Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) { |
| 107 | int ret = mbedtls_md5_finish_ret((mbedtls_md5_context *)ctx, r_hash); |
| 108 | return ret ? FAILED : OK; |
| 109 | } |
| 110 | |
| 111 | // SHA1 |
| 112 | CryptoCore::SHA1Context::SHA1Context() { |
| 113 | ctx = memalloc(sizeof(mbedtls_sha1_context)); |
| 114 | mbedtls_sha1_init((mbedtls_sha1_context *)ctx); |
| 115 | } |
| 116 | |
| 117 | CryptoCore::SHA1Context::~SHA1Context() { |
| 118 | mbedtls_sha1_free((mbedtls_sha1_context *)ctx); |
| 119 | memfree((mbedtls_sha1_context *)ctx); |
| 120 | } |
| 121 | |
| 122 | Error CryptoCore::SHA1Context::start() { |
| 123 | int ret = mbedtls_sha1_starts_ret((mbedtls_sha1_context *)ctx); |
| 124 | return ret ? FAILED : OK; |
| 125 | } |
| 126 | |
| 127 | Error CryptoCore::SHA1Context::update(const uint8_t *p_src, size_t p_len) { |
| 128 | int ret = mbedtls_sha1_update_ret((mbedtls_sha1_context *)ctx, p_src, p_len); |
| 129 | return ret ? FAILED : OK; |
| 130 | } |
| 131 | |
| 132 | Error CryptoCore::SHA1Context::finish(unsigned char r_hash[20]) { |
| 133 | int ret = mbedtls_sha1_finish_ret((mbedtls_sha1_context *)ctx, r_hash); |
| 134 | return ret ? FAILED : OK; |
| 135 | } |
| 136 | |
| 137 | // SHA256 |
| 138 | CryptoCore::SHA256Context::SHA256Context() { |
| 139 | ctx = memalloc(sizeof(mbedtls_sha256_context)); |
| 140 | mbedtls_sha256_init((mbedtls_sha256_context *)ctx); |
| 141 | } |
| 142 | |
| 143 | CryptoCore::SHA256Context::~SHA256Context() { |
| 144 | mbedtls_sha256_free((mbedtls_sha256_context *)ctx); |
| 145 | memfree((mbedtls_sha256_context *)ctx); |
| 146 | } |
| 147 | |
| 148 | Error CryptoCore::SHA256Context::start() { |
| 149 | int ret = mbedtls_sha256_starts_ret((mbedtls_sha256_context *)ctx, 0); |
| 150 | return ret ? FAILED : OK; |
| 151 | } |
| 152 | |
| 153 | Error CryptoCore::SHA256Context::update(const uint8_t *p_src, size_t p_len) { |
| 154 | int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len); |
| 155 | return ret ? FAILED : OK; |
| 156 | } |
| 157 | |
| 158 | Error CryptoCore::SHA256Context::finish(unsigned char r_hash[32]) { |
| 159 | int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash); |
| 160 | return ret ? FAILED : OK; |
| 161 | } |
| 162 | |
| 163 | // AES256 |
| 164 | CryptoCore::AESContext::AESContext() { |
| 165 | ctx = memalloc(sizeof(mbedtls_aes_context)); |
| 166 | mbedtls_aes_init((mbedtls_aes_context *)ctx); |
| 167 | } |
| 168 | |
| 169 | CryptoCore::AESContext::~AESContext() { |
| 170 | mbedtls_aes_free((mbedtls_aes_context *)ctx); |
| 171 | memfree((mbedtls_aes_context *)ctx); |
| 172 | } |
| 173 | |
| 174 | Error CryptoCore::AESContext::set_encode_key(const uint8_t *p_key, size_t p_bits) { |
| 175 | int ret = mbedtls_aes_setkey_enc((mbedtls_aes_context *)ctx, p_key, p_bits); |
| 176 | return ret ? FAILED : OK; |
| 177 | } |
| 178 | |
| 179 | Error CryptoCore::AESContext::set_decode_key(const uint8_t *p_key, size_t p_bits) { |
| 180 | int ret = mbedtls_aes_setkey_dec((mbedtls_aes_context *)ctx, p_key, p_bits); |
| 181 | return ret ? FAILED : OK; |
| 182 | } |
| 183 | |
| 184 | Error CryptoCore::AESContext::encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) { |
| 185 | int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_src, r_dst); |
| 186 | return ret ? FAILED : OK; |
| 187 | } |
| 188 | |
| 189 | Error CryptoCore::AESContext::encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst) { |
| 190 | int ret = mbedtls_aes_crypt_cbc((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_length, r_iv, p_src, r_dst); |
| 191 | return ret ? FAILED : OK; |
| 192 | } |
| 193 | |
| 194 | Error CryptoCore::AESContext::encrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst) { |
| 195 | size_t iv_off = 0; // Ignore and assume 16-byte alignment. |
| 196 | int ret = mbedtls_aes_crypt_cfb128((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_length, &iv_off, p_iv, p_src, r_dst); |
| 197 | return ret ? FAILED : OK; |
| 198 | } |
| 199 | |
| 200 | Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) { |
| 201 | int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst); |
| 202 | return ret ? FAILED : OK; |
| 203 | } |
| 204 | |
| 205 | Error CryptoCore::AESContext::decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst) { |
| 206 | int ret = mbedtls_aes_crypt_cbc((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_length, r_iv, p_src, r_dst); |
| 207 | return ret ? FAILED : OK; |
| 208 | } |
| 209 | |
| 210 | Error CryptoCore::AESContext::decrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst) { |
| 211 | size_t iv_off = 0; // Ignore and assume 16-byte alignment. |
| 212 | int ret = mbedtls_aes_crypt_cfb128((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_length, &iv_off, p_iv, p_src, r_dst); |
| 213 | return ret ? FAILED : OK; |
| 214 | } |
| 215 | |
| 216 | // CryptoCore |
| 217 | String CryptoCore::b64_encode_str(const uint8_t *p_src, size_t p_src_len) { |
| 218 | size_t b64len = p_src_len / 3 * 4 + 4 + 1; |
| 219 | Vector<uint8_t> b64buff; |
| 220 | b64buff.resize(b64len); |
| 221 | uint8_t *w64 = b64buff.ptrw(); |
| 222 | size_t strlen = 0; |
| 223 | int ret = b64_encode(&w64[0], b64len, &strlen, p_src, p_src_len); |
| 224 | w64[strlen] = 0; |
| 225 | return ret ? String() : (const char *)&w64[0]; |
| 226 | } |
| 227 | |
| 228 | Error CryptoCore::b64_encode(uint8_t *r_dst, size_t p_dst_len, size_t *r_len, const uint8_t *p_src, size_t p_src_len) { |
| 229 | int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len); |
| 230 | return ret ? FAILED : OK; |
| 231 | } |
| 232 | |
| 233 | Error CryptoCore::b64_decode(uint8_t *r_dst, size_t p_dst_len, size_t *r_len, const uint8_t *p_src, size_t p_src_len) { |
| 234 | int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len); |
| 235 | return ret ? FAILED : OK; |
| 236 | } |
| 237 | |
| 238 | Error CryptoCore::md5(const uint8_t *p_src, size_t p_src_len, unsigned char r_hash[16]) { |
| 239 | int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash); |
| 240 | return ret ? FAILED : OK; |
| 241 | } |
| 242 | |
| 243 | Error CryptoCore::sha1(const uint8_t *p_src, size_t p_src_len, unsigned char r_hash[20]) { |
| 244 | int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash); |
| 245 | return ret ? FAILED : OK; |
| 246 | } |
| 247 | |
| 248 | Error CryptoCore::sha256(const uint8_t *p_src, size_t p_src_len, unsigned char r_hash[32]) { |
| 249 | int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0); |
| 250 | return ret ? FAILED : OK; |
| 251 | } |