Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / ccpl.h / ccpl_crypt.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>

void
ccpl_crypt(void *dst, const void *data, size_t dlen, int encrypt, void *ctx_)
{
    ccpl_ctx *ctx = ctx_;

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

    if (!encrypt)
        poly1305_update(data, dlen, &ctx->pl);

    chacha20_crypt(dst, data, dlen, &ctx->cc);
    ctx->dlen += dlen;

    if (encrypt)
        poly1305_update(dst, dlen, &ctx->pl);
}