crypto_core.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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/reference.h"
  33. class CryptoCore {
  34. public:
  35. class MD5Context {
  36. private:
  37. void *ctx; // To include, or not to include...
  38. public:
  39. MD5Context();
  40. ~MD5Context();
  41. Error start();
  42. Error update(const uint8_t *p_src, size_t p_len);
  43. Error finish(unsigned char r_hash[16]);
  44. };
  45. class SHA1Context {
  46. private:
  47. void *ctx; // To include, or not to include...
  48. public:
  49. SHA1Context();
  50. ~SHA1Context();
  51. Error start();
  52. Error update(const uint8_t *p_src, size_t p_len);
  53. Error finish(unsigned char r_hash[20]);
  54. };
  55. class SHA256Context {
  56. private:
  57. void *ctx; // To include, or not to include...
  58. public:
  59. SHA256Context();
  60. ~SHA256Context();
  61. Error start();
  62. Error update(const uint8_t *p_src, size_t p_len);
  63. Error finish(unsigned char r_hash[32]);
  64. };
  65. class AESContext {
  66. private:
  67. void *ctx; // To include, or not to include...
  68. public:
  69. AESContext();
  70. ~AESContext();
  71. Error set_encode_key(const uint8_t *p_key, size_t p_bits);
  72. Error set_decode_key(const uint8_t *p_key, size_t p_bits);
  73. Error encrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
  74. Error decrypt_ecb(const uint8_t p_src[16], uint8_t r_dst[16]);
  75. Error encrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
  76. Error decrypt_cbc(size_t p_length, uint8_t r_iv[16], const uint8_t *p_src, uint8_t *r_dst);
  77. };
  78. static String b64_encode_str(const uint8_t *p_src, int p_src_len);
  79. 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);
  80. 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);
  81. static Error md5(const uint8_t *p_src, int p_src_len, unsigned char r_hash[16]);
  82. static Error sha1(const uint8_t *p_src, int p_src_len, unsigned char r_hash[20]);
  83. static Error sha256(const uint8_t *p_src, int p_src_len, unsigned char r_hash[32]);
  84. };
  85. #endif // CRYPTO_CORE_H