Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / shldata.h / shldata_initw.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 <errno.h>
#include <string.h>
#include <limb/bytestr.h>
#include <limb/hasher.h>
#include <limb/patrim.h>
#include <limb/pbkdf2.h>
#include <limb/random.h>
#include <limb/shldata.h>
#include "shldata.h"

ssize_t
shldata_initw(char *dst, size_t dlen, const char *pwd, size_t plen,
              unsigned algo, unsigned iter, size_t len, shldata_ctx *ctx)
{
    if (algo >= NB_ALGOS || !iter)
        return (errno = EINVAL, -1);

    char salt[SALT_LEN];
    random_buf(salt, sizeof(salt));

    size_t l = 0;
    int r;

    r = patrim_put(dst, dlen, l, ID_ALGO, algo);
    if (r < 0) return (errno = ENOBUFS, -1);
    l += r;
    r = patrim_put(dst, dlen, l, ID_ITER, iter);
    if (r < 0) return (errno = ENOBUFS, -1);
    l += r;
    r = patrim_put(dst, dlen, l, ID_SALT, sizeof(salt));
    if (r < 0) return (errno = ENOBUFS, -1);
    l += r;
    if (dlen - l < sizeof(salt))
        return (errno = ENOBUFS, -1);
    memcpy(dst + l, salt, sizeof(salt));
    l += sizeof(salt);

    ctx->len = len;
    ctx->done = 0;

    if (len) {
        r = patrim_put(dst, dlen, l, ID_DATA, len);
        if (r < 0) return (errno = ENOBUFS, -1);
        l += r;
    } else {
        r = 0;
    }

    char key[KEY_LEN];
    char nonce[NONCE_LEN] = { 0 };

    pbkdf2(key, sizeof(key), hashers[algo], pwd, plen, salt, sizeof(salt), iter);
    ccpl_init(key, nonce, &ctx->ccpl);
    ccpl_aad(dst, l - r, &ctx->ccpl);
    byte_zero(key, sizeof(key));

    return l;
}