AESCrypt.h 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  1. /*
  2. * Tencent is pleased to support the open source community by making
  3. * MMKV available.
  4. *
  5. * Copyright (C) 2018 THL A29 Limited, a Tencent company.
  6. * All rights reserved.
  7. *
  8. * Licensed under the BSD 3-Clause License (the "License"); you may not use
  9. * this file except in compliance with the License. You may obtain a copy of
  10. * the License at
  11. *
  12. * https://opensource.org/licenses/BSD-3-Clause
  13. *
  14. * Unless required by applicable law or agreed to in writing, software
  15. * distributed under the License is distributed on an "AS IS" BASIS,
  16. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  17. * See the License for the specific language governing permissions and
  18. * limitations under the License.
  19. */
  20. #ifndef AES_CRYPT_H_
  21. #define AES_CRYPT_H_
  22. #ifdef __cplusplus
  23. #include "../MMKVPredef.h"
  24. #include <cstddef>
  25. #include <cstdint>
  26. #ifdef MMKV_DISABLE_CRYPT
  27. namespace mmkv {
  28. class AESCrypt {
  29. public:
  30. static uint32_t randomItemSizeHolder(uint32_t size);
  31. };
  32. }
  33. #else
  34. namespace openssl {
  35. struct AES_KEY;
  36. }
  37. namespace mmkv {
  38. #pragma pack(push, 1)
  39. struct AESCryptStatus {
  40. uint8_t m_number;
  41. uint8_t m_vector[AES_KEY_LEN];
  42. };
  43. #pragma pack(pop)
  44. class CodedInputDataCrypt;
  45. // a AES CFB-128 encrypt-decrypt full-duplex wrapper
  46. class AESCrypt {
  47. bool m_isClone = false;
  48. uint32_t m_number = 0;
  49. openssl::AES_KEY *m_aesKey = nullptr;
  50. openssl::AES_KEY *m_aesRollbackKey = nullptr;
  51. uint8_t m_key[AES_KEY_LEN] = {};
  52. public:
  53. uint8_t m_vector[AES_KEY_LEN] = {};
  54. private:
  55. // for cloneWithStatus()
  56. AESCrypt(const AESCrypt &other, const AESCryptStatus &status);
  57. public:
  58. AESCrypt(const void *key, size_t keyLength, const void *iv = nullptr, size_t ivLength = 0);
  59. AESCrypt(AESCrypt &&other) = default;
  60. ~AESCrypt();
  61. void encrypt(const void *input, void *output, size_t length);
  62. void decrypt(const void *input, void *output, size_t length);
  63. void getCurStatus(AESCryptStatus &status);
  64. void statusBeforeDecrypt(const void *input, const void *output, size_t length, AESCryptStatus &status);
  65. AESCrypt cloneWithStatus(const AESCryptStatus &status) const;
  66. void resetIV(const void *iv = nullptr, size_t ivLength = 0);
  67. void resetStatus(const AESCryptStatus &status);
  68. // output must have [AES_KEY_LEN] space
  69. void getKey(void *output) const;
  70. static void fillRandomIV(void *vector);
  71. static uint32_t randomItemSizeHolder(uint32_t size);
  72. // just forbid it for possibly misuse
  73. explicit AESCrypt(const AESCrypt &other) = delete;
  74. AESCrypt &operator=(const AESCrypt &other) = delete;
  75. friend CodedInputDataCrypt;
  76. #ifdef MMKV_DEBUG
  77. // check if AESCrypt is encrypt-decrypt full-duplex
  78. static void testAESCrypt();
  79. #endif
  80. };
  81. } // namespace mmkv
  82. #endif // MMKV_DISABLE_CRYPT
  83. #endif // __cplusplus
  84. #endif /* AES_CRYPT_H_ */