/ external / libecc / src / hash / bash512.c
bash512.c
  1  /*
  2   *  Copyright (C) 2022 - This file is part of libecc project
  3   *
  4   *  Authors:
  5   *      Ryad BENADJILA <ryadbenadjila@gmail.com>
  6   *      Arnaud EBALARD <arnaud.ebalard@ssi.gouv.fr>
  7   *
  8   *  This software is licensed under a dual BSD and GPL v2 license.
  9   *  See LICENSE file at the root folder of the project.
 10   */
 11  #include <libecc/lib_ecc_config.h>
 12  #ifdef WITH_HASH_BASH512
 13  
 14  #include <libecc/hash/bash512.h>
 15  
 16  /* Init hash function. Returns 0 on success, -1 on error. */
 17  int bash512_init(bash512_context *ctx)
 18  {
 19  	int ret;
 20  
 21  	ret = _bash_init(ctx, BASH512_DIGEST_SIZE); EG(ret, err);
 22  
 23  	/* Tell that we are initialized */
 24  	ctx->magic = BASH512_HASH_MAGIC;
 25  
 26  err:
 27  	return ret;
 28  }
 29  
 30  /* Update hash function. Returns 0 on success, -1 on error. */
 31  int bash512_update(bash512_context *ctx, const u8 *input, u32 ilen)
 32  {
 33  	int ret;
 34  
 35  	BASH512_HASH_CHECK_INITIALIZED(ctx, ret, err);
 36  
 37  	ret = _bash_update((bash_context *)ctx, input, ilen);
 38  
 39  err:
 40  	return ret;
 41  }
 42  
 43  /* Finalize hash function. Returns 0 on success, -1 on error. */
 44  int bash512_final(bash512_context *ctx, u8 output[BASH512_DIGEST_SIZE])
 45  {
 46  	int ret;
 47  
 48  	BASH512_HASH_CHECK_INITIALIZED(ctx, ret, err);
 49  
 50  	ret = _bash_finalize((bash_context *)ctx, output); EG(ret, err);
 51  
 52  	/* Tell that we are uninitialized */
 53  	ctx->magic = WORD(0);
 54  	ret = 0;
 55  
 56  err:
 57  	return ret;
 58  }
 59  
 60  /*
 61   * Scattered version performing init/update/finalize on a vector of buffers
 62   * 'inputs' with the length of each buffer passed via 'ilens'. The function
 63   * loops on pointers in 'inputs' until it finds a NULL pointer. The function
 64   * returns 0 on success, -1 on error.
 65   */
 66  int bash512_scattered(const u8 **inputs, const u32 *ilens,
 67  			u8 output[BASH512_DIGEST_SIZE])
 68  {
 69  	bash512_context ctx;
 70  	int ret, pos = 0;
 71  
 72  	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
 73  
 74  	ret = bash512_init(&ctx); EG(ret, err);
 75  
 76  	while (inputs[pos] != NULL) {
 77  		ret = bash512_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
 78  		pos += 1;
 79  	}
 80  
 81  	ret = bash512_final(&ctx, output);
 82  
 83  err:
 84  	return ret;
 85  }
 86  
 87  /*
 88   * Single call version performing init/update/final on given input.
 89   * Returns 0 on success, -1 on error.
 90   */
 91  int bash512(const u8 *input, u32 ilen, u8 output[BASH512_DIGEST_SIZE])
 92  {
 93  	bash512_context ctx;
 94  	int ret;
 95  
 96  	ret = bash512_init(&ctx); EG(ret, err);
 97  	ret = bash512_update(&ctx, input, ilen); EG(ret, err);
 98  	ret = bash512_final(&ctx, output);
 99  
100  err:
101  	return ret;
102  }
103  
104  #else /* WITH_HASH_BASH512 */
105  
106  /*
107   * Dummy definition to avoid the empty translation unit ISO C warning
108   */
109  typedef int dummy;
110  #endif /* WITH_HASH_BASH512 */