27 #include "in_compile.h" 34 #define HASH_REG_OFFSET_HASH_H0 0x00000640UL 35 #define HASH_REG_OFFSET_HASH_H1 0x00000644UL 36 #define HASH_REG_OFFSET_HASH_H2 0x00000648UL 37 #define HASH_REG_OFFSET_HASH_H3 0x0000064CUL 38 #define HASH_REG_OFFSET_HASH_H4 0x00000650UL 39 #define HASH_REG_OFFSET_HASH_H5 0x00000654UL 40 #define HASH_REG_OFFSET_HASH_H6 0x00000658UL 41 #define HASH_REG_OFFSET_HASH_H7 0x0000065CUL 42 #define HASH_REG_OFFSET_HASH_HX_MASK 0xFFFFFFFFUL 43 #define HASH_REG_OFFSET_HASH_CONTROL 0x000007C0UL 44 #define HASH_REG_OFFSET_HASH_CONTROL_MASK 0x00000003UL 45 #define HASH_REG_OFFSET_HASH_PAD_EN 0x000007C4UL 46 #define HASH_REG_OFFSET_HASH_PAD_EN_MASK 0x00000001UL 47 #define HASH_REG_OFFSET_HASH_PAD_CFG 0x000007C8UL 48 #define HASH_REG_OFFSET_HASH_PAD_CFG_MASK 0x00000004UL 49 #define HASH_REG_OFFSET_HASH_CUR_LEN_0 0x000007CCUL 50 #define HASH_REG_OFFSET_HASH_CUR_LEN_1 0x000007D0UL 51 #define HASH_REG_OFFSET_HASH_CUR_LEN_X_MASK 0xFFFFFFFFUL 52 #define HASH_REG_OFFSET_HASH_PARAM 0x000007DCUL 53 #define HASH_REG_OFFSET_HASH_INT_BUSY 0x000007E0UL 54 #define HASH_REG_OFFSET_HASH_INT_BUSY_MASK 0x00000001UL 55 #define HASH_REG_OFFSET_HASH_SW_RESET 0x000007E4UL 56 #define HASH_REG_OFFSET_HASH_SW_RESET_MASK 0x00000001UL 57 #define HASH_REG_OFFSET_HASH_SWAP 0x000007E8UL 58 #define HASH_REG_OFFSET_HASH_SWAP_MASK 0x00000001UL 59 #define HASH_REG_OFFSET_HASH_DIRECT_DATA 0x000007ECUL 60 #define HASH_REG_OFFSET_HASH_DIRECT_DATA_MASK 0xFFFFFFFFUL 63 #define HASH_REG_OFFSET_HASH_CONTROL_MD5 0x00000000UL 64 #define HASH_REG_OFFSET_HASH_CONTROL_SHA1 0x00000001UL 65 #define HASH_REG_OFFSET_HASH_CONTROL_SHA256 0x00000002UL 77 typedef struct hash_init {
80 void (*callback)(
void *arg,
int error);
83 static INLINE
void hash_md5_mode(
void)
85 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CONTROL);
86 reg &= ~(HASH_REG_OFFSET_HASH_CONTROL_MASK);
87 reg |= HASH_REG_OFFSET_HASH_CONTROL_MD5;
88 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CONTROL, reg);
91 static INLINE
void hash_sha1_mode(
void)
93 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CONTROL);
94 reg &= ~(HASH_REG_OFFSET_HASH_CONTROL_MASK);
95 reg |= HASH_REG_OFFSET_HASH_CONTROL_SHA1;
96 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CONTROL, reg);
99 static INLINE
void hash_sha256_mode(
void)
101 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CONTROL);
102 reg &= ~(HASH_REG_OFFSET_HASH_CONTROL_MASK);
103 reg |= HASH_REG_OFFSET_HASH_CONTROL_SHA256;
104 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CONTROL, reg);
107 static INLINE
void hash_pad_block_enable(
void)
109 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_EN);
110 reg |= (HASH_REG_OFFSET_HASH_PAD_EN_MASK);
111 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_EN, reg);
114 static INLINE
void hash_pad_block_disable(
void)
116 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_EN);
117 reg &= ~(HASH_REG_OFFSET_HASH_PAD_EN_MASK);
118 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_EN, reg);
121 static INLINE
void hash_padding_generation_set(
void)
123 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_CFG);
124 reg |= (HASH_REG_OFFSET_HASH_PAD_CFG_MASK);
125 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_CFG, reg);
128 static INLINE
void hash_padding_generation_reset(
void)
130 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_CFG);
131 reg &= ~(HASH_REG_OFFSET_HASH_PAD_CFG_MASK);
132 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_PAD_CFG, reg);
135 static INLINE
void hash_len_low_word(uint32_t len0)
137 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CUR_LEN_0, len0);
140 static INLINE
void hash_len_high_word(uint32_t len1)
142 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CUR_LEN_1, len1);
145 static INLINE
int hash_busy(
void)
147 return (RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_INT_BUSY) & HASH_REG_OFFSET_HASH_INT_BUSY_MASK);
150 static INLINE
void hash_sw_reset(
void)
152 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_SW_RESET);
153 reg |= (HASH_REG_OFFSET_HASH_SW_RESET_MASK);
154 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_SW_RESET, reg);
157 static INLINE
void hash_data_swap_enable(
void)
159 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_SWAP);
160 reg &= ~(HASH_REG_OFFSET_HASH_SWAP_MASK);
161 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_SWAP, reg);
164 static INLINE
void hash_data_swap_disable(
void)
166 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_SWAP);
167 reg |= (HASH_REG_OFFSET_HASH_SWAP_MASK);
168 WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_SWAP, reg);
171 #define HASH_REG_OFFSET_DATA_WR_PORT 0x00000900UL 173 static INLINE
void hash_write_data(uint32_t data)
175 while(hash_busy() == 0x1) ;
176 WR_WORD(SHA_BASE + HASH_REG_OFFSET_DATA_WR_PORT, data);
179 static INLINE
void hash_write_hx(
int idx, uint32_t data)
181 WR_WORD((SHA_BASE + HASH_REG_OFFSET_HASH_H0 + (4 * idx)), (data & HASH_REG_OFFSET_HASH_HX_MASK));
184 static INLINE uint32_t hash_read_hx(
int idx)
186 return RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_H0 + (4 * idx));
189 static INLINE
void hash_write_to_port(uint8_t *chunk) {
190 for(
int i = 0; i < 64; i+= 4) {
191 hash_write_data((chunk[i+3]<<24) | (chunk[i+2]<<16) | (chunk[i+1]<<8) | (chunk[i]));
195 #define HASH_REG_OFFSET_INTR 0x00000800UL 196 #define HASH_REG_OFFSET_INTR_EN 0x00000040UL 197 #define HASH_REG_OFFSET_INTR_CLR 0x00000080UL 198 #define HASH_REG_OFFSET_INTR_STATUS 0x00000100UL 200 static INLINE
void hash_intr_unmask() {
201 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_INTR);
202 reg |= (HASH_REG_OFFSET_INTR_EN);
203 WR_WORD(SHA_BASE + HASH_REG_OFFSET_INTR, reg);
206 static INLINE
void hash_intr_mask() {
207 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_INTR);
208 reg &= ~(HASH_REG_OFFSET_INTR_EN);
209 WR_WORD(SHA_BASE + HASH_REG_OFFSET_INTR, reg);
212 static INLINE
void hash_intr_clr() {
213 uint32_t reg = RD_WORD(SHA_BASE + HASH_REG_OFFSET_INTR);
214 reg &= ~(HASH_REG_OFFSET_INTR_CLR);
215 WR_WORD(SHA_BASE + HASH_REG_OFFSET_INTR, reg);
218 static INLINE uint8_t hash_intr_status() {
219 return ((RD_WORD(SHA_BASE + HASH_REG_OFFSET_INTR) & HASH_REG_OFFSET_INTR_STATUS) > 0) ? 1 : 0;
int hal_hash_process(uint8_t *data, uint32_t data_len)
Process hash based on input payload.
HASH_result_status
HASH function status return.
Definition: hal_hash.h:223
Error, HASH driver not initialized.
Definition: hal_hash.h:227
Error, HASH driver already initialized.
Definition: hal_hash.h:229
HASH type MD-5.
Definition: hal_hash.h:70
hash_type
HASH function type.
Definition: hal_hash.h:68
No errors, good status.
Definition: hal_hash.h:225
HASH type SHA-1.
Definition: hal_hash.h:72
HASH type SHA-256.
Definition: hal_hash.h:74
Error, HASH bad state.
Definition: hal_hash.h:231
int hal_hash_open(hash_init_t *init)
Initialize hash driver for power management and interrupt purposes.
Error, invalid HASH parameter.
Definition: hal_hash.h:233
int hal_hash_close(void)
De-Initialize hash driver for power management and interrupt purposes.
int hal_hash_start(hash_type type)
Set-up and initialize hash functionality.
int hal_hash_finish(uint8_t *out, uint16_t len)
Write hash result to output buffer.