/*1* Copyright (C) 2017 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6* Jean-Pierre FLORI <[email protected]>7*8* Contributors:9* Nicolas VIVET <[email protected]>10* Karim KHALFALLAH <[email protected]>11*12* This software is licensed under a dual BSD and GPL v2 license.13* See LICENSE file at the root folder of the project.14*/15#include <libecc/lib_ecc_config.h>16#ifdef WITH_HASH_SHA3_3841718#include <libecc/hash/sha3-384.h>1920/* Init hash function. Returns 0 on success, -1 on error. */21int sha3_384_init(sha3_384_context *ctx)22{23int ret;2425ret = _sha3_init(ctx, SHA3_384_DIGEST_SIZE); EG(ret, err);2627/* Tell that we are initialized */28ctx->magic = SHA3_384_HASH_MAGIC;2930err:31return ret;32}3334/* Update hash function. Returns 0 on success, -1 on error. */35int sha3_384_update(sha3_384_context *ctx, const u8 *input, u32 ilen)36{37int ret;3839SHA3_384_HASH_CHECK_INITIALIZED(ctx, ret, err);4041ret = _sha3_update((sha3_context *)ctx, input, ilen);4243err:44return ret;45}4647/* Finalize hash function. Returns 0 on success, -1 on error. */48int sha3_384_final(sha3_384_context *ctx, u8 output[SHA3_384_DIGEST_SIZE])49{50int ret;5152SHA3_384_HASH_CHECK_INITIALIZED(ctx, ret, err);5354ret = _sha3_finalize((sha3_context *)ctx, output); EG(ret, err);5556/* Tell that we are uninitialized */57ctx->magic = WORD(0);58ret = 0;5960err:61return ret;62}6364/*65* Scattered version performing init/update/finalize on a vector of buffers66* 'inputs' with the length of each buffer passed via 'ilens'. The function67* loops on pointers in 'inputs' until it finds a NULL pointer. The function68* returns 0 on success, -1 on error.69*/70int sha3_384_scattered(const u8 **inputs, const u32 *ilens,71u8 output[SHA3_384_DIGEST_SIZE])72{73sha3_384_context ctx;74int ret, pos = 0;7576MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);7778ret = sha3_384_init(&ctx); EG(ret, err);7980while (inputs[pos] != NULL) {81const u8 *buf = inputs[pos];82u32 buflen = ilens[pos];8384ret = sha3_384_update(&ctx, buf, buflen); EG(ret, err);8586pos += 1;87}8889ret = sha3_384_final(&ctx, output);9091err:92return ret;93}9495/*96* Single call version performing init/update/final on given input.97* Returns 0 on success, -1 on error.98*/99int sha3_384(const u8 *input, u32 ilen, u8 output[SHA3_384_DIGEST_SIZE])100{101sha3_384_context ctx;102int ret;103104ret = sha3_384_init(&ctx); EG(ret, err);105ret = sha3_384_update(&ctx, input, ilen); EG(ret, err);106ret = sha3_384_final(&ctx, output);107108err:109return ret;110}111112#else /* WITH_HASH_SHA3_384 */113114/*115* Dummy definition to avoid the empty translation unit ISO C warning116*/117typedef int dummy;118#endif /* WITH_HASH_SHA3_384 */119120121