~ubuntu-branches/ubuntu/vivid/unrar-nonfree/vivid

« back to all changes in this revision

Viewing changes to crypt3.cpp

  • Committer: Package Import Robot
  • Author(s): Martin Meredith
  • Date: 2015-02-03 12:58:01 UTC
  • mfrom: (1.1.18) (5.1.18 sid)
  • Revision ID: package-import@ubuntu.com-20150203125801-od6ev8cqy1er51vz
Tags: 1:5.2.5-1
New upstream release

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
struct CryptKeyCacheItem
2
 
{
3
 
  CryptKeyCacheItem()
4
 
  {
5
 
    Password.Set(L"");
6
 
  }
7
 
 
8
 
  ~CryptKeyCacheItem()
9
 
  {
10
 
    cleandata(AESKey,sizeof(AESKey));
11
 
    cleandata(AESInit,sizeof(AESInit));
12
 
    cleandata(&Password,sizeof(Password));
13
 
  }
14
 
 
15
 
  byte AESKey[16],AESInit[16];
16
 
  SecPassword Password;
17
 
  bool SaltPresent;
18
 
  byte Salt[SIZE_SALT30];
19
 
};
20
 
 
21
 
static CryptKeyCacheItem Cache[4];
22
 
static int CachePos=0;
23
 
 
24
1
void CryptData::SetKey30(bool Encrypt,SecPassword *Password,const wchar *PwdW,const byte *Salt)
25
2
{
26
3
  byte AESKey[16],AESInit[16];
27
4
 
28
5
  bool Cached=false;
29
 
  for (uint I=0;I<ASIZE(Cache);I++)
30
 
    if (Cache[I].Password==*Password &&
31
 
        (Salt==NULL && !Cache[I].SaltPresent || Salt!=NULL &&
32
 
        Cache[I].SaltPresent && memcmp(Cache[I].Salt,Salt,SIZE_SALT30)==0))
 
6
  for (uint I=0;I<ASIZE(KDF3Cache);I++)
 
7
    if (KDF3Cache[I].Pwd==*Password &&
 
8
        (Salt==NULL && !KDF3Cache[I].SaltPresent || Salt!=NULL &&
 
9
        KDF3Cache[I].SaltPresent && memcmp(KDF3Cache[I].Salt,Salt,SIZE_SALT30)==0))
33
10
    {
34
 
      memcpy(AESKey,Cache[I].AESKey,sizeof(AESKey));
35
 
      memcpy(AESInit,Cache[I].AESInit,sizeof(AESInit));
 
11
      memcpy(AESKey,KDF3Cache[I].Key,sizeof(AESKey));
 
12
      memcpy(AESInit,KDF3Cache[I].Init,sizeof(AESInit));
36
13
      Cached=true;
37
14
      break;
38
15
    }
47
24
      memcpy(RawPsw+RawLength,Salt,SIZE_SALT30);
48
25
      RawLength+=SIZE_SALT30;
49
26
    }
50
 
    hash_context c;
51
 
    hash_initial(&c);
 
27
    sha1_context c;
 
28
    sha1_init(&c);
52
29
 
53
30
    const int HashRounds=0x40000;
54
31
    for (int I=0;I<HashRounds;I++)
55
32
    {
56
 
      hash_process( &c, RawPsw, RawLength, false);
 
33
      sha1_process( &c, RawPsw, RawLength, false);
57
34
      byte PswNum[3];
58
35
      PswNum[0]=(byte)I;
59
36
      PswNum[1]=(byte)(I>>8);
60
37
      PswNum[2]=(byte)(I>>16);
61
 
      hash_process( &c, PswNum, 3, false);
 
38
      sha1_process( &c, PswNum, 3, false);
62
39
      if (I%(HashRounds/16)==0)
63
40
      {
64
 
        hash_context tempc=c;
 
41
        sha1_context tempc=c;
65
42
        uint32 digest[5];
66
 
        hash_final( &tempc, digest, false);
 
43
        sha1_done( &tempc, digest, false);
67
44
        AESInit[I/(HashRounds/16)]=(byte)digest[4];
68
45
      }
69
46
    }
70
47
    uint32 digest[5];
71
 
    hash_final( &c, digest, false);
 
48
    sha1_done( &c, digest, false);
72
49
    for (int I=0;I<4;I++)
73
50
      for (int J=0;J<4;J++)
74
51
        AESKey[I*4+J]=(byte)(digest[I]>>(J*8));
75
52
 
76
 
    Cache[CachePos].Password=*Password;
77
 
    if ((Cache[CachePos].SaltPresent=(Salt!=NULL))==true)
78
 
      memcpy(Cache[CachePos].Salt,Salt,SIZE_SALT30);
79
 
    memcpy(Cache[CachePos].AESKey,AESKey,sizeof(AESKey));
80
 
    memcpy(Cache[CachePos].AESInit,AESInit,sizeof(AESInit));
81
 
    CachePos=(CachePos+1)%ASIZE(Cache);
 
53
    KDF3Cache[KDF3CachePos].Pwd=*Password;
 
54
    if ((KDF3Cache[KDF3CachePos].SaltPresent=(Salt!=NULL))==true)
 
55
      memcpy(KDF3Cache[KDF3CachePos].Salt,Salt,SIZE_SALT30);
 
56
    memcpy(KDF3Cache[KDF3CachePos].Key,AESKey,sizeof(AESKey));
 
57
    memcpy(KDF3Cache[KDF3CachePos].Init,AESInit,sizeof(AESInit));
 
58
    KDF3CachePos=(KDF3CachePos+1)%ASIZE(KDF3Cache);
82
59
 
83
60
    cleandata(RawPsw,sizeof(RawPsw));
84
61
  }