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_SHA3_384 18 19 #include <libecc/hash/sha3-384.h> 20 21 /* Init hash function. Returns 0 on success, -1 on error. */ 22 int sha3_384_init(sha3_384_context *ctx) 23 { 24 int ret; 25 26 ret = _sha3_init(ctx, SHA3_384_DIGEST_SIZE); EG(ret, err); 27 28 /* Tell that we are initialized */ 29 ctx->magic = SHA3_384_HASH_MAGIC; 30 31 err: 32 return ret; 33 } 34 35 /* Update hash function. Returns 0 on success, -1 on error. */ 36 int sha3_384_update(sha3_384_context *ctx, const u8 *input, u32 ilen) 37 { 38 int ret; 39 40 SHA3_384_HASH_CHECK_INITIALIZED(ctx, ret, err); 41 42 ret = _sha3_update((sha3_context *)ctx, input, ilen); 43 44 err: 45 return ret; 46 } 47 48 /* Finalize hash function. Returns 0 on success, -1 on error. */ 49 int sha3_384_final(sha3_384_context *ctx, u8 output[SHA3_384_DIGEST_SIZE]) 50 { 51 int ret; 52 53 SHA3_384_HASH_CHECK_INITIALIZED(ctx, ret, err); 54 55 ret = _sha3_finalize((sha3_context *)ctx, output); EG(ret, err); 56 57 /* Tell that we are uninitialized */ 58 ctx->magic = WORD(0); 59 ret = 0; 60 61 err: 62 return ret; 63 } 64 65 /* 66 * Scattered version performing init/update/finalize on a vector of buffers 67 * 'inputs' with the length of each buffer passed via 'ilens'. The function 68 * loops on pointers in 'inputs' until it finds a NULL pointer. The function 69 * returns 0 on success, -1 on error. 70 */ 71 int sha3_384_scattered(const u8 **inputs, const u32 *ilens, 72 u8 output[SHA3_384_DIGEST_SIZE]) 73 { 74 sha3_384_context ctx; 75 int ret, pos = 0; 76 77 MUST_HAVE((inputs != NULL) && (ilens != NULL) && (output != NULL), ret, err); 78 79 ret = sha3_384_init(&ctx); EG(ret, err); 80 81 while (inputs[pos] != NULL) { 82 const u8 *buf = inputs[pos]; 83 u32 buflen = ilens[pos]; 84 85 ret = sha3_384_update(&ctx, buf, buflen); EG(ret, err); 86 87 pos += 1; 88 } 89 90 ret = sha3_384_final(&ctx, output); 91 92 err: 93 return ret; 94 } 95 96 /* 97 * Single call version performing init/update/final on given input. 98 * Returns 0 on success, -1 on error. 99 */ 100 int sha3_384(const u8 *input, u32 ilen, u8 output[SHA3_384_DIGEST_SIZE]) 101 { 102 sha3_384_context ctx; 103 int ret; 104 105 ret = sha3_384_init(&ctx); EG(ret, err); 106 ret = sha3_384_update(&ctx, input, ilen); EG(ret, err); 107 ret = sha3_384_final(&ctx, output); 108 109 err: 110 return ret; 111 } 112 113 #else /* WITH_HASH_SHA3_384 */ 114 115 /* 116 * Dummy definition to avoid the empty translation unit ISO C warning 117 */ 118 typedef int dummy; 119 #endif /* WITH_HASH_SHA3_384 */ 120