-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathECCX08.cpp
969 lines (715 loc) · 14.5 KB
/
ECCX08.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
/*
This file is part of the ArduinoECCX08 library.
Copyright (c) 2018 Arduino SA. All rights reserved.
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
#include <Arduino.h>
#include "ECCX08.h"
const uint32_t ECCX08Class::_wakeupFrequency = 100000u; // 100 kHz
#ifdef __AVR__
const uint32_t ECCX08Class::_normalFrequency = 400000u; // 400 kHz
#else
const uint32_t ECCX08Class::_normalFrequency = 1000000u; // 1 MHz
#endif
ECCX08Class::ECCX08Class(TwoWire& wire, uint8_t address) :
_wire(&wire),
_address(address)
{
}
ECCX08Class::~ECCX08Class()
{
}
int ECCX08Class::begin(uint8_t i2cAddress)
{
_address = i2cAddress;
return begin();
}
int ECCX08Class::begin()
{
_wire->begin();
wakeup();
idle();
long ver = version() & 0x0F00000;
if (ver != 0x0500000 && ver != 0x0600000) {
return 0;
}
return 1;
}
void ECCX08Class::end()
{
// First wake up the device otherwise the chip didn't react to a sleep command
wakeup();
sleep();
#ifdef WIRE_HAS_END
_wire->end();
#endif
}
int ECCX08Class::serialNumber(byte sn[])
{
if (!read(0, 0, &sn[0], 4)) {
return 0;
}
if (!read(0, 2, &sn[4], 4)) {
return 0;
}
if (!read(0, 3, &sn[8], 4)) {
return 0;
}
return 1;
}
String ECCX08Class::serialNumber()
{
String result = (char*)NULL;
byte sn[12];
if (!serialNumber(sn)) {
return result;
}
result.reserve(18);
for (int i = 0; i < 9; i++) {
byte b = sn[i];
if (b < 16) {
result += "0";
}
result += String(b, HEX);
}
result.toUpperCase();
return result;
}
long ECCX08Class::random(long max)
{
return random(0, max);
}
long ECCX08Class::random(long min, long max)
{
if (min >= max)
{
return min;
}
long diff = max - min;
long r;
random((byte*)&r, sizeof(r));
if (r < 0) {
r = -r;
}
r = (r % diff);
return (r + min);
}
int ECCX08Class::random(byte data[], size_t length)
{
if (!wakeup()) {
return 0;
}
while (length) {
if (!sendCommand(0x1b, 0x00, 0x0000)) {
return 0;
}
delay(23);
byte response[32];
if (!receiveResponse(response, sizeof(response))) {
return 0;
}
int copyLength = min(32, (int)length);
memcpy(data, response, copyLength);
length -= copyLength;
data += copyLength;
}
delay(1);
idle();
return 1;
}
int ECCX08Class::generatePrivateKey(int slot, byte publicKey[])
{
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x40, 0x04, slot)) {
return 0;
}
delay(115);
if (!receiveResponse(publicKey, 64)) {
return 0;
}
delay(1);
idle();
return 1;
}
int ECCX08Class::generatePublicKey(int slot, byte publicKey[])
{
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x40, 0x00, slot)) {
return 0;
}
delay(115);
if (!receiveResponse(publicKey, 64)) {
return 0;
}
delay(1);
idle();
return 1;
}
int ECCX08Class::ecdsaVerify(const byte message[], const byte signature[], const byte pubkey[])
{
if (!challenge(message)) {
return 0;
}
if (!verify(signature, pubkey)) {
return 0;
}
return 1;
}
int ECCX08Class::ecSign(int slot, const byte message[], byte signature[])
{
byte rand[32];
if (!random(rand, sizeof(rand))) {
return 0;
}
if (!challenge(message)) {
return 0;
}
if (!sign(slot, signature)) {
return 0;
}
return 1;
}
int ECCX08Class::beginSHA256()
{
uint8_t status;
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x47, 0x00, 0x0000)) {
return 0;
}
delay(9);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::updateSHA256(const byte data[])
{
uint8_t status;
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x47, 0x01, 64, data, 64)) {
return 0;
}
delay(9);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::endSHA256(byte result[])
{
return endSHA256(NULL, 0, result);
}
int ECCX08Class::endSHA256(const byte data[], int length, byte result[])
{
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x47, 0x02, length, data, length)) {
return 0;
}
delay(9);
if (!receiveResponse(result, 32)) {
return 0;
}
delay(1);
idle();
return 1;
}
int ECCX08Class::readSlot(int slot, byte data[], int length)
{
if (slot < 0 || slot > 15) {
return -1;
}
if (length % 4 != 0) {
return 0;
}
int chunkSize = 32;
for (int i = 0; i < length; i += chunkSize) {
if ((length - i) < 32) {
chunkSize = 4;
}
if (!read(2, addressForSlotOffset(slot, i), &data[i], chunkSize)) {
return 0;
}
}
return 1;
}
int ECCX08Class::writeSlot(int slot, const byte data[], int length)
{
if (slot < 0 || slot > 15) {
return -1;
}
if (length % 4 != 0) {
return 0;
}
int chunkSize = 32;
for (int i = 0; i < length; i += chunkSize) {
if ((length - i) < 32) {
chunkSize = 4;
}
if (!write(2, addressForSlotOffset(slot, i), &data[i], chunkSize)) {
return 0;
}
}
return 1;
}
int ECCX08Class::locked()
{
byte config[4];
if (!read(0, 0x15, config, sizeof(config))) {
return 0;
}
if (config[2] == 0x00 && config[3] == 0x00) {
return 1; // locked
}
return 0;
}
int ECCX08Class::writeConfiguration(const byte data[])
{
// skip first 16 bytes, they are not writable
for (int i = 16; i < 128; i += 4) {
if (i == 84) {
// not writable
continue;
}
if (!write(0, i / 4, &data[i], 4)) {
return 0;
}
}
return 1;
}
int ECCX08Class::readConfiguration(byte data[])
{
for (int i = 0; i < 128; i += 32) {
if (!read(0, i / 4, &data[i], 32)) {
return 0;
}
}
return 1;
}
int ECCX08Class::lock()
{
// lock config
if (!lock(0)) {
return 0;
}
// lock data and OTP
if (!lock(1)) {
return 0;
}
return 1;
}
int ECCX08Class::beginHMAC(uint16_t keySlot)
{
// HMAC implementation is only for ATECC608
uint8_t status;
long ecc608ver = 0x0600000;
long eccCurrVer = version() & 0x0F00000;
if (eccCurrVer != ecc608ver) {
return 0;
}
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x47, 0x04, keySlot)) {
return 0;
}
delay(9);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::updateHMAC(const byte data[], int length) {
uint8_t status;
if (!wakeup()) {
return 0;
}
// Processing message
int currLength = 0;
while (length) {
data += currLength;
if (length > 64) {
currLength = 64;
} else {
currLength = length;
}
length -= currLength;
if (!sendCommand(0x47, 0x01, currLength, data, currLength)) {
return 0;
}
delay(9);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
}
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::endHMAC(byte result[])
{
return endHMAC(NULL, 0, result);
}
int ECCX08Class::endHMAC(const byte data[], int length, byte result[])
{
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x47, 0x02, length, data, length)) {
return 0;
}
delay(9);
if (!receiveResponse(result, 32)) {
return 0;
}
delay(1);
idle();
return 1;
}
int ECCX08Class::nonce(const byte data[])
{
return challenge(data);
}
int ECCX08Class::incrementCounter(int counterId, long& counter)
{
if (counterId < 0 || counterId > 1) {
return 0;
}
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x24, 1, counterId)) {
return 0;
}
delay(20);
if (!receiveResponse(&counter, sizeof(counter))) {
return 0;
}
delay(1);
idle();
return 1;
}
long ECCX08Class::incrementCounter(int counterId)
{
long counter; // the counter can go up to 2,097,151
if(!incrementCounter(counterId, counter)) {
return -1;
}
return counter;
}
int ECCX08Class::readCounter(int counterId, long& counter)
{
if (counterId < 0 || counterId > 1) {
return 0;
}
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x24, 0, counterId)) {
return 0;
}
delay(20);
if (!receiveResponse(&counter, sizeof(counter))) {
return 0;
}
delay(1);
idle();
return 1;
}
long ECCX08Class::readCounter(int counterId)
{
long counter; // the counter can go up to 2,097,151
if(!readCounter(counterId, counter)) {
return -1;
}
return counter;
}
int ECCX08Class::wakeup()
{
_wire->setClock(_wakeupFrequency);
_wire->beginTransmission(0x00);
_wire->endTransmission();
delayMicroseconds(1500);
byte response;
if (!receiveResponse(&response, sizeof(response)) || response != 0x11) {
return 0;
}
_wire->setClock(_normalFrequency);
return 1;
}
int ECCX08Class::sleep()
{
_wire->beginTransmission(_address);
_wire->write(0x01);
if (_wire->endTransmission() != 0) {
return 0;
}
delay(1);
return 1;
}
int ECCX08Class::idle()
{
_wire->beginTransmission(_address);
_wire->write(0x02);
if (_wire->endTransmission() != 0) {
return 0;
}
delay(1);
return 1;
}
long ECCX08Class::version()
{
uint32_t version = 0;
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x30, 0x00, 0x0000)) {
return 0;
}
delay(2);
if (!receiveResponse(&version, sizeof(version))) {
return 0;
}
delay(1);
idle();
return version;
}
int ECCX08Class::challenge(const byte message[])
{
uint8_t status;
if (!wakeup()) {
return 0;
}
// Nonce, pass through
if (!sendCommand(0x16, 0x03, 0x0000, message, 32)) {
return 0;
}
delay(29);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::verify(const byte signature[], const byte pubkey[])
{
uint8_t status;
if (!wakeup()) {
return 0;
}
byte data[128];
memcpy(&data[0], signature, 64);
memcpy(&data[64], pubkey, 64);
// Verify, external, P256
if (!sendCommand(0x45, 0x02, 0x0004, data, sizeof(data))) {
return 0;
}
delay(72);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::sign(int slot, byte signature[])
{
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x41, 0x80, slot)) {
return 0;
}
delay(70);
if (!receiveResponse(signature, 64)) {
return 0;
}
delay(1);
idle();
return 1;
}
int ECCX08Class::read(int zone, int address, byte buffer[], int length)
{
if (!wakeup()) {
return 0;
}
if (length != 4 && length != 32) {
return 0;
}
if (length == 32) {
zone |= 0x80;
}
if (!sendCommand(0x02, zone, address)) {
return 0;
}
delay(5);
if (!receiveResponse(buffer, length)) {
return 0;
}
delay(1);
idle();
return length;
}
int ECCX08Class::write(int zone, int address, const byte buffer[], int length)
{
uint8_t status;
if (!wakeup()) {
return 0;
}
if (length != 4 && length != 32) {
return 0;
}
if (length == 32) {
zone |= 0x80;
}
if (!sendCommand(0x12, zone, address, buffer, length)) {
return 0;
}
delay(26);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::lock(int zone)
{
uint8_t status;
if (!wakeup()) {
return 0;
}
if (!sendCommand(0x17, 0x80 | zone, 0x0000)) {
return 0;
}
delay(32);
if (!receiveResponse(&status, sizeof(status))) {
return 0;
}
delay(1);
idle();
if (status != 0) {
return 0;
}
return 1;
}
int ECCX08Class::addressForSlotOffset(int slot, int offset)
{
int block = offset / 32;
offset = (offset % 32) / 4;
return (slot << 3) | (block << 8) | (offset);
}
int ECCX08Class::sendCommand(uint8_t opcode, uint8_t param1, uint16_t param2, const byte data[], size_t dataLength)
{
int commandLength = 8 + dataLength; // 1 for type, 1 for length, 1 for opcode, 1 for param1, 2 for param2, 2 for CRC
byte command[commandLength];
command[0] = 0x03;
command[1] = sizeof(command) - 1;
command[2] = opcode;
command[3] = param1;
memcpy(&command[4], ¶m2, sizeof(param2));
memcpy(&command[6], data, dataLength);
uint16_t crc = crc16(&command[1], 8 - 3 + dataLength);
memcpy(&command[6 + dataLength], &crc, sizeof(crc));
_wire->beginTransmission(_address);
_wire->write(command, commandLength);
if (_wire->endTransmission() != 0) {
return 0;
}
return 1;
}
int ECCX08Class::receiveResponse(void* response, size_t length)
{
int retries = 20;
size_t responseSize = length + 3; // 1 for length header, 2 for CRC
byte responseBuffer[responseSize];
while (_wire->requestFrom((uint8_t)_address, (size_t)responseSize, (bool)true) != responseSize && retries--);
responseBuffer[0] = _wire->read();
// make sure length matches
if (responseBuffer[0] != responseSize) {
return 0;
}
for (size_t i = 1; _wire->available(); i++) {
responseBuffer[i] = _wire->read();
}
// verify CRC
uint16_t responseCrc = responseBuffer[length + 1] | (responseBuffer[length + 2] << 8);
if (responseCrc != crc16(responseBuffer, responseSize - 2)) {
return 0;
}
memcpy(response, &responseBuffer[1], length);
return 1;
}
uint16_t ECCX08Class::crc16(const byte data[], size_t length)
{
if (data == NULL || length == 0) {
return 0;
}
uint16_t crc = 0;
while (length) {
byte b = *data;
for (uint8_t shift = 0x01; shift > 0x00; shift <<= 1) {
uint8_t dataBit = (b & shift) ? 1 : 0;
uint8_t crcBit = crc >> 15;
crc <<= 1;
if (dataBit != crcBit) {
crc ^= 0x8005;
}
}
length--;
data++;
}
return crc;
}
#ifdef CRYPTO_WIRE
ECCX08Class ECCX08(CRYPTO_WIRE, 0x60);
#else
ECCX08Class ECCX08(Wire, 0x60);
#endif