InPlay API
hal_hash.h
1 
13 #ifndef HAL_HASH_H
14 #define HAL_HASH_H
15 
25 #include "stdint.h"
26 #include "in_mmap.h"
27 #include "in_compile.h"
28 /*
29  ****************************************************************************************
30  * DEFINES
31  ****************************************************************************************
32  */
33 
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
61 
62 
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
66 
68 typedef enum {
75 } hash_type;
76 
77 typedef struct hash_init {
78  int prio;
79  void *arg;
80  void (*callback)(void *arg, int error);
81 } hash_init_t;
82 
83 static INLINE void hash_md5_mode(void)
84 {
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);
89 }
90 
91 static INLINE void hash_sha1_mode(void)
92 {
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);
97 }
98 
99 static INLINE void hash_sha256_mode(void)
100 {
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);
105 }
106 
107 static INLINE void hash_pad_block_enable(void)
108 {
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);
112 }
113 
114 static INLINE void hash_pad_block_disable(void)
115 {
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);
119 }
120 
121 static INLINE void hash_padding_generation_set(void)
122 {
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);
126 }
127 
128 static INLINE void hash_padding_generation_reset(void)
129 {
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);
133 }
134 
135 static INLINE void hash_len_low_word(uint32_t len0)
136 {
137  WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CUR_LEN_0, len0);
138 }
139 
140 static INLINE void hash_len_high_word(uint32_t len1)
141 {
142  WR_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_CUR_LEN_1, len1);
143 }
144 
145 static INLINE int hash_busy(void)
146 {
147  return (RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_INT_BUSY) & HASH_REG_OFFSET_HASH_INT_BUSY_MASK);
148 }
149 
150 static INLINE void hash_sw_reset(void)
151 {
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);
155 }
156 
157 static INLINE void hash_data_swap_enable(void)
158 {
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);
162 }
163 
164 static INLINE void hash_data_swap_disable(void)
165 {
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);
169 }
170 
171 #define HASH_REG_OFFSET_DATA_WR_PORT 0x00000900UL
172 
173 static INLINE void hash_write_data(uint32_t data)
174 {
175  while(hash_busy() == 0x1) ;
176  WR_WORD(SHA_BASE + HASH_REG_OFFSET_DATA_WR_PORT, data);
177 }
178 
179 static INLINE void hash_write_hx(int idx, uint32_t data)
180 {
181  WR_WORD((SHA_BASE + HASH_REG_OFFSET_HASH_H0 + (4 * idx)), (data & HASH_REG_OFFSET_HASH_HX_MASK));
182 }
183 
184 static INLINE uint32_t hash_read_hx(int idx)
185 {
186  return RD_WORD(SHA_BASE + HASH_REG_OFFSET_HASH_H0 + (4 * idx));
187 }
188 
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]));
192  }
193 }
194 
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
199 
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);
204 }
205 
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);
210 }
211 
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);
216 }
217 
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;
220 }
221 
223 typedef enum {
234  HASH_ERR_BUSY = -5,
236 
244 int hal_hash_open(hash_init_t *init);
245 
253 int hal_hash_close(void);
254 
264 int hal_hash_start(hash_type type);
265 
276 int hal_hash_process(uint8_t *data, uint32_t data_len);
277 
288 int hal_hash_finish(uint8_t *out, uint16_t len);
289 
290 
291 
293 
294 
295 
296 #endif
297 
298 
299 
300 
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.