Welcome to mirror list, hosted at ThFree Co, Russian Federation.

Rar5Aes.h « Crypto « 7zip « CPP - github.com/kornelski/7z.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: c69b24e4821734eb94c6b50314185c3f1d1b2919 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
// Crypto/Rar5Aes.h

#ifndef __CRYPTO_RAR5_AES_H
#define __CRYPTO_RAR5_AES_H

#include "../../../C/Aes.h"

#include "../../Common/MyBuffer.h"

#include "HmacSha256.h"
#include "MyAes.h"

namespace NCrypto {
namespace NRar5 {

const unsigned kSaltSize = 16;
const unsigned kPswCheckSize = 8;
const unsigned kAesKeySize = 32;

namespace NCryptoFlags
{
  const unsigned kPswCheck = 1 << 0;
  const unsigned kUseMAC   = 1 << 1;
}

struct CKey
{
  bool _needCalc;

  unsigned _numIterationsLog;
  Byte _salt[kSaltSize];
  CByteBuffer _password;
  
  Byte _key[kAesKeySize];
  Byte _check_Calced[kPswCheckSize];
  Byte _hashKey[SHA256_DIGEST_SIZE];

  void CopyCalcedKeysFrom(const CKey &k)
  {
    memcpy(_key, k._key, sizeof(_key));
    memcpy(_check_Calced, k._check_Calced, sizeof(_check_Calced));
    memcpy(_hashKey, k._hashKey, sizeof(_hashKey));
  }

  bool IsKeyEqualTo(const CKey &key)
  {
    return (_numIterationsLog == key._numIterationsLog
        && memcmp(_salt, key._salt, sizeof(_salt)) == 0
        && _password == key._password);
  }
  
  CKey();
};


class CDecoder:
  public CAesCbcDecoder,
  public CKey
{
  Byte _check[kPswCheckSize];
  bool _canCheck;
  UInt64 Flags;

  bool IsThereCheck() const { return ((Flags & NCryptoFlags::kPswCheck) != 0); }
public:
  Byte _iv[AES_BLOCK_SIZE];
  
  CDecoder();

  STDMETHOD(Init)();

  void SetPassword(const Byte *data, size_t size);
  HRESULT SetDecoderProps(const Byte *data, unsigned size, bool includeIV, bool isService);

  bool CalcKey_and_CheckPassword();

  bool UseMAC() const { return (Flags & NCryptoFlags::kUseMAC) != 0; }
  UInt32 Hmac_Convert_Crc32(UInt32 crc) const;
  void Hmac_Convert_32Bytes(Byte *data) const;
};

}}

#endif