1 #ifndef _CORE_SHA256_H_ 2 #define _CORE_SHA256_H_ 3 4 #if defined(__cplusplus) 5 extern "C" { 6 #endif 7 8 #include "core_stdinc.h" 9 10 #define CORE_SHA256_DIGEST_LENGTH (32) 11 #define CORE_SHA256_BLOCK_LENGTH (64) 12 #define CORE_SHA256_SHORT_BLOCK_LENGTH (CORE_SHA256_BLOCK_LENGTH - 8) 13 #define CORE_SHA256_DIGEST_STRING_LENGTH (CORE_SHA256_DIGEST_LENGTH * 2 + 1) 14 15 /** 16 * \brief SHA-256 context structure 17 */ 18 typedef struct { 19 uint32_t total[2]; /*!< number of bytes processed */ 20 uint32_t state[8]; /*!< intermediate digest state */ 21 unsigned char buffer[64]; /*!< data block being processed */ 22 uint8_t is224; /*!< 0 => SHA-256, else SHA-224 */ 23 } core_sha256_context_t; 24 25 /** 26 * \brief Initialize SHA-256 context 27 * 28 * \param ctx SHA-256 context to be initialized 29 */ 30 void core_sha256_init(core_sha256_context_t *ctx); 31 32 /** 33 * \brief Clear SHA-256 context 34 * 35 * \param ctx SHA-256 context to be cleared 36 */ 37 void core_sha256_free(core_sha256_context_t *ctx); 38 39 40 /** 41 * \brief SHA-256 context setup 42 * 43 * \param ctx context to be initialized 44 */ 45 void core_sha256_starts(core_sha256_context_t *ctx); 46 47 /** 48 * \brief SHA-256 process buffer 49 * 50 * \param ctx SHA-256 context 51 * \param input buffer holding the data 52 * \param ilen length of the input data 53 */ 54 void core_sha256_update(core_sha256_context_t *ctx, const unsigned char *input, uint32_t ilen); 55 56 /** 57 * \brief SHA-256 final digest 58 * 59 * \param ctx SHA-256 context 60 * \param output SHA-256 checksum result 61 */ 62 void core_sha256_finish(core_sha256_context_t *ctx, uint8_t output[32]); 63 64 /* Internal use */ 65 void core_sha256_process(core_sha256_context_t *ctx, const unsigned char data[64]); 66 67 /** 68 * \brief Output = SHA-256( input buffer ) 69 * 70 * \param input buffer holding the data 71 * \param ilen length of the input data 72 * \param output SHA-256 checksum result 73 */ 74 void core_sha256(const uint8_t *input, uint32_t ilen, uint8_t output[32]); 75 76 void core_hmac_sha256(const uint8_t *msg, uint32_t msg_len, const uint8_t *key, uint32_t key_len, uint8_t output[32]); 77 78 #if defined(__cplusplus) 79 } 80 #endif 81 82 #endif 83 84