Welcome to little lamb

Code » limb » master » tree

[master] / src / liblimb / sha3.h / sha3_final.c

/* This file is part of limb                           https://lila.oss/limb
 * Copyright (C) 2023 Olivier Brunel                          jjk@jjacky.com */
/* Based on RHash: http://rhash.sourceforge.net/
 * Based on The Keccak SHA-3 submission. Submission to NIST (Round 3), 2011
 * by Guido Bertoni, Joan Daemen, Michaƫl Peeters and Gilles Van Assche
 * Copyright (c) 2013 Aleksey Kravchenko */
/* SPDX-License-Identifier: 0BSD */

#include <assert.h>
#include <string.h>
#include "sha3/sha3.h"


/**
 * Store calculated hash into the given array.
 *
 * @param md  calculated hash in binary form
 * @param ctx the algorithm context containing current hashing state
 */
void
sha3_final(void * restrict md, void *ctx_)
{
    sha3_ctx *ctx = ctx_;
    size_t digest_length = 100 - ctx->block_size / 2;
    const size_t block_size = ctx->block_size;

    if (!(ctx->rest & SHA3_FINALIZED))
    {
        /* clear the rest of the data queue */
        memset((char*)ctx->message + ctx->rest, 0, block_size - ctx->rest);
        ((char*)ctx->message)[ctx->rest] |= 0x06;
        ((char*)ctx->message)[block_size - 1] |= 0x80;

        /* process final block */
        rhash_sha3_process_block(ctx->hash, ctx->message, block_size);
        ctx->rest = SHA3_FINALIZED; /* mark context as finalized */
    }

    assert(block_size > digest_length);
    if (md) me64_to_le_str(md, ctx->hash, digest_length);
}