crypto_core.cpp 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. /*************************************************************************/
  2. /* crypto_core.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2021 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2021 Godot Engine contributors (cf. AUTHORS.md). */
  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 <mbedtls/aes.h>
  32. #include <mbedtls/base64.h>
  33. #include <mbedtls/md5.h>
  34. #include <mbedtls/sha1.h>
  35. #include <mbedtls/sha256.h>
  36. // MD5
  37. CryptoCore::MD5Context::MD5Context() {
  38. ctx = memalloc(sizeof(mbedtls_md5_context));
  39. mbedtls_md5_init((mbedtls_md5_context *)ctx);
  40. }
  41. CryptoCore::MD5Context::~MD5Context() {
  42. mbedtls_md5_free((mbedtls_md5_context *)ctx);
  43. memfree((mbedtls_md5_context *)ctx);
  44. }
  45. Error CryptoCore::MD5Context::start() {
  46. int ret = mbedtls_md5_starts_ret((mbedtls_md5_context *)ctx);
  47. return ret ? FAILED : OK;
  48. }
  49. Error CryptoCore::MD5Context::update(const uint8_t *p_src, size_t p_len) {
  50. int ret = mbedtls_md5_update_ret((mbedtls_md5_context *)ctx, p_src, p_len);
  51. return ret ? FAILED : OK;
  52. }
  53. Error CryptoCore::MD5Context::finish(unsigned char r_hash[16]) {
  54. int ret = mbedtls_md5_finish_ret((mbedtls_md5_context *)ctx, r_hash);
  55. return ret ? FAILED : OK;
  56. }
  57. // SHA1
  58. CryptoCore::SHA1Context::SHA1Context() {
  59. ctx = memalloc(sizeof(mbedtls_sha1_context));
  60. mbedtls_sha1_init((mbedtls_sha1_context *)ctx);
  61. }
  62. CryptoCore::SHA1Context::~SHA1Context() {
  63. mbedtls_sha1_free((mbedtls_sha1_context *)ctx);
  64. memfree((mbedtls_sha1_context *)ctx);
  65. }
  66. Error CryptoCore::SHA1Context::start() {
  67. int ret = mbedtls_sha1_starts_ret((mbedtls_sha1_context *)ctx);
  68. return ret ? FAILED : OK;
  69. }
  70. Error CryptoCore::SHA1Context::update(const uint8_t *p_src, size_t p_len) {
  71. int ret = mbedtls_sha1_update_ret((mbedtls_sha1_context *)ctx, p_src, p_len);
  72. return ret ? FAILED : OK;
  73. }
  74. Error CryptoCore::SHA1Context::finish(unsigned char r_hash[20]) {
  75. int ret = mbedtls_sha1_finish_ret((mbedtls_sha1_context *)ctx, r_hash);
  76. return ret ? FAILED : OK;
  77. }
  78. // SHA256
  79. CryptoCore::SHA256Context::SHA256Context() {
  80. ctx = memalloc(sizeof(mbedtls_sha256_context));
  81. mbedtls_sha256_init((mbedtls_sha256_context *)ctx);
  82. }
  83. CryptoCore::SHA256Context::~SHA256Context() {
  84. mbedtls_sha256_free((mbedtls_sha256_context *)ctx);
  85. memfree((mbedtls_sha256_context *)ctx);
  86. }
  87. Error CryptoCore::SHA256Context::start() {
  88. int ret = mbedtls_sha256_starts_ret((mbedtls_sha256_context *)ctx, 0);
  89. return ret ? FAILED : OK;
  90. }
  91. Error CryptoCore::SHA256Context::update(const uint8_t *p_src, size_t p_len) {
  92. int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len);
  93. return ret ? FAILED : OK;
  94. }
  95. Error CryptoCore::SHA256Context::finish(unsigned char r_hash[32]) {
  96. int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash);
  97. return ret ? FAILED : OK;
  98. }
  99. // AES256
  100. CryptoCore::AESContext::AESContext() {
  101. ctx = memalloc(sizeof(mbedtls_aes_context));
  102. mbedtls_aes_init((mbedtls_aes_context *)ctx);
  103. }
  104. CryptoCore::AESContext::~AESContext() {
  105. mbedtls_aes_free((mbedtls_aes_context *)ctx);
  106. memfree((mbedtls_aes_context *)ctx);
  107. }
  108. Error CryptoCore::AESContext::set_encode_key(const uint8_t *p_key, size_t p_bits) {
  109. int ret = mbedtls_aes_setkey_enc((mbedtls_aes_context *)ctx, p_key, p_bits);
  110. return ret ? FAILED : OK;
  111. }
  112. Error CryptoCore::AESContext::set_decode_key(const uint8_t *p_key, size_t p_bits) {
  113. int ret = mbedtls_aes_setkey_dec((mbedtls_aes_context *)ctx, p_key, p_bits);
  114. return ret ? FAILED : OK;
  115. }
  116. Error CryptoCore::AESContext::encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
  117. int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_src, r_dst);
  118. return ret ? FAILED : OK;
  119. }
  120. Error CryptoCore::AESContext::encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst) {
  121. int ret = mbedtls_aes_crypt_cbc((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_length, r_iv, p_src, r_dst);
  122. return ret ? FAILED : OK;
  123. }
  124. Error CryptoCore::AESContext::encrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst) {
  125. size_t iv_off = 0; // Ignore and assume 16-byte alignment.
  126. int ret = mbedtls_aes_crypt_cfb128((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_length, &iv_off, p_iv, p_src, r_dst);
  127. return ret ? FAILED : OK;
  128. }
  129. Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
  130. int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst);
  131. return ret ? FAILED : OK;
  132. }
  133. Error CryptoCore::AESContext::decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst) {
  134. int ret = mbedtls_aes_crypt_cbc((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_length, r_iv, p_src, r_dst);
  135. return ret ? FAILED : OK;
  136. }
  137. Error CryptoCore::AESContext::decrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst) {
  138. size_t iv_off = 0; // Ignore and assume 16-byte alignment.
  139. int ret = mbedtls_aes_crypt_cfb128((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_length, &iv_off, p_iv, p_src, r_dst);
  140. return ret ? FAILED : OK;
  141. }
  142. // CryptoCore
  143. String CryptoCore::b64_encode_str(const uint8_t *p_src, int p_src_len) {
  144. int b64len = p_src_len / 3 * 4 + 4 + 1;
  145. Vector<uint8_t> b64buff;
  146. b64buff.resize(b64len);
  147. uint8_t *w64 = b64buff.ptrw();
  148. size_t strlen = 0;
  149. int ret = b64_encode(&w64[0], b64len, &strlen, p_src, p_src_len);
  150. w64[strlen] = 0;
  151. return ret ? String() : (const char *)&w64[0];
  152. }
  153. 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) {
  154. int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len);
  155. return ret ? FAILED : OK;
  156. }
  157. 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) {
  158. int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len);
  159. return ret ? FAILED : OK;
  160. }
  161. Error CryptoCore::md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]) {
  162. int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash);
  163. return ret ? FAILED : OK;
  164. }
  165. Error CryptoCore::sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]) {
  166. int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash);
  167. return ret ? FAILED : OK;
  168. }
  169. Error CryptoCore::sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]) {
  170. int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0);
  171. return ret ? FAILED : OK;
  172. }