Path: blob/main/crypto/libecc/src/examples/hash/sha1.h
34889 views
/*1* Copyright (C) 2021 - This file is part of libecc project2*3* Authors:4* Ryad BENADJILA <[email protected]>5* Arnaud EBALARD <[email protected]>6*7* This software is licensed under a dual BSD and GPL v2 license.8* See LICENSE file at the root folder of the project.9*/10#ifndef __SHA1_H__11#define __SHA1_H__1213/* Include libec for useful types and macros */14#include <libecc/libec.h>1516/****************************************************/17/*18* 32-bit integer manipulation macros19*/20#ifndef GET_UINT32_BE21#define GET_UINT32_BE(n, b, i) \22do { \23(n) = ( ((u32) (b)[(i) ]) << 24 ) \24| ( ((u32) (b)[(i) + 1]) << 16 ) \25| ( ((u32) (b)[(i) + 2]) << 8 ) \26| ( ((u32) (b)[(i) + 3]) ); \27} while( 0 )28#endif29#ifndef GET_UINT32_LE30#define GET_UINT32_LE(n, b, i) \31do { \32(n) = ( ((u32) (b)[(i) + 3]) << 24 ) \33| ( ((u32) (b)[(i) + 2]) << 16 ) \34| ( ((u32) (b)[(i) + 1]) << 8 ) \35| ( ((u32) (b)[(i) ]) ); \36} while( 0 )37#endif383940#ifndef PUT_UINT32_BE41#define PUT_UINT32_BE(n, b, i) \42do { \43(b)[(i) ] = (u8) ( (n) >> 24 ); \44(b)[(i) + 1] = (u8) ( (n) >> 16 ); \45(b)[(i) + 2] = (u8) ( (n) >> 8 ); \46(b)[(i) + 3] = (u8) ( (n) ); \47} while( 0 )48#endif4950#ifndef PUT_UINT32_LE51#define PUT_UINT32_LE(n, b, i) \52do { \53(b)[(i) + 3] = (u8) ( (n) >> 24 ); \54(b)[(i) + 2] = (u8) ( (n) >> 16 ); \55(b)[(i) + 1] = (u8) ( (n) >> 8 ); \56(b)[(i) ] = (u8) ( (n) ); \57} while( 0 )58#endif5960/*61* 64-bit integer manipulation macros62*/63#ifndef PUT_UINT64_BE64#define PUT_UINT64_BE(n,b,i) \65do { \66(b)[(i) ] = (u8) ( (n) >> 56 ); \67(b)[(i) + 1] = (u8) ( (n) >> 48 ); \68(b)[(i) + 2] = (u8) ( (n) >> 40 ); \69(b)[(i) + 3] = (u8) ( (n) >> 32 ); \70(b)[(i) + 4] = (u8) ( (n) >> 24 ); \71(b)[(i) + 5] = (u8) ( (n) >> 16 ); \72(b)[(i) + 6] = (u8) ( (n) >> 8 ); \73(b)[(i) + 7] = (u8) ( (n) ); \74} while( 0 )75#endif /* PUT_UINT64_BE */7677#ifndef PUT_UINT64_LE78#define PUT_UINT64_LE(n,b,i) \79do { \80(b)[(i) + 7] = (u8) ( (n) >> 56 ); \81(b)[(i) + 6] = (u8) ( (n) >> 48 ); \82(b)[(i) + 5] = (u8) ( (n) >> 40 ); \83(b)[(i) + 4] = (u8) ( (n) >> 32 ); \84(b)[(i) + 3] = (u8) ( (n) >> 24 ); \85(b)[(i) + 2] = (u8) ( (n) >> 16 ); \86(b)[(i) + 1] = (u8) ( (n) >> 8 ); \87(b)[(i) ] = (u8) ( (n) ); \88} while( 0 )89#endif /* PUT_UINT64_LE */9091#define SHA1_STATE_SIZE 592#define SHA1_BLOCK_SIZE 6493#define SHA1_DIGEST_SIZE 2094#define SHA1_DIGEST_SIZE_BITS 1609596#define SHA1_HASH_MAGIC ((word_t)(0x1120387132308110ULL))97#define SHA1_HASH_CHECK_INITIALIZED(A, ret, err) \98MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == SHA1_HASH_MAGIC), ret, err)99100typedef struct {101/* Number of bytes processed */102u64 sha1_total;103/* Internal state */104u32 sha1_state[SHA1_STATE_SIZE];105/* Internal buffer to handle updates in a block */106u8 sha1_buffer[SHA1_BLOCK_SIZE];107/* Initialization magic value */108word_t magic;109} sha1_context;110111/* Init hash function. Returns 0 on success, -1 on error. */112ATTRIBUTE_WARN_UNUSED_RET int sha1_init(sha1_context *ctx);113114ATTRIBUTE_WARN_UNUSED_RET int sha1_update(sha1_context *ctx, const u8 *input, u32 ilen);115116/* Finalize. Returns 0 on success, -1 on error.*/117ATTRIBUTE_WARN_UNUSED_RET int sha1_final(sha1_context *ctx, u8 output[SHA1_DIGEST_SIZE]);118119/*120* Scattered version performing init/update/finalize on a vector of buffers121* 'inputs' with the length of each buffer passed via 'ilens'. The function122* loops on pointers in 'inputs' until it finds a NULL pointer. The function123* returns 0 on success, -1 on error.124*/125ATTRIBUTE_WARN_UNUSED_RET int sha1_scattered(const u8 **inputs, const u32 *ilens,126u8 output[SHA1_DIGEST_SIZE]);127128/*129* Single call version performing init/update/final on given input.130* Returns 0 on success, -1 on error.131*/132ATTRIBUTE_WARN_UNUSED_RET int sha1(const u8 *input, u32 ilen, u8 output[SHA1_DIGEST_SIZE]);133134#endif /* __SHA1_H__ */135136137