Path: blob/main/crypto/libecc/src/examples/hash/md4.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 __MD4_H__1112/* Include libec for useful types and macros */13#include <libecc/libec.h>1415/****************************************************/16/*17* 32-bit integer manipulation macros18*/19#ifndef GET_UINT32_BE20#define GET_UINT32_BE(n, b, i) \21do { \22(n) = ( ((u32) (b)[(i) ]) << 24 ) \23| ( ((u32) (b)[(i) + 1]) << 16 ) \24| ( ((u32) (b)[(i) + 2]) << 8 ) \25| ( ((u32) (b)[(i) + 3]) ); \26} while( 0 )27#endif28#ifndef GET_UINT32_LE29#define GET_UINT32_LE(n, b, i) \30do { \31(n) = ( ((u32) (b)[(i) + 3]) << 24 ) \32| ( ((u32) (b)[(i) + 2]) << 16 ) \33| ( ((u32) (b)[(i) + 1]) << 8 ) \34| ( ((u32) (b)[(i) ]) ); \35} while( 0 )36#endif373839#ifndef PUT_UINT32_BE40#define PUT_UINT32_BE(n, b, i) \41do { \42(b)[(i) ] = (u8) ( (n) >> 24 ); \43(b)[(i) + 1] = (u8) ( (n) >> 16 ); \44(b)[(i) + 2] = (u8) ( (n) >> 8 ); \45(b)[(i) + 3] = (u8) ( (n) ); \46} while( 0 )47#endif4849#ifndef PUT_UINT32_LE50#define PUT_UINT32_LE(n, b, i) \51do { \52(b)[(i) + 3] = (u8) ( (n) >> 24 ); \53(b)[(i) + 2] = (u8) ( (n) >> 16 ); \54(b)[(i) + 1] = (u8) ( (n) >> 8 ); \55(b)[(i) ] = (u8) ( (n) ); \56} while( 0 )57#endif5859/*60* 64-bit integer manipulation macros61*/62#ifndef PUT_UINT64_BE63#define PUT_UINT64_BE(n,b,i) \64do { \65(b)[(i) ] = (u8) ( (n) >> 56 ); \66(b)[(i) + 1] = (u8) ( (n) >> 48 ); \67(b)[(i) + 2] = (u8) ( (n) >> 40 ); \68(b)[(i) + 3] = (u8) ( (n) >> 32 ); \69(b)[(i) + 4] = (u8) ( (n) >> 24 ); \70(b)[(i) + 5] = (u8) ( (n) >> 16 ); \71(b)[(i) + 6] = (u8) ( (n) >> 8 ); \72(b)[(i) + 7] = (u8) ( (n) ); \73} while( 0 )74#endif /* PUT_UINT64_BE */7576#ifndef PUT_UINT64_LE77#define PUT_UINT64_LE(n,b,i) \78do { \79(b)[(i) + 7] = (u8) ( (n) >> 56 ); \80(b)[(i) + 6] = (u8) ( (n) >> 48 ); \81(b)[(i) + 5] = (u8) ( (n) >> 40 ); \82(b)[(i) + 4] = (u8) ( (n) >> 32 ); \83(b)[(i) + 3] = (u8) ( (n) >> 24 ); \84(b)[(i) + 2] = (u8) ( (n) >> 16 ); \85(b)[(i) + 1] = (u8) ( (n) >> 8 ); \86(b)[(i) ] = (u8) ( (n) ); \87} while( 0 )88#endif /* PUT_UINT64_LE */8990#define MD4_STATE_SIZE 491#define MD4_BLOCK_SIZE 6492#define MD4_DIGEST_SIZE 1693#define MD4_DIGEST_SIZE_BITS 1289495#define MD4_HASH_MAGIC ((word_t)(0x4423955132399122ULL))96#define MD4_HASH_CHECK_INITIALIZED(A, ret, err) \97MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == MD4_HASH_MAGIC), ret, err)9899#define ROTL_MD4(x, n) ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))100101typedef struct {102/* Number of bytes processed */103u64 md4_total;104/* Internal state */105u32 md4_state[MD4_STATE_SIZE];106/* Internal buffer to handle updates in a block */107u8 md4_buffer[MD4_BLOCK_SIZE];108/* Initialization magic value */109word_t magic;110} md4_context;111112/* Init hash function. Returns 0 on success, -1 on error. */113ATTRIBUTE_WARN_UNUSED_RET int md4_init(md4_context *ctx);114115ATTRIBUTE_WARN_UNUSED_RET int md4_update(md4_context *ctx, const u8 *input, u32 ilen);116117/* Finalize. Returns 0 on success, -1 on error.*/118ATTRIBUTE_WARN_UNUSED_RET int md4_final(md4_context *ctx, u8 output[MD4_DIGEST_SIZE]);119120/*121* Scattered version performing init/update/finalize on a vector of buffers122* 'inputs' with the length of each buffer passed via 'ilens'. The function123* loops on pointers in 'inputs' until it finds a NULL pointer. The function124* returns 0 on success, -1 on error.125*/126ATTRIBUTE_WARN_UNUSED_RET int md4_scattered(const u8 **inputs, const u32 *ilens,127u8 output[MD4_DIGEST_SIZE]);128129/*130* Single call version performing init/update/final on given input.131* Returns 0 on success, -1 on error.132*/133ATTRIBUTE_WARN_UNUSED_RET int md4(const u8 *input, u32 ilen, u8 output[MD4_DIGEST_SIZE]);134135#endif /* __MD4_H__ */136137138