forked from CryptoThings/AtCryptoAuthLib
-
Notifications
You must be signed in to change notification settings - Fork 0
/
AtCryptoAuthLib.cpp
1362 lines (1157 loc) · 38.8 KB
/
AtCryptoAuthLib.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
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/**
* Copyright (c) 2016-2017 Robert Totte
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*/
/**
*
* ** This copyright applies to some of the code in this file **
*
* Copyright (c) 2015 Atmel Corporation. All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. The name of Atmel may not be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* 4. This software may only be redistributed and used in connection with an
* Atmel integrated circuit.
*
* THIS SOFTWARE IS PROVIDED BY ATMEL "AS IS" AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
* EXPRESSLY AND SPECIFICALLY DISCLAIMED. IN NO EVENT SHALL ATMEL BE LIABLE FOR
* ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
* OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*
*/
#include <stdint.h>
#include "AtCryptoAuthLib.h"
#include "atca_command.h"
#include "atcacert/atcacert_host_hw.h"
extern "C"
void hexdump(const void *buffer, uint32_t len, uint8_t cols);
extern const atcacert_def_t my_signer_cert_def;
extern const atcacert_def_t my_device_cert_def;
#ifdef LIB_DEBUG
#include <Arduino.h>
static char g_printf_buf[90];
#define AWS_PRINTF(...) \
{ \
snprintf(g_printf_buf, 90, __VA_ARGS__); \
Serial.print(g_printf_buf); \
Serial.flush(); \
}
#else
#define AWS_PRINTF(...)
#endif
// Get a pointer to the default configuration based on the compiler switch
#ifdef ATCA_HAL_KIT_CDC
ATCAIfaceCfg* gCfg = &cfg_ecc508_kitcdc_default;
#elif ATCA_HAL_KIT_HID
ATCAIfaceCfg* gCfg = &cfg_ecc508_kithid_default;
#else //#elif ATCA_HAL_I2C
ATCAIfaceCfg* gCfg = &cfg_ateccx08a_i2c_default;
#endif
AtCryptoAuthLib::AtCryptoAuthLib()
: sha256_init(false)
{
}
AtCryptoAuthLib::~AtCryptoAuthLib()
{
}
ATCA_STATUS AtCryptoAuthLib::init(const uint8_t *access_key)
{
bool lockstate;
ATCA_STATUS ret;
ret = atcatls_init(gCfg);
if (ret != ATCA_SUCCESS)
return ret;
if (access_key == NULL) {
set_enc_key(ENC_KEY);
} else {
set_enc_key(access_key);
}
ret = config_locked(lockstate);
if ((ret != ATCA_SUCCESS) || !lockstate)
return ret;
return atca_tls_init_enc_key();
}
ATCA_STATUS AtCryptoAuthLib::random(uint8_t rand_out[32])
{
return atcatls_random(rand_out);
}
ATCA_STATUS AtCryptoAuthLib::get_pub_key(SlotCfg slot, uint8_t pubKey[64])
{
switch (slot) {
case AUTH_PRIV:
case AUTH_PRIV_2:
case ECDHE_PRIV:
case ECC_KEY_1:
case ECC_KEY_2:
case ECC_KEY_3:
return atcab_get_pubkey((uint16_t)slot, pubKey);
case SIGNER_PUBKEY:
case SIGNER_CERT:
case FEATURE_CERT:
case PKICA_PUBKEY:
case AUTH_CERT_DATA:
return atcab_read_pubkey((uint16_t)slot, pubKey);
default:
break;
}
return ATCA_BAD_PARAM;
}
ATCA_STATUS AtCryptoAuthLib::gen_key(SlotCfg slot, uint8_t pubKey[64])
{
switch (slot) {
case AUTH_PRIV:
case AUTH_PRIV_2:
case ECDHE_PRIV:
case ECC_KEY_1:
case ECC_KEY_2:
case ECC_KEY_3:
return atcab_genkey((uint16_t)slot, pubKey);
default:
break;
}
return ATCA_BAD_PARAM;
}
ATCA_STATUS AtCryptoAuthLib::priv_key_write(SlotCfg slot, const uint8_t priv_key[32])
{
int i;
uint8_t pad_key[36];
switch (slot) {
case ECC_KEY_1:
case ECC_KEY_2:
case ECC_KEY_3:
break;
default:
return ATCA_BAD_PARAM;
}
for (i = 0; i < 32; i++) {
pad_key[i+4] = priv_key[i];
}
pad_key[0] = 0;
pad_key[1] = 0;
pad_key[2] = 0;
pad_key[3] = 0;
return atcab_priv_write(slot, pad_key, ENC_PARENT, m_enc_key);
}
ATCA_STATUS AtCryptoAuthLib::write_pub_key(SlotCfg slot, const uint8_t pubKey[64])
{
switch (slot) {
case SIGNER_PUBKEY:
case SIGNER_CERT:
case FEATURE_CERT:
case PKICA_PUBKEY:
case AUTH_CERT_DATA:
return atcab_write_pubkey((uint16_t)slot, pubKey);
default:
break;
}
return ATCA_BAD_PARAM;
}
ATCA_STATUS AtCryptoAuthLib::sign(SlotCfg slot, const uint8_t to_sign[32], uint8_t signature[64])
{
switch (slot) {
case AUTH_PRIV:
case AUTH_PRIV_2:
case ECDHE_PRIV:
case ECC_KEY_1:
case ECC_KEY_2:
case ECC_KEY_3:
return atcab_sign((uint16_t)slot, to_sign, signature);
default:
break;
}
return ATCA_BAD_PARAM;
}
ATCA_STATUS AtCryptoAuthLib::sign(const uint8_t to_sign[32], uint8_t signature[64])
{
return sign(AUTH_PRIV, to_sign, signature);
}
ATCA_STATUS AtCryptoAuthLib::verify(const uint8_t key[64], const uint8_t to_verify[32],
const uint8_t signature[64], bool &verify_success)
{
bool is_verif = false;
ATCA_STATUS ret;
ret = atcab_verify_extern(to_verify, signature, key, &is_verif);
if (ret == ATCA_SUCCESS)
verify_success = is_verif;
return ret;
}
ATCA_STATUS AtCryptoAuthLib::verify(SlotCfg slot, const uint8_t to_verify[32],
const uint8_t signature[64], bool &verify_success)
{
ATCA_STATUS ret;
uint8_t key[64];
ret = get_pub_key(slot, key);
if (ret != ATCA_SUCCESS) {
return ret;
}
return verify(key, to_verify, signature, verify_success);
}
ATCA_STATUS AtCryptoAuthLib::verify(const uint8_t to_verify[64],
const uint8_t signature[64], bool &verify_success)
{
return verify(AUTH_PRIV, to_verify, signature, verify_success);
}
/* Doesn't work
ATCA_STATUS AtCryptoAuthLib::verify_stored(SlotCfg slot, const uint8_t to_verify[32],
const uint8_t signature[64], bool &verify_success)
{
bool is_verif = false;
ATCA_STATUS ret;
ret = atcab_verify_stored(to_verify, signature, (uint16_t)slot, &is_verif);
if (ret == ATCA_SUCCESS)
verify_success = is_verif;
return ret;
}
*/
ATCA_STATUS AtCryptoAuthLib::hash_start()
{
ATCA_STATUS ret;
if (sha256_init)
return ATCA_BAD_PARAM;
ret = (ATCA_STATUS)atcab_hw_sha2_256_init(&sha256_ctx);
if (ret == ATCA_SUCCESS)
sha256_init = true;
return ret;
}
ATCA_STATUS AtCryptoAuthLib::hash_update(const uint8_t* data, size_t data_size)
{
if (!sha256_init)
return ATCA_BAD_PARAM;
return (ATCA_STATUS)atcab_hw_sha2_256_update(&sha256_ctx, data, data_size);
}
ATCA_STATUS AtCryptoAuthLib::hash_finish(uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE])
{
if (!sha256_init) {
sha256_init = false;
return ATCA_BAD_PARAM;
}
sha256_init = false;
return (ATCA_STATUS)atcab_hw_sha2_256_finish(&sha256_ctx, digest);
}
ATCA_STATUS AtCryptoAuthLib::hash(const uint8_t* data, size_t data_size,
uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE])
{
ATCA_STATUS ret;
ret = hash_start();
if (ret != ATCA_SUCCESS)
return ret;
ret = hash_update(data, data_size);
if (ret != ATCA_SUCCESS)
return ret;
return hash_finish(digest);
}
ATCA_STATUS AtCryptoAuthLib::hmac_start(uint8_t *hmac_key, size_t key_size)
{
ATCA_STATUS ret;
uint8_t pad[32];
int i;
if (sha256_init)
return ATCA_BAD_PARAM;
if (key_size > 32) {
ret = atcab_hw_sha2_256(hmac_key, key_size, m_hmac_key);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: AtCryptoAuthLib::hmac_start @%d %d\n", __LINE__, ret);
return ret;
}
} else {
memset(m_hmac_key, 0, 32);
memcpy(m_hmac_key, hmac_key, key_size);
}
memset(pad, 0x36, 32);
for (i = 0; i < 32; i++)
m_hmac_key[i] = m_hmac_key[i] ^ 0x36;
ret = atcab_hw_sha2_256_init(&sha256_ctx);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: SHA_MODE_SHA256_START @%d %d\n",
__LINE__, (int)ret);
return ret;
}
ret = atcab_hw_sha2_256_update(&sha256_ctx, m_hmac_key, 32);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: SHA_MODE_SHA256_UPDATE @%d %d\n",
__LINE__, (int)ret);
return ret;
}
ret = atcab_hw_sha2_256_update(&sha256_ctx, pad, 32);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: SHA_MODE_SHA256_UPDATE @%d %d\n",
__LINE__, (int)ret);
return ret;
}
sha256_init = true;
return ATCA_SUCCESS;
}
ATCA_STATUS AtCryptoAuthLib::hmac_update(const uint8_t* data, size_t data_size)
{
if (!sha256_init)
return ATCA_BAD_PARAM;
return atcab_hw_sha2_256_update(&sha256_ctx, data, data_size);
}
ATCA_STATUS AtCryptoAuthLib::hmac_finish(uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE])
{
ATCA_STATUS ret;
uint8_t pad[96];
int i;
if (!sha256_init) {
return ATCA_BAD_PARAM;
}
sha256_init = false;
memset(pad, 0x5c, 64);
for (i = 0; i < 32; i++)
pad[i] = m_hmac_key[i] ^ (0x5c ^ 0x36);
ret = atcab_hw_sha2_256_finish(&sha256_ctx, &(pad[64]));
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: AtCryptoAuthLib::hmac_finish @%d %d\n",
__LINE__, (int)ret);
return ret;
}
memset(&sha256_ctx, 0, sizeof(sha256_ctx));
memset(m_hmac_key, 0, 32);
return atcab_hw_sha2_256(pad, 96, digest);
}
ATCA_STATUS AtCryptoAuthLib::ecdh_gen_key(uint8_t key[64])
{
return gen_key(ECDHE_PRIV, key);
}
ATCA_STATUS AtCryptoAuthLib::ecdh(const uint8_t other_key[64], uint8_t pmk[32], SlotCfg sl)
{
// sends an encrypted command for slot ECDHE_PRIV only
switch (sl) {
case AUTH_PRIV:
case AUTH_PRIV_2:
case ECC_KEY_1:
case ECC_KEY_2:
case ECC_KEY_3:
return atcab_ecdh((uint8_t)sl, other_key, pmk);
case ECDHE_PRIV:
return atcab_ecdh_enc((uint8_t)sl, other_key, pmk, m_enc_key, ENC_PARENT);
default:
break;
}
return ATCA_BAD_PARAM;
}
//uint8_t AtCryptoAuthLib::m_enc_key[ATCA_KEY_SIZE] = {0};
const uint8_t AtCryptoAuthLib::ENC_KEY[ATCA_KEY_SIZE] = {
0xF9, 0x47, 0x2C, 0xBD, 0x33, 0x1D, 0x51, 0x00,
0x0C, 0x45, 0x36, 0x34, 0x81, 0x37, 0x2F, 0xD8,
0x5D, 0x5F, 0xB4, 0xB7, 0x25, 0x21, 0xF8, 0x90,
0x52, 0xFA, 0xFC, 0x41, 0x02, 0x40, 0xE6, 0xF5,
};
ATCA_STATUS AtCryptoAuthLib::set_enc_key(const uint8_t* key)
{
memcpy(m_enc_key, key, ATCA_KEY_SIZE);
return ATCA_SUCCESS;
}
ATCA_STATUS AtCryptoAuthLib::atca_tls_init_enc_key()
{
ATCA_STATUS ret = ATCA_SUCCESS;
do {
// ret = atcatls_set_enckey((uint8_t*)m_enc_key, ENC_PARENT, false);
// if (ret != ATCA_SUCCESS) { break; }
ret = atcatlsfn_set_get_enckey(atca_tls_set_enc_key, this);
if (ret != ATCA_SUCCESS) { break; }
} while(0);
return ret;
}
ATCA_STATUS AtCryptoAuthLib::atca_tls_set_enc_key(uint8_t* outKey, int16_t keysize, void *ctx)
{
AtCryptoAuthLib *lib = static_cast<AtCryptoAuthLib*>(ctx);
ATCA_STATUS ret = ATCA_SUCCESS;
do {
if (outKey == NULL || keysize != ATCA_KEY_SIZE || lib == NULL) { break; }
memcpy(outKey, lib->m_enc_key, keysize);
} while(0);
return ret;
}
size_t AtCryptoAuthLib::slot_size(SlotCfg slot)
{
switch (slot) {
case ENC_STORE:
return 416;
case ENC_STORE_9:
case AUTH_CERT:
case SIGNER_PUBKEY:
case SIGNER_CERT:
case FEATURE_CERT:
case PKICA_PUBKEY:
case AUTH_CERT_DATA:
return 72;
case ECDH_PMK:
case ENC_PARENT:
return 36;
default:
break;
}
return 0;
}
bool AtCryptoAuthLib::slot_encrypted(SlotCfg slot)
{
switch (slot) {
case ENC_STORE:
case ENC_STORE_9:
case ECDH_PMK:
return true;
default:
break;
}
return false;
}
ATCA_STATUS AtCryptoAuthLib::read_slot(SlotCfg slot, uint8_t *data, size_t offs, size_t len)
{
ATCA_STATUS ret = ATCA_SUCCESS;
uint8_t dataout[32] = { 0 };
uint8_t block;
size_t i;
size_t dptr;
if ((len + offs) > slot_size(slot)) {
return ATCA_BAD_PARAM;
}
block = offs/32;
i = offs % 32;
dptr = 0;
/*
if (slot_encrypted(slot)) {
ret = atcab_write_zone(ATCA_ZONE_DATA, ENC_PARENT, 0, 0, m_enc_key, ATCA_BLOCK_SIZE);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcatls_set_enckey %d\n", ret);
return ret;;
}
}
*/
memset(data, 0, len);
while (dptr < len) {
if (slot_encrypted(slot)) {
ret = atcab_read_enc(slot, block, dataout, m_enc_key, ENC_PARENT);
} else {
ret = atcab_read_zone(ATCA_ZONE_DATA, slot, block, 0, dataout, 32);
}
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_read %d\n", ret);
break;
}
for ( ; (i < 32) && (dptr < len); i++) {
data[dptr++] = dataout[i];
}
i = 0;
block++;
}
memset(dataout, 0, 32);
return ret;
}
ATCA_STATUS AtCryptoAuthLib::write_slot(SlotCfg slot, uint8_t *data, size_t offs, size_t len)
{
ATCA_STATUS ret = ATCA_SUCCESS;
uint8_t dataout[32] = { 0 };
uint8_t block;
size_t i;
size_t dptr;
if ((len + offs) > slot_size(slot)) {
return ATCA_BAD_PARAM;
}
block = offs/32;
i = offs % 32;
dptr = 0;
while (dptr < len) {
if ((i == 0) && ((len-dptr) >= 32)) {
// write entire block
memcpy(dataout, &(data[dptr]), 32);
dptr += 32;
} else {
// read-modify-write
if (slot_encrypted(slot)) {
ret = atcab_read_enc(slot, block, dataout, m_enc_key, ENC_PARENT);
} else {
ret = atcab_read_zone(ATCA_ZONE_DATA, slot, block, 0, dataout, 32);
}
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_read %d\n", ret);
break;
}
for ( ; (i < 32) && (dptr < len); i++) {
dataout[i] = data[dptr++];
}
i = 0;
}
if (slot_encrypted(slot)) {
ret = atcab_write_enc(slot, block, dataout, m_enc_key, ENC_PARENT);
} else {
ret = atcab_write_zone(ATCA_ZONE_DATA, slot, block, 0, dataout, 32);
}
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_write %d\n", ret);
break;
}
block++;
}
memset(dataout, 0, 32);
return ret;
}
bool AtCryptoAuthLib::verify_cert_chain(uint8_t *caPubKey,
uint8_t *signerCert, size_t *signerCertSize,
uint8_t *deviceCert, size_t *deviceCertSize)
{
ATCA_STATUS status = ATCA_GEN_FAIL;
uint8_t signerPubkey[64] = { 0 };
do {
// Get the signer certificate - ATCACERT_E_WRONG_CERT_DEF
status = atcatls_get_cert(&my_signer_cert_def, caPubKey, signerCert, signerCertSize);
if (status == ATCACERT_E_WRONG_CERT_DEF) {
AWS_PRINTF("ERROR: atcatls_get_cert signer Wrong Cert Def\n");
} else if (status != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcatls_get_cert signer %x\n", status);
break;
}
// Verify the signer certificate
status = (ATCA_STATUS)atcacert_verify_cert_hw(&my_signer_cert_def, signerCert, *signerCertSize, caPubKey);
if (status != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcacert_verify_cert_hw signer %d\n", status);
break;
}
// Get the signer public key from the signer certificate
status = (ATCA_STATUS)atcacert_get_subj_public_key(&my_signer_cert_def, signerCert, *signerCertSize, signerPubkey);
if (status != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcacert_get_subj_public_key %d\n", status);
break;
}
// Get the device certificate
status = atcatls_get_cert(&my_device_cert_def, signerPubkey, deviceCert, deviceCertSize);
if (status == ATCACERT_E_WRONG_CERT_DEF) {
AWS_PRINTF("ERROR: atcatls_get_cert device Wrong Cert Def\n");
} else if (status != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcatls_get_cert device %d\n", status);
break;
}
// Verify the device certificate
status = (ATCA_STATUS)atcacert_verify_cert_hw(&my_device_cert_def, deviceCert, *deviceCertSize, signerPubkey);
if (status != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcacert_verify_cert_hw device %d\n", status);
break;
}
} while (0);
if (status == ATCA_SUCCESS) {
AWS_PRINTF("Verify Cert PASS %d\n", __LINE__);
return true;
} else {
AWS_PRINTF("Verify Cert FAIL %d\n", __LINE__);
return false;
}
}
// modified W25 ECC508 configuration
// (slot 7 has external sign turned on for cert testing)
const uint8_t AtCryptoAuthLib::golden_ecc_configdata[ATCA_CONFIG_SIZE] = {
// block 0
// Not Written: First 16 bytes are not written
0x01, 0x23, 0x00, 0x00, // SN
0x00, 0x00, 0x50, 0x00, // rev num
0x04, 0x05, 0x06, 0x07, // SN
0xEE, 0x00, 0x01, 0x00, // SN, I2C en
// I2C, reserved, OtpMode, ChipMode
0xC0, 0x00, 0x55, 0x00,
// SlotConfig
0x87, 0x20, // slot 0 - TLS_SLOT_AUTH_PRIV (0)
0x87, 0x20, // slot 0 - TLS_SLOT_AUTH_PRIV (1)
0x8F, 0x20, // slot 2 - TLS_SLOT_ECDHE_PRIV (2)
0xC4, 0x44, // slot 3 - TLS_SLOT_ECDH_PMK (3)
0x8F, 0x0F, // slot 4 - TLS_SLOT_ENC_PARENT (4)
0x87, 0x64, // slot 7 - TLS_SLOT_FEATURE_PRIV (5)
0x87, 0x64, // slot 7 - TLS_SLOT_FEATURE_PRIV (6)
0x87, 0x64, // slot 7 - TLS_SLOT_FEATURE_PRIV (7)
0xC4, 0x44, // slot 8 - TLS_SLOT8_ENC_STORE (8)
0xC4, 0x44, // slot 9 - TLS_SLOT9_ENC_STORE (9)
0x0F, 0x0F, // slot 10 - TLS_SLOT_AUTH_CERT (A)
0x0F, 0x0F, // slot 11 - TLS_SLOT_SIGNER_PUBKEY (B)
0x0F, 0x0F, // slot 12 - TLS_SLOT_SIGNER_CERT (C)
0x0F, 0x0F, // slot 13 - TLS_SLOT_FEATURE_CERT (D)
0x0F, 0x0F, // slot 14 - TLS_SLOT_PKICA_PUBKEY (E)
0x0F, 0x0F, // slot 15 - TLS_SLOT_DEVICE_CERT (F)
// Counters
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
// Last Key Use
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
// Not Written: UserExtra, Selector, LockValue, LockConfig (word offset = 5)
0x00, 0x00, 0x00, 0x00,
// -- end of common sha204 data -- (88 bytes)
// SlotLock[2], RFU[2]
0xFF, 0xFF, 0x00, 0x00,
// X.509 Format
0x00, 0x00, 0x00, 0x00,
// KeyConfig
0x13, 0x00, // slot 0 - TLS_SLOT_AUTH_PRIV (0)
0x13, 0x00, // slot 0 - TLS_SLOT_AUTH_PRIV (1)
0x33, 0x00, // slot 2 - TLS_SLOT_ECDHE_PRIV (2)
0x1C, 0x00, // slot 3 - TLS_SLOT_ECDH_PMK (3)
0x3C, 0x00, // slot 4 - TLS_SLOT_ENC_PARENT (4)
0x33, 0x00, // slot 7 - TLS_SLOT_FEATURE_PRIV (5)
0x33, 0x00, // slot 7 - TLS_SLOT_FEATURE_PRIV (6)
0x33, 0x00, // slot 7 - TLS_SLOT_FEATURE_PRIV (7)
0x1C, 0x00, // slot 8 - TLS_SLOT8_ENC_STORE (8)
0x1C, 0x00, // slot 9 - TLS_SLOT9_ENC_STORE (9)
0x3C, 0x00, // slot 10 - TLS_SLOT_AUTH_CERT (A)
0x3C, 0x00, // slot 11 - TLS_SLOT_SIGNER_PUBKEY (B)
0x3C, 0x00, // slot 12 - TLS_SLOT_SIGNER_CERT (C)
0x3C, 0x00, // slot 13 - TLS_SLOT_FEATURE_CERT (D)
0x3C, 0x00, // slot 14 - TLS_SLOT_PKICA_PUBKEY (E)
0x3C, 0x00, // slot 15 - TLS_SLOT_DEVICE_CERT (F)
};
/* Config notes
slot 0,1,5,6,7 can do unencrypted ECDH
slot 2 writes encrypted ECDH to slot+1
slot 5,6,7 allows priv_write
*/
// minimal config without encrypted slots
const uint8_t AtCryptoAuthLib::minimal_ecc_configdata[ATCA_CONFIG_SIZE] = {
// block 0
// Not Written: First 16 bytes are not written
0x01, 0x23, 0x00, 0x00, // SN
0x00, 0x00, 0x50, 0x00, // rev num
0x04, 0x05, 0x06, 0x07, // SN
0xEE, 0x00, 0x01, 0x00, // SN, I2C en
// I2C, reserved, OtpMode, ChipMode
0xC0, 0x00, 0x55, 0x00,
// SlotConfig
0x87, 0x20, // slot 0 - TLS_SLOT_AUTH_PRIV (0)
0x87, 0x20, // slot 0 - TLS_SLOT_AUTH_PRIV (1)
0x87, 0x20, // slot 2 - TLS_SLOT_ECDHE_PRIV (2)
0x87, 0x20, // slot 3 - TLS_SLOT_ECDH_PMK (3)
0x87, 0x20, // slot 4 - TLS_SLOT_ENC_PARENT (4)
0x87, 0x20, // slot 7 - TLS_SLOT_FEATURE_PRIV (5)
0x87, 0x20, // slot 7 - TLS_SLOT_FEATURE_PRIV (6)
0x87, 0x20, // slot 7 - TLS_SLOT_FEATURE_PRIV (7)
0x0F, 0x0F, // slot 8 - TLS_SLOT8_ENC_STORE (8)
0x0F, 0x0F, // slot 9 - TLS_SLOT9_ENC_STORE (9)
0x0F, 0x0F, // slot 10 - TLS_SLOT_AUTH_CERT (A)
0x0F, 0x0F, // slot 11 - TLS_SLOT_SIGNER_PUBKEY (B)
0x0F, 0x0F, // slot 12 - TLS_SLOT_SIGNER_CERT (C)
0x0F, 0x0F, // slot 13 - TLS_SLOT_FEATURE_CERT (D)
0x0F, 0x0F, // slot 14 - TLS_SLOT_PKICA_PUBKEY (E)
0x0F, 0x0F, // slot 15 - TLS_SLOT_DEVICE_CERT (F)
// Counters
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
0xFF, 0xFF, 0xFF, 0xFF,
0x00, 0x00, 0x00, 0x00,
// Last Key Use
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
0xFF, 0xFF, 0xFF, 0xFF,
// Not Written: UserExtra, Selector, LockValue, LockConfig (word offset = 5)
0x00, 0x00, 0x00, 0x00,
// -- end of common sha204 data -- (88 bytes)
// SlotLock[2], RFU[2]
0xFF, 0xFF, 0x00, 0x00,
// X.509 Format
0x00, 0x00, 0x00, 0x00,
// KeyConfig
0x13, 0x00, // slot 0 - TLS_SLOT_AUTH_PRIV (0)
0x13, 0x00, // slot 0 - TLS_SLOT_AUTH_PRIV (1)
0x13, 0x00, // slot 2 - TLS_SLOT_ECDHE_PRIV (2)
0x13, 0x00, // slot 3 - TLS_SLOT_ECDH_PMK (3)
0x13, 0x00, // slot 4 - TLS_SLOT_ENC_PARENT (4)
0x13, 0x00, // slot 7 - TLS_SLOT_FEATURE_PRIV (5)
0x13, 0x00, // slot 7 - TLS_SLOT_FEATURE_PRIV (6)
0x13, 0x00, // slot 7 - TLS_SLOT_FEATURE_PRIV (7)
0x3C, 0x00, // slot 8 - TLS_SLOT8_ENC_STORE (8)
0x3C, 0x00, // slot 9 - TLS_SLOT9_ENC_STORE (9)
0x3C, 0x00, // slot 10 - TLS_SLOT_AUTH_CERT (A)
0x3C, 0x00, // slot 11 - TLS_SLOT_SIGNER_PUBKEY (B)
0x3C, 0x00, // slot 12 - TLS_SLOT_SIGNER_CERT (C)
0x3C, 0x00, // slot 13 - TLS_SLOT_FEATURE_CERT (D)
0x3C, 0x00, // slot 14 - TLS_SLOT_PKICA_PUBKEY (E)
0x3C, 0x00, // slot 15 - TLS_SLOT_DEVICE_CERT (F)
};
ATCA_STATUS AtCryptoAuthLib::config_locked(bool &lockstate)
{
ATCA_STATUS ret = ATCA_SUCCESS;
bool ls = 0;
do {
ret = atcab_is_locked(LOCK_ZONE_CONFIG, &ls);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_is_locked(LOCK_ZONE_CONFIG %d\n", ret);
break;
}
lockstate = ls;
} while (0);
return ret;
}
ATCA_STATUS AtCryptoAuthLib::check_config(bool &match)
{
ATCA_STATUS ret = ATCA_SUCCESS;
uint8_t configdata[ATCA_CONFIG_SIZE] = { 0 };
bool lock;
match = false;
do {
ret = config_locked(lock);
if ((ret != ATCA_SUCCESS) || !lock) {
break;
}
ret = atcab_read_ecc_config_zone((uint8_t*)configdata);
if (ret != ATCA_SUCCESS) {
break;
}
if (memcmp(&(configdata[20]), &(golden_ecc_configdata[20]), ATCA_CONFIG_SIZE-20) == 0) {
match = true;
}
} while (0);
return ret;
}
ATCA_STATUS AtCryptoAuthLib::config_chip(const uint8_t *access_key)
{
ATCA_STATUS ret = ATCA_SUCCESS;
bool lockstate = 0;
uint8_t lock_response;
uint8_t configdata[ATCA_CONFIG_SIZE] = { 0 };
if (access_key != NULL)
set_enc_key(access_key);
do {
// Check the lock state of the config zone. If unlocked, then write and lock.
ret = atcab_is_locked(LOCK_ZONE_CONFIG, &lockstate);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_is_locked(LOCK_ZONE_CONFIG %d\n", ret);
break;
}
if (!lockstate) {
ret = atcab_write_ecc_config_zone(golden_ecc_configdata);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_write_ecc_config_zone %d\n", ret);
break;
}
ret = atcab_lock_config_zone(&lock_response);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_lock_config_zone %d\n", ret);
break;
}
} else {
AWS_PRINTF("Already Locked\n");
}
// Refresh the configuration zone data with the bytes that are now on the device
ret = atcab_read_ecc_config_zone((uint8_t*)&configdata);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_read_ecc_config_zone %d\n", ret);
break;
}
// Check the Data zone lock
ret = atcab_is_locked(LOCK_ZONE_DATA, &lockstate);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_is_locked(LOCK_ZONE_DATA %d\n", ret);
break;
}
if (!lockstate) {
uint8_t pubKey[64];
ret = atcab_write_zone(DEVZONE_DATA, ENC_PARENT, 0, 0, m_enc_key, 32);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_write_zone(DEVZONE_DATA %d\n", ret);
break;
}
ret = atcab_genkey((uint16_t)AUTH_PRIV, pubKey);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_genkey AUTH_PRIV %d\n", ret);
break;
}
ret = atcab_lock_data_zone(&lock_response);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcab_lock_data_zone %d\n", ret);
break;
}
}
} while (0);
return ret;
}
/*
extern "C"
void atca_delay_ms(uint32_t d)
{
delay(d);
}
*/
// ** Provision With Device Stored Certs **
#include "atcacert/atcacert_def.h"
#include "atcacert/atcacert_client.h"
#define CERT_DEF_DEVICE (&my_device_cert_def)
#define CERT_DEF_SIGNER (&my_signer_cert_def)
#define AWS_CERT_LENGH_MAX (1024)
#define AWS_ROOT_CERT_ID (uint8_t)(0x00) // Root Cert Identifier
#define AWS_SIGNER_CERT_ID (uint8_t)(0x01) // Signer Cert Identifier
#define AWS_VERIF_CERT_ID (uint8_t)(0x02) // Verification Cert Identifier
#define AWS_DEVICE_CERT_ID (uint8_t)(0x03) // Device Cert Identifier
int AtCryptoAuthLib::provision_load_signer_cert(
const char *pem_cert,
size_t pem_cert_len,
uint8_t *tbs_data, // ATCA_SHA_DIGEST_SIZE
uint8_t signer_id[2],
const atcacert_tm_utc_t *issue_date)
{
int ret = ATCA_SUCCESS;
uint8_t der_cert[AWS_CERT_LENGH_MAX];
size_t der_cert_len;
uint8_t signer_pub_key[ATCA_PUB_KEY_SIZE] = { 0 };
do {
der_cert_len = AWS_CERT_LENGH_MAX;
ret = atcacert_decode_pem_cert(pem_cert, pem_cert_len,
der_cert, &der_cert_len);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcatls_random %d\n", ret);
break;
}
AWS_PRINTF("provision_load_signer_cert %d\n", __LINE__);
ret = atcacert_get_subj_public_key(CERT_DEF_SIGNER,
der_cert, der_cert_len, signer_pub_key);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcacert_get_subj_public_key %d\n", ret);
break;
}
AWS_PRINTF("provision_load_signer_cert %d\n", __LINE__);
ret = atcacert_get_signer_id(CERT_DEF_SIGNER,
der_cert, der_cert_len, signer_id);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: atcacert_get_signer_id %d\n", ret);
break;
}
AWS_PRINTF("provision_load_signer_cert %d\n", __LINE__);
ret = aws_prov_save_cert(AWS_SIGNER_CERT_ID, pem_cert, pem_cert_len);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: aws_prov_save_cert %d\n", ret);
break;
}
AWS_PRINTF("provision_load_signer_cert %d\n", __LINE__);
ret = aws_prov_save_signer_public_key(signer_pub_key);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: aws_prov_save_signer_public_key %d\n", ret);
break;
}
AWS_PRINTF("provision_load_signer_cert %d\n", __LINE__);
ret = aws_prov_build_device_tbs(tbs_data, signer_pub_key, signer_id, issue_date);
if (ret != ATCA_SUCCESS) {
AWS_PRINTF("ERROR: aws_prov_build_device_tbs %d\n", ret);
break;
}
AWS_PRINTF("provision_load_signer_cert %d\n", __LINE__);
} while(0);
return ret;
}