crypt.hpp 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. #ifndef _RAR_CRYPT_
  2. #define _RAR_CRYPT_
  3. enum CRYPT_METHOD {
  4. CRYPT_NONE,CRYPT_RAR13,CRYPT_RAR15,CRYPT_RAR20,CRYPT_RAR30,CRYPT_RAR50
  5. };
  6. #define SIZE_SALT50 16
  7. #define SIZE_SALT30 8
  8. #define SIZE_INITV 16
  9. #define SIZE_PSWCHECK 8
  10. #define SIZE_PSWCHECK_CSUM 4
  11. #define CRYPT_BLOCK_SIZE 16
  12. #define CRYPT_BLOCK_MASK (CRYPT_BLOCK_SIZE-1) // 0xf
  13. #define CRYPT5_KDF_LG2_COUNT 15 // LOG2 of PDKDF2 iteration count.
  14. #define CRYPT5_KDF_LG2_COUNT_MAX 24 // LOG2 of maximum accepted iteration count.
  15. #define CRYPT_VERSION 0 // Supported encryption version.
  16. class CryptData
  17. {
  18. struct KDF5CacheItem
  19. {
  20. SecPassword Pwd;
  21. byte Salt[SIZE_SALT50];
  22. byte Key[32];
  23. uint Lg2Count; // Log2 of PBKDF2 repetition count.
  24. byte PswCheckValue[SHA256_DIGEST_SIZE];
  25. byte HashKeyValue[SHA256_DIGEST_SIZE];
  26. };
  27. struct KDF3CacheItem
  28. {
  29. SecPassword Pwd;
  30. byte Salt[SIZE_SALT30];
  31. byte Key[16];
  32. byte Init[16];
  33. bool SaltPresent;
  34. };
  35. private:
  36. void SetKey13(const char *Password);
  37. void Decrypt13(byte *Data,size_t Count);
  38. void SetKey15(const char *Password);
  39. void Crypt15(byte *Data,size_t Count);
  40. void SetKey20(const char *Password);
  41. void Swap20(byte *Ch1,byte *Ch2);
  42. void UpdKeys20(byte *Buf);
  43. void EncryptBlock20(byte *Buf);
  44. void DecryptBlock20(byte *Buf);
  45. void SetKey30(bool Encrypt,SecPassword *Password,const wchar *PwdW,const byte *Salt);
  46. void SetKey50(bool Encrypt,SecPassword *Password,const wchar *PwdW,const byte *Salt,const byte *InitV,uint Lg2Cnt,byte *HashKey,byte *PswCheck);
  47. KDF3CacheItem KDF3Cache[4];
  48. uint KDF3CachePos;
  49. KDF5CacheItem KDF5Cache[4];
  50. uint KDF5CachePos;
  51. CRYPT_METHOD Method;
  52. Rijndael rin;
  53. uint CRCTab[256]; // For RAR 1.5 and RAR 2.0 encryption.
  54. byte SubstTable20[256];
  55. uint Key20[4];
  56. byte Key13[3];
  57. ushort Key15[4];
  58. public:
  59. CryptData();
  60. ~CryptData();
  61. bool SetCryptKeys(bool Encrypt,CRYPT_METHOD Method,SecPassword *Password,
  62. const byte *Salt,const byte *InitV,uint Lg2Cnt,
  63. byte *HashKey,byte *PswCheck);
  64. void SetAV15Encryption();
  65. void SetCmt13Encryption();
  66. void EncryptBlock(byte *Buf,size_t Size);
  67. void DecryptBlock(byte *Buf,size_t Size);
  68. static void SetSalt(byte *Salt,size_t SaltSize);
  69. };
  70. void GetRnd(byte *RndBuf,size_t BufSize);
  71. void hmac_sha256(const byte *Key,size_t KeyLength,const byte *Data,
  72. size_t DataLength,byte *ResDigest);
  73. void pbkdf2(const byte *pass, size_t pass_len, const byte *salt,
  74. size_t salt_len,byte *key, byte *Value1, byte *Value2,
  75. uint rounds);
  76. void ConvertHashToMAC(HashValue *Value,byte *Key);
  77. #endif