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 26 /* Define to prevent recursive inclusion */ 27 #ifndef __APM32F4XX_HASH_H 28 #define __APM32F4XX_HASH_H 29 30 #ifdef __cplusplus 31 extern "C" { 32 #endif 33 34 /* Includes */ 35 #include "apm32f4xx.h" 36 37 /** @addtogroup APM32F4xx_StdPeriphDriver 38 @{ 39 */ 40 41 /** @addtogroup HASH_Driver 42 @{ 43 */ 44 45 /** @defgroup HASH_Enumerations 46 @{ 47 */ 48 49 /** 50 * @brief HASH Algo Selection 51 */ 52 typedef enum 53 { 54 HASH_ALGO_SELECTION_SHA1, /*!< Select SHA-1 algorithm */ 55 HASH_ALGO_SELECTION_MD5 /*!< Select MD5 algorithm */ 56 } HASH_ALGO_SELECTION_T; 57 58 /** 59 * @brief HASH processor Algorithm Mode 60 */ 61 typedef enum 62 { 63 HASH_ALGO_MODE_HASH, /*!< HASH mode */ 64 HASH_ALGO_MODE_HMAC /*!< HMAC mode */ 65 } HASH_ALGO_MODE_T; 66 67 /** 68 * @brief HASH Data Type 69 */ 70 typedef enum 71 { 72 HASH_DATA_TYPE_32B, /*!< 32-bit data type */ 73 HASH_DATA_TYPE_16B, /*!< 16-bit data type */ 74 HASH_DATA_TYPE_8B, /*!< 8-bit data type */ 75 HASH_DATA_TYPE_1B /*!< 1-bit data type */ 76 } HASH_DATA_TYPE_T; 77 78 /** 79 * @brief HASH HMAC Long key only for HMAC mode 80 */ 81 typedef enum 82 { 83 HASH_HMAC_KEY_TYPE_SHORTKEY, /*!< Short key type */ 84 HASH_HMAC_KEY_TYPE_LONGKEY /*!< Long key type */ 85 } HASH_HMAC_KEY_TYPE_T; 86 87 /** 88 * @brief HASH interrupts 89 */ 90 typedef enum 91 { 92 HASH_INT_INDATAINT = BIT0, /*!< Input Data interrupt mask */ 93 HASH_INT_DCALCINT = BIT1, /*!< Digest calculation completion Data interrupt mask */ 94 } HASH_INT_T; 95 96 /** 97 * @brief HASH flag 98 */ 99 typedef enum 100 { 101 HASH_FLAG_INDATAINT = BIT0, /*!< Data input interrupt status flag */ 102 HASH_FLAG_DCALCINT = BIT1, /*!< Digest calculation completion interrupt status flag */ 103 HASH_FLAG_DMA = BIT2, /*!< DMAS Status flag */ 104 HASH_FLAG_BUSY = BIT3, /*!< Busy flag */ 105 HASH_FLAG_DINNEMPT = BIT12 /*!< Data Input register (DIN) not empty status flag */ 106 } HASH_FLAG_T; 107 108 /** 109 * @brief HASH interrupt flag 110 */ 111 typedef enum 112 { 113 HASH_INT_FLAG_INDATA = BIT0, /*!< Input Data interrupt */ 114 HASH_INT_FLAG_DCALC = BIT1 /*!< Digest Calculation Completion Interrupt */ 115 } HASH_INT_FLAG_T; 116 117 /**@} end of group HASH_Enumerations*/ 118 119 /** @addtogroup HASH_Structure Data Structure 120 @{ 121 */ 122 123 /** 124 * @brief HASH Init structure 125 */ 126 typedef struct 127 { 128 HASH_ALGO_SELECTION_T algoSelect; /*!< SHA-1 or MD5 */ 129 HASH_ALGO_MODE_T algoMode; /*!< HASH or HMAC */ 130 HASH_DATA_TYPE_T dataType; /*!< 32-bit data, 16-bit data, 8-bit data or bit string */ 131 HASH_HMAC_KEY_TYPE_T hmacKeyType; /*!< HMAC Short key or HMAC Long Key */ 132 } HASH_Config_T; 133 134 /** 135 * @brief HASH message digest result structure 136 */ 137 typedef struct 138 { 139 uint32_t Data[5]; /*!< Message digest result : 140 5x 32bit words for SHA-1 or 141 4x 32bit words for MD5 */ 142 } HASH_MessageDigest_T; 143 144 /** 145 * @brief HASH context swapping structure 146 */ 147 typedef struct 148 { 149 uint32_t HASH_INT; /*!< HASH interrupt register */ 150 uint32_t HASH_START; /*!< HASH shart register */ 151 uint32_t HASH_CTRL; /*!< HASH CTRL register */ 152 uint32_t HASH_CTSWAP[51]; /*!< HASH CTSWAP register */ 153 } HASH_Context_T; 154 155 /**@} end of group HASH_Structure*/ 156 157 /** @defgroup HASH_Functions 158 @{ 159 */ 160 161 /* HASH Reset */ 162 void HASH_Reset(void); 163 164 /* Configuration */ 165 void HASH_Config(HASH_Config_T* hashConfig); 166 void HASH_ConfigStructInit(HASH_Config_T* hashConfig); 167 void HASH_ResetProceCore(void); 168 169 /* Message Digest start */ 170 void HASH_ConfigLastWordValidBitsNbr(uint16_t validNumber); 171 void HASH_WritesInputData(uint32_t data); 172 uint8_t HASH_ReadInFIFOWordsNbr(void); 173 void HASH_ReadDigest(HASH_MessageDigest_T* messageDigest); 174 void HASH_StartDigest(void); 175 176 /* Context swapping */ 177 void HASH_ReadContext(HASH_Context_T* contextRead); 178 void HASH_WriteContext(HASH_Context_T* contextWrite); 179 180 /* Regular Channels DMA */ 181 void HASH_EnableDMA(void); 182 void HASH_DisableDMA(void); 183 184 /* Injected channels Configuration */ 185 void HASH_EnableInterrupt(uint32_t interrupt); 186 void HASH_DisableInterrupt(uint32_t interrupt); 187 uint8_t HASH_ReadFlagStatus(HASH_FLAG_T flag); 188 void HASH_ClearStatusFlag(HASH_FLAG_T flag); 189 uint8_t HASH_ReadIntFlag(HASH_INT_FLAG_T flag); 190 void HASH_ClearIntFlag(HASH_INT_FLAG_T flag); 191 192 /* Waits for processing data */ 193 uint8_t HASH_WaitForCompute(uint32_t timeOut); 194 195 /* High Level SHA1 Compute */ 196 uint8_t HASH_ComputeSHA1(uint8_t* inBuffer, uint32_t lenBuffer, 197 uint8_t outBuffer[20]); 198 uint8_t HMAC_ComputeSHA1(uint8_t* key, uint32_t lenkey, uint8_t* inBuffer, 199 uint32_t lenBuffer, uint8_t outBuffer[20]); 200 201 /* High Level MD5 Compute */ 202 uint8_t HASH_ComputeMD5(uint8_t* inBuffer, uint32_t lenBuffer, 203 uint8_t outBuffer[16]); 204 uint8_t HMAC_ComputeMD5(uint8_t* key, uint32_t keylen, uint8_t* inBuffer, 205 uint32_t lenBuffer, uint8_t outBuffer[16]); 206 207 #ifdef __cplusplus 208 } 209 #endif 210 211 #endif /*__APM32F4XX_HASH_H */ 212 213 /**@} end of group HASH_Enumerations */ 214 /**@} end of group HASH_Driver */ 215 /**@} end of group APM32F4xx_StdPeriphDriver */ 216