crypto_core.cpp 9.8 KB

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