/* 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 <limb/hasher.h>
#include <limb/patrim.h>
#include <limb/pbkdf2.h>
#include <limb/shldata.h>
#include "shldata.h"
ssize_t
shldata_initr(const char *sce, size_t slen, const char *pwd, size_t plen,
unsigned *algo, unsigned *iter, shldata_ctx *ctx)
{
unsigned o;
const char *salt;
size_t sltlen;
ssize_t aadoff;
int r;
/* silence warnings */
salt = NULL;
sltlen = 0;
aadoff = -1;
o = *algo = *iter = 0;
for (;;) {
u64 id, u;
r = patrim_get(&id, &u, sce, slen, o);
if (r < 0) return (errno = ENODATA, -1);
if (!patrim_isshldata(id)) {
/* skip any ID that isn't ours */
o += r;
if (patrim_isblob(id))
o += u;
continue;
}
if (aadoff < 0)
aadoff = o;
switch (id) {
case ID_ALGO:
*algo = u + 1;
break;
case ID_ITER:
*iter = u;
break;
case ID_SALT:
salt = sce + o + r;
sltlen = u;
o += sltlen;
break;
case ID_DATA:
ctx->len = u;
ctx->done = 0;
break;
default:
return (errno = EINVAL, -1);
}
if (id == ID_DATA)
break;
o += r;
}
if (!*algo || !*iter || !sltlen)
return (errno = EINVAL, -1);
--*algo;
char key[KEY_LEN];
char nonce[NONCE_LEN] = { 0 };
pbkdf2(key, sizeof(key), hashers[*algo], pwd, plen, salt, sltlen, *iter);
ccpl_init(key, nonce, &ctx->ccpl);
ccpl_aad(sce + aadoff, o - aadoff, &ctx->ccpl);
/* return offset of begining of encrypted data */
return o + r;
}