n32g45x_hash.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. /*****************************************************************************
  2. * Copyright (c) 2019, Nations Technologies Inc.
  3. *
  4. * All rights reserved.
  5. * ****************************************************************************
  6. *
  7. * Redistribution and use in source and binary forms, with or without
  8. * modification, are permitted provided that the following conditions are met:
  9. *
  10. * - Redistributions of source code must retain the above copyright notice,
  11. * this list of conditions and the disclaimer below.
  12. *
  13. * Nations' name may not be used to endorse or promote products derived from
  14. * this software without specific prior written permission.
  15. *
  16. * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
  17. * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
  19. * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
  20. * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  21. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
  22. * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  23. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  24. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
  25. * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. * ****************************************************************************/
  27. /**
  28. * @file n32g45x_hash.h
  29. * @author Nations
  30. * @version v1.0.1
  31. *
  32. * @copyright Copyright (c) 2019, Nations Technologies Inc. All rights reserved.
  33. */
  34. #ifndef __N32G45X_HASH_H__
  35. #define __N32G45X_HASH_H__
  36. #include <stdint.h>
  37. /** @addtogroup N32G45X_Algorithm_Library
  38. * @{
  39. */
  40. /** @addtogroup HASH
  41. * @brief Message digest algorithms
  42. * @{
  43. */
  44. #define ALG_SHA1 (uint16_t)(0x0004)
  45. #define ALG_SHA224 (uint16_t)(0x000A)
  46. #define ALG_SHA256 (uint16_t)(0x000B)
  47. #define ALG_MD5 (uint16_t)(0x000C)
  48. #define ALG_SM3 (uint16_t)(0x0012)
  49. enum
  50. {
  51. HASH_SEQUENCE_TRUE = 0x0105A5A5,//save IV
  52. HASH_SEQUENCE_FALSE = 0x010A5A5A, //not save IV
  53. HASH_Init_OK = 0,//hash init success
  54. HASH_Start_OK = 0,//hash update success
  55. HASH_Update_OK = 0,//hash update success
  56. HASH_Complete_OK = 0,//hash complete success
  57. HASH_Close_OK = 0,//hash close success
  58. HASH_ByteLenPlus_OK = 0,//byte length plus success
  59. HASH_PadMsg_OK = 0,//message padding success
  60. HASH_ProcMsgBuf_OK = 0, //message processing success
  61. SHA1_Hash_OK = 0,//sha1 operation success
  62. SM3_Hash_OK = 0,//sm3 operation success
  63. SHA224_Hash_OK = 0,//sha224 operation success
  64. SHA256_Hash_OK = 0,//sha256 operation success
  65. MD5_Hash_OK = 0,//MD5 operation success
  66. HASH_Init_ERROR = 0x01044400,//hash init error
  67. HASH_Start_ERROR, //hash start error
  68. HASH_Update_ERROR, //hash update error
  69. HASH_ByteLenPlus_ERROR,//hash byte plus error
  70. };
  71. struct _HASH_CTX_;
  72. typedef struct
  73. {
  74. const uint16_t HashAlgID;//choice hash algorithm
  75. const uint32_t * const K, KLen;//K and word length of K
  76. const uint32_t * const IV, IVLen;//IV and word length of IV
  77. const uint32_t HASH_SACCR, HASH_HASHCTRL;//relate registers
  78. const uint32_t BlockByteLen, BlockWordLen; //byte length of block, word length of block
  79. const uint32_t DigestByteLen, DigestWordLen; //byte length of digest,word length of digest
  80. const uint32_t Cycle; //interation times
  81. uint32_t (* const ByteLenPlus)(uint32_t *, uint32_t); //function pointer
  82. uint32_t (* const PadMsg)(struct _HASH_CTX_ *); //function pointer
  83. }HASH_ALG;
  84. typedef struct _HASH_CTX_
  85. {
  86. const HASH_ALG *hashAlg;//pointer to HASH_ALG
  87. uint32_t sequence; // TRUE if the IV should be saved
  88. uint32_t IV[16];
  89. uint32_t msgByteLen[4];
  90. uint8_t msgBuf[128+4];
  91. uint32_t msgIdx;
  92. }HASH_CTX;
  93. extern const HASH_ALG HASH_ALG_SHA1[1];
  94. extern const HASH_ALG HASH_ALG_SHA224[1];
  95. extern const HASH_ALG HASH_ALG_SHA256[1];
  96. extern const HASH_ALG HASH_ALG_MD5[1];
  97. extern const HASH_ALG HASH_ALG_SM3[1];
  98. /**
  99. * @brief Hash init
  100. * @param[in] ctx pointer to HASH_CTX struct
  101. * @return HASH_Init_OK, Hash init success; othets: Hash init fail
  102. * @note 1.Please refer to the demo in user guidance before using this function
  103. */
  104. uint32_t HASH_Init(HASH_CTX *ctx);
  105. /**
  106. * @brief Hash start
  107. * @param[in] ctx pointer to HASH_CTX struct
  108. * @return HASH_Start_OK, Hash start success; othets: Hash start fail
  109. * @note 1.Please refer to the demo in user guidance before using this function
  110. * 2.HASH_Init() should be recalled before use this function
  111. */
  112. uint32_t HASH_Start(HASH_CTX *ctx);
  113. /**
  114. * @brief Hash update
  115. * @param[in] ctx pointer to HASH_CTX struct
  116. * @param[in] in pointer to message
  117. * @param[out] out pointer tohash result,digest
  118. * @return HASH_Update_OK, Hash update success; othets: Hash update fail
  119. * @note 1.Please refer to the demo in user guidance before using this function
  120. * 2.HASH_Init() and HASH_Start() should be recalled before use this function
  121. */
  122. uint32_t HASH_Update(HASH_CTX *ctx, uint8_t *in, uint32_t byteLen);
  123. /**
  124. * @brief Hash complete
  125. * @param[in] ctx pointer to HASH_CTX struct
  126. * @param[out] out pointer tohash result,digest
  127. * @return HASH_Complete_OK, Hash complete success; othets: Hash complete fail
  128. * @note 1.Please refer to the demo in user guidance before using this function
  129. * 2.HASH_Init(), HASH_Start() and HASH_Update() should be recalled before use this function
  130. */
  131. uint32_t HASH_Complete(HASH_CTX *ctx, uint8_t *out);
  132. /**
  133. * @brief Hash close
  134. * @return HASH_Close_OK, Hash close success; othets: Hash close fail
  135. * @note 1.Please refer to the demo in user guidance before using this function
  136. */
  137. uint32_t HASH_Close(void);
  138. /**
  139. * @brief SM3 Hash for 256bits digest
  140. * @param[in] in pointer to message
  141. * @param[in] byte length of in
  142. * @param[out] out pointer tohash result,digest
  143. * @return SM3_Hash_OK, SM3 hash success; othets: SM3 hash fail
  144. * @note 1.Please refer to the demo in user guidance before using this function
  145. */
  146. uint32_t SM3_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
  147. /**
  148. * @brief SHA1 Hash
  149. * @param[in] in pointer to message
  150. * @param[in] byte length of in
  151. * @param[out] out pointer tohash result,digest
  152. * @return SHA1_Hash_OK, SHA1 hash success; othets: SHA1 hash fail
  153. * @note 1.Please refer to the demo in user guidance before using this function
  154. */
  155. uint32_t SHA1_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
  156. /**
  157. * @brief SHA224 Hash
  158. * @param[in] in pointer to message
  159. * @param[in] byte length of in
  160. * @param[out] out pointer tohash result,digest
  161. * @return SHA224_Hash_OK, SHA224 hash success; othets: SHA224 hash fail
  162. * @note 1.Please refer to the demo in user guidance before using this function
  163. */
  164. uint32_t SHA224_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
  165. /**
  166. * @brief SHA256 Hash
  167. * @param[in] in pointer to message
  168. * @param[in] byte length of in
  169. * @param[out] out pointer tohash result,digest
  170. * @return SHA256_Hash_OK, SHA256 hash success; othets: SHA256 hash fail
  171. * @note 1.Please refer to the demo in user guidance before using this function
  172. */
  173. uint32_t SHA256_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
  174. /**
  175. * @brief MD5 Hash
  176. * @param[in] in pointer to message
  177. * @param[in] byte length of in
  178. * @param[in] out pointer tohash result,digest
  179. * @return MD5_Hash_OK, MD5 hash success; othets: MD5 hash fail
  180. * @note 1.Please refer to the demo in user guidance before using this function
  181. */
  182. uint32_t MD5_Hash(uint8_t* in,uint32_t byteLen, uint8_t* out);
  183. /**
  184. * @brief Get HASH lib version
  185. * @param[out] type pointer one byte type information represents the type of the lib, like Commercial version.\
  186. * @Bits 0~4 stands for Commercial (C), Security (S), Normal (N), Evaluation (E), Test (T), Bits 5~7 are reserved. e.g. 0x09 stands for CE version.
  187. * @param[out] customer pointer one byte customer information represents customer ID. for example, 0x00 stands for standard version, 0x01 is for Tianyu customized version...
  188. * @param[out] date pointer array which include three bytes date information. If the returned bytes are 18,9,13,this denotes September 13,2018
  189. * @param[out] version pointer one byte version information represents develop version of the lib. e.g. 0x12 denotes version 1.2.
  190. * @return none
  191. * @1.You can recall this function to get RSA lib information
  192. */
  193. void HASH_Version(uint8_t *type, uint8_t *customer, uint8_t date[3], uint8_t *version);
  194. #endif