3 #ifndef sha204_library_H
4 #define sha204_library_H
8 #define PORT_ACCESS_TIME (630)
9 #define START_PULSE_WIDTH (4340)
11 #define RX_TX_DELAY (15)
12 #define START_PULSE_TIME_OUT (255)
13 #define ZERO_PULSE_TIME_OUT (26)
17 #define SWI_FUNCTION_RETCODE_SUCCESS ((uint8_t) 0x00)
18 #define SWI_FUNCTION_RETCODE_TIMEOUT ((uint8_t) 0xF1)
19 #define SWI_FUNCTION_RETCODE_RX_FAIL ((uint8_t) 0xF9)
23 #define SHA204_RSP_SIZE_MIN ((uint8_t) 4)
24 #define SHA204_RSP_SIZE_MAX ((uint8_t) 35)
25 #define SHA204_BUFFER_POS_COUNT (0)
26 #define SHA204_BUFFER_POS_DATA (1)
27 #define SHA204_WAKEUP_PULSE_WIDTH (uint8_t) (6.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)
28 #define SHA204_WAKEUP_DELAY (uint8_t) (3.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5)
31 #define SHA204_SWI_FLAG_CMD ((uint8_t) 0x77)
32 #define SHA204_SWI_FLAG_TX ((uint8_t) 0x88)
33 #define SHA204_SWI_FLAG_IDLE ((uint8_t) 0xBB)
34 #define SHA204_SWI_FLAG_SLEEP ((uint8_t) 0xCC)
38 #define SHA204_CHECKMAC ((uint8_t) 0x28)
39 #define SHA204_DERIVE_KEY ((uint8_t) 0x1C)
40 #define SHA204_DEVREV ((uint8_t) 0x30)
41 #define SHA204_GENDIG ((uint8_t) 0x15)
42 #define SHA204_HMAC ((uint8_t) 0x11)
43 #define SHA204_LOCK ((uint8_t) 0x17)
44 #define SHA204_MAC ((uint8_t) 0x08)
45 #define SHA204_NONCE ((uint8_t) 0x16)
46 #define SHA204_PAUSE ((uint8_t) 0x01)
47 #define SHA204_RANDOM ((uint8_t) 0x1B)
48 #define SHA204_READ ((uint8_t) 0x02)
49 #define SHA204_UPDATE_EXTRA ((uint8_t) 0x20)
50 #define SHA204_WRITE ((uint8_t) 0x12)
53 #define SHA204_RSP_SIZE_VAL ((uint8_t) 7)
56 #define SHA204_KEY_ID_MAX ((uint8_t) 15)
57 #define SHA204_OTP_BLOCK_MAX ((uint8_t) 1)
60 #define SHA204_COUNT_IDX ( 0)
61 #define SHA204_OPCODE_IDX ( 1)
62 #define SHA204_PARAM1_IDX ( 2)
63 #define SHA204_PARAM2_IDX ( 3)
64 #define SHA204_DATA_IDX ( 5)
67 #define SHA204_ZONE_CONFIG ((uint8_t) 0x00)
68 #define SHA204_ZONE_OTP ((uint8_t) 0x01)
69 #define SHA204_ZONE_DATA ((uint8_t) 0x02)
70 #define SHA204_ZONE_MASK ((uint8_t) 0x03)
71 #define SHA204_ZONE_COUNT_FLAG ((uint8_t) 0x80)
72 #define SHA204_ZONE_ACCESS_4 ((uint8_t) 4)
73 #define SHA204_ZONE_ACCESS_32 ((uint8_t) 32)
74 #define SHA204_ADDRESS_MASK_CONFIG ( 0x001F)
75 #define SHA204_ADDRESS_MASK_OTP ( 0x000F)
76 #define SHA204_ADDRESS_MASK ( 0x007F)
79 #define CHECKMAC_MODE_IDX SHA204_PARAM1_IDX
80 #define CHECKMAC_KEYID_IDX SHA204_PARAM2_IDX
81 #define CHECKMAC_CLIENT_CHALLENGE_IDX SHA204_DATA_IDX
82 #define CHECKMAC_CLIENT_RESPONSE_IDX (37)
83 #define CHECKMAC_DATA_IDX (69)
84 #define CHECKMAC_COUNT (84)
85 #define CHECKMAC_MODE_MASK ((uint8_t) 0x27)
86 #define CHECKMAC_CLIENT_CHALLENGE_SIZE (32)
87 #define CHECKMAC_CLIENT_RESPONSE_SIZE (32)
88 #define CHECKMAC_OTHER_DATA_SIZE (13)
91 #define DERIVE_KEY_RANDOM_IDX SHA204_PARAM1_IDX
92 #define DERIVE_KEY_TARGETKEY_IDX SHA204_PARAM2_IDX
93 #define DERIVE_KEY_MAC_IDX SHA204_DATA_IDX
94 #define DERIVE_KEY_COUNT_SMALL SHA204_CMD_SIZE_MIN
95 #define DERIVE_KEY_COUNT_LARGE (39)
96 #define DERIVE_KEY_RANDOM_FLAG ((uint8_t) 4)
97 #define DERIVE_KEY_MAC_SIZE (32)
100 #define DEVREV_PARAM1_IDX SHA204_PARAM1_IDX
101 #define DEVREV_PARAM2_IDX SHA204_PARAM2_IDX
102 #define DEVREV_COUNT SHA204_CMD_SIZE_MIN
105 #define GENDIG_ZONE_IDX SHA204_PARAM1_IDX
106 #define GENDIG_KEYID_IDX SHA204_PARAM2_IDX
107 #define GENDIG_DATA_IDX SHA204_DATA_IDX
108 #define GENDIG_COUNT SHA204_CMD_SIZE_MIN
109 #define GENDIG_COUNT_DATA (11)
110 #define GENDIG_OTHER_DATA_SIZE (4)
111 #define GENDIG_ZONE_CONFIG ((uint8_t) 0)
112 #define GENDIG_ZONE_OTP ((uint8_t) 1)
113 #define GENDIG_ZONE_DATA ((uint8_t) 2)
116 #define HMAC_MODE_IDX SHA204_PARAM1_IDX
117 #define HMAC_KEYID_IDX SHA204_PARAM2_IDX
118 #define HMAC_COUNT SHA204_CMD_SIZE_MIN
119 #define HMAC_MODE_MASK ((uint8_t) 0x74)
122 #define LOCK_ZONE_IDX SHA204_PARAM1_IDX
123 #define LOCK_SUMMARY_IDX SHA204_PARAM2_IDX
124 #define LOCK_COUNT SHA204_CMD_SIZE_MIN
125 #define LOCK_ZONE_NO_CONFIG ((uint8_t) 0x01)
126 #define LOCK_ZONE_NO_CRC ((uint8_t) 0x80)
127 #define LOCK_ZONE_MASK (0x81)
130 #define MAC_MODE_IDX SHA204_PARAM1_IDX
131 #define MAC_KEYID_IDX SHA204_PARAM2_IDX
132 #define MAC_CHALLENGE_IDX SHA204_DATA_IDX
133 #define MAC_COUNT_SHORT SHA204_CMD_SIZE_MIN
134 #define MAC_COUNT_LONG (39)
135 #define MAC_MODE_BLOCK2_TEMPKEY ((uint8_t) 0x01)
136 #define MAC_MODE_BLOCK1_TEMPKEY ((uint8_t) 0x02)
137 #define MAC_MODE_SOURCE_FLAG_MATCH ((uint8_t) 0x04)
138 #define MAC_MODE_PASSTHROUGH ((uint8_t) 0x07)
139 #define MAC_MODE_INCLUDE_OTP_88 ((uint8_t) 0x10)
140 #define MAC_MODE_INCLUDE_OTP_64 ((uint8_t) 0x20)
141 #define MAC_MODE_INCLUDE_SN ((uint8_t) 0x40)
142 #define MAC_CHALLENGE_SIZE (32)
143 #define MAC_MODE_MASK ((uint8_t) 0x77)
146 #define NONCE_MODE_IDX SHA204_PARAM1_IDX
147 #define NONCE_PARAM2_IDX SHA204_PARAM2_IDX
148 #define NONCE_INPUT_IDX SHA204_DATA_IDX
149 #define NONCE_COUNT_SHORT (27)
150 #define NONCE_COUNT_LONG (39)
151 #define NONCE_MODE_MASK ((uint8_t) 3)
152 #define NONCE_MODE_SEED_UPDATE ((uint8_t) 0x00)
153 #define NONCE_MODE_NO_SEED_UPDATE ((uint8_t) 0x01)
154 #define NONCE_MODE_INVALID ((uint8_t) 0x02)
155 #define NONCE_MODE_PASSTHROUGH ((uint8_t) 0x03)
156 #define NONCE_NUMIN_SIZE (20)
157 #define NONCE_NUMIN_SIZE_PASSTHROUGH (32)
160 #define PAUSE_SELECT_IDX SHA204_PARAM1_IDX
161 #define PAUSE_PARAM2_IDX SHA204_PARAM2_IDX
162 #define PAUSE_COUNT SHA204_CMD_SIZE_MIN
165 #define RANDOM_MODE_IDX SHA204_PARAM1_IDX
166 #define RANDOM_PARAM2_IDX SHA204_PARAM2_IDX
167 #define RANDOM_COUNT SHA204_CMD_SIZE_MIN
168 #define RANDOM_SEED_UPDATE ((uint8_t) 0x00)
169 #define RANDOM_NO_SEED_UPDATE ((uint8_t) 0x01)
172 #define READ_ZONE_IDX SHA204_PARAM1_IDX
173 #define READ_ADDR_IDX SHA204_PARAM2_IDX
174 #define READ_COUNT SHA204_CMD_SIZE_MIN
175 #define READ_ZONE_MASK ((uint8_t) 0x83)
176 #define READ_ZONE_MODE_32_BYTES ((uint8_t) 0x80)
179 #define UPDATE_MODE_IDX SHA204_PARAM1_IDX
180 #define UPDATE_VALUE_IDX SHA204_PARAM2_IDX
181 #define UPDATE_COUNT SHA204_CMD_SIZE_MIN
182 #define UPDATE_CONFIG_BYTE_86 ((uint8_t) 0x01)
185 #define WRITE_ZONE_IDX SHA204_PARAM1_IDX
186 #define WRITE_ADDR_IDX SHA204_PARAM2_IDX
187 #define WRITE_VALUE_IDX SHA204_DATA_IDX
188 #define WRITE_MAC_VS_IDX ( 9)
189 #define WRITE_MAC_VL_IDX (37)
190 #define WRITE_COUNT_SHORT (11)
191 #define WRITE_COUNT_LONG (39)
192 #define WRITE_COUNT_SHORT_MAC (43)
193 #define WRITE_COUNT_LONG_MAC (71)
194 #define WRITE_MAC_SIZE (32)
195 #define WRITE_ZONE_MASK ((uint8_t) 0xC3)
196 #define WRITE_ZONE_WITH_MAC ((uint8_t) 0x40)
199 #define CHECKMAC_RSP_SIZE SHA204_RSP_SIZE_MIN
200 #define DERIVE_KEY_RSP_SIZE SHA204_RSP_SIZE_MIN
201 #define DEVREV_RSP_SIZE SHA204_RSP_SIZE_VAL
202 #define GENDIG_RSP_SIZE SHA204_RSP_SIZE_MIN
203 #define HMAC_RSP_SIZE SHA204_RSP_SIZE_MAX
204 #define LOCK_RSP_SIZE SHA204_RSP_SIZE_MIN
205 #define MAC_RSP_SIZE SHA204_RSP_SIZE_MAX
206 #define NONCE_RSP_SIZE_SHORT SHA204_RSP_SIZE_MIN
207 #define NONCE_RSP_SIZE_LONG SHA204_RSP_SIZE_MAX
208 #define PAUSE_RSP_SIZE SHA204_RSP_SIZE_MIN
209 #define RANDOM_RSP_SIZE SHA204_RSP_SIZE_MAX
210 #define READ_4_RSP_SIZE SHA204_RSP_SIZE_VAL
211 #define READ_32_RSP_SIZE SHA204_RSP_SIZE_MAX
212 #define TEMP_SENSE_RSP_SIZE SHA204_RSP_SIZE_VAL
213 #define UPDATE_RSP_SIZE SHA204_RSP_SIZE_MIN
214 #define WRITE_RSP_SIZE SHA204_RSP_SIZE_MIN
217 #define CHECKMAC_DELAY ((uint8_t) (12.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
218 #define DERIVE_KEY_DELAY ((uint8_t) (14.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
219 #define DEVREV_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
220 #define GENDIG_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
221 #define HMAC_DELAY ((uint8_t) (27.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
222 #define LOCK_DELAY ((uint8_t) ( 5.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
223 #define MAC_DELAY ((uint8_t) (12.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
224 #define NONCE_DELAY ((uint8_t) (22.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
225 #define PAUSE_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
226 #define RANDOM_DELAY ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
227 #define READ_DELAY ((uint8_t) ( 0.4 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
228 #define TEMP_SENSE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
229 #define UPDATE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
230 #define WRITE_DELAY ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_NEGATIVE - 0.5))
233 #define CHECKMAC_EXEC_MAX ((uint8_t) (38.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
234 #define DERIVE_KEY_EXEC_MAX ((uint8_t) (62.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
235 #define DEVREV_EXEC_MAX ((uint8_t) ( 2.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
236 #define GENDIG_EXEC_MAX ((uint8_t) (43.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
237 #define HMAC_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
238 #define LOCK_EXEC_MAX ((uint8_t) (24.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
239 #define MAC_EXEC_MAX ((uint8_t) (35.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
240 #define NONCE_EXEC_MAX ((uint8_t) (60.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
241 #define PAUSE_EXEC_MAX ((uint8_t) ( 2.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
242 #define RANDOM_EXEC_MAX ((uint8_t) (50.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
243 #define READ_EXEC_MAX ((uint8_t) ( 4.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
244 #define TEMP_SENSE_EXEC_MAX ((uint8_t) (11.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
245 #define UPDATE_EXEC_MAX ((uint8_t) ( 6.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
246 #define WRITE_EXEC_MAX ((uint8_t) (42.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
250 #define CPU_CLOCK_DEVIATION_POSITIVE (1.01)
251 #define CPU_CLOCK_DEVIATION_NEGATIVE (0.99)
252 #define SHA204_RETRY_COUNT (1)
253 #define SWI_RECEIVE_TIME_OUT ((uint16_t) 163)
254 #define SWI_US_PER_BYTE ((uint16_t) 313)
255 #define SHA204_SYNC_TIMEOUT ((uint8_t) 85)
256 #define SHA204_RESPONSE_TIMEOUT ((uint16_t) SWI_RECEIVE_TIME_OUT + SWI_US_PER_BYTE)
260 #define SHA204_COMMAND_EXEC_MAX ((uint8_t) (69.0 * CPU_CLOCK_DEVIATION_POSITIVE + 0.5))
261 #define SHA204_CMD_SIZE_MIN ((uint8_t) 7)
262 #ifndef SHA204_CMD_SIZE_MAX
263 #define SHA204_CMD_SIZE_MAX ((uint8_t) 84)
265 #define SHA204_CRC_SIZE ((uint8_t) 2)
266 #define SHA204_BUFFER_POS_STATUS (1)
267 #define SHA204_BUFFER_POS_DATA (1)
268 #define SHA204_STATUS_BYTE_WAKEUP ((uint8_t) 0x11)
269 #define SHA204_STATUS_BYTE_PARSE ((uint8_t) 0x03)
270 #define SHA204_STATUS_BYTE_EXEC ((uint8_t) 0x0F)
271 #define SHA204_STATUS_BYTE_COMM ((uint8_t) 0xFF)
275 #define ADDRESS_SN03 0 // SN[0:3] are bytes 0->3 of configuration zone
276 #define ADDRESS_RevNum 4 // bytes 4->7 of config zone are RevNum
277 #define ADDRESS_SN47 8 // SN[4:7] are bytes 8->11 of config zone
278 #define ADDRESS_SN8 12 // SN[8] is byte 12 of config zone, should be 0xEE
279 #define ADDRESS_I2CEN 14 // I2C Enable, bit 0 represents I2C enable status
280 #define ADDRESS_I2CADD 16 // Defines I2C address of SHA204
281 #define ADDRESS_OTPMODE 18 // Sets the One-time-programmable mode
282 #define ADDRESS_SELECTOR 19 // Controls writability of Selector
286 #if !defined(ARDUINO_ARCH_AVR) // For everything else than AVR use pinMode / digitalWrite
287 #define SHA204_SET_OUTPUT() pinMode(device_pin, OUTPUT)
288 #define SHA204_SET_INPUT() pinMode(device_pin, INPUT)
289 #define SHA204_POUT_HIGH() digitalWrite(device_pin, HIGH)
290 #define SHA204_POUT_LOW() digitalWrite(device_pin, LOW)
291 #define SHA204_PIN_READ() digitalRead(device_pin)
293 #define SHA204_SET_INPUT() *device_port_DDR &= ~device_pin
294 #define SHA204_SET_OUTPUT() *device_port_DDR |= device_pin
295 #define SHA204_POUT_HIGH() *device_port_OUT |= device_pin
296 #define SHA204_POUT_LOW() *device_port_OUT &= ~device_pin
297 #define SHA204_PIN_READ() (*device_port_IN & device_pin)
307 #ifdef ARDUINO_ARCH_AVR
308 volatile uint8_t *device_port_DDR, *device_port_OUT, *device_port_IN;
310 void sha204c_calculate_crc(uint8_t length, uint8_t *
data, uint8_t *crc);
311 uint8_t sha204c_check_crc(uint8_t *response);
312 void swi_set_signal_pin(uint8_t is_high);
313 uint8_t swi_receive_bytes(uint8_t count, uint8_t *buffer);
314 uint8_t swi_send_bytes(uint8_t count, uint8_t *buffer);
315 uint8_t swi_send_byte(uint8_t value);
316 uint8_t sha204p_receive_response(uint8_t size, uint8_t *response);
317 uint8_t sha204p_wakeup();
318 uint8_t sha204p_send_command(uint8_t count, uint8_t * command);
319 uint8_t sha204p_sleep();
320 uint8_t sha204p_resync(uint8_t size, uint8_t *response);
352 uint8_t execution_delay, uint8_t execution_timeout);
373 uint8_t
sha204m_random(uint8_t * tx_buffer, uint8_t * rx_buffer, uint8_t mode);
395 uint8_t
sha204m_read(uint8_t *tx_buffer, uint8_t *rx_buffer, uint8_t zone, uint16_t address);
416 uint8_t
sha204m_execute(uint8_t op_code, uint8_t param1, uint16_t param2,
417 uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3,
419 uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);
441 uint8_t datalen1, uint8_t *data1, uint8_t datalen2, uint8_t *data2, uint8_t datalen3,
443 uint8_t tx_size, uint8_t *tx_buffer, uint8_t rx_size, uint8_t *rx_buffer);