Path: blob/main/crypto/libecc/src/hash/hash_algs.c
34878 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/hash/hash_algs.h>1617/*18* Return the hash mapping entry 'hm' associated with given hash name19* 'hash_name'. The function returns 0 on success, -1 on error. 'hm'20* is only meaningful on success.21*/22ATTRIBUTE_WARN_UNUSED_RET int get_hash_by_name(const char *hash_name, const hash_mapping **hm)23{24const hash_mapping *_hm = NULL;25int ret, check;26u8 i;2728MUST_HAVE(((hash_name != NULL) && (hm != NULL)), ret, err);2930ret = -1;31for (i = 0, _hm = &hash_maps[i]; _hm->type != UNKNOWN_HASH_ALG;32_hm = &hash_maps[++i]) {33const char *exp_name = (const char *)_hm->name;3435if ((!are_str_equal(hash_name, exp_name, &check)) && check) {36(*hm) = _hm;37ret = 0;38break;39}40}4142err:43return ret;44}4546/*47* Return the hash mapping entry 'hm' associated with given hash type value.48* The function returns 0 on success, -1 on error. 'hm' is not meaningfull49* on error.50*/51ATTRIBUTE_WARN_UNUSED_RET int get_hash_by_type(hash_alg_type hash_type, const hash_mapping **hm)52{53const hash_mapping *_hm = NULL;54int ret;55u8 i;5657MUST_HAVE((hm != NULL), ret, err);5859ret = -1;60for (i = 0, _hm = &hash_maps[i]; _hm->type != UNKNOWN_HASH_ALG;61_hm = &hash_maps[++i]) {62if (_hm->type == hash_type) {63(*hm) = _hm;64ret = 0;65break;66}67}6869err:70return ret;71}7273/*74* Returns respectively in digest_size and block_size param the digest size75* and block size for given hash function, if return value of the function is 0.76* If return value is -1, then the hash algorithm is not known and output77* parameters are not modified.78*/79ATTRIBUTE_WARN_UNUSED_RET int get_hash_sizes(hash_alg_type hash_type, u8 *digest_size, u8 *block_size)80{81const hash_mapping *m;82int ret;83u8 i;8485ret = -1;86for (i = 0, m = &hash_maps[i]; m->type != UNKNOWN_HASH_ALG;87m = &hash_maps[++i]) {88if (m->type == hash_type) {89if (digest_size != NULL) {90(*digest_size) = m->digest_size;91}92if (block_size != NULL) {93(*block_size) = m->block_size;94}95ret = 0;96break;97}98}99100return ret;101}102103/*104* Helper that sanity checks the provided hash mapping against our105* constant ones. Returns 0 on success, -1 on error.106*/107ATTRIBUTE_WARN_UNUSED_RET int hash_mapping_callbacks_sanity_check(const hash_mapping *h)108{109const hash_mapping *m;110int ret = -1, check;111u8 i;112113MUST_HAVE((h != NULL), ret, err);114115/* We just check is our mapping is indeed116* one of the registered mappings.117*/118for (i = 0, m = &hash_maps[i]; m->type != UNKNOWN_HASH_ALG;119m = &hash_maps[++i]) {120if (m->type == h->type) {121if ((!are_str_equal_nlen(m->name, h->name, MAX_HASH_ALG_NAME_LEN, &check)) && (!check)){122goto err;123} else if (m->digest_size != h->digest_size) {124goto err;125} else if(m->block_size != h->block_size) {126goto err;127} else if(m->hfunc_init != h->hfunc_init) {128goto err;129} else if(m->hfunc_update != h->hfunc_update) {130goto err;131} else if(m->hfunc_finalize != h->hfunc_finalize) {132goto err;133} else if(m->hfunc_scattered != h->hfunc_scattered) {134goto err;135} else{136ret = 0;137}138}139}140141err:142return ret;143}144145/*****************************************/146/* Trampolines to each specific function to147* handle typing of our generic union structure.148*/149#ifdef WITH_HASH_SHA224150ATTRIBUTE_WARN_UNUSED_RET int _sha224_init(hash_context * hctx)151{152return sha224_init((sha224_context*)hctx);153}154ATTRIBUTE_WARN_UNUSED_RET int _sha224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)155{156return sha224_update((sha224_context*)hctx, chunk, chunklen);157}158ATTRIBUTE_WARN_UNUSED_RET int _sha224_final(hash_context * hctx, unsigned char *output)159{160return sha224_final((sha224_context*)hctx, output);161}162#endif163#ifdef WITH_HASH_SHA256164ATTRIBUTE_WARN_UNUSED_RET int _sha256_init(hash_context * hctx)165{166return sha256_init((sha256_context*)hctx);167}168ATTRIBUTE_WARN_UNUSED_RET int _sha256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)169{170return sha256_update((sha256_context*)hctx, chunk, chunklen);171}172ATTRIBUTE_WARN_UNUSED_RET int _sha256_final(hash_context * hctx, unsigned char *output)173{174return sha256_final((sha256_context*)hctx, output);175}176#endif177#ifdef WITH_HASH_SHA384178ATTRIBUTE_WARN_UNUSED_RET int _sha384_init(hash_context * hctx)179{180return sha384_init((sha384_context*)hctx);181}182ATTRIBUTE_WARN_UNUSED_RET int _sha384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)183{184return sha384_update((sha384_context*)hctx, chunk, chunklen);185}186ATTRIBUTE_WARN_UNUSED_RET int _sha384_final(hash_context * hctx, unsigned char *output)187{188return sha384_final((sha384_context*)hctx, output);189}190#endif191#ifdef WITH_HASH_SHA512192ATTRIBUTE_WARN_UNUSED_RET int _sha512_init(hash_context * hctx)193{194return sha512_init((sha512_context*)hctx);195}196ATTRIBUTE_WARN_UNUSED_RET int _sha512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)197{198return sha512_update((sha512_context*)hctx, chunk, chunklen);199}200ATTRIBUTE_WARN_UNUSED_RET int _sha512_final(hash_context * hctx, unsigned char *output)201{202return sha512_final((sha512_context*)hctx, output);203}204#endif205#ifdef WITH_HASH_SHA512_224206ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_init(hash_context * hctx)207{208return sha512_224_init((sha512_224_context*)hctx);209}210ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)211{212return sha512_224_update((sha512_224_context*)hctx, chunk, chunklen);213}214ATTRIBUTE_WARN_UNUSED_RET int _sha512_224_final(hash_context * hctx, unsigned char *output)215{216return sha512_224_final((sha512_224_context*)hctx, output);217}218#endif219#ifdef WITH_HASH_SHA512_256220ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_init(hash_context * hctx)221{222return sha512_256_init((sha512_256_context*)hctx);223}224ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)225{226return sha512_256_update((sha512_256_context*)hctx, chunk, chunklen);227}228ATTRIBUTE_WARN_UNUSED_RET int _sha512_256_final(hash_context * hctx, unsigned char *output)229{230return sha512_256_final((sha512_256_context*)hctx, output);231}232#endif233#ifdef WITH_HASH_SHA3_224234ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_init(hash_context * hctx)235{236return sha3_224_init((sha3_224_context*)hctx);237}238ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)239{240return sha3_224_update((sha3_224_context*)hctx, chunk, chunklen);241}242ATTRIBUTE_WARN_UNUSED_RET int _sha3_224_final(hash_context * hctx, unsigned char *output)243{244return sha3_224_final((sha3_224_context*)hctx, output);245}246#endif247#ifdef WITH_HASH_SHA3_256248ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_init(hash_context * hctx)249{250return sha3_256_init((sha3_256_context*)hctx);251}252ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)253{254return sha3_256_update((sha3_256_context*)hctx, chunk, chunklen);255}256ATTRIBUTE_WARN_UNUSED_RET int _sha3_256_final(hash_context * hctx, unsigned char *output)257{258return sha3_256_final((sha3_256_context*)hctx, output);259}260#endif261#ifdef WITH_HASH_SHA3_384262ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_init(hash_context * hctx)263{264return sha3_384_init((sha3_384_context*)hctx);265}266ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)267{268return sha3_384_update((sha3_384_context*)hctx, chunk, chunklen);269}270ATTRIBUTE_WARN_UNUSED_RET int _sha3_384_final(hash_context * hctx, unsigned char *output)271{272return sha3_384_final((sha3_384_context*)hctx, output);273}274#endif275#ifdef WITH_HASH_SHA3_512276ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_init(hash_context * hctx)277{278return sha3_512_init((sha3_512_context*)hctx);279}280ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)281{282return sha3_512_update((sha3_512_context*)hctx, chunk, chunklen);283}284ATTRIBUTE_WARN_UNUSED_RET int _sha3_512_final(hash_context * hctx, unsigned char *output)285{286return sha3_512_final((sha3_512_context*)hctx, output);287}288#endif289#ifdef WITH_HASH_SM3290ATTRIBUTE_WARN_UNUSED_RET int _sm3_init(hash_context * hctx)291{292return sm3_init((sm3_context*)hctx);293}294ATTRIBUTE_WARN_UNUSED_RET int _sm3_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)295{296return sm3_update((sm3_context*)hctx, chunk, chunklen);297}298ATTRIBUTE_WARN_UNUSED_RET int _sm3_final(hash_context * hctx, unsigned char *output)299{300return sm3_final((sm3_context*)hctx, output);301}302#endif303#ifdef WITH_HASH_SHAKE256304ATTRIBUTE_WARN_UNUSED_RET int _shake256_init(hash_context * hctx)305{306return shake256_init((shake256_context*)hctx);307}308ATTRIBUTE_WARN_UNUSED_RET int _shake256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)309{310return shake256_update((shake256_context*)hctx, chunk, chunklen);311}312ATTRIBUTE_WARN_UNUSED_RET int _shake256_final(hash_context * hctx, unsigned char *output)313{314return shake256_final((shake256_context*)hctx, output);315}316#endif317#ifdef WITH_HASH_STREEBOG256318ATTRIBUTE_WARN_UNUSED_RET int _streebog256_init(hash_context * hctx)319{320return streebog256_init((streebog256_context*)hctx);321}322ATTRIBUTE_WARN_UNUSED_RET int _streebog256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)323{324return streebog256_update((streebog256_context*)hctx, chunk, chunklen);325}326ATTRIBUTE_WARN_UNUSED_RET int _streebog256_final(hash_context * hctx, unsigned char *output)327{328return streebog256_final((streebog256_context*)hctx, output);329}330#endif331#ifdef WITH_HASH_STREEBOG512332ATTRIBUTE_WARN_UNUSED_RET int _streebog512_init(hash_context * hctx)333{334return streebog512_init((streebog512_context*)hctx);335}336ATTRIBUTE_WARN_UNUSED_RET int _streebog512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)337{338return streebog512_update((streebog512_context*)hctx, chunk, chunklen);339}340ATTRIBUTE_WARN_UNUSED_RET int _streebog512_final(hash_context * hctx, unsigned char *output)341{342return streebog512_final((streebog512_context*)hctx, output);343}344#endif345#ifdef WITH_HASH_RIPEMD160346ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_init(hash_context * hctx)347{348return ripemd160_init((ripemd160_context*)hctx);349}350ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)351{352return ripemd160_update((ripemd160_context*)hctx, chunk, chunklen);353}354ATTRIBUTE_WARN_UNUSED_RET int _ripemd160_final(hash_context * hctx, unsigned char *output)355{356return ripemd160_final((ripemd160_context*)hctx, output);357}358#endif359#ifdef WITH_HASH_BELT_HASH360ATTRIBUTE_WARN_UNUSED_RET int _belt_hash_init(hash_context * hctx)361{362return belt_hash_init((belt_hash_context*)hctx);363}364ATTRIBUTE_WARN_UNUSED_RET int _belt_hash_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)365{366return belt_hash_update((belt_hash_context*)hctx, chunk, chunklen);367}368ATTRIBUTE_WARN_UNUSED_RET int _belt_hash_final(hash_context * hctx, unsigned char *output)369{370return belt_hash_final((belt_hash_context*)hctx, output);371}372#endif373#ifdef WITH_HASH_BASH224374ATTRIBUTE_WARN_UNUSED_RET int _bash224_init(hash_context * hctx)375{376return bash224_init((bash224_context*)hctx);377}378ATTRIBUTE_WARN_UNUSED_RET int _bash224_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)379{380return bash224_update((bash224_context*)hctx, chunk, chunklen);381}382ATTRIBUTE_WARN_UNUSED_RET int _bash224_final(hash_context * hctx, unsigned char *output)383{384return bash224_final((bash224_context*)hctx, output);385}386#endif387#ifdef WITH_HASH_BASH256388ATTRIBUTE_WARN_UNUSED_RET int _bash256_init(hash_context * hctx)389{390return bash256_init((bash256_context*)hctx);391}392ATTRIBUTE_WARN_UNUSED_RET int _bash256_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)393{394return bash256_update((bash256_context*)hctx, chunk, chunklen);395}396ATTRIBUTE_WARN_UNUSED_RET int _bash256_final(hash_context * hctx, unsigned char *output)397{398return bash256_final((bash256_context*)hctx, output);399}400#endif401#ifdef WITH_HASH_BASH384402ATTRIBUTE_WARN_UNUSED_RET int _bash384_init(hash_context * hctx)403{404return bash384_init((bash384_context*)hctx);405}406ATTRIBUTE_WARN_UNUSED_RET int _bash384_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)407{408return bash384_update((bash384_context*)hctx, chunk, chunklen);409}410ATTRIBUTE_WARN_UNUSED_RET int _bash384_final(hash_context * hctx, unsigned char *output)411{412return bash384_final((bash384_context*)hctx, output);413}414#endif415#ifdef WITH_HASH_BASH512416ATTRIBUTE_WARN_UNUSED_RET int _bash512_init(hash_context * hctx)417{418return bash512_init((bash512_context*)hctx);419}420ATTRIBUTE_WARN_UNUSED_RET int _bash512_update(hash_context * hctx, const unsigned char *chunk, u32 chunklen)421{422return bash512_update((bash512_context*)hctx, chunk, chunklen);423}424ATTRIBUTE_WARN_UNUSED_RET int _bash512_final(hash_context * hctx, unsigned char *output)425{426return bash512_final((bash512_context*)hctx, output);427}428#endif429430431