Path: blob/main/crypto/libecc/src/hash/sha512-256.c
34879 views
/*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_SHA512_2561718#include <libecc/hash/sha512-256.h>1920/* Init hash function. Returns 0 on success, -1 on error. */21int sha512_256_init(sha512_256_context *ctx)22{23int ret;2425MUST_HAVE((ctx != NULL), ret, err);2627ctx->sha512_total[0] = ctx->sha512_total[1] = 0;28ctx->sha512_state[0] = (u64)(0x22312194FC2BF72C);29ctx->sha512_state[1] = (u64)(0x9F555FA3C84C64C2);30ctx->sha512_state[2] = (u64)(0x2393B86B6F53B151);31ctx->sha512_state[3] = (u64)(0x963877195940EABD);32ctx->sha512_state[4] = (u64)(0x96283EE2A88EFFE3);33ctx->sha512_state[5] = (u64)(0xBE5E1E2553863992);34ctx->sha512_state[6] = (u64)(0x2B0199FC2C85B8AA);35ctx->sha512_state[7] = (u64)(0x0EB72DDC81C52CA2);3637/* Tell that we are initialized */38ctx->magic = SHA512_256_HASH_MAGIC;39ret = 0;4041err:42return ret;43}4445/* Update hash function. Returns 0 on success, -1 on error. */46int sha512_256_update(sha512_256_context *ctx, const u8 *input, u32 ilen)47{48int ret;4950SHA512_256_HASH_CHECK_INITIALIZED(ctx, ret, err);5152ret = sha512_core_update(ctx, input, ilen);5354err:55return ret;56}5758/*59* Finalize hash function. Returns 0 on success, -1 on error.60*/61int sha512_256_final(sha512_256_context *ctx, u8 output[SHA512_256_DIGEST_SIZE])62{63int ret;6465SHA512_256_HASH_CHECK_INITIALIZED(ctx, ret, err);66ret = sha512_core_final(ctx, output, SHA512_256_DIGEST_SIZE); EG(ret, err);6768ctx->magic = WORD(0);69ret = 0;7071err:72return ret;73}7475/*76* Scattered version performing init/update/finalize on a vector of buffers77* 'inputs' with the length of each buffer passed via 'ilens'. The function78* loops on pointers in 'inputs' until it finds a NULL pointer. The function79* returns 0 on success, -1 on error.80*/81int sha512_256_scattered(const u8 **inputs, const u32 *ilens,82u8 output[SHA512_256_DIGEST_SIZE])83{84sha512_256_context ctx;85int pos = 0;86int ret;8788MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);8990ret = sha512_256_init(&ctx); EG(ret, err);9192while (inputs[pos] != NULL) {93ret = sha512_256_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);94pos += 1;95}9697ret = sha512_256_final(&ctx, output);9899err:100return ret;101}102103/* init/update/finalize on a single buffer 'input' of length 'ilen'. */104int sha512_256(const u8 *input, u32 ilen, u8 output[SHA512_256_DIGEST_SIZE])105{106sha512_256_context ctx;107int ret;108109ret = sha512_256_init(&ctx); EG(ret, err);110ret = sha512_256_update(&ctx, input, ilen); EG(ret, err);111ret = sha512_256_final(&ctx, output);112113err:114return ret;115}116117#else /* WITH_HASH_SHA512_256 */118119/*120* Dummy definition to avoid the empty translation unit ISO C warning121*/122typedef int dummy;123#endif /* WITH_HASH_SHA512_256 */124125126