"Fossies" - the Fresh Open Source Software Archive

Member "unrar/crypt.hpp" (4 May 2022, 2776 Bytes) of package /linux/misc/unrarsrc-6.1.7.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "crypt.hpp" see the Fossies "Dox" file reference documentation.

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