Skip to content

Commit e0ba3c4

Browse files
committed
Adapt config to create big enough data zone on slot 8
Signed-off-by: cybnon <stefan.weber93@googlemail.com>
1 parent 92bf392 commit e0ba3c4

File tree

2 files changed

+191
-90
lines changed

2 files changed

+191
-90
lines changed

include/atecc608_handler.h

Lines changed: 161 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -1,80 +1,164 @@
1-
#include <iostream>
2-
#include <vector>
3-
#include <bitset>
4-
5-
6-
7-
// Exemple of configuration for ATECC608A. Look at page 8 on the datasheet for ATECC608A-TFLXTLS
8-
const uint8_t ECCX08_DEFAULT_CONFIGURATION_VALS[112] = {
9-
// Read only - end
10-
// I2C_Address
11-
0xC0,
12-
// Reserved
13-
0x00,
14-
// OTPmode
15-
0x55,
16-
// ChipMode
17-
0x00,
18-
// SlotConfig
19-
0x83, 0x60,
20-
0x87, 0x60,
21-
0x8F, 0x60,
22-
0x83, 0x60,
23-
0x83, 0x60,
24-
0x8F, 0x8F,
25-
0x9F, 0x8F,
26-
0xAF, 0x8F,
27-
0x00, 0x00,
28-
0x00, 0x00,
29-
0x00, 0x00,
30-
0x00, 0x00,
31-
0x00, 0x00,
32-
0x00, 0x00,
33-
0x00, 0x00,
34-
0xAF, 0x8F,
35-
// Counter[0]
36-
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
37-
// Counter[1]
38-
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
39-
// LastKeyUse
40-
0xFF, 0xFF, 0xFF, 0xFF,
41-
0xFF, 0xFF, 0xFF, 0xFF,
42-
0xFF, 0xFF, 0xFF, 0xFF,
43-
0xFF, 0xFF, 0xFF, 0xFF,
44-
// Write via commands only - start
45-
// UserExtra
46-
0x00,
47-
// Selector
48-
0x00,
49-
// LockValue
50-
0x55,
51-
// LockConfig
52-
0x55,
53-
// SlotLocked
54-
0xFF, 0xFF,
55-
// Write via commands only - end
56-
// RFU
57-
0x00, 0x00,
58-
// X509format
59-
0x00, 0x00, 0x00, 0x00,
60-
// KeyConfig
61-
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
62-
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
63-
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
64-
0x1C, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
65-
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
66-
0x1C, 0x00,
67-
0x1C, 0x00,
68-
0x1C, 0x00,
69-
0x3C, 0x00,
70-
0x3C, 0x00,
71-
0x3C, 0x00,
72-
0x3C, 0x00,
73-
0x3C, 0x00,
74-
0x3C, 0x00,
75-
0x3C, 0x00,
76-
0x1C, 0x00
77-
};
1+
#include <iostream>
2+
#include <vector>
3+
#include <bitset>
4+
5+
6+
// Exemple of configuration for ATECC608A. Look at page 8 on the datasheet for ATECC608A-TFLXTLS
7+
const uint8_t ECCX08_DEFAULT_CONFIGURATION_VALS[112] = {
8+
// Read only - end
9+
// I2C_Address
10+
0xC0,
11+
// Reserved
12+
0x00,
13+
// OTPmode
14+
0x55,
15+
// ChipMode
16+
0x00,
17+
// SlotConfig
18+
0x83, 0x60,
19+
0x87, 0x60,
20+
0x8F, 0x60,
21+
0x83, 0x60,
22+
0x83, 0x60,
23+
0x8F, 0x8F,
24+
0x9F, 0x8F,
25+
0xAF, 0x8F,
26+
0x00, 0x00,
27+
0x00, 0x00,
28+
0x00, 0x00,
29+
0x00, 0x00,
30+
0x00, 0x00,
31+
0x00, 0x00,
32+
0x00, 0x00,
33+
0xAF, 0x8F,
34+
// Counter[0]
35+
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
36+
// Counter[1]
37+
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
38+
// LastKeyUse
39+
0xFF, 0xFF, 0xFF, 0xFF,
40+
0xFF, 0xFF, 0xFF, 0xFF,
41+
0xFF, 0xFF, 0xFF, 0xFF,
42+
0xFF, 0xFF, 0xFF, 0xFF,
43+
// Write via commands only - start
44+
// UserExtra
45+
0x00,
46+
// Selector
47+
0x00,
48+
// LockValue
49+
0x55,
50+
// LockConfig
51+
0x55,
52+
// SlotLocked
53+
0xFF, 0xFF,
54+
// Write via commands only - end
55+
// RFU
56+
0x00, 0x00,
57+
// X509format
58+
0x00, 0x00, 0x00, 0x00,
59+
// KeyConfig
60+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
61+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
62+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
63+
0x1C, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
64+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
65+
0x1C, 0x00,
66+
0x1C, 0x00,
67+
0x1C, 0x00,
68+
0x3C, 0x00,
69+
0x3C, 0x00,
70+
0x3C, 0x00,
71+
0x3C, 0x00,
72+
0x3C, 0x00,
73+
0x3C, 0x00,
74+
0x3C, 0x00,
75+
0x1C, 0x00
76+
};
77+
78+
const uint8_t ENERGY_AGENT_ATEC_CONFIG[128] = {
79+
// Bytes 0-3: Device-specific settings (I2C_Address, Reserved, OTPmode, ChipMode)
80+
0xC0, 0x00, 0x55, 0x00,
81+
82+
// Bytes 4-35: SlotConfig for slots 0-15 (2 bytes per slot)
83+
// Slots 0-7: Unchanged SlotConfig settings
84+
0x83, 0x60, // Slot 0 (Bytes 4-5)
85+
0x87, 0x60, // Slot 1 (Bytes 6-7)
86+
0x8F, 0x60, // Slot 2 (Bytes 8-9)
87+
0x83, 0x60, // Slot 3 (Bytes 10-11)
88+
0x83, 0x60, // Slot 4 (Bytes 12-13)
89+
0x8F, 0x8F, // Slot 5 (Bytes 14-15)
90+
0x9F, 0x8F, // Slot 6 (Bytes 16-17)
91+
0xAF, 0x8F, // Slot 7 (Bytes 18-19)
92+
93+
// Slot 8: Configured for plaintext read/write
94+
0x00, 0x00, // Slot 8 (Bytes 20-21)
95+
96+
// Slots 9-15: Unchanged SlotConfig settings
97+
0x00, 0x00, // Slot 9 (Bytes 22-23)
98+
0x00, 0x00, // Slot 10 (Bytes 24-25)
99+
0x00, 0x00, // Slot 11 (Bytes 26-27)
100+
0x00, 0x00, // Slot 12 (Bytes 28-29)
101+
0x00, 0x00, // Slot 13 (Bytes 30-31)
102+
0x00, 0x00, // Slot 14 (Bytes 32-33)
103+
0xAF, 0x8F, // Slot 15 (Bytes 34-35)
104+
105+
// Bytes 36-43: Counter[0]
106+
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
107+
108+
// Bytes 44-51: Counter[1]
109+
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
110+
111+
// Bytes 52-67: LastKeyUse (16 bytes)
112+
0xFF, 0xFF, 0xFF, 0xFF, // Slots 0-3
113+
0xFF, 0xFF, 0xFF, 0xFF, // Slots 4-7
114+
0xFF, 0xFF, 0xFF, 0xFF, // Slots 8-11
115+
0xFF, 0xFF, 0xFF, 0xFF, // Slots 12-15
116+
117+
// Bytes 68-71: Write via commands only - start
118+
0x00, // UserExtra (Byte 68)
119+
0x00, // Selector (Byte 69)
120+
0x55, // LockValue (Byte 70)
121+
0x55, // LockConfig (Byte 71)
122+
123+
// Bytes 72-73: SlotLocked
124+
0xFF, 0xFF,
125+
126+
// Bytes 74-75: RFU
127+
0x00, 0x00,
128+
129+
// Bytes 76-79: X509format
130+
0x00, 0x00, 0x00, 0x00,
131+
132+
// Bytes 80-111: KeyConfig for slots 0-15 (2 bytes per slot)
133+
// Slots 0-6: Unchanged KeyConfig settings
134+
0x33, 0x00, // Slot 0 (Bytes 80-81)
135+
0x33, 0x00, // Slot 1 (Bytes 82-83)
136+
0x33, 0x00, // Slot 2 (Bytes 84-85)
137+
0x1C, 0x00, // Slot 3 (Bytes 86-87)
138+
0x33, 0x00, // Slot 4 (Bytes 88-89)
139+
0x1C, 0x00, // Slot 5 (Bytes 90-91)
140+
0x1C, 0x00, // Slot 6 (Bytes 92-93)
141+
0x1C, 0x00, // Slot 7 (Bytes 94-95)
142+
143+
// Slot 8: KeyConfig adjusted for plaintext read/write
144+
0x00, 0x00, // Slot 8 (Bytes 96-97)
145+
146+
// Slots 9-15: Unchanged KeyConfig settings
147+
0x3C, 0x00, // Slot 9 (Bytes 98-99)
148+
0x3C, 0x00, // Slot 10 (Bytes 100-101)
149+
0x3C, 0x00, // Slot 11 (Bytes 102-103)
150+
0x3C, 0x00, // Slot 12 (Bytes 104-105)
151+
0x3C, 0x00, // Slot 13 (Bytes 106-107)
152+
0x3C, 0x00, // Slot 14 (Bytes 108-109)
153+
0x1C, 0x00, // Slot 15 (Bytes 110-111)
154+
155+
// Bytes 112-127: Remaining bytes (RFU or padding)
156+
0x00, 0x00, 0x00, 0x00, // Bytes 112-115
157+
0x00, 0x00, 0x00, 0x00, // Bytes 116-119
158+
0x00, 0x00, 0x00, 0x00, // Bytes 120-123
159+
0x00, 0x00, 0x00, 0x00 // Bytes 124-127
160+
};
161+
78162

79163
extern "C" int atecc_handler_write_configuration(const uint8_t *config, size_t len);
80164
extern "C" int atecc_handle_write_default_configuration();

src/atecc608_handler.cpp

Lines changed: 30 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ int atecc_handle_write_default_configuration(){
3838
status = atcab_init(&cfg);
3939
if (status == ATCA_SUCCESS)
4040
{
41-
status = atecc_handler_write_configuration(ECCX08_DEFAULT_CONFIGURATION_VALS, sizeof(ECCX08_DEFAULT_CONFIGURATION_VALS));
41+
status = atecc_handler_write_configuration(ENERGY_AGENT_ATEC_CONFIG, sizeof(ENERGY_AGENT_ATEC_CONFIG));
4242
return status;
4343
}
4444
return status;
@@ -215,39 +215,56 @@ int atecc_handler_inject_priv_key(int slot, uint8_t* priv_key){
215215
* \return ATCA_SUCCESS on success, otherwise an error code.
216216
*/
217217
int atecc_handler_write_data(int slot, uint8_t* data, size_t data_len) {
218-
if (!data || data_len == 0) {
218+
if (!data || data_len == 0 || data_len % 4 != 0) {
219219
return ATCA_BAD_PARAM;
220220
}
221221

222-
ATCA_STATUS status = atcab_wakeup();
222+
size_t slot_size;
223+
ATCA_STATUS status = atcab_get_zone_size(ATCA_ZONE_DATA, slot, &slot_size);
223224
if (status != ATCA_SUCCESS) {
224225
return status;
225226
}
226227

227-
// Calculate the number of blocks and any remaining bytes
228-
size_t num_blocks = data_len / 32;
229-
size_t remaining_bytes = data_len % 32;
228+
if (data_len > slot_size) {
229+
return ATCA_BAD_PARAM; // Data length exceeds slot size
230+
}
230231

231-
// Write full blocks
232-
for (size_t i = 0; i < num_blocks; i++) {
233-
status = atcab_write_zone(ATCA_ZONE_DATA, slot, i, 0, &data[i * 32], 32);
232+
status = atcab_wakeup();
233+
if (status != ATCA_SUCCESS) {
234+
return status;
235+
}
236+
237+
size_t bytes_written = 0;
238+
size_t remaining_bytes = data_len;
239+
240+
// Write full 32-byte blocks
241+
while (remaining_bytes >= 32) {
242+
size_t block = bytes_written / 32;
243+
status = atcab_write_zone(ATCA_ZONE_DATA, slot, block, 0, &data[bytes_written], 32);
234244
if (status != ATCA_SUCCESS) {
235245
return status;
236246
}
247+
bytes_written += 32;
248+
remaining_bytes -= 32;
237249
}
238250

239-
// Write remaining bytes if any
240-
if (remaining_bytes > 0) {
241-
status = atcab_write_zone(ATCA_ZONE_DATA, slot, num_blocks, 0, &data[num_blocks * 32], remaining_bytes);
251+
// Write remaining 4-byte words
252+
while (remaining_bytes >= 4) {
253+
size_t block = bytes_written / 32;
254+
size_t offset = (bytes_written % 32) / 4;
255+
status = atcab_write_zone(ATCA_ZONE_DATA, slot, block, offset, &data[bytes_written], 4);
242256
if (status != ATCA_SUCCESS) {
243257
return status;
244258
}
259+
bytes_written += 4;
260+
remaining_bytes -= 4;
245261
}
246262

247263
return ATCA_SUCCESS;
248264
}
249265

250266

267+
251268
/* read data from slot
252269
* \param[in] slot slot number from which data is to be read
253270
* \param[out] data buffer to store the read data
@@ -280,7 +297,7 @@ int atecc_handler_read_data(int slot, uint8_t* data, size_t data_len) {
280297

281298
// Read full 32-byte blocks
282299
for (size_t i = 0; i < num_blocks; i++) {
283-
status = atcab_read_zone(ATCA_ZONE_DATA, slot, i, 0, &data[bytes_read], 32);
300+
status = atcab_read_zone(ATCA_ZONE_DATA, slot, i, 0, &data[bytes_read], 32);
284301
if (status != ATCA_SUCCESS) {
285302
return status;
286303
}

0 commit comments

Comments
 (0)