Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / shldata.h / shldata_finalr.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/patrim.h>
#include <limb/shldata.h>
#include "shldata.h"

ssize_t
shldata_finalr(const char *sce, size_t slen, shldata_ctx *ctx)
{
    char mac[16];

    if (ctx->done != ctx->len)
        return (errno = EINVAL, -1);

    u64 id, u;
    int r;
    r = patrim_get(&id, &u, sce, slen, 0);
    if (r < 0) return (errno = ENODATA, -1);

    if (id != ID_MAC || u != sizeof(mac))
        return (errno = EINVAL, -1);

    ccpl_final(mac, &ctx->ccpl);
    if (memcmp(sce + r, mac, sizeof(mac)))
        return (errno = EBADMSG, -1);

    return r + sizeof(mac);
}