apm32f4xx_hash.h 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. /*!
  2. * @file apm32f4xx_hash.c
  3. *
  4. * @brief This file provides all the HASH firmware functions
  5. *
  6. * @version V1.0.2
  7. *
  8. * @date 2022-06-23
  9. *
  10. * @attention
  11. *
  12. * Copyright (C) 2021-2022 Geehy Semiconductor
  13. *
  14. * You may not use this file except in compliance with the
  15. * GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
  16. *
  17. * The program is only for reference, which is distributed in the hope
  18. * that it will be usefull and instructional for customers to develop
  19. * their software. Unless required by applicable law or agreed to in
  20. * writing, the program is distributed on an "AS IS" BASIS, WITHOUT
  21. * ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
  22. * See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
  23. * and limitations under the License.
  24. */
  25. /* Define to prevent recursive inclusion */
  26. #ifndef __APM32F4XX_HASH_H
  27. #define __APM32F4XX_HASH_H
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. /* Includes */
  32. #include "apm32f4xx.h"
  33. /** @addtogroup APM32F4xx_StdPeriphDriver
  34. @{
  35. */
  36. /** @addtogroup HASH_Driver
  37. @{
  38. */
  39. /** @defgroup HASH_Enumerations
  40. @{
  41. */
  42. /**
  43. * @brief HASH Algo Selection
  44. */
  45. typedef enum
  46. {
  47. HASH_ALGO_SELECTION_SHA1, /*!< Select SHA-1 algorithm */
  48. HASH_ALGO_SELECTION_MD5 /*!< Select MD5 algorithm */
  49. } HASH_ALGO_SELECTION_T;
  50. /**
  51. * @brief HASH processor Algorithm Mode
  52. */
  53. typedef enum
  54. {
  55. HASH_ALGO_MODE_HASH, /*!< HASH mode */
  56. HASH_ALGO_MODE_HMAC /*!< HMAC mode */
  57. } HASH_ALGO_MODE_T;
  58. /**
  59. * @brief HASH Data Type
  60. */
  61. typedef enum
  62. {
  63. HASH_DATA_TYPE_32B, /*!< 32-bit data type */
  64. HASH_DATA_TYPE_16B, /*!< 16-bit data type */
  65. HASH_DATA_TYPE_8B, /*!< 8-bit data type */
  66. HASH_DATA_TYPE_1B /*!< 1-bit data type */
  67. } HASH_DATA_TYPE_T;
  68. /**
  69. * @brief HASH HMAC Long key only for HMAC mode
  70. */
  71. typedef enum
  72. {
  73. HASH_HMAC_KEY_TYPE_SHORTKEY, /*!< Short key type */
  74. HASH_HMAC_KEY_TYPE_LONGKEY /*!< Long key type */
  75. } HASH_HMAC_KEY_TYPE_T;
  76. /**
  77. * @brief HASH interrupts
  78. */
  79. typedef enum
  80. {
  81. HASH_INT_INDATAINT = BIT0, /*!< Input Data interrupt mask */
  82. HASH_INT_DCALCINT = BIT1, /*!< Digest calculation completion Data interrupt mask */
  83. } HASH_INT_T;
  84. /**
  85. * @brief HASH flag
  86. */
  87. typedef enum
  88. {
  89. HASH_FLAG_INDATAINT = BIT0, /*!< Data input interrupt status flag */
  90. HASH_FLAG_DCALCINT = BIT1, /*!< Digest calculation completion interrupt status flag */
  91. HASH_FLAG_DMA = BIT2, /*!< DMAS Status flag */
  92. HASH_FLAG_BUSY = BIT3, /*!< Busy flag */
  93. HASH_FLAG_DINNEMPT = BIT12 /*!< Data Input register (DIN) not empty status flag */
  94. } HASH_FLAG_T;
  95. /**
  96. * @brief HASH interrupt flag
  97. */
  98. typedef enum
  99. {
  100. HASH_INT_FLAG_INDATA = BIT0, /*!< Input Data interrupt */
  101. HASH_INT_FLAG_DCALC = BIT1 /*!< Digest Calculation Completion Interrupt */
  102. } HASH_INT_FLAG_T;
  103. /**@} end of group HASH_Enumerations*/
  104. /** @addtogroup HASH_Structure Data Structure
  105. @{
  106. */
  107. /**
  108. * @brief HASH Init structure
  109. */
  110. typedef struct
  111. {
  112. HASH_ALGO_SELECTION_T algoSelect; /*!< SHA-1 or MD5 */
  113. HASH_ALGO_MODE_T algoMode; /*!< HASH or HMAC */
  114. HASH_DATA_TYPE_T dataType; /*!< 32-bit data, 16-bit data, 8-bit data or bit string */
  115. HASH_HMAC_KEY_TYPE_T hmacKeyType; /*!< HMAC Short key or HMAC Long Key */
  116. } HASH_Config_T;
  117. /**
  118. * @brief HASH message digest result structure
  119. */
  120. typedef struct
  121. {
  122. uint32_t Data[5]; /*!< Message digest result :
  123. 5x 32bit words for SHA-1 or
  124. 4x 32bit words for MD5 */
  125. } HASH_MessageDigest_T;
  126. /**
  127. * @brief HASH context swapping structure
  128. */
  129. typedef struct
  130. {
  131. uint32_t HASH_INT; /*!< HASH interrupt register */
  132. uint32_t HASH_START; /*!< HASH shart register */
  133. uint32_t HASH_CTRL; /*!< HASH CTRL register */
  134. uint32_t HASH_CTSWAP[51]; /*!< HASH CTSWAP register */
  135. } HASH_Context_T;
  136. /**@} end of group HASH_Structure*/
  137. /** @defgroup HASH_Functions
  138. @{
  139. */
  140. /* HASH Reset */
  141. void HASH_Reset(void);
  142. /* Configuration */
  143. void HASH_Config(HASH_Config_T* hashConfig);
  144. void HASH_ConfigStructInit(HASH_Config_T* hashConfig);
  145. void HASH_ResetProceCore(void);
  146. /* Message Digest start */
  147. void HASH_ConfigLastWordValidBitsNbr(uint16_t validNumber);
  148. void HASH_WritesInputData(uint32_t data);
  149. uint8_t HASH_ReadInFIFOWordsNbr(void);
  150. void HASH_ReadDigest(HASH_MessageDigest_T* messageDigest);
  151. void HASH_StartDigest(void);
  152. /* Context swapping */
  153. void HASH_ReadContext(HASH_Context_T* contextRead);
  154. void HASH_WriteContext(HASH_Context_T* contextWrite);
  155. /* Regular Channels DMA */
  156. void HASH_EnableDMA(void);
  157. void HASH_DisableDMA(void);
  158. /* Injected channels Configuration */
  159. void HASH_EnableInterrupt(uint32_t interrupt);
  160. void HASH_DisableInterrupt(uint32_t interrupt);
  161. uint8_t HASH_ReadFlagStatus(HASH_FLAG_T flag);
  162. void HASH_ClearStatusFlag(HASH_FLAG_T flag);
  163. uint8_t HASH_ReadIntFlag(HASH_INT_FLAG_T flag);
  164. void HASH_ClearIntFlag(HASH_INT_FLAG_T flag);
  165. /* Waits for processing data */
  166. uint8_t HASH_WaitForCompute(uint32_t timeOut);
  167. /* High Level SHA1 Compute */
  168. uint8_t HASH_ComputeSHA1(uint8_t* inBuffer, uint32_t lenBuffer,
  169. uint8_t outBuffer[20]);
  170. uint8_t HMAC_ComputeSHA1(uint8_t* key, uint32_t lenkey, uint8_t* inBuffer,
  171. uint32_t lenBuffer, uint8_t outBuffer[20]);
  172. /* High Level MD5 Compute */
  173. uint8_t HASH_ComputeMD5(uint8_t* inBuffer, uint32_t lenBuffer,
  174. uint8_t outBuffer[16]);
  175. uint8_t HMAC_ComputeMD5(uint8_t* key, uint32_t keylen, uint8_t* inBuffer,
  176. uint32_t lenBuffer, uint8_t outBuffer[16]);
  177. #ifdef __cplusplus
  178. }
  179. #endif
  180. #endif /*__APM32F4XX_HASH_H */
  181. /**@} end of group HASH_Enumerations */
  182. /**@} end of group HASH_Driver */
  183. /**@} end of group APM32F4xx_StdPeriphDriver */