crypto_core.cpp 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  1. /*************************************************************************/
  2. /* crypto_core.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2020 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2020 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::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
  121. int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst);
  122. return ret ? FAILED : OK;
  123. }
  124. Error CryptoCore::AESContext::encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst) {
  125. int ret = mbedtls_aes_crypt_cbc((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_length, r_iv, p_src, r_dst);
  126. return ret ? FAILED : OK;
  127. }
  128. Error CryptoCore::AESContext::decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst) {
  129. int ret = mbedtls_aes_crypt_cbc((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_length, r_iv, p_src, r_dst);
  130. return ret ? FAILED : OK;
  131. }
  132. // CryptoCore
  133. String CryptoCore::b64_encode_str(const uint8_t *p_src, int p_src_len) {
  134. int b64len = p_src_len / 3 * 4 + 4 + 1;
  135. Vector<uint8_t> b64buff;
  136. b64buff.resize(b64len);
  137. uint8_t *w64 = b64buff.ptrw();
  138. size_t strlen = 0;
  139. int ret = b64_encode(&w64[0], b64len, &strlen, p_src, p_src_len);
  140. w64[strlen] = 0;
  141. return ret ? String() : (const char *)&w64[0];
  142. }
  143. 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) {
  144. int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len);
  145. return ret ? FAILED : OK;
  146. }
  147. 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) {
  148. int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len);
  149. return ret ? FAILED : OK;
  150. }
  151. Error CryptoCore::md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]) {
  152. int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash);
  153. return ret ? FAILED : OK;
  154. }
  155. Error CryptoCore::sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]) {
  156. int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash);
  157. return ret ? FAILED : OK;
  158. }
  159. Error CryptoCore::sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]) {
  160. int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0);
  161. return ret ? FAILED : OK;
  162. }