~ubuntu-branches/ubuntu/precise/p7zip/precise-updates

« back to all changes in this revision

Viewing changes to CPP/7zip/Crypto/Hash/Pbkdf2HmacSha1.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mohammed Adnène Trojette
  • Date: 2009-02-14 20:12:27 UTC
  • mfrom: (1.1.11 upstream) (2.1.3 sid)
  • Revision ID: james.westby@ubuntu.com-20090214201227-go63qxm9ozfdma60
Tags: 4.65~dfsg.1-1
* New upstream release.
* Remove wx2.8 Build-Depends added by mistakes (7zG is not yet
  intended to be built).
* Use dh_clean without -k.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
// Pbkdf2HmacSha1.cpp
2
 
 
3
 
#include "StdAfx.h"
4
 
 
5
 
#include "HmacSha1.h"
6
 
 
7
 
namespace NCrypto {
8
 
namespace NSha1 {
9
 
 
10
 
void Pbkdf2Hmac(const Byte *pwd, size_t pwdSize, const Byte *salt, size_t saltSize, 
11
 
    UInt32 numIterations, Byte *key, size_t keySize)
12
 
{
13
 
  CHmac baseCtx;
14
 
  baseCtx.SetKey(pwd, pwdSize);
15
 
  for (UInt32 i = 1; keySize > 0; i++)
16
 
  {
17
 
    CHmac ctx = baseCtx;
18
 
    ctx.Update(salt, saltSize);
19
 
    Byte u[kDigestSize] = { (Byte)(i >> 24), (Byte)(i >> 16), (Byte)(i >> 8), (Byte)(i) };
20
 
    const unsigned int curSize = (keySize < kDigestSize) ? (unsigned int)keySize : kDigestSize;
21
 
    ctx.Update(u, 4);
22
 
    ctx.Final(u, kDigestSize);
23
 
 
24
 
    unsigned int s;
25
 
    for (s = 0; s < curSize; s++)
26
 
      key[s] = u[s];
27
 
    
28
 
    for (UInt32 j = numIterations; j > 1; j--)
29
 
    {
30
 
      ctx = baseCtx;
31
 
      ctx.Update(u, kDigestSize);
32
 
      ctx.Final(u, kDigestSize);
33
 
      for (s = 0; s < curSize; s++)
34
 
        key[s] ^= u[s];
35
 
    }
36
 
 
37
 
    key += curSize;
38
 
    keySize -= curSize;
39
 
  }
40
 
}
41
 
 
42
 
void Pbkdf2Hmac32(const Byte *pwd, size_t pwdSize, const UInt32 *salt, size_t saltSize, 
43
 
    UInt32 numIterations, UInt32 *key, size_t keySize)
44
 
{
45
 
  CHmac32 baseCtx;
46
 
  baseCtx.SetKey(pwd, pwdSize);
47
 
  for (UInt32 i = 1; keySize > 0; i++)
48
 
  {
49
 
    CHmac32 ctx = baseCtx;
50
 
    ctx.Update(salt, saltSize);
51
 
    UInt32 u[kDigestSizeInWords] = { i };
52
 
    const unsigned int curSize = (keySize < kDigestSizeInWords) ? (unsigned int)keySize : kDigestSizeInWords;
53
 
    ctx.Update(u, 1);
54
 
    ctx.Final(u, kDigestSizeInWords);
55
 
 
56
 
    // Speed-optimized code start
57
 
    ctx = baseCtx;
58
 
    ctx.GetLoopXorDigest(u, numIterations - 1);
59
 
    // Speed-optimized code end
60
 
    
61
 
    unsigned int s;
62
 
    for (s = 0; s < curSize; s++)
63
 
      key[s] = u[s];
64
 
    
65
 
    /*
66
 
    // Default code start
67
 
    for (UInt32 j = numIterations; j > 1; j--)
68
 
    {
69
 
      ctx = baseCtx;
70
 
      ctx.Update(u, kDigestSizeInWords);
71
 
      ctx.Final(u, kDigestSizeInWords);
72
 
      for (s = 0; s < curSize; s++)
73
 
        key[s] ^= u[s];
74
 
    }
75
 
    // Default code end
76
 
    */
77
 
 
78
 
    key += curSize;
79
 
    keySize -= curSize;
80
 
  }
81
 
}
82
 
 
83
 
}}