diff src/win32/7zip/7z/CPP/7zip/Crypto/Pbkdf2HmacSha1.cpp @ 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/Pbkdf2HmacSha1.cpp	Sat Mar 03 10:31:27 2012 -0600
     1.3 @@ -0,0 +1,83 @@
     1.4 +// Pbkdf2HmacSha1.cpp
     1.5 +
     1.6 +#include "StdAfx.h"
     1.7 +
     1.8 +#include "HmacSha1.h"
     1.9 +
    1.10 +namespace NCrypto {
    1.11 +namespace NSha1 {
    1.12 +
    1.13 +void Pbkdf2Hmac(const Byte *pwd, size_t pwdSize, const Byte *salt, size_t saltSize,
    1.14 +    UInt32 numIterations, Byte *key, size_t keySize)
    1.15 +{
    1.16 +  CHmac baseCtx;
    1.17 +  baseCtx.SetKey(pwd, pwdSize);
    1.18 +  for (UInt32 i = 1; keySize > 0; i++)
    1.19 +  {
    1.20 +    CHmac ctx = baseCtx;
    1.21 +    ctx.Update(salt, saltSize);
    1.22 +    Byte u[kDigestSize] = { (Byte)(i >> 24), (Byte)(i >> 16), (Byte)(i >> 8), (Byte)(i) };
    1.23 +    const unsigned int curSize = (keySize < kDigestSize) ? (unsigned int)keySize : kDigestSize;
    1.24 +    ctx.Update(u, 4);
    1.25 +    ctx.Final(u, kDigestSize);
    1.26 +
    1.27 +    unsigned int s;
    1.28 +    for (s = 0; s < curSize; s++)
    1.29 +      key[s] = u[s];
    1.30 +    
    1.31 +    for (UInt32 j = numIterations; j > 1; j--)
    1.32 +    {
    1.33 +      ctx = baseCtx;
    1.34 +      ctx.Update(u, kDigestSize);
    1.35 +      ctx.Final(u, kDigestSize);
    1.36 +      for (s = 0; s < curSize; s++)
    1.37 +        key[s] ^= u[s];
    1.38 +    }
    1.39 +
    1.40 +    key += curSize;
    1.41 +    keySize -= curSize;
    1.42 +  }
    1.43 +}
    1.44 +
    1.45 +void Pbkdf2Hmac32(const Byte *pwd, size_t pwdSize, const UInt32 *salt, size_t saltSize,
    1.46 +    UInt32 numIterations, UInt32 *key, size_t keySize)
    1.47 +{
    1.48 +  CHmac32 baseCtx;
    1.49 +  baseCtx.SetKey(pwd, pwdSize);
    1.50 +  for (UInt32 i = 1; keySize > 0; i++)
    1.51 +  {
    1.52 +    CHmac32 ctx = baseCtx;
    1.53 +    ctx.Update(salt, saltSize);
    1.54 +    UInt32 u[kDigestSizeInWords] = { i };
    1.55 +    const unsigned int curSize = (keySize < kDigestSizeInWords) ? (unsigned int)keySize : kDigestSizeInWords;
    1.56 +    ctx.Update(u, 1);
    1.57 +    ctx.Final(u, kDigestSizeInWords);
    1.58 +
    1.59 +    // Speed-optimized code start
    1.60 +    ctx = baseCtx;
    1.61 +    ctx.GetLoopXorDigest(u, numIterations - 1);
    1.62 +    // Speed-optimized code end
    1.63 +    
    1.64 +    unsigned int s;
    1.65 +    for (s = 0; s < curSize; s++)
    1.66 +      key[s] = u[s];
    1.67 +    
    1.68 +    /*
    1.69 +    // Default code start
    1.70 +    for (UInt32 j = numIterations; j > 1; j--)
    1.71 +    {
    1.72 +      ctx = baseCtx;
    1.73 +      ctx.Update(u, kDigestSizeInWords);
    1.74 +      ctx.Final(u, kDigestSizeInWords);
    1.75 +      for (s = 0; s < curSize; s++)
    1.76 +        key[s] ^= u[s];
    1.77 +    }
    1.78 +    // Default code end
    1.79 +    */
    1.80 +
    1.81 +    key += curSize;
    1.82 +    keySize -= curSize;
    1.83 +  }
    1.84 +}
    1.85 +
    1.86 +}}