crypto_core.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. /*************************************************************************/
  2. /* crypto_core.cpp */
  3. /*************************************************************************/
  4. /* This file is part of: */
  5. /* GODOT ENGINE */
  6. /* https://godotengine.org */
  7. /*************************************************************************/
  8. /* Copyright (c) 2007-2019 Juan Linietsky, Ariel Manzur. */
  9. /* Copyright (c) 2014-2019 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(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. // SHA256
  58. CryptoCore::SHA256Context::SHA256Context() {
  59. ctx = memalloc(sizeof(mbedtls_sha256_context));
  60. mbedtls_sha256_init((mbedtls_sha256_context *)ctx);
  61. }
  62. CryptoCore::SHA256Context::~SHA256Context() {
  63. mbedtls_sha256_free((mbedtls_sha256_context *)ctx);
  64. memfree((mbedtls_sha256_context *)ctx);
  65. }
  66. Error CryptoCore::SHA256Context::start() {
  67. int ret = mbedtls_sha256_starts_ret((mbedtls_sha256_context *)ctx, 0);
  68. return ret ? FAILED : OK;
  69. }
  70. Error CryptoCore::SHA256Context::update(uint8_t *p_src, size_t p_len) {
  71. int ret = mbedtls_sha256_update_ret((mbedtls_sha256_context *)ctx, p_src, p_len);
  72. return ret ? FAILED : OK;
  73. }
  74. Error CryptoCore::SHA256Context::finish(unsigned char r_hash[16]) {
  75. int ret = mbedtls_sha256_finish_ret((mbedtls_sha256_context *)ctx, r_hash);
  76. return ret ? FAILED : OK;
  77. }
  78. // AES256
  79. CryptoCore::AESContext::AESContext() {
  80. ctx = memalloc(sizeof(mbedtls_aes_context));
  81. mbedtls_aes_init((mbedtls_aes_context *)ctx);
  82. }
  83. CryptoCore::AESContext::~AESContext() {
  84. mbedtls_aes_free((mbedtls_aes_context *)ctx);
  85. memfree((mbedtls_aes_context *)ctx);
  86. }
  87. Error CryptoCore::AESContext::set_encode_key(const uint8_t *p_key, size_t p_bits) {
  88. int ret = mbedtls_aes_setkey_enc((mbedtls_aes_context *)ctx, p_key, p_bits);
  89. return ret ? FAILED : OK;
  90. }
  91. Error CryptoCore::AESContext::set_decode_key(const uint8_t *p_key, size_t p_bits) {
  92. int ret = mbedtls_aes_setkey_dec((mbedtls_aes_context *)ctx, p_key, p_bits);
  93. return ret ? FAILED : OK;
  94. }
  95. Error CryptoCore::AESContext::encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
  96. int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_ENCRYPT, p_src, r_dst);
  97. return ret ? FAILED : OK;
  98. }
  99. Error CryptoCore::AESContext::decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]) {
  100. int ret = mbedtls_aes_crypt_ecb((mbedtls_aes_context *)ctx, MBEDTLS_AES_DECRYPT, p_src, r_dst);
  101. return ret ? FAILED : OK;
  102. }
  103. // CryptoCore
  104. 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) {
  105. int ret = mbedtls_base64_encode(r_dst, p_dst_len, r_len, p_src, p_src_len);
  106. return ret ? FAILED : OK;
  107. }
  108. 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) {
  109. int ret = mbedtls_base64_decode(r_dst, p_dst_len, r_len, p_src, p_src_len);
  110. return ret ? FAILED : OK;
  111. }
  112. Error CryptoCore::md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]) {
  113. int ret = mbedtls_md5_ret(p_src, p_src_len, r_hash);
  114. return ret ? FAILED : OK;
  115. }
  116. Error CryptoCore::sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]) {
  117. int ret = mbedtls_sha1_ret(p_src, p_src_len, r_hash);
  118. return ret ? FAILED : OK;
  119. }
  120. Error CryptoCore::sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]) {
  121. int ret = mbedtls_sha256_ret(p_src, p_src_len, r_hash, 0);
  122. return ret ? FAILED : OK;
  123. }