xref: /aosp_15_r20/external/lzma/CPP/7zip/Crypto/7zAes.h (revision f6dc9357d832569d4d1f5d24eacdb3935a1ae8e6)
1 // 7zAes.h
2 
3 #ifndef ZIP7_INC_CRYPTO_7Z_AES_H
4 #define ZIP7_INC_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 
Wipe()41   void Wipe()
42   {
43     Password.Wipe();
44     NumCyclesPower = 0;
45     SaltSize = 0;
46     Z7_memset_0_ARRAY(Salt);
47     Z7_memset_0_ARRAY(Key);
48   }
49 
50 #ifdef Z7_CPP_IS_SUPPORTED_default
51   CKeyInfo(const CKeyInfo &) = default;
52 #endif
~CKeyInfo()53   ~CKeyInfo() { Wipe(); }
54 };
55 
56 class CKeyInfoCache
57 {
58   unsigned Size;
59   CObjectVector<CKeyInfo> Keys;
60 public:
CKeyInfoCache(unsigned size)61   CKeyInfoCache(unsigned size): Size(size) {}
62   bool GetKey(CKeyInfo &key);
63   void Add(const CKeyInfo &key);
64   void FindAndAdd(const CKeyInfo &key);
65 };
66 
67 class CBase
68 {
69   CKeyInfoCache _cachedKeys;
70 protected:
71   CKeyInfo _key;
72   Byte _iv[kIvSizeMax];
73   unsigned _ivSize;
74 
75   void PrepareKey();
76   CBase();
77 };
78 
79 class CBaseCoder:
80   public ICompressFilter,
81   public ICryptoSetPassword,
82   public CMyUnknownImp,
83   public CBase
84 {
85   Z7_IFACE_COM7_IMP(ICompressFilter)
Z7_IFACE_COM7_IMP(ICryptoSetPassword)86   Z7_IFACE_COM7_IMP(ICryptoSetPassword)
87 protected:
88   virtual ~CBaseCoder() {}
89   CMyComPtr<ICompressFilter> _aesFilter;
90 };
91 
92 #ifndef Z7_EXTRACT_ONLY
93 
94 class CEncoder Z7_final:
95   public CBaseCoder,
96   public ICompressWriteCoderProperties,
97   // public ICryptoResetSalt,
98   public ICryptoResetInitVector
99 {
100   Z7_COM_UNKNOWN_IMP_4(
101       ICompressFilter,
102       ICryptoSetPassword,
103       ICompressWriteCoderProperties,
104       // ICryptoResetSalt,
105       ICryptoResetInitVector)
106   Z7_IFACE_COM7_IMP(ICompressWriteCoderProperties)
107   // Z7_IFACE_COM7_IMP(ICryptoResetSalt)
108   Z7_IFACE_COM7_IMP(ICryptoResetInitVector)
109 public:
110   CEncoder();
111 };
112 
113 #endif
114 
115 class CDecoder Z7_final:
116   public CBaseCoder,
117   public ICompressSetDecoderProperties2
118 {
119   Z7_COM_UNKNOWN_IMP_3(
120       ICompressFilter,
121       ICryptoSetPassword,
122       ICompressSetDecoderProperties2)
123   Z7_IFACE_COM7_IMP(ICompressSetDecoderProperties2)
124 public:
125   CDecoder();
126 };
127 
128 }}
129 
130 #endif
131