Welcome to little lamb

Code » limb » release » tree

[release] / src / liblimb / poly1305.h / poly1305_update.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* Based on poly1305-donna
 * Copyright (C) 2016 Andrew Moon */
/* SPDX-License-Identifier: GPL-2.0-only */
#include <string.h>
#include <limb/poly1305.h>
#include "poly1305.h"

void
poly1305_update(const void *msg_, size_t mlen, void *ctx_)
{
    struct poly1305_ctx *ctx = ctx_;
    const u8 *msg = msg_;

    /* handle leftover */
    if (ctx->leftover) {
        size_t want = POLY1305_BLOCKSIZE - ctx->leftover;
        if (want > mlen)
            want = mlen;
        memcpy(ctx->buf + ctx->leftover, msg, want);
        mlen -= want;
        msg += want;
        ctx->leftover += want;
        if (ctx->leftover < POLY1305_BLOCKSIZE)
            return;
        poly1305_blocks(ctx->buf, POLY1305_BLOCKSIZE, ctx);
        ctx->leftover = 0;
    }

    /* process full blocks */
    if (mlen >= POLY1305_BLOCKSIZE) {
        size_t want = (mlen & ~(POLY1305_BLOCKSIZE - 1));
        poly1305_blocks(msg, want, ctx);
        mlen -= want;
        msg += want;
    }

    /* store leftover */
    if (mlen) {
        memcpy(ctx->buf + ctx->leftover, msg, mlen);
        ctx->leftover += mlen;
    }
}