crypto_core.h 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. /**************************************************************************/
  2. /* crypto_core.h */
  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. #ifndef CRYPTO_CORE_H
  31. #define CRYPTO_CORE_H
  32. #include "core/object/ref_counted.h"
  33. class CryptoCore {
  34. public:
  35. class RandomGenerator {
  36. private:
  37. void *entropy = nullptr;
  38. void *ctx = nullptr;
  39. static int _entropy_poll(void *p_data, unsigned char *r_buffer, size_t p_len, size_t *r_len);
  40. public:
  41. RandomGenerator();
  42. ~RandomGenerator();
  43. Error init();
  44. Error get_random_bytes(uint8_t *r_buffer, size_t p_bytes);
  45. };
  46. class MD5Context {
  47. private:
  48. void *ctx = nullptr;
  49. public:
  50. MD5Context();
  51. ~MD5Context();
  52. Error start();
  53. Error update(const uint8_t *p_src, size_t p_len);
  54. Error finish(unsigned char r_hash[16]);
  55. };
  56. class SHA1Context {
  57. private:
  58. void *ctx = nullptr;
  59. public:
  60. SHA1Context();
  61. ~SHA1Context();
  62. Error start();
  63. Error update(const uint8_t *p_src, size_t p_len);
  64. Error finish(unsigned char r_hash[20]);
  65. };
  66. class SHA256Context {
  67. private:
  68. void *ctx = nullptr;
  69. public:
  70. SHA256Context();
  71. ~SHA256Context();
  72. Error start();
  73. Error update(const uint8_t *p_src, size_t p_len);
  74. Error finish(unsigned char r_hash[32]);
  75. };
  76. class AESContext {
  77. private:
  78. void *ctx = nullptr;
  79. public:
  80. AESContext();
  81. ~AESContext();
  82. Error set_encode_key(const uint8_t *p_key, size_t p_bits);
  83. Error set_decode_key(const uint8_t *p_key, size_t p_bits);
  84. Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
  85. Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
  86. Error encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
  87. Error decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
  88. Error encrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst);
  89. Error decrypt_cfb(size_t p_length, uint8_t p_iv[16], const uint8_t *p_src, uint8_t *r_dst);
  90. };
  91. static String b64_encode_str(const uint8_t *p_src, int p_src_len);
  92. static Error b64_encode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len);
  93. static Error b64_decode(uint8_t *r_dst, int p_dst_len, size_t *r_len, const uint8_t *p_src, int p_src_len);
  94. static Error md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]);
  95. static Error sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]);
  96. static Error sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]);
  97. };
  98. #endif // CRYPTO_CORE_H