Path: blob/main/crypto/libecc/src/examples/hash/md5.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 __MD5_H__11#define __MD5_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 MD5_STATE_SIZE 492#define MD5_BLOCK_SIZE 6493#define MD5_DIGEST_SIZE 1694#define MD5_DIGEST_SIZE_BITS 1289596#define MD5_HASH_MAGIC ((word_t)(0x5684922132353193ULL))97#define MD5_HASH_CHECK_INITIALIZED(A, ret, err) \98MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == MD5_HASH_MAGIC), ret, err)99100#define ROTL_MD5(x, n) ((((u32)(x)) << (n)) | (((u32)(x)) >> (32-(n))))101102typedef struct {103/* Number of bytes processed */104u64 md5_total;105/* Internal state */106u32 md5_state[MD5_STATE_SIZE];107/* Internal buffer to handle updates in a block */108u8 md5_buffer[MD5_BLOCK_SIZE];109/* Initialization magic value */110word_t magic;111} md5_context;112113/* Init hash function. Returns 0 on success, -1 on error. */114ATTRIBUTE_WARN_UNUSED_RET int md5_init(md5_context *ctx);115116ATTRIBUTE_WARN_UNUSED_RET int md5_update(md5_context *ctx, const u8 *input, u32 ilen);117118/* Finalize. Returns 0 on success, -1 on error.*/119ATTRIBUTE_WARN_UNUSED_RET int md5_final(md5_context *ctx, u8 output[MD5_DIGEST_SIZE]);120121/*122* Scattered version performing init/update/finalize on a vector of buffers123* 'inputs' with the length of each buffer passed via 'ilens'. The function124* loops on pointers in 'inputs' until it finds a NULL pointer. The function125* returns 0 on success, -1 on error.126*/127ATTRIBUTE_WARN_UNUSED_RET int md5_scattered(const u8 **inputs, const u32 *ilens,128u8 output[MD5_DIGEST_SIZE]);129130/*131* Single call version performing init/update/final on given input.132* Returns 0 on success, -1 on error.133*/134ATTRIBUTE_WARN_UNUSED_RET int md5(const u8 *input, u32 ilen, u8 output[MD5_DIGEST_SIZE]);135136#endif /* __MD5_H__ */137138139