• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 N7z {
15 
16 const unsigned kKeySize = 32;
17 const unsigned kSaltSizeMax = 16;
18 const unsigned kIvSizeMax = 16; // AES_BLOCK_SIZE;
19 
20 class CKeyInfo
21 {
22 public:
23   unsigned NumCyclesPower;
24   unsigned SaltSize;
25   Byte Salt[kSaltSizeMax];
26   CByteBuffer Password;
27   Byte Key[kKeySize];
28 
29   bool IsEqualTo(const CKeyInfo &a) const;
30   void CalcKey();
31 
CKeyInfo()32   CKeyInfo() { ClearProps(); }
ClearProps()33   void ClearProps()
34   {
35     NumCyclesPower = 0;
36     SaltSize = 0;
37     for (unsigned i = 0; i < sizeof(Salt); i++)
38       Salt[i] = 0;
39   }
40 };
41 
42 class CKeyInfoCache
43 {
44   unsigned Size;
45   CObjectVector<CKeyInfo> Keys;
46 public:
CKeyInfoCache(unsigned size)47   CKeyInfoCache(unsigned size): Size(size) {}
48   bool GetKey(CKeyInfo &key);
49   void Add(const CKeyInfo &key);
50   void FindAndAdd(const CKeyInfo &key);
51 };
52 
53 class CBase
54 {
55   CKeyInfoCache _cachedKeys;
56 protected:
57   CKeyInfo _key;
58   Byte _iv[kIvSizeMax];
59   unsigned _ivSize;
60 
61   void PrepareKey();
62   CBase();
63 };
64 
65 class CBaseCoder:
66   public ICompressFilter,
67   public ICryptoSetPassword,
68   public CMyUnknownImp,
69   public CBase
70 {
71 protected:
72   CMyComPtr<ICompressFilter> _aesFilter;
73 
74 public:
75   INTERFACE_ICompressFilter(;)
76 
77   STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
78 };
79 
80 #ifndef EXTRACT_ONLY
81 
82 class CEncoder:
83   public CBaseCoder,
84   public ICompressWriteCoderProperties,
85   // public ICryptoResetSalt,
86   public ICryptoResetInitVector
87 {
88 public:
89   MY_UNKNOWN_IMP4(
90       ICompressFilter,
91       ICryptoSetPassword,
92       ICompressWriteCoderProperties,
93       // ICryptoResetSalt,
94       ICryptoResetInitVector)
95   STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
96   // STDMETHOD(ResetSalt)();
97   STDMETHOD(ResetInitVector)();
98   CEncoder();
99 };
100 
101 #endif
102 
103 class CDecoder:
104   public CBaseCoder,
105   public ICompressSetDecoderProperties2
106 {
107 public:
108   MY_UNKNOWN_IMP3(
109       ICompressFilter,
110       ICryptoSetPassword,
111       ICompressSetDecoderProperties2)
112   STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
113   CDecoder();
114 };
115 
116 }}
117 
118 #endif
119