OpenSSL EVP_BytesToKey 方法的 C 版本

标签 c openssl sha512

我正在寻找 OpenSSL EVP_BytesToKey 函数的 C 实现。

这是 EVP_BytesToKey 方法的伪代码解释(在 OpenSSL 源代码的/doc/ssleay.txt 中):

/* M[] is an array of message digests
 * MD() is the message digest function */
M[0]=MD(data . salt);
for (i=1; i<count; i++) M[0]=MD(M[0]);

i=1
while (data still needed for key and iv)
{
    M[i]=MD(M[i-1] . data . salt);
    for (i=1; i<count; i++) M[i]=MD(M[i]);
    i++;
}

If the salt is NULL, it is not used.
The digests are concatenated together.
M = M[0] . M[1] . M[2] .......

这是我的代码(MD()是sha512。我需要 key 是32字节,iv是16字节):

int main()
{
    unsigned long long out[8]; 
    unsigned char key[9] = {0x4b,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c,0x0c};
    int len, i;

    len = sizeof(key);
    sha512(out, key, len);

    unsigned char a[sizeof(out)];
    for (i = 0; i < count; i++) 
    {
        long2char(out, a); // unsigned long long to unsigned char;
        sha512(out, a, sizeof(out));
    }

    return 0;
}

经过 countsha512() 后,输出为 64 字节,所以我认为我不需要其余的伪代码。但结果不正确,不知道哪里出了问题。

我希望你能帮我解决这个问题。谢谢!

最佳答案

I'm looking for C implementation of the OpenSSL EVP_BytesToKey function...

也许您应该只使用 OpenSSL 提供的一个?您知道,evp_key.c 中名为 EVP_BytesToKey 的那个。 (如下所示)。

OpenSSL 1.1.0c changed the digest algorithm用于一些内部组件。之前使用MD5,1.1.0改用SHA256。请注意,更改不会影响 EVP_BytesToKey 和 openssl enc 等命令

<小时/>
int EVP_BytesToKey(const EVP_CIPHER *type, const EVP_MD *md,
                   const unsigned char *salt, const unsigned char *data,
                   int datal, int count, unsigned char *key,
                   unsigned char *iv)
{
    EVP_MD_CTX *c;
    unsigned char md_buf[EVP_MAX_MD_SIZE];
    int niv, nkey, addmd = 0;
    unsigned int mds = 0, i;
    int rv = 0;
    nkey = EVP_CIPHER_key_length(type);
    niv = EVP_CIPHER_iv_length(type);
    OPENSSL_assert(nkey <= EVP_MAX_KEY_LENGTH);
    OPENSSL_assert(niv <= EVP_MAX_IV_LENGTH);

    if (data == NULL)
        return nkey;

    c = EVP_MD_CTX_new();
    if (c == NULL)
        goto err;
    for (;;) {
        if (!EVP_DigestInit_ex(c, md, NULL))
            goto err;
        if (addmd++)
            if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
                goto err;
        if (!EVP_DigestUpdate(c, data, datal))
            goto err;
        if (salt != NULL)
            if (!EVP_DigestUpdate(c, salt, PKCS5_SALT_LEN))
                goto err;
        if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
            goto err;

        for (i = 1; i < (unsigned int)count; i++) {
            if (!EVP_DigestInit_ex(c, md, NULL))
                goto err;
            if (!EVP_DigestUpdate(c, &(md_buf[0]), mds))
                goto err;
            if (!EVP_DigestFinal_ex(c, &(md_buf[0]), &mds))
                goto err;
        }
        i = 0;
        if (nkey) {
            for (;;) {
                if (nkey == 0)
                    break;
                if (i == mds)
                    break;
                if (key != NULL)
                    *(key++) = md_buf[i];
                nkey--;
                i++;
            }
        }
        if (niv && (i != mds)) {
            for (;;) {
                if (niv == 0)
                    break;
                if (i == mds)
                    break;
                if (iv != NULL)
                    *(iv++) = md_buf[i];
                niv--;
                i++;
            }
        }
        if ((nkey == 0) && (niv == 0))
            break;
    }
    rv = EVP_CIPHER_key_length(type);
 err:
    EVP_MD_CTX_free(c);
    OPENSSL_cleanse(md_buf, sizeof(md_buf));
    return rv;
}

关于OpenSSL EVP_BytesToKey 方法的 C 版本,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29534656/

相关文章:

swift - HMAC SHA512 在 Swift 3.1 中使用 CommonCrypto

编译错误 C2059、C2061、C2146 和偶发的 IntelliSense 错误

ssl - 无法 openssl 验证 SSL 证书

bash - 使用 OpenSSL 生成质数

java - 在 Java 中提供各种哈希算法(MD5、SHA1、SHA256 等)的库?

java - Android 的 SHA-512 哈希

c - 未定义对 main 的引用 - collect2 : ld returned 1 exit status

c - 段错误错误,将元素分配给字符数组

c - 使用Floyd-Warshall算法确定 "odd"矩阵

node.js - Node.js 中 OpenSSL base64 enc 的等效项 (openssl enc -base64 -d -A)?