AESCrypt.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifdef MMKV_DISABLE_CRYPT
  26. namespace mmkv {
  27. class AESCrypt;
  28. }
  29. #else
  30. namespace openssl {
  31. struct AES_KEY;
  32. }
  33. namespace mmkv {
  34. #pragma pack(push, 1)
  35. struct AESCryptStatus {
  36. uint8_t m_number;
  37. uint8_t m_vector[AES_KEY_LEN];
  38. };
  39. #pragma pack(pop)
  40. class CodedInputDataCrypt;
  41. // a AES CFB-128 encrypt-decrypt full-duplex wrapper
  42. class AESCrypt {
  43. bool m_isClone = false;
  44. uint32_t m_number = 0;
  45. openssl::AES_KEY *m_aesKey = nullptr;
  46. openssl::AES_KEY *m_aesRollbackKey = nullptr;
  47. uint8_t m_key[AES_KEY_LEN] = {};
  48. public:
  49. uint8_t m_vector[AES_KEY_LEN] = {};
  50. private:
  51. // for cloneWithStatus()
  52. AESCrypt(const AESCrypt &other, const AESCryptStatus &status);
  53. public:
  54. AESCrypt(const void *key, size_t keyLength, const void *iv = nullptr, size_t ivLength = 0);
  55. AESCrypt(AESCrypt &&other) = default;
  56. ~AESCrypt();
  57. void encrypt(const void *input, void *output, size_t length);
  58. void decrypt(const void *input, void *output, size_t length);
  59. void getCurStatus(AESCryptStatus &status);
  60. void statusBeforeDecrypt(const void *input, const void *output, size_t length, AESCryptStatus &status);
  61. AESCrypt cloneWithStatus(const AESCryptStatus &status) const;
  62. void resetIV(const void *iv = nullptr, size_t ivLength = 0);
  63. void resetStatus(const AESCryptStatus &status);
  64. // output must have [AES_KEY_LEN] space
  65. void getKey(void *output) const;
  66. static void fillRandomIV(void *vector);
  67. // just forbid it for possibly misuse
  68. explicit AESCrypt(const AESCrypt &other) = delete;
  69. AESCrypt &operator=(const AESCrypt &other) = delete;
  70. friend CodedInputDataCrypt;
  71. #ifdef MMKV_DEBUG
  72. // check if AESCrypt is encrypt-decrypt full-duplex
  73. static void testAESCrypt();
  74. #endif
  75. };
  76. } // namespace mmkv
  77. #endif // MMKV_DISABLE_CRYPT
  78. #endif // __cplusplus
  79. #endif /* AES_CRYPT_H_ */