dmazzella / ucryptoauthlib

Lightweight driver for Microchip's Crypto Authentication secure elements written in pure python for micropython
MIT License
41 stars 9 forks source link

Error in constructor of ATECCX08A #2

Closed chrisb2 closed 3 years ago

chrisb2 commented 4 years ago

I am building Micropython for ESP32 as per the instructions https://github.com/micropython/micropython/tree/master/ports/esp32, having first copied the files in modules/cryptoauthlib to the modules/cryptoauthlib in the esp32 directory. This builds successfully and I can see it compiling the cryptoauthlib files. I then deployed this and attempt to test that the device is working. I confirmed that its visible on I2C bus with

from machine import Pin, I2C

scl = Pin(5)
sda = Pin(18)
i2c = I2C(-1, scl, sda)
print(i2c.scan())

which gave 96, which is correct. I then tried to initialize the device with:

from cryptoauthlib.device import ATECCX08A
from machine import Pin, I2C

device = ATECCX08A(bus=I2C(-1, Pin(5), Pin(18), freq=133000))

but this fails with:

Traceback (most recent call last):
  File "<stdin>", line 4, in <module>
  File "cryptoauthlib/device.py", line 36, in __init__
  File "cryptoauthlib/packet.py", line 59, in __getitem__
IndexError: memoryview index out of range

I actually have your library working correctly on another esp32 and device, but unfortunately I did not keep a copy of this binary. I have the swapped the devices between the esp32's and they both work correctly with my first esp32. I have tried this new binary on two different esp32's and it gives the same error on both.

Any suggests on how to fix this? Thx

Chris

dmazzella commented 4 years ago

Hi Chris, the version of micropython on both boards is the same? I haven't an esp32, i can't test in hardware different of stm32, if you can please give me more software setup info.

P.S. try lowering the frequency of then i2c bus.

regards, D.

chrisb2 commented 4 years ago

Yes same version of micropython, have already tried lowering frequency.

Micropython gives following system info on start: MicroPython v1.12-357-g740946736 on 2020-11-13; ESP32 module with ESP32

This is not now the latest version, so I am going to update before investigating some more.

Chris

chrisb2 commented 4 years ago

I update version of Micropython, so now on 1.13:

MicroPython v1.13 on 2020-11-15; ESP32 module with ESP32

I have managed to work around the error by hardcoding the device at line 36 of device.py:

            self._device = "ATECC508A"  # SUPPORTED_DEVICES[self.atcab_info()[1+2]]

I can then create an instance of ATECCX08A with no error. What is interesting is that if I then call _atcabinfo() I get no response_data bytes, so that is no doubt what is causing the error:

>>> device.atcab_info()
<ATCAPacket txsize=7 opcode=0x30 param1=0x00 param2=0x0000 request_data= response_data= device=ATECC508A>

But the question is why is there no response data?

Chris