Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / pbkdf2.h / pbkdf2.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <string.h>
#include <limb/hasher.h>
#include <limb/hmac.h>
#include <limb/memxor.h>
#include <limb/u32.h>

static void
block(void *dst, hasher *hr, const char *pwd, size_t plen, const char *salt, size_t slen,
      size_t iter, size_t num, size_t r)
{
    size_t blen, mlen = slen + sizeof(u32);
    if (hr->hlen > mlen)
        blen = hr->hlen;
    else
        blen = mlen;

    char buf[blen];
    memcpy(buf, salt, slen);
    u32_pack_big(buf + slen, num);
    hmac(buf, hr, pwd, plen, buf, mlen);

    memcpy(dst, buf, r);
    for (size_t i = 1; i < iter; ++i) {
        hmac(buf, hr, pwd, plen, buf, hr->hlen);
        memxor(dst, buf, r);
    }
}

int
pbkdf2(char *dst, size_t dlen, hasher *hr, const char *pwd, size_t plen,
       const char *salt, size_t slen, size_t iter)
{
    if (dlen > (1ULL << 32) - 1)
        return 0;

    size_t l = dlen / hr->hlen;
    if (dlen % hr->hlen) ++l;

    size_t r = dlen - (l - 1) * hr->hlen;
    for (size_t i = 1; i <= l; ++i)
        block(dst + (i - 1) * hr->hlen, hr, pwd, plen, salt, slen, iter, i,
              (i < l) ? hr->hlen : r);

    return 1;
}