Home | History | Annotate | Download | only in Crypto
      1 // 7zAes.h
      2 
      3 #ifndef __CRYPTO_7Z_AES_H
      4 #define __CRYPTO_7Z_AES_H
      5 
      6 #include "../../Common/MyBuffer.h"
      7 #include "../../Common/MyCom.h"
      8 #include "../../Common/MyVector.h"
      9 
     10 #include "../ICoder.h"
     11 #include "../IPassword.h"
     12 
     13 namespace NCrypto {
     14 namespace NSevenZ {
     15 
     16 const int kKeySize = 32;
     17 
     18 class CKeyInfo
     19 {
     20 public:
     21   int NumCyclesPower;
     22   UInt32 SaltSize;
     23   Byte Salt[16];
     24   CByteBuffer Password;
     25   Byte Key[kKeySize];
     26 
     27   bool IsEqualTo(const CKeyInfo &a) const;
     28   void CalculateDigest();
     29 
     30   CKeyInfo() { Init(); }
     31   void Init()
     32   {
     33     NumCyclesPower = 0;
     34     SaltSize = 0;
     35     for (int i = 0; i < sizeof(Salt); i++)
     36       Salt[i] = 0;
     37   }
     38 };
     39 
     40 class CKeyInfoCache
     41 {
     42   unsigned Size;
     43   CObjectVector<CKeyInfo> Keys;
     44 public:
     45   CKeyInfoCache(unsigned size): Size(size) {}
     46   bool Find(CKeyInfo &key);
     47   // HRESULT Calculate(CKeyInfo &key);
     48   void Add(CKeyInfo &key);
     49 };
     50 
     51 class CBase
     52 {
     53   CKeyInfoCache _cachedKeys;
     54 protected:
     55   CKeyInfo _key;
     56   Byte _iv[16];
     57   UInt32 _ivSize;
     58   void CalculateDigest();
     59   CBase();
     60 };
     61 
     62 class CBaseCoder:
     63   public ICompressFilter,
     64   public ICryptoSetPassword,
     65   public CMyUnknownImp,
     66   public CBase
     67 {
     68 protected:
     69   CMyComPtr<ICompressFilter> _aesFilter;
     70 
     71   virtual HRESULT CreateFilter() = 0;
     72   #ifndef CRYPTO_AES
     73   HRESULT CreateFilterFromDLL(REFCLSID clsID);
     74   #endif
     75 public:
     76   STDMETHOD(Init)();
     77   STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
     78 
     79   STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
     80 };
     81 
     82 #ifndef EXTRACT_ONLY
     83 
     84 class CEncoder:
     85   public CBaseCoder,
     86   public ICompressWriteCoderProperties,
     87   // public ICryptoResetSalt,
     88   public ICryptoResetInitVector
     89 {
     90   virtual HRESULT CreateFilter();
     91 public:
     92   MY_UNKNOWN_IMP3(
     93       ICryptoSetPassword,
     94       ICompressWriteCoderProperties,
     95       // ICryptoResetSalt,
     96       ICryptoResetInitVector)
     97   STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
     98   // STDMETHOD(ResetSalt)();
     99   STDMETHOD(ResetInitVector)();
    100 };
    101 #endif
    102 
    103 class CDecoder:
    104   public CBaseCoder,
    105   public ICompressSetDecoderProperties2
    106 {
    107   virtual HRESULT CreateFilter();
    108 public:
    109   MY_UNKNOWN_IMP2(
    110       ICryptoSetPassword,
    111       ICompressSetDecoderProperties2)
    112   STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
    113 };
    114 
    115 }}
    116 
    117 #endif
    118