Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / ccpl.h / ccpl_final.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/ccpl.h>
#include <limb/u64.h>

void
ccpl_final(void *dst, void *ctx_)
{
    ccpl_ctx *ctx = ctx_;

    size_t l = 16 - (((ctx->w) ? ctx->dlen : ctx->alen) % 16);
    if (l) {
        char pad[l];
        memset(pad, 0, l);
        poly1305_update(pad, l, &ctx->pl);
    }

    u64p_le(&ctx->alen);
    poly1305_update(&ctx->alen, sizeof(u64), &ctx->pl);

    u64p_le(&ctx->dlen);
    poly1305_update(&ctx->dlen, sizeof(u64), &ctx->pl);

    poly1305_final(dst, &ctx->pl);
    chacha20_clear(&ctx->cc);
}