Skip to content

Commit 3210c44

Browse files
committed
Add ECC508 for reading/writing config, lock status, locking and serial number
1 parent 9c21762 commit 3210c44

File tree

2 files changed

+194
-0
lines changed

2 files changed

+194
-0
lines changed

src/utility/ECC508.cpp

Lines changed: 183 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,37 @@ void ECC508Class::end()
2929
_wire->end();
3030
}
3131

32+
String ECC508Class::serialNumber()
33+
{
34+
String result = (char*)NULL;
35+
byte sn[12];
36+
37+
if (!read(0, 0, &sn[0], 4)) {
38+
return result;
39+
}
40+
41+
if (!read(0, 2, &sn[4], 4)) {
42+
return result;
43+
}
44+
45+
if (!read(0, 3, &sn[8], 4)) {
46+
return result;
47+
}
48+
49+
result.reserve(18);
50+
51+
for (int i = 0; i < 8; i++) {
52+
byte b = sn[i];
53+
54+
if (b < 16) {
55+
result += "0";
56+
}
57+
result += String(b, HEX);
58+
}
59+
60+
return result;
61+
}
62+
3263
int ECC508Class::random(byte data[], size_t length)
3364
{
3465
if (!wakeup()) {
@@ -140,6 +171,64 @@ int ECC508Class::ecSign(int slot, const byte message[], byte signature[])
140171
return 1;
141172
}
142173

174+
int ECC508Class::locked()
175+
{
176+
byte config[4];
177+
178+
if (!read(0, 0x15, config, sizeof(config))) {
179+
return 0;
180+
}
181+
182+
if (config[2] == 0x00 && config[3] == 0x00) {
183+
return 1; // locked
184+
}
185+
186+
return 0;
187+
}
188+
189+
int ECC508Class::writeConfiguration(const byte data[])
190+
{
191+
// skip first 16 bytes, they are not writable
192+
for (int i = 16; i < 128; i += 4) {
193+
if (i == 84) {
194+
// not writable
195+
continue;
196+
}
197+
198+
if (!write(0, i / 4, &data[i], 4)) {
199+
return 0;
200+
}
201+
}
202+
203+
return 1;
204+
}
205+
206+
int ECC508Class::readConfiguration(byte data[])
207+
{
208+
for (int i = 0; i < 128; i += 32) {
209+
if (!read(0, i / 4, &data[i], 32)) {
210+
return 0;
211+
}
212+
}
213+
214+
return 1;
215+
}
216+
217+
int ECC508Class::lock()
218+
{
219+
// lock config
220+
if (!lock(0)) {
221+
return 0;
222+
}
223+
224+
// lock data and OTP
225+
if (!lock(1)) {
226+
return 0;
227+
}
228+
229+
return 1;
230+
}
231+
143232
int ECC508Class::wakeup()
144233
{
145234
_wire->beginTransmission(0x00);
@@ -288,6 +377,100 @@ int ECC508Class::sign(int slot, byte signature[])
288377
return 1;
289378
}
290379

380+
int ECC508Class::read(int zone, int address, byte buffer[], int length)
381+
{
382+
if (!wakeup()) {
383+
return 0;
384+
}
385+
386+
if (length != 4 && length != 32) {
387+
return 0;
388+
}
389+
390+
if (length == 32) {
391+
zone |= 0x80;
392+
}
393+
394+
if (!sendCommand(0x02, zone, address)) {
395+
return 0;
396+
}
397+
398+
delay(1);
399+
400+
if (!receiveResponse(buffer, length)) {
401+
return 0;
402+
}
403+
404+
delay(1);
405+
idle();
406+
407+
return length;
408+
}
409+
410+
int ECC508Class::write(int zone, int address, const byte buffer[], int length)
411+
{
412+
uint8_t status;
413+
414+
if (!wakeup()) {
415+
return 0;
416+
}
417+
418+
if (length != 4 && length != 32) {
419+
return 0;
420+
}
421+
422+
if (length == 32) {
423+
zone |= 0x80;
424+
}
425+
426+
if (!sendCommand(0x12, zone, address, buffer, length)) {
427+
return 0;
428+
}
429+
430+
delay(26);
431+
432+
if (!receiveResponse(&status, sizeof(status))) {
433+
return 0;
434+
}
435+
436+
delay(1);
437+
idle();
438+
439+
if (status != 0) {
440+
return 0;
441+
}
442+
443+
return 1;
444+
}
445+
446+
int ECC508Class::lock(int zone)
447+
{
448+
uint8_t status;
449+
450+
if (!wakeup()) {
451+
return 0;
452+
}
453+
454+
if (!sendCommand(0x17, 0x80 | zone, 0x0000)) {
455+
return 0;
456+
}
457+
458+
delay(32);
459+
460+
if (!receiveResponse(&status, sizeof(status))) {
461+
return 0;
462+
}
463+
464+
delay(1);
465+
idle();
466+
467+
if (status != 0) {
468+
return 0;
469+
}
470+
471+
return 1;
472+
}
473+
291474
int ECC508Class::sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[], size_t dataLength)
292475
{
293476
byte command[8 + dataLength]; // 1 for type, 1 for length, 1 for opcode, 1 for param1, 2 for param2, 2 for crc

src/utility/ECC508.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,8 @@ class ECC508Class
1313
int begin();
1414
void end();
1515

16+
String serialNumber();
17+
1618
int random(byte data[], size_t length);
1719

1820
int generatePrivateKey(int slot, byte publicKey[]);
@@ -21,6 +23,11 @@ class ECC508Class
2123
int ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[]);
2224
int ecSign(int slot, const byte message[], byte signature[]);
2325

26+
int locked();
27+
int writeConfiguration(const byte data[]);
28+
int readConfiguration(byte data[]);
29+
int lock();
30+
2431
private:
2532
int wakeup();
2633
int sleep();
@@ -31,6 +38,10 @@ class ECC508Class
3138
int verify(const byte signature[], const byte pubkey[]);
3239
int sign(int slot, byte signature[]);
3340

41+
int read(int zone, int address, byte buffer[], int length);
42+
int write(int zone, int address, const byte buffer[], int length);
43+
int lock(int zone);
44+
3445
int sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[] = NULL, size_t dataLength = 0);
3546
int receiveResponse(void* response, size_t length);
3647
uint16_t crc16(const byte data[], size_t length);

0 commit comments

Comments
 (0)