/**
  * \file		sha_256_hash.h
  *
  * \brief		SHA-2 256 hash function
  *
  * \author		Dmitriy Dorogovtsev (d.dorogovtse@samsung.com)
  *
  * \version	1.0
  *
  * \date		Created Apr 16, 2012
  *
  * \par		In Samsung Ukraine R&D Center (SURC) under a contract between
  * \par		LLC "Samsung Electronics Ukraine Company" (Kiev, Ukraine) and
  * \par		"Samsung Electronics Co", Ltd (Seoul, Republic of Korea)
  * \par		Copyright: (c) Samsung Electronics Co, Ltd 2012. All rights reserved.
  */

#ifndef SHA_256_HASH_H
#define SHA_256_HASH_H

#include <stdint.h>

/*!
  Amount of bytes in SHA-2 256 hash function
  */
#define SHA256_HASH_LENGTH 32

/*!
  Amount of 32-bit words in SHA-2 256 intermediate hash storage
  */
#define SHA256_HASH_BLOCK_LENGTH 64

/*!
  Amount of bytes in message block for SHA-2 256 hash function
  */
#define SHA256_MESSAGE_BLOCK_LENGTH 64

/**
  * SHA-2 256 hash function structure
  */
typedef struct
{
	uint32_t m_hash[ SHA256_HASH_LENGTH / 4 ]; /**< SHA-2 256 hash result */
	uint32_t m_hashBlock[ SHA256_HASH_BLOCK_LENGTH ]; /**< SHA-2 256 hash intermediate storage */
	uint8_t  m_messageBlock[ SHA256_MESSAGE_BLOCK_LENGTH ]; /**< SHA-2 256 message block */

	uint32_t m_lengthLow; /**< Low 4 bytes of message length representation */
	uint32_t m_lengthHigh; /**< High 4 bytes of message length representation */
	uint8_t  m_messageBlockIndex; /**< Index of current free position in message block */
} SHA_256_Hash;

/**
  * \brief		Initialization of SHA-2 256 hash function
  *
  * \param h	SHA_256_Hash structure to initialize
  *
  * \return		void
  */
void initSHA256Hash( SHA_256_Hash* h );

/**
  * \brief			Process part of message with given length. Should be called as many times as
					needed for the whole message after initialization.
  * \param h		SHA_256_Hash structure that will process message
  * \param message	Pointer to message
  * \param length	Length (in bytes) of message
  *
  * \return			void
  */
void updateSHA256Hash( SHA_256_Hash* h, const uint8_t* message, unsigned int length );

/**
  * \brief		Calculates SHA-2 256 hash of the whole message passed by parts and resets SHA-2
				structure, making it clear and ready for using again
  * \param h	SHA_256_Hash structure to get resulting hash values from and reset
  * \param hash	Storage for resulting hash values. Must point to 32 bytes of memory.
  *
  * \return		void
  */
void getResultAndResetSHA256Hash( SHA_256_Hash* h, uint8_t* hash );

#endif 
