11#include < Arduino.h>
2- #include < atca_helpers.h>
3- #include < atcacert/atcacert_client.h>
4- #include < cryptoauthlib.h>
2+
3+ #include < ecc608.h>
54#include < log.h>
65
7- #include " cert_def_1_signer.h"
8- #include " cert_def_3_device.h"
6+ static void printCertificate (const uint8_t * certificate, const size_t size) {
7+
8+ size_t buffer_size = ECC608.calculateBase64EncodedCertificateSize (size);
9+
10+ char buffer[buffer_size];
911
10- void printCertificate (uint8_t * certificate, uint16_t size) {
11- char buffer[1024 ];
12- size_t buffer_size = sizeof (buffer);
13- ATCA_STATUS result =
14- atcab_base64encode (certificate, size, buffer, &buffer_size);
12+ ATCA_STATUS status =
13+ ECC608.base64EncodeCertificate (certificate, size, buffer, &buffer_size);
1514
16- if (result != ATCA_SUCCESS) {
17- Log.errorf (" Failed to encode into base64: %x\r\n " , result );
15+ if (status != ATCA_SUCCESS) {
16+ Log.errorf (" Failed to encode into base64: %x\r\n " , status );
1817 return ;
1918 }
2019
21- buffer[buffer_size] = 0 ;
2220 Log.rawf (
2321 " -----BEGIN CERTIFICATE-----\r\n %s\r\n -----END CERTIFICATE-----\r\n " ,
2422 buffer);
@@ -27,62 +25,113 @@ void printCertificate(uint8_t* certificate, uint16_t size) {
2725void setup () {
2826 Log.begin (115200 );
2927
30- int status;
31-
32- static ATCAIfaceCfg cfg_atecc608b_i2c = {ATCA_I2C_IFACE,
33- ATECC608B,
34- {
35- 0x58 , // 7 bit address of ECC
36- 2 , // Bus number
37- 100000 // Baud rate
38- },
39- 1560 ,
40- 20 ,
41- NULL };
42-
43- if (ATCA_SUCCESS != (status = atcab_init (&cfg_atecc608b_i2c))) {
44- Log.errorf (" Failed to init: %d\r\n " , status);
28+ ATCA_STATUS atca_status = ECC608.begin ();
29+
30+ if (atca_status != ATCA_SUCCESS) {
31+ Log.errorf (" Failed to initialize ECC608, status code: 0x%X\r\n " ,
32+ atca_status);
33+ }
34+
35+ // Extract the max size of the certificates first
36+
37+ size_t max_root_certificate_size = 0 , max_signer_certificate_size = 0 ,
38+ max_device_certificate_size = 0 ;
39+
40+ int atca_cert_status = ATCACERT_E_SUCCESS;
41+
42+ if ((atca_cert_status = ECC608.getRootCertificateSize (
43+ &max_root_certificate_size)) != ATCACERT_E_SUCCESS) {
44+
45+ Log.errorf (" Failed to get root certificate's max size, status code: "
46+ " 0x%X\r\n " ,
47+ atca_cert_status);
4548 return ;
46- } else {
47- Log.info (" Initialized ECC\r\n " );
4849 }
4950
50- // Retrieve public root key
51- uint8_t public_key[ATCA_PUB_KEY_SIZE];
52- if (ATCA_SUCCESS != (status = atcab_get_pubkey (0 , public_key))) {
53- Log.errorf (" Failed to get public key: %x\r\n " , status);
51+ if ((atca_cert_status = ECC608.getSignerCertificateSize (
52+ &max_signer_certificate_size)) != ATCACERT_E_SUCCESS) {
53+
54+ Log.errorf (" Failed to get signer certificate's max size, status code: "
55+ " 0x%X\r\n " ,
56+ atca_cert_status);
5457 return ;
5558 }
5659
60+ if ((atca_cert_status = ECC608.getDeviceCertificateSize (
61+ &max_device_certificate_size)) != ATCACERT_E_SUCCESS) {
62+
63+ Log.errorf (" Failed to get device certificate's max size, status code: "
64+ " 0x%X\r\n " ,
65+ atca_cert_status);
66+ return ;
67+ }
68+
69+ // We reuse the buffer for the certificates, so have to find the max
70+ // size of them so we have enough space for the biggest certificate
71+ const size_t certificate_buffer_size = max (
72+ max (max_root_certificate_size, max_signer_certificate_size),
73+ max_device_certificate_size);
74+
75+ uint8_t certificate_buffer[certificate_buffer_size];
76+
77+ // --- Root certificate ---
78+
79+ size_t root_certificate_size = certificate_buffer_size;
80+
5781 Log.raw (" \r\n\r\n " );
5882
59- // Retrive sign certificate
60- uint8_t buffer[g_cert_def_1_signer. cert_template_size + 4 ];
61- size_t size = sizeof (buffer);
83+ if ((atca_cert_status = ECC608. getRootCertificate (
84+ certificate_buffer,
85+ &root_certificate_size)) != ATCACERT_E_SUCCESS) {
6286
63- if (ATCA_SUCCESS != (status = atcacert_read_cert (&g_cert_def_1_signer,
64- public_key,
65- buffer,
66- &size))) {
67- Log.errorf (" Failed to read signing certificate: %d\r\n " , status);
87+ Log.errorf (" Failed to get root certificate, status code: "
88+ " 0x%X\r\n " ,
89+ atca_cert_status);
6890 return ;
6991 } else {
70- Log.info (" Printing signing certificate...\r\n " );
71- printCertificate (buffer, size);
92+
93+ Log.info (" Printing root certificate...\r\n " );
94+ printCertificate (certificate_buffer, root_certificate_size);
7295 }
7396
97+ // --- Signer certificate ---
98+
99+ size_t signer_certificate_size = max_signer_certificate_size;
100+
74101 Log.raw (" \r\n\r\n " );
75102
76- // Retrive device certificate
77- if (ATCA_SUCCESS != (status = atcacert_read_cert (&g_cert_def_3_device,
78- public_key,
79- buffer,
80- &size))) {
81- Log.errorf (" Failed to read device certificate: %d\r\n " , status);
103+ if ((atca_cert_status = ECC608.getSignerCertificate (
104+ certificate_buffer,
105+ &signer_certificate_size)) != ATCACERT_E_SUCCESS) {
106+
107+ Log.errorf (" Failed to get signer certificate, status code: "
108+ " 0x%X\r\n " ,
109+ atca_cert_status);
82110 return ;
83111 } else {
112+
113+ Log.info (" Printing signer certificate...\r\n " );
114+ printCertificate (certificate_buffer, signer_certificate_size);
115+ }
116+
117+ // --- Device certificate ---
118+
119+ size_t device_certificate_size = max_device_certificate_size;
120+
121+ Log.raw (" \r\n\r\n " );
122+
123+ if ((atca_cert_status = ECC608.getDeviceCertificate (
124+ certificate_buffer,
125+ &device_certificate_size)) != ATCACERT_E_SUCCESS) {
126+
127+ Log.errorf (" Failed to get device certificate, status code: "
128+ " 0x%X\r\n " ,
129+ atca_cert_status);
130+ return ;
131+ } else {
132+
84133 Log.info (" Printing device certificate...\r\n " );
85- printCertificate (buffer, size );
134+ printCertificate (certificate_buffer, device_certificate_size );
86135 }
87136}
88137
0 commit comments