gpt4 book ai didi

openssl - OpenSSL 中的 PBKDF2 实现

转载 作者:行者123 更新时间:2023-12-01 06:23:57 35 4
gpt4 key购买 nike

我在 DevC 中使用 OpenSSl。我在编程 PBKDF 时遇到了问题。有人建议我使用名为 PKCS5_PBKDF2_HMAC 的默认函数。我已经访问了许多在线链接,但无法使其正常工作。我的 main() 代码如下

unsigned char pass[1024];      // passphrase read from stdin
unsigned char salt[1024]; // salt
int iter=1000, keylen=128; // iteration
unsigned char result[1024]; // result
PKCS5_PBKDF2_HMAC (pass, strlen(pass), salt, strlen(salt), iter, EVP_MD(), keylen , result);

我只有两个编译错误,如下所示:
  • 函数“PKCS5_PBKDF2_HMAC”的参数太少
  • 'EVP_MD' 之前的预期表达式

  • 为了排除故障,我检查了头文件并验证了我提供的参数和顺序正确,但我没有解决方案,我只是感到困惑。

    最佳答案

    你有一些重大错误,但这个想法是可靠的。

  • EVP_* 需要是一个特定的函数。
  • keylen=128 是密码散列的错误,您的示例似乎是这样。不要忘记 - 永远不要要求比 native 哈希函数支持的更多(二进制)字节的输出,因为那样你正在做你的迭代计数 *(keylen/ native 哈希大小)次,而攻击者只需要做迭代计数* 1 次。
  • SHA-1 为 20
  • SHA-224 为 28
  • SHA-256 为 32
  • SHA-384 为 48
  • SHA-512 为 64
  • 结果 [1024] 太大了。结果 [keylen] 是正确的。
  • 如果有人放入超过 1024 个字节,请注意缓冲区溢出。

  • 我在 my Github repository 中有 OpenSSL PBKDF2 示例代码(以及 PolarSSL 和其他各种),但关键示例是(使用 PBKDF2-HMAC-SHA-512 作为最佳选项):
    void PBKDF2_HMAC_SHA_512(const char* pass, const unsigned char* salt, int32_t iterations, uint32_t outputBytes, char* hexResult, uint8_t* binResult)
    {
    unsigned int i;
    unsigned char digest[outputBytes];
    PKCS5_PBKDF2_HMAC(pass, strlen(pass), salt, strlen(salt), iterations, EVP_sha512(), outputBytes, digest);
    for (i = 0; i < sizeof(digest); i++)
    {
    sprintf(hexResult + (i * 2), "%02x", 255 & digest[i]);
    binResult[i] = digest[i];
    };

    }

    它将被称为:
     // 2*outputBytes+1 is 2 hex bytes per binary byte, and one character at the end for the string-terminating \0
    char hexResult[2*outputBytes+1];
    memset(hexResult,0,sizeof(hexResult));
    uint8_t binResult[outputBytes];
    memset(hexResult,0,sizeof(binResult));

    PBKDF2_HMAC_SHA_512(pass, salt, iterations, outputBytes, hexResult, binResult);

    尝试使用 SHA-384 或 SHA-512 作为基础哈希函数;它们包括 64 位操作,可减少大多数基于 GPU 的攻击者对您的优势。

    使用大量(数十万到数万)次迭代。 SHA-1 更大。

    我也有一个大名单 test vectors in my Github repository - 您可以使用它们来验证您的代码是否返回了应有的结果。

    关于openssl - OpenSSL 中的 PBKDF2 实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35381403/

    35 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com