1 // Crypto/Rar5Aes.h 2 3 #ifndef ZIP7_INC_CRYPTO_RAR5_AES_H 4 #define ZIP7_INC_CRYPTO_RAR5_AES_H 5 6 #include "../../../C/Sha256.h" 7 8 #include "../../Common/MyBuffer.h" 9 10 #include "MyAes.h" 11 12 namespace NCrypto { 13 namespace NRar5 { 14 15 const unsigned kSaltSize = 16; 16 const unsigned kPswCheckSize = 8; 17 const unsigned kAesKeySize = 32; 18 19 namespace NCryptoFlags 20 { 21 const unsigned kPswCheck = 1 << 0; 22 const unsigned kUseMAC = 1 << 1; 23 } 24 25 struct CKey 26 { 27 bool _needCalc; 28 29 unsigned _numIterationsLog; 30 Byte _salt[kSaltSize]; 31 CByteBuffer _password; 32 33 Byte _key[kAesKeySize]; 34 Byte _check_Calced[kPswCheckSize]; 35 Byte _hashKey[SHA256_DIGEST_SIZE]; 36 CopyCalcedKeysFromCKey37 void CopyCalcedKeysFrom(const CKey &k) 38 { 39 memcpy(_key, k._key, sizeof(_key)); 40 memcpy(_check_Calced, k._check_Calced, sizeof(_check_Calced)); 41 memcpy(_hashKey, k._hashKey, sizeof(_hashKey)); 42 } 43 IsKeyEqualToCKey44 bool IsKeyEqualTo(const CKey &key) 45 { 46 return (_numIterationsLog == key._numIterationsLog 47 && memcmp(_salt, key._salt, sizeof(_salt)) == 0 48 && _password == key._password); 49 } 50 51 CKey(); 52 WipeCKey53 void Wipe() 54 { 55 _password.Wipe(); 56 Z7_memset_0_ARRAY(_salt); 57 Z7_memset_0_ARRAY(_key); 58 Z7_memset_0_ARRAY(_check_Calced); 59 Z7_memset_0_ARRAY(_hashKey); 60 } 61 62 #ifdef Z7_CPP_IS_SUPPORTED_default 63 // CKey(const CKey &) = default; 64 CKey& operator =(const CKey &) = default; 65 #endif ~CKeyCKey66 ~CKey() { Wipe(); } 67 }; 68 69 70 class CDecoder Z7_final: 71 public CAesCbcDecoder, 72 public CKey 73 { 74 Byte _check[kPswCheckSize]; 75 bool _canCheck; 76 UInt64 Flags; 77 IsThereCheck()78 bool IsThereCheck() const { return ((Flags & NCryptoFlags::kPswCheck) != 0); } 79 public: 80 Byte _iv[AES_BLOCK_SIZE]; 81 82 CDecoder(); 83 84 Z7_COM7F_IMP(Init()) 85 86 void SetPassword(const Byte *data, size_t size); 87 HRESULT SetDecoderProps(const Byte *data, unsigned size, bool includeIV, bool isService); 88 89 bool CalcKey_and_CheckPassword(); 90 UseMAC()91 bool UseMAC() const { return (Flags & NCryptoFlags::kUseMAC) != 0; } 92 UInt32 Hmac_Convert_Crc32(UInt32 crc) const; 93 void Hmac_Convert_32Bytes(Byte *data) const; 94 }; 95 96 }} 97 98 #endif 99