crypto_core.cpp 9.8 KB

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