【问题标题】:Constant time password digest compares using Crypto++使用 Crypto++ 进行恒定时间密码摘要比较
【发布时间】:2014-03-20 08:54:17
【问题描述】:

我正在编写一个使用pbkdf2 method using cryptopp 对密码进行哈希处理的程序。

我在验证密码时遇到问题。我试图在“长度恒定”时间内比较输出,但它总是失败并返回错误。

// a and b are std strings containing the output of the DeriveKey function

unsigned diff = a.length() ^ b.length();
for(unsigned i = 0; i < a.length() && i < b.length(); i++)
{
      diff |= (unsigned)a[i] ^ (unsigned)b[i];
}

bool equal = diff == 0;

是否使用“slow equals”甚至是验证 pbkdf2 密码的正确方法?我对此有点困惑。

【问题讨论】:

    标签: c++ hash crypto++ pbkdf2


    【解决方案1】:

    我正在编写一个程序,它使用 pbkdf2 方法使用 cryptopp 对密码进行哈希处理。

    您链接到 Crypto++ 主页,而不是您对 PBKDF 的特定用途。这是some code以防万一(它使用来自RFC 6070的IETF测试向量):

    int main(int argc, char* argv[])
    {
        byte password[] ="password";
        size_t plen = strlen((const char*)password);
    
        byte salt[] = "salt";
        size_t slen = strlen((const char*)salt);
    
        int c = 1;
        byte derived[20];
    
        PKCS5_PBKDF2_HMAC<CryptoPP::SHA1> pbkdf2;
        pbkdf2.DeriveKey(derived, sizeof(derived), 0, password, plen, salt, slen, c);
    
        string result;
        HexEncoder encoder(new StringSink(result));
    
        encoder.Put(derived, sizeof(derived));
        encoder.MessageEnd();
    
        cout << "Derived: " << result << endl;
    
        return 0;
    }
    

    我尝试在“长度恒定”时间内比较输出,但它总是失败并返回 false。

    Crypto++ 内置了一个恒定的时间比较。使用来自misc.hVerifyBufsEqual。源代码在misc.cpp

    $ cd cryptopp
    $ grep -R VerifyBufsEqual *
    cryptlib.cpp:   return VerifyBufsEqual(digest, digestIn, digestLength);
    default.cpp:    if (!VerifyBufsEqual(check, check+BLOCKSIZE, BLOCKSIZE))
    fipstest.cpp:   if (!VerifyBufsEqual(expectedModuleMac, actualMac, macSize))
    fipstest.cpp:   if (VerifyBufsEqual(expectedModuleMac, actualMac, macSize))
    misc.cpp:bool VerifyBufsEqual(const byte *buf, const byte *mask, size_t count)
    misc.h:CRYPTOPP_DLL bool CRYPTOPP_API VerifyBufsEqual(const byte *buf1, const byte *buf2, size_t count);
    pssr.cpp:   valid = VerifyBufsEqual(representative + representativeByteLength - u, hashIdentifier.first, hashIdentifier.second) && valid;
    pubkey.cpp: return VerifyBufsEqual(representative, computedRepresentative, computedRepresentative.size());
    secblock.h:     return m_size == t.m_size && VerifyBufsEqual(m_ptr, t.m_ptr, m_size*sizeof(T));
    

    我不清楚的是:VerifyBufsEqual 是基于相等长度的缓冲区。我不确定是否可以忽略“不等长”的情况。


    在 Information Stack Exchange 上还有一个可能相关的问题:Timing attacks on password hashes。但我不确定它是否/如何推广到任意缓冲区比较。

    这个问题激起了我对一般问题的答案的兴趣(这个问题一直存在):Constant time compares when array sizes are not equal?。这应该告诉我们在VerifyBufsEqual (Crypto++)、CRYPTO_memcmp (OpenSSL) 等中是否有合适的工具。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-03-25
      • 2020-04-18
      • 1970-01-01
      • 1970-01-01
      • 2014-10-11
      • 1970-01-01
      • 2016-09-19
      • 2012-04-01
      相关资源
      最近更新 更多