Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / chacha20.h / chacha20_init.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/chacha20.h>
#include <limb/u32.h>

void
chacha20_init(const void *key, const void *nonce, void *ctx_)
{
    chacha20_ctx *ctx = ctx_;

    /* fresh start */
    ctx->part = 0;
    /* sigma */
    memcpy(&ctx->in[0], "expand 32-byte k", 16);
    /* key must be 32 bytes */
    memcpy(&ctx->in[4], key, 32);
    /* init counter */
    ctx->in[12] = 0;
    /* nonce is 96bit */
    memcpy(&ctx->in[13], nonce, 12);

    u32pa_le(ctx->in, CHACHA_U32_BLOCK_SIZE);
}