view src/win32/7zip/7z/CPP/7zip/Crypto/WzAes.h @ 1:f9f4f1b99eed

importing src directory
author Robert McIntyre <rlm@mit.edu>
date Sat, 03 Mar 2012 10:31:27 -0600
parents
children
line wrap: on
line source
1 // Crypto/WzAes.h
2 /*
3 This code implements Brian Gladman's scheme
4 specified in password Based File Encryption Utility:
5 - AES encryption (128,192,256-bit) in Counter (CTR) mode.
6 - HMAC-SHA1 authentication for encrypted data (10 bytes)
7 - Keys are derived by PPKDF2(RFC2898)-HMAC-SHA1 from ASCII password and
8 Salt (saltSize = aesKeySize / 2).
9 - 2 bytes contain Password Verifier's Code
10 */
12 #ifndef __CRYPTO_WZ_AES_H
13 #define __CRYPTO_WZ_AES_H
15 extern "C"
16 {
17 #include "../../../C/Aes.h"
18 }
20 #include "Common/Buffer.h"
21 #include "Common/MyCom.h"
22 #include "Common/MyVector.h"
24 #include "../ICoder.h"
25 #include "../IPassword.h"
27 #include "HmacSha1.h"
29 namespace NCrypto {
30 namespace NWzAes {
32 const unsigned int kSaltSizeMax = 16;
33 const unsigned int kMacSize = 10;
35 const UInt32 kPasswordSizeMax = 99; // 128;
37 // Password Verification Code Size
38 const unsigned int kPwdVerifCodeSize = 2;
40 class CKeyInfo
41 {
42 public:
43 Byte KeySizeMode; // 1 - 128-bit , 2 - 192-bit , 3 - 256-bit
44 Byte Salt[kSaltSizeMax];
45 Byte PwdVerifComputed[kPwdVerifCodeSize];
47 CByteBuffer Password;
49 UInt32 GetKeySize() const { return (8 * (KeySizeMode & 3) + 8); }
50 UInt32 GetSaltSize() const { return (4 * (KeySizeMode & 3) + 4); }
52 CKeyInfo() { Init(); }
53 void Init() { KeySizeMode = 3; }
54 };
56 class CBaseCoder:
57 public ICompressFilter,
58 public ICryptoSetPassword,
59 public CMyUnknownImp
60 {
61 protected:
62 CKeyInfo _key;
63 UInt32 _counter[AES_BLOCK_SIZE / 4];
64 Byte _buffer[AES_BLOCK_SIZE];
65 NSha1::CHmac _hmac;
66 unsigned int _blockPos;
67 Byte _pwdVerifFromArchive[kPwdVerifCodeSize];
69 void EncryptData(Byte *data, UInt32 size);
71 CAes Aes;
73 public:
74 STDMETHOD(Init)();
75 STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) = 0;
77 STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
79 UInt32 GetHeaderSize() const { return _key.GetSaltSize() + kPwdVerifCodeSize; }
80 };
82 class CEncoder:
83 public CBaseCoder
84 // public ICompressWriteCoderProperties
85 {
86 public:
87 MY_UNKNOWN_IMP1(ICryptoSetPassword)
88 // ICompressWriteCoderProperties
89 // STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
90 STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
91 HRESULT WriteHeader(ISequentialOutStream *outStream);
92 HRESULT WriteFooter(ISequentialOutStream *outStream);
93 bool SetKeyMode(Byte mode)
94 {
95 if (mode < 1 || mode > 3)
96 return false;
97 _key.KeySizeMode = mode;
98 return true;
99 }
100 };
102 class CDecoder:
103 public CBaseCoder,
104 public ICompressSetDecoderProperties2
105 {
106 public:
107 MY_UNKNOWN_IMP2(
108 ICryptoSetPassword,
109 ICompressSetDecoderProperties2)
110 STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
111 STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
112 HRESULT ReadHeader(ISequentialInStream *inStream);
113 bool CheckPasswordVerifyCode();
114 HRESULT CheckMac(ISequentialInStream *inStream, bool &isOK);
115 };
117 }}
119 #endif