xref: /third_party/lzma/CPP/7zip/Crypto/7zAes.h (revision 370b324c)
1370b324cSopenharmony_ci// 7zAes.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_CRYPTO_7Z_AES_H
4370b324cSopenharmony_ci#define ZIP7_INC_CRYPTO_7Z_AES_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include "../../Common/MyBuffer.h"
7370b324cSopenharmony_ci#include "../../Common/MyCom.h"
8370b324cSopenharmony_ci#include "../../Common/MyVector.h"
9370b324cSopenharmony_ci
10370b324cSopenharmony_ci#include "../ICoder.h"
11370b324cSopenharmony_ci#include "../IPassword.h"
12370b324cSopenharmony_ci
13370b324cSopenharmony_cinamespace NCrypto {
14370b324cSopenharmony_cinamespace N7z {
15370b324cSopenharmony_ci
16370b324cSopenharmony_ciconst unsigned kKeySize = 32;
17370b324cSopenharmony_ciconst unsigned kSaltSizeMax = 16;
18370b324cSopenharmony_ciconst unsigned kIvSizeMax = 16; // AES_BLOCK_SIZE;
19370b324cSopenharmony_ci
20370b324cSopenharmony_ciclass CKeyInfo
21370b324cSopenharmony_ci{
22370b324cSopenharmony_cipublic:
23370b324cSopenharmony_ci  unsigned NumCyclesPower;
24370b324cSopenharmony_ci  unsigned SaltSize;
25370b324cSopenharmony_ci  Byte Salt[kSaltSizeMax];
26370b324cSopenharmony_ci  CByteBuffer Password;
27370b324cSopenharmony_ci  Byte Key[kKeySize];
28370b324cSopenharmony_ci
29370b324cSopenharmony_ci  bool IsEqualTo(const CKeyInfo &a) const;
30370b324cSopenharmony_ci  void CalcKey();
31370b324cSopenharmony_ci
32370b324cSopenharmony_ci  CKeyInfo() { ClearProps(); }
33370b324cSopenharmony_ci  void ClearProps()
34370b324cSopenharmony_ci  {
35370b324cSopenharmony_ci    NumCyclesPower = 0;
36370b324cSopenharmony_ci    SaltSize = 0;
37370b324cSopenharmony_ci    for (unsigned i = 0; i < sizeof(Salt); i++)
38370b324cSopenharmony_ci      Salt[i] = 0;
39370b324cSopenharmony_ci  }
40370b324cSopenharmony_ci
41370b324cSopenharmony_ci  void Wipe()
42370b324cSopenharmony_ci  {
43370b324cSopenharmony_ci    Password.Wipe();
44370b324cSopenharmony_ci    NumCyclesPower = 0;
45370b324cSopenharmony_ci    SaltSize = 0;
46370b324cSopenharmony_ci    Z7_memset_0_ARRAY(Salt);
47370b324cSopenharmony_ci    Z7_memset_0_ARRAY(Key);
48370b324cSopenharmony_ci  }
49370b324cSopenharmony_ci
50370b324cSopenharmony_ci#ifdef Z7_CPP_IS_SUPPORTED_default
51370b324cSopenharmony_ci  CKeyInfo(const CKeyInfo &) = default;
52370b324cSopenharmony_ci#endif
53370b324cSopenharmony_ci  ~CKeyInfo() { Wipe(); }
54370b324cSopenharmony_ci};
55370b324cSopenharmony_ci
56370b324cSopenharmony_ciclass CKeyInfoCache
57370b324cSopenharmony_ci{
58370b324cSopenharmony_ci  unsigned Size;
59370b324cSopenharmony_ci  CObjectVector<CKeyInfo> Keys;
60370b324cSopenharmony_cipublic:
61370b324cSopenharmony_ci  CKeyInfoCache(unsigned size): Size(size) {}
62370b324cSopenharmony_ci  bool GetKey(CKeyInfo &key);
63370b324cSopenharmony_ci  void Add(const CKeyInfo &key);
64370b324cSopenharmony_ci  void FindAndAdd(const CKeyInfo &key);
65370b324cSopenharmony_ci};
66370b324cSopenharmony_ci
67370b324cSopenharmony_ciclass CBase
68370b324cSopenharmony_ci{
69370b324cSopenharmony_ci  CKeyInfoCache _cachedKeys;
70370b324cSopenharmony_ciprotected:
71370b324cSopenharmony_ci  CKeyInfo _key;
72370b324cSopenharmony_ci  Byte _iv[kIvSizeMax];
73370b324cSopenharmony_ci  unsigned _ivSize;
74370b324cSopenharmony_ci
75370b324cSopenharmony_ci  void PrepareKey();
76370b324cSopenharmony_ci  CBase();
77370b324cSopenharmony_ci};
78370b324cSopenharmony_ci
79370b324cSopenharmony_ciclass CBaseCoder:
80370b324cSopenharmony_ci  public ICompressFilter,
81370b324cSopenharmony_ci  public ICryptoSetPassword,
82370b324cSopenharmony_ci  public CMyUnknownImp,
83370b324cSopenharmony_ci  public CBase
84370b324cSopenharmony_ci{
85370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICompressFilter)
86370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICryptoSetPassword)
87370b324cSopenharmony_ciprotected:
88370b324cSopenharmony_ci  virtual ~CBaseCoder() {}
89370b324cSopenharmony_ci  CMyComPtr<ICompressFilter> _aesFilter;
90370b324cSopenharmony_ci};
91370b324cSopenharmony_ci
92370b324cSopenharmony_ci#ifndef Z7_EXTRACT_ONLY
93370b324cSopenharmony_ci
94370b324cSopenharmony_ciclass CEncoder Z7_final:
95370b324cSopenharmony_ci  public CBaseCoder,
96370b324cSopenharmony_ci  public ICompressWriteCoderProperties,
97370b324cSopenharmony_ci  // public ICryptoResetSalt,
98370b324cSopenharmony_ci  public ICryptoResetInitVector
99370b324cSopenharmony_ci{
100370b324cSopenharmony_ci  Z7_COM_UNKNOWN_IMP_4(
101370b324cSopenharmony_ci      ICompressFilter,
102370b324cSopenharmony_ci      ICryptoSetPassword,
103370b324cSopenharmony_ci      ICompressWriteCoderProperties,
104370b324cSopenharmony_ci      // ICryptoResetSalt,
105370b324cSopenharmony_ci      ICryptoResetInitVector)
106370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICompressWriteCoderProperties)
107370b324cSopenharmony_ci  // Z7_IFACE_COM7_IMP(ICryptoResetSalt)
108370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICryptoResetInitVector)
109370b324cSopenharmony_cipublic:
110370b324cSopenharmony_ci  CEncoder();
111370b324cSopenharmony_ci};
112370b324cSopenharmony_ci
113370b324cSopenharmony_ci#endif
114370b324cSopenharmony_ci
115370b324cSopenharmony_ciclass CDecoder Z7_final:
116370b324cSopenharmony_ci  public CBaseCoder,
117370b324cSopenharmony_ci  public ICompressSetDecoderProperties2
118370b324cSopenharmony_ci{
119370b324cSopenharmony_ci  Z7_COM_UNKNOWN_IMP_3(
120370b324cSopenharmony_ci      ICompressFilter,
121370b324cSopenharmony_ci      ICryptoSetPassword,
122370b324cSopenharmony_ci      ICompressSetDecoderProperties2)
123370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICompressSetDecoderProperties2)
124370b324cSopenharmony_cipublic:
125370b324cSopenharmony_ci  CDecoder();
126370b324cSopenharmony_ci};
127370b324cSopenharmony_ci
128370b324cSopenharmony_ci}}
129370b324cSopenharmony_ci
130370b324cSopenharmony_ci#endif
131