/ external / libecc / src / hash / sha512.c
sha512.c
  1  /*
  2   *  Copyright (C) 2017 - 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   *      Jean-Pierre FLORI <jean-pierre.flori@ssi.gouv.fr>
  8   *
  9   *  Contributors:
 10   *      Nicolas VIVET <nicolas.vivet@ssi.gouv.fr>
 11   *      Karim KHALFALLAH <karim.khalfallah@ssi.gouv.fr>
 12   *
 13   *  This software is licensed under a dual BSD and GPL v2 license.
 14   *  See LICENSE file at the root folder of the project.
 15   */
 16  #include <libecc/lib_ecc_config.h>
 17  #ifdef WITH_HASH_SHA512
 18  
 19  #include <libecc/hash/sha512.h>
 20  
 21  /* Init hash function. Returns 0 on success, -1 on error. */
 22  int sha512_init(sha512_context *ctx)
 23  {
 24  	int ret;
 25  
 26  	MUST_HAVE((ctx != NULL), ret, err);
 27  
 28  	ctx->sha512_total[0] = ctx->sha512_total[1] = 0;
 29  	ctx->sha512_state[0] = (u64)(0x6A09E667F3BCC908);
 30  	ctx->sha512_state[1] = (u64)(0xBB67AE8584CAA73B);
 31  	ctx->sha512_state[2] = (u64)(0x3C6EF372FE94F82B);
 32  	ctx->sha512_state[3] = (u64)(0xA54FF53A5F1D36F1);
 33  	ctx->sha512_state[4] = (u64)(0x510E527FADE682D1);
 34  	ctx->sha512_state[5] = (u64)(0x9B05688C2B3E6C1F);
 35  	ctx->sha512_state[6] = (u64)(0x1F83D9ABFB41BD6B);
 36  	ctx->sha512_state[7] = (u64)(0x5BE0CD19137E2179);
 37  
 38  	/* Tell that we are initialized */
 39  	ctx->magic = SHA512_HASH_MAGIC;
 40  	ret = 0;
 41  
 42  err:
 43  	return ret;
 44  }
 45  
 46  /* Update hash function. Returns 0 on success, -1 on error. */
 47  int sha512_update(sha512_context *ctx, const u8 *input, u32 ilen)
 48  {
 49  	int ret;
 50  
 51  	SHA512_HASH_CHECK_INITIALIZED(ctx, ret, err);
 52  
 53  	ret = sha512_core_update(ctx, input, ilen);
 54  
 55  err:
 56  	return ret;
 57  }
 58  
 59  /*
 60   * Finalize hash function. Returns 0 on success, -1 on error. */
 61  int sha512_final(sha512_context *ctx, u8 output[SHA512_DIGEST_SIZE])
 62  {
 63  	int ret;
 64  
 65  	SHA512_HASH_CHECK_INITIALIZED(ctx, ret, err);
 66  
 67  	ret = sha512_core_final(ctx, output, SHA512_DIGEST_SIZE); EG(ret, err);
 68  
 69  	/* Tell that we are uninitialized */
 70  	ctx->magic = WORD(0);
 71  	ret = 0;
 72  
 73  err:
 74  	return ret;
 75  }
 76  
 77  /*
 78   * Scattered version performing init/update/finalize on a vector of buffers
 79   * 'inputs' with the length of each buffer passed via 'ilens'. The function
 80   * loops on pointers in 'inputs' until it finds a NULL pointer. The function
 81   * returns 0 on success, -1 on error.
 82   */
 83  int sha512_scattered(const u8 **inputs, const u32 *ilens,
 84  		      u8 output[SHA512_DIGEST_SIZE])
 85  {
 86  	sha512_context ctx;
 87  	int pos = 0;
 88  	int ret;
 89  
 90  	MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err);
 91  
 92  	ret = sha512_init(&ctx); EG(ret, err);
 93  
 94  	while (inputs[pos] != NULL) {
 95  		ret = sha512_update(&ctx, inputs[pos], ilens[pos]); EG(ret, err);
 96  		pos += 1;
 97  	}
 98  
 99  	ret = sha512_final(&ctx, output);
100  
101  err:
102  	return ret;
103  }
104  
105  /* init/update/finalize on a single buffer 'input' of length 'ilen'. */
106  int sha512(const u8 *input, u32 ilen, u8 output[SHA512_DIGEST_SIZE])
107  {
108  	sha512_context ctx;
109  	int ret;
110  
111  	ret = sha512_init(&ctx); EG(ret, err);
112  	ret = sha512_update(&ctx, input, ilen); EG(ret, err);
113  	ret = sha512_final(&ctx, output);
114  
115  err:
116  	return ret;
117  }
118  
119  #else /* WITH_HASH_SHA512 */
120  
121  /*
122   * Dummy definition to avoid the empty translation unit ISO C warning
123   */
124  typedef int dummy;
125  #endif /* WITH_HASH_SHA512 */