diff 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 diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/win32/7zip/7z/CPP/7zip/Crypto/WzAes.h	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,119 @@
     1.4 +// Crypto/WzAes.h
     1.5 +/*
     1.6 +This code implements Brian Gladman's scheme
     1.7 +specified in password Based File Encryption Utility:
     1.8 +  - AES encryption (128,192,256-bit) in Counter (CTR) mode.
     1.9 +  - HMAC-SHA1 authentication for encrypted data (10 bytes)
    1.10 +  - Keys are derived by PPKDF2(RFC2898)-HMAC-SHA1 from ASCII password and
    1.11 +    Salt (saltSize = aesKeySize / 2).
    1.12 +  - 2 bytes contain Password Verifier's Code
    1.13 +*/
    1.14 +
    1.15 +#ifndef __CRYPTO_WZ_AES_H
    1.16 +#define __CRYPTO_WZ_AES_H
    1.17 +
    1.18 +extern "C"
    1.19 +{
    1.20 +#include "../../../C/Aes.h"
    1.21 +}
    1.22 +
    1.23 +#include "Common/Buffer.h"
    1.24 +#include "Common/MyCom.h"
    1.25 +#include "Common/MyVector.h"
    1.26 +
    1.27 +#include "../ICoder.h"
    1.28 +#include "../IPassword.h"
    1.29 +
    1.30 +#include "HmacSha1.h"
    1.31 +
    1.32 +namespace NCrypto {
    1.33 +namespace NWzAes {
    1.34 +
    1.35 +const unsigned int kSaltSizeMax = 16;
    1.36 +const unsigned int kMacSize = 10;
    1.37 +
    1.38 +const UInt32 kPasswordSizeMax = 99; // 128;
    1.39 +
    1.40 +// Password Verification Code Size
    1.41 +const unsigned int kPwdVerifCodeSize = 2;
    1.42 +
    1.43 +class CKeyInfo
    1.44 +{
    1.45 +public:
    1.46 +  Byte KeySizeMode; // 1 - 128-bit , 2 - 192-bit , 3 - 256-bit
    1.47 +  Byte Salt[kSaltSizeMax];
    1.48 +  Byte PwdVerifComputed[kPwdVerifCodeSize];
    1.49 +
    1.50 +  CByteBuffer Password;
    1.51 +
    1.52 +  UInt32 GetKeySize() const  { return (8 * (KeySizeMode & 3) + 8); }
    1.53 +  UInt32 GetSaltSize() const { return (4 * (KeySizeMode & 3) + 4); }
    1.54 +
    1.55 +  CKeyInfo() { Init(); }
    1.56 +  void Init() { KeySizeMode = 3; }
    1.57 +};
    1.58 +
    1.59 +class CBaseCoder:
    1.60 +  public ICompressFilter,
    1.61 +  public ICryptoSetPassword,
    1.62 +  public CMyUnknownImp
    1.63 +{
    1.64 +protected:
    1.65 +  CKeyInfo _key;
    1.66 +  UInt32 _counter[AES_BLOCK_SIZE / 4];
    1.67 +  Byte _buffer[AES_BLOCK_SIZE];
    1.68 +  NSha1::CHmac _hmac;
    1.69 +  unsigned int _blockPos;
    1.70 +  Byte _pwdVerifFromArchive[kPwdVerifCodeSize];
    1.71 +
    1.72 +  void EncryptData(Byte *data, UInt32 size);
    1.73 +
    1.74 +  CAes Aes;
    1.75 +
    1.76 +public:
    1.77 +  STDMETHOD(Init)();
    1.78 +  STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size) = 0;
    1.79 +  
    1.80 +  STDMETHOD(CryptoSetPassword)(const Byte *data, UInt32 size);
    1.81 +
    1.82 +  UInt32 GetHeaderSize() const { return _key.GetSaltSize() + kPwdVerifCodeSize; }
    1.83 +};
    1.84 +
    1.85 +class CEncoder:
    1.86 +  public CBaseCoder
    1.87 +  // public ICompressWriteCoderProperties
    1.88 +{
    1.89 +public:
    1.90 +  MY_UNKNOWN_IMP1(ICryptoSetPassword)
    1.91 +  //  ICompressWriteCoderProperties
    1.92 +  // STDMETHOD(WriteCoderProperties)(ISequentialOutStream *outStream);
    1.93 +  STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
    1.94 +  HRESULT WriteHeader(ISequentialOutStream *outStream);
    1.95 +  HRESULT WriteFooter(ISequentialOutStream *outStream);
    1.96 +  bool SetKeyMode(Byte mode)
    1.97 +  {
    1.98 +    if (mode < 1 || mode > 3)
    1.99 +      return false;
   1.100 +    _key.KeySizeMode = mode;
   1.101 +    return true;
   1.102 +  }
   1.103 +};
   1.104 +
   1.105 +class CDecoder:
   1.106 +  public CBaseCoder,
   1.107 +  public ICompressSetDecoderProperties2
   1.108 +{
   1.109 +public:
   1.110 +  MY_UNKNOWN_IMP2(
   1.111 +      ICryptoSetPassword,
   1.112 +      ICompressSetDecoderProperties2)
   1.113 +  STDMETHOD_(UInt32, Filter)(Byte *data, UInt32 size);
   1.114 +  STDMETHOD(SetDecoderProperties2)(const Byte *data, UInt32 size);
   1.115 +  HRESULT ReadHeader(ISequentialInStream *inStream);
   1.116 +  bool CheckPasswordVerifyCode();
   1.117 +  HRESULT CheckMac(ISequentialInStream *inStream, bool &isOK);
   1.118 +};
   1.119 +
   1.120 +}}
   1.121 +
   1.122 +#endif