Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ESP32 support for M5Stack Core2 IoT EduKit #30

Merged
merged 8 commits into from
Nov 10, 2021

Conversation

torntrousers
Copy link
Contributor

@torntrousers torntrousers commented Mar 13, 2021

I'd like to use this library with the M5Stack Core2 IoT EduKit but there is a compile error on an ESP32 and the I2C address of the ATECC608 is different.

The compile error fix is from #25

For the I2C address I can see that with the existing code you can create your own instance with a custom I2C address, but then all the provided utilities don't work so you'd need you own of those too, so this new begin(i2cAddr) function makes it all work more seemlessly.

What do you think?

@torntrousers
Copy link
Contributor Author

I've tried this on the ESP32 and an Arduino Nano 33 IoT so can confirm it still works with both.

@github-actions
Copy link

Memory usage change @ 44baa6c

Board flash % RAM for global variables %
arduino:samd:mkr1000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table
Board examples/ECCX08RandomNumber
flash
% examples/ECCX08RandomNumber
RAM for global variables
% examples/ESP32EduKit
flash
% examples/ESP32EduKit
RAM for global variables
% examples/Tools/ECCX08CSR
flash
% examples/Tools/ECCX08CSR
RAM for global variables
% examples/Tools/ECCX08JWSPublicKey
flash
% examples/Tools/ECCX08JWSPublicKey
RAM for global variables
% examples/Tools/ECCX08SelfSignedCert
flash
% examples/Tools/ECCX08SelfSignedCert
RAM for global variables
%
arduino:samd:mkr1000 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrgsm1400 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrnb1500 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwan1300 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwan1310 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 N/A N/A N/A N/A 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
Click for full report CSV
Board,examples/ECCX08RandomNumber<br>flash,%,examples/ECCX08RandomNumber<br>RAM for global variables,%,examples/ESP32EduKit<br>flash,%,examples/ESP32EduKit<br>RAM for global variables,%,examples/Tools/ECCX08CSR<br>flash,%,examples/Tools/ECCX08CSR<br>RAM for global variables,%,examples/Tools/ECCX08JWSPublicKey<br>flash,%,examples/Tools/ECCX08JWSPublicKey<br>RAM for global variables,%,examples/Tools/ECCX08SelfSignedCert<br>flash,%,examples/Tools/ECCX08SelfSignedCert<br>RAM for global variables,%
arduino:samd:mkr1000,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrgsm1400,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrnb1500,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwan1300,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwan1310,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,N/A,N/A,N/A,N/A,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0

@github-actions
Copy link

Memory usage change @ 9864c4c

Board flash % RAM for global variables %
arduino:mbed:envie_m7 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:uno2018 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table
Board examples/ECCX08RandomNumber
flash
% examples/ECCX08RandomNumber
RAM for global variables
% examples/Tools/ECCX08CSR
flash
% examples/Tools/ECCX08CSR
RAM for global variables
% examples/Tools/ECCX08JWSPublicKey
flash
% examples/Tools/ECCX08JWSPublicKey
RAM for global variables
% examples/Tools/ECCX08SelfSignedCert
flash
% examples/Tools/ECCX08SelfSignedCert
RAM for global variables
%
arduino:mbed:envie_m7 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:megaavr:uno2018 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrgsm1400 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrnb1500 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwan1300 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwan1310 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
Click for full report CSV
Board,examples/ECCX08RandomNumber<br>flash,%,examples/ECCX08RandomNumber<br>RAM for global variables,%,examples/Tools/ECCX08CSR<br>flash,%,examples/Tools/ECCX08CSR<br>RAM for global variables,%,examples/Tools/ECCX08JWSPublicKey<br>flash,%,examples/Tools/ECCX08JWSPublicKey<br>RAM for global variables,%,examples/Tools/ECCX08SelfSignedCert<br>flash,%,examples/Tools/ECCX08SelfSignedCert<br>RAM for global variables,%
arduino:mbed:envie_m7,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:megaavr:uno2018,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrgsm1400,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrnb1500,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwan1300,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwan1310,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0

@CLAassistant
Copy link

CLAassistant commented Apr 9, 2021

CLA assistant check
All committers have signed the CLA.

@rashedtalukder
Copy link

@facchinm, would you be so kind as to review this PR and merge? This patch lets me use this library on other Arduino boards with external ECCx08 boards that have a different i2c address.

@facchinm
Copy link
Contributor

Hi @rashedtalukder , reviewing right now, sorry but I lost the notification 😓

@torntrousers I really like the begin() overload; about the requestFrom change, I'd prefer to have the whole code without ifdefs, since the ESP32 version could not read more than 256 bytes and would wrap if more are requested while the other can (this shouldn't hurt the behaviour of the library but it's not very clear anyway).

Maybe it's better to convince the ESP32 people to patch their core to be compliant with the API specifications https://github.com/arduino/ArduinoCore-API/blob/173e8eadced2ad32eeb93bcbd5c49f8d6a055ea6/api/HardwareI2C.h#L39 😉

@torntrousers
Copy link
Contributor Author

torntrousers commented Oct 16, 2021

Maybe it's better to convince the ESP32 people to patch their core to be compliant with the API specifications https://github.com/arduino/ArduinoCore-API/blob/173e8eadced2ad32eeb93bcbd5c49f8d6a055ea6/api/HardwareI2C.h#L39 😉

@facchinm I can't work out how to do this from the ESP32 side, see espressif/arduino-esp32#5768 (comment), any suggestions?

@torntrousers
Copy link
Contributor Author

Hi @facchinm , how about simply changing the (size_t)responseSize to (uint8_t)responseSize on the requestFrom call? Maybe that will seem imperfect, but it would fix #25 and #36 and keep the existing Arduino boards working fine.

@torntrousers
Copy link
Contributor Author

torntrousers commented Nov 9, 2021

Happy days, the requestFrom function has now been updated in the ESP32 core code in espressif/arduino-esp32#5768 thanks to help from @me-no-dev, and this is now out in the 2.0.1 release.
So I've updated this PR to just have the I2C address update. How does this look now @facchinm ?

@github-actions
Copy link

github-actions bot commented Nov 9, 2021

Memory usage change @ 70563e0

Board flash % RAM for global variables %
arduino:mbed:envie_m7 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:mbed_nano:nanorp2040connect 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:megaavr:uno2018 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkr1000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrgsm1400 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrnb1500 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrvidor4000 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1300 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwan1310 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:mkrwifi1010 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
arduino:samd:nano_33_iot 0 - 0 0.0 - 0.0 0 - 0 0.0 - 0.0
Click for full report table
Board examples/ECCX08RandomNumber
flash
% examples/ECCX08RandomNumber
RAM for global variables
% examples/Tools/ECCX08CSR
flash
% examples/Tools/ECCX08CSR
RAM for global variables
% examples/Tools/ECCX08JWSPublicKey
flash
% examples/Tools/ECCX08JWSPublicKey
RAM for global variables
% examples/Tools/ECCX08SelfSignedCert
flash
% examples/Tools/ECCX08SelfSignedCert
RAM for global variables
%
arduino:mbed:envie_m7 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:mbed_nano:nanorp2040connect 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:megaavr:uno2018 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkr1000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrgsm1400 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrnb1500 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrvidor4000 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwan1300 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwan1310 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:mkrwifi1010 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
arduino:samd:nano_33_iot 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0 0 0.0
Click for full report CSV
Board,examples/ECCX08RandomNumber<br>flash,%,examples/ECCX08RandomNumber<br>RAM for global variables,%,examples/Tools/ECCX08CSR<br>flash,%,examples/Tools/ECCX08CSR<br>RAM for global variables,%,examples/Tools/ECCX08JWSPublicKey<br>flash,%,examples/Tools/ECCX08JWSPublicKey<br>RAM for global variables,%,examples/Tools/ECCX08SelfSignedCert<br>flash,%,examples/Tools/ECCX08SelfSignedCert<br>RAM for global variables,%
arduino:mbed:envie_m7,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:mbed_nano:nanorp2040connect,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:megaavr:uno2018,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkr1000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrgsm1400,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrnb1500,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrvidor4000,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwan1300,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwan1310,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:mkrwifi1010,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0
arduino:samd:nano_33_iot,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0,0,0.0

@rashedtalukder
Copy link

Awesome work @torntrousers! @facchinm, there shouldn't be any blockers to this getting merged at this point, correct?

@facchinm
Copy link
Contributor

LGTM! I'm squashing into a single commit and merging! Thanks for the contribution

@facchinm facchinm merged commit 9e9cf8f into arduino-libraries:master Nov 10, 2021
@torntrousers
Copy link
Contributor Author

Any chance of a release of this library soon?

@rashedtalukder - I'm now trying to get a mutual TLS connection to AWS IoT with the client private key in the EduKit's 608. Using BearSSL as I've got that working with the 608 on a Nano 33 IoT board, but if you have this with MBedTLS or any ideas would be happy to hear about it.

@rashedtalukder
Copy link

@torntrousers, you can find an example of it working on the Core2 for AWS using BearSSL below. It would be great to have the device certificate get retrieved in BearSSL so there's less the user needs to do:
https://github.com/aws-samples/aws-iot-edukit-examples/tree/main/Basic_Arduino

You can see how to generate the public key in either of these examples:

Here is how the device certificate is retrieved using the Microchip cryptoauthlib (along w/ espressif's port). You'll have to ignore the gibberish logging outputs in that function, it was clearly copied and pasted from the the signer cert retrieval.: https://github.com/espressif/esp-cryptoauthlib/blob/feature/add_firmware_source_code/esp_cryptoauth_utility/firmware/main/handlers.c#L513

@gannaramu
Copy link

@rashedtalukder I was able to extract the signer certificate and signer public key from the ATECC608B-TNGTLS but I am kind of stuck in reconstructing the certificate to X.509 format. with some guidance, I can get that going.

@torntrousers
Copy link
Contributor Author

@rashedtalukder

It would be great to have the device certificate get retrieved in BearSSL so there's less the user needs to do

I guess that would need something like a new ArduinoECCX08 function that reconstructs a certificate to PEM format from a 608 slot and a new ArduinoBearSSL setEccSlot function that uses that:

void BearSSLClient::setEccSlot(int ecc508KeySlot, int ecc508CertificateSlot)
{
   String certPEM = ECCX08Cert.certificate(ecc508CertificateSlot);
   setEccSlot(ecc508KeySlot, certPEM.c_str());
}

Seems pretty dooable, and I do actually need the ECCX08Cert.certificate function for another thing anyway .

@torntrousers
Copy link
Contributor Author

@rashedtalukder I was able to extract the signer certificate and signer public key from the ATECC608B-TNGTLS but I am kind of stuck in reconstructing the certificate to X.509 format. with some guidance, I can get that going.

Hi @gannaramu , i could help you but I don't know what you know. The compressed certificates are described in section 2.2.1.3 here and the app note that refers too, here.

There is a really handy online utility for showing the ASN.1 structure https://lapo.it/asn1js/ which can be super helpful while debugging.

@rashedtalukder
Copy link

@torntrousers, that's correct you'd need to start by adding the function here to get the public key from the requested slot. I can't remember if it's in PEM or DER format, but I think it's DER. After that, BearSSL needs to have it modified that if using a secure element or PK from a slot, it just uses the certificate from the hardware instead.

@gannaramu
Copy link

@torntrousers Thanks for those links. Both the documents didn't cover where the organizationName, commonName is inferred from. I was using the Jupiter notebooks from this repository with Cryptoauth Trust Platform Development Kit.
From resource generation notebook i was able extract the device certificate and signer certificate and used it for cross verify my functions on Arduino (Teensy) are generating it correctly. I observed the common name is a serial number appended with a " ATECC". do you know if there are application notes that talk about reconstructing the TNGTLS certificate

@torntrousers
Copy link
Contributor Author

torntrousers commented Nov 12, 2021

... do you know if there are application notes that talk about reconstructing the TNGTLS certificate

How about section 4.5.3 in here: https://datasheet.octopart.com/ATSAMA5D27-WLSOM1-Microchip-datasheet-149595509.pdf

If thats not enough I have contacts in Microchip I can ask.

@rashedtalukder
Copy link

@gannaramu, the data sheet provided and looking at some of the function definitions in CryptoAuthLib, I can start to see how it comes together but it would take a while to trace through all the calls. I would start here though:
https://github.com/MicrochipTech/cryptoauthlib/blob/d5fcc6002ab070264696a0621f40c626861630eb/app/tng/tng_atcacert_client.c#L58-L99

@rashedtalukder
Copy link

Here is a feature request issue in BearSSL for the subsequent feature after this repository supports the device certificate and public key feature: arduino-libraries/ArduinoBearSSL#55

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants