/* 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_finalw(char *dst, size_t dlen, shldata_ctx *ctx)
{
if (!ctx->len || ctx->len != ctx->done)
return (errno = EINVAL, -1);
char mac[16];
ccpl_final(mac, &ctx->ccpl);
int r;
r = patrim_put(dst, dlen, 0, ID_MAC, sizeof(mac));
if (r < 0 || dlen - r < sizeof(mac))
return (errno = ENOBUFS, -1);
memcpy(dst + r, mac, sizeof(mac));
return r + sizeof(mac);
}