/ external / libecc / src / examples / hash / sha0.h
sha0.h
  1  /*
  2   *  Copyright (C) 2021 - 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   *
  8   *  This software is licensed under a dual BSD and GPL v2 license.
  9   *  See LICENSE file at the root folder of the project.
 10   */
 11  #ifndef __SHA0_H__
 12  #define __SHA0_H__
 13  
 14  /* Include libec for useful types and macros */
 15  #include <libecc/libec.h>
 16  
 17  /****************************************************/
 18  /*
 19   * 32-bit integer manipulation macros
 20   */
 21  #ifndef GET_UINT32_BE
 22  #define GET_UINT32_BE(n, b, i)			  	\
 23  do {						    	\
 24  	(n) =     ( ((u32) (b)[(i)    ]) << 24 )   	\
 25  		| ( ((u32) (b)[(i) + 1]) << 16 )	\
 26  		| ( ((u32) (b)[(i) + 2]) <<  8 )	\
 27  		| ( ((u32) (b)[(i) + 3])       );       \
 28  } while( 0 )
 29  #endif
 30  #ifndef GET_UINT32_LE
 31  #define GET_UINT32_LE(n, b, i)			  	\
 32  do {						    	\
 33  	(n) =     ( ((u32) (b)[(i) + 3]) << 24 )   	\
 34  		| ( ((u32) (b)[(i) + 2]) << 16 )	\
 35  		| ( ((u32) (b)[(i) + 1]) <<  8 )	\
 36  		| ( ((u32) (b)[(i)    ])       );       \
 37  } while( 0 )
 38  #endif
 39  
 40  
 41  #ifndef PUT_UINT32_BE
 42  #define PUT_UINT32_BE(n, b, i)		  	\
 43  do {					    	\
 44  	(b)[(i)    ] = (u8) ( (n) >> 24 );      \
 45  	(b)[(i) + 1] = (u8) ( (n) >> 16 );      \
 46  	(b)[(i) + 2] = (u8) ( (n) >>  8 );      \
 47  	(b)[(i) + 3] = (u8) ( (n)       );      \
 48  } while( 0 )
 49  #endif
 50  
 51  #ifndef PUT_UINT32_LE
 52  #define PUT_UINT32_LE(n, b, i)		  	\
 53  do {					    	\
 54  	(b)[(i) + 3] = (u8) ( (n) >> 24 );      \
 55  	(b)[(i) + 2] = (u8) ( (n) >> 16 );      \
 56  	(b)[(i) + 1] = (u8) ( (n) >>  8 );      \
 57  	(b)[(i)    ] = (u8) ( (n)       );      \
 58  } while( 0 )
 59  #endif
 60  
 61  /*
 62   * 64-bit integer manipulation macros
 63   */
 64  #ifndef PUT_UINT64_BE
 65  #define PUT_UINT64_BE(n,b,i)            \
 66  do {                                    \
 67      (b)[(i)    ] = (u8) ( (n) >> 56 );  \
 68      (b)[(i) + 1] = (u8) ( (n) >> 48 );  \
 69      (b)[(i) + 2] = (u8) ( (n) >> 40 );  \
 70      (b)[(i) + 3] = (u8) ( (n) >> 32 );  \
 71      (b)[(i) + 4] = (u8) ( (n) >> 24 );  \
 72      (b)[(i) + 5] = (u8) ( (n) >> 16 );  \
 73      (b)[(i) + 6] = (u8) ( (n) >>  8 );  \
 74      (b)[(i) + 7] = (u8) ( (n)       );  \
 75  } while( 0 )
 76  #endif /* PUT_UINT64_BE */
 77  
 78  #ifndef PUT_UINT64_LE
 79  #define PUT_UINT64_LE(n,b,i)            \
 80  do {                                    \
 81      (b)[(i) + 7] = (u8) ( (n) >> 56 );  \
 82      (b)[(i) + 6] = (u8) ( (n) >> 48 );  \
 83      (b)[(i) + 5] = (u8) ( (n) >> 40 );  \
 84      (b)[(i) + 4] = (u8) ( (n) >> 32 );  \
 85      (b)[(i) + 3] = (u8) ( (n) >> 24 );  \
 86      (b)[(i) + 2] = (u8) ( (n) >> 16 );  \
 87      (b)[(i) + 1] = (u8) ( (n) >>  8 );  \
 88      (b)[(i)    ] = (u8) ( (n)       );  \
 89  } while( 0 )
 90  #endif /* PUT_UINT64_LE */
 91  
 92  #define SHA0_STATE_SIZE   5
 93  #define SHA0_BLOCK_SIZE   64
 94  #define SHA0_DIGEST_SIZE  20
 95  #define SHA0_DIGEST_SIZE_BITS  160
 96  
 97  #define SHA0_HASH_MAGIC ((word_t)(0x6611302537891263ULL))
 98  #define SHA0_HASH_CHECK_INITIALIZED(A, ret, err) \
 99  	MUST_HAVE((((void *)(A)) != NULL) && ((A)->magic == SHA0_HASH_MAGIC), ret, err)
100  
101  typedef struct {
102  	/* Number of bytes processed */
103  	u64 sha0_total;
104  	/* Internal state */
105  	u32 sha0_state[SHA0_STATE_SIZE];
106  	/* Internal buffer to handle updates in a block */
107  	u8 sha0_buffer[SHA0_BLOCK_SIZE];
108  	/* Initialization magic value */
109  	word_t magic;
110  } sha0_context;
111  
112  /* Init hash function. Returns 0 on success, -1 on error. */
113  ATTRIBUTE_WARN_UNUSED_RET int sha0_init(sha0_context *ctx);
114  
115  ATTRIBUTE_WARN_UNUSED_RET int sha0_update(sha0_context *ctx, const u8 *input, u32 ilen);
116  
117  /* Finalize. Returns 0 on success, -1 on error.*/
118  ATTRIBUTE_WARN_UNUSED_RET int sha0_final(sha0_context *ctx, u8 output[SHA0_DIGEST_SIZE]);
119  
120  /*
121   * Scattered version performing init/update/finalize on a vector of buffers
122   * 'inputs' with the length of each buffer passed via 'ilens'. The function
123   * loops on pointers in 'inputs' until it finds a NULL pointer. The function
124   * returns 0 on success, -1 on error.
125   */
126  ATTRIBUTE_WARN_UNUSED_RET int sha0_scattered(const u8 **inputs, const u32 *ilens,
127  		      u8 output[SHA0_DIGEST_SIZE]);
128  
129  /*
130   * Single call version performing init/update/final on given input.
131   * Returns 0 on success, -1 on error.
132   */
133  ATTRIBUTE_WARN_UNUSED_RET int sha0(const u8 *input, u32 ilen, u8 output[SHA0_DIGEST_SIZE]);
134  
135  #endif /* __SHA0_H__ */