1370b324cSopenharmony_ci// Crypto/MyAes.h
2370b324cSopenharmony_ci
3370b324cSopenharmony_ci#ifndef ZIP7_INC_CRYPTO_MY_AES_H
4370b324cSopenharmony_ci#define ZIP7_INC_CRYPTO_MY_AES_H
5370b324cSopenharmony_ci
6370b324cSopenharmony_ci#include "../../../C/Aes.h"
7370b324cSopenharmony_ci
8370b324cSopenharmony_ci#include "../../Common/MyBuffer2.h"
9370b324cSopenharmony_ci#include "../../Common/MyCom.h"
10370b324cSopenharmony_ci
11370b324cSopenharmony_ci#include "../ICoder.h"
12370b324cSopenharmony_ci
13370b324cSopenharmony_cinamespace NCrypto {
14370b324cSopenharmony_ci
15370b324cSopenharmony_ci#ifdef Z7_EXTRACT_ONLY
16370b324cSopenharmony_ci#define Z7_IFACEN_IAesCoderSetFunctions(x)
17370b324cSopenharmony_ci#else
18370b324cSopenharmony_ci#define Z7_IFACEN_IAesCoderSetFunctions(x) \
19370b324cSopenharmony_ci  virtual bool SetFunctions(UInt32 algo) x
20370b324cSopenharmony_ci#endif
21370b324cSopenharmony_ci
22370b324cSopenharmony_ci
23370b324cSopenharmony_ciclass CAesCoder:
24370b324cSopenharmony_ci  public ICompressFilter,
25370b324cSopenharmony_ci  public ICryptoProperties,
26370b324cSopenharmony_ci #ifndef Z7_EXTRACT_ONLY
27370b324cSopenharmony_ci  public ICompressSetCoderProperties,
28370b324cSopenharmony_ci #endif
29370b324cSopenharmony_ci  public CMyUnknownImp
30370b324cSopenharmony_ci{
31370b324cSopenharmony_ci  Z7_COM_QI_BEGIN2(ICompressFilter)
32370b324cSopenharmony_ci  Z7_COM_QI_ENTRY(ICryptoProperties)
33370b324cSopenharmony_ci #ifndef Z7_EXTRACT_ONLY
34370b324cSopenharmony_ci  Z7_COM_QI_ENTRY(ICompressSetCoderProperties)
35370b324cSopenharmony_ci #endif
36370b324cSopenharmony_ci  Z7_COM_QI_END
37370b324cSopenharmony_ci  Z7_COM_ADDREF_RELEASE
38370b324cSopenharmony_ci
39370b324cSopenharmony_cipublic:
40370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP_NONFINAL(ICompressFilter)
41370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICryptoProperties)
42370b324cSopenharmony_ciprivate:
43370b324cSopenharmony_ci #ifndef Z7_EXTRACT_ONLY
44370b324cSopenharmony_ci  Z7_IFACE_COM7_IMP(ICompressSetCoderProperties)
45370b324cSopenharmony_ci #endif
46370b324cSopenharmony_ci
47370b324cSopenharmony_ciprotected:
48370b324cSopenharmony_ci  bool _keyIsSet;
49370b324cSopenharmony_ci  // bool _encodeMode;
50370b324cSopenharmony_ci  // bool _ctrMode;
51370b324cSopenharmony_ci  // unsigned _offset;
52370b324cSopenharmony_ci  unsigned _keySize;
53370b324cSopenharmony_ci  unsigned _ctrPos; // we need _ctrPos here for Init() / SetInitVector()
54370b324cSopenharmony_ci  AES_CODE_FUNC _codeFunc;
55370b324cSopenharmony_ci  AES_SET_KEY_FUNC _setKeyFunc;
56370b324cSopenharmony_ciprivate:
57370b324cSopenharmony_ci  // UInt32 _aes[AES_NUM_IVMRK_WORDS + 3];
58370b324cSopenharmony_ci  CAlignedBuffer1 _aes;
59370b324cSopenharmony_ci
60370b324cSopenharmony_ci  Byte _iv[AES_BLOCK_SIZE];
61370b324cSopenharmony_ci
62370b324cSopenharmony_ci  // UInt32 *Aes() { return _aes + _offset; }
63370b324cSopenharmony_ciprotected:
64370b324cSopenharmony_ci  UInt32 *Aes() { return (UInt32 *)(void *)(Byte *)_aes; }
65370b324cSopenharmony_ci
66370b324cSopenharmony_ci Z7_IFACE_PURE(IAesCoderSetFunctions)
67370b324cSopenharmony_ci
68370b324cSopenharmony_cipublic:
69370b324cSopenharmony_ci  CAesCoder(
70370b324cSopenharmony_ci      // bool encodeMode,
71370b324cSopenharmony_ci      unsigned keySize
72370b324cSopenharmony_ci      // , bool ctrMode
73370b324cSopenharmony_ci      );
74370b324cSopenharmony_ci  virtual ~CAesCoder() {}   // we need virtual destructor for derived classes
75370b324cSopenharmony_ci  void SetKeySize(unsigned size) { _keySize = size; }
76370b324cSopenharmony_ci};
77370b324cSopenharmony_ci
78370b324cSopenharmony_ci
79370b324cSopenharmony_ci#ifndef Z7_EXTRACT_ONLY
80370b324cSopenharmony_cistruct CAesCbcEncoder: public CAesCoder
81370b324cSopenharmony_ci{
82370b324cSopenharmony_ci  CAesCbcEncoder(unsigned keySize = 0): CAesCoder(keySize)
83370b324cSopenharmony_ci  {
84370b324cSopenharmony_ci    _setKeyFunc = Aes_SetKey_Enc;
85370b324cSopenharmony_ci    _codeFunc = g_AesCbc_Encode;
86370b324cSopenharmony_ci  }
87370b324cSopenharmony_ci  Z7_IFACE_IMP(IAesCoderSetFunctions)
88370b324cSopenharmony_ci};
89370b324cSopenharmony_ci#endif
90370b324cSopenharmony_ci
91370b324cSopenharmony_cistruct CAesCbcDecoder: public CAesCoder
92370b324cSopenharmony_ci{
93370b324cSopenharmony_ci  CAesCbcDecoder(unsigned keySize = 0): CAesCoder(keySize)
94370b324cSopenharmony_ci  {
95370b324cSopenharmony_ci    _setKeyFunc = Aes_SetKey_Dec;
96370b324cSopenharmony_ci    _codeFunc = g_AesCbc_Decode;
97370b324cSopenharmony_ci  }
98370b324cSopenharmony_ci  Z7_IFACE_IMP(IAesCoderSetFunctions)
99370b324cSopenharmony_ci};
100370b324cSopenharmony_ci
101370b324cSopenharmony_ci#ifndef Z7_SFX
102370b324cSopenharmony_cistruct CAesCtrCoder: public CAesCoder
103370b324cSopenharmony_ci{
104370b324cSopenharmony_ciprivate:
105370b324cSopenharmony_ci  // unsigned _ctrPos;
106370b324cSopenharmony_ci  // Z7_IFACE_COM7_IMP(ICompressFilter)
107370b324cSopenharmony_ci  // Z7_COM7F_IMP(Init())
108370b324cSopenharmony_ci  Z7_COM7F_IMP2(UInt32, Filter(Byte *data, UInt32 size))
109370b324cSopenharmony_cipublic:
110370b324cSopenharmony_ci  CAesCtrCoder(unsigned keySize = 0): CAesCoder(keySize)
111370b324cSopenharmony_ci  {
112370b324cSopenharmony_ci    _ctrPos = 0;
113370b324cSopenharmony_ci    _setKeyFunc = Aes_SetKey_Enc;
114370b324cSopenharmony_ci    _codeFunc = g_AesCtr_Code;
115370b324cSopenharmony_ci  }
116370b324cSopenharmony_ci  Z7_IFACE_IMP(IAesCoderSetFunctions)
117370b324cSopenharmony_ci};
118370b324cSopenharmony_ci#endif
119370b324cSopenharmony_ci
120370b324cSopenharmony_ci}
121370b324cSopenharmony_ci
122370b324cSopenharmony_ci#endif
123