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

RuntimeError: CRC Mismatch / ATECC608A not reachable via I2C anymore #16

Closed
rrottmann opened this issue Jun 19, 2020 · 15 comments
Closed
Labels
bug Something isn't working

Comments

@rrottmann
Copy link
Contributor

I used the following Adafruit sample code in order to initialize the ATECC608A chip connected to a Pi0 via I2C using GPIO 3 and 5.

import board
import busio
from adafruit_atecc.adafruit_atecc import ATECC, _WAKE_CLK_FREQ, CFG_TLS

import adafruit_atecc.adafruit_atecc_cert_util as cert_utils

# -- Enter your configuration below -- #

# Lock the ATECC module when the code is run?
LOCK_ATECC = True
# 2-letter country code
MY_COUNTRY = "DE"
# State or Province Name
MY_STATE = "Bavaria"
# City Name
MY_CITY = "Munich"
# Organization Name
MY_ORG = "Rottmann.IT"
# Organizational Unit Name
MY_SECTION = "Crypto"
# Which ATECC slot (0-4) to use
ATECC_SLOT = 0
# Generate new private key, or use existing key
GENERATE_PRIVATE_KEY = True

# -- END Configuration, code below -- #

# Initialize the i2c bus
i2c = busio.I2C(board.SCL, board.SDA, frequency=_WAKE_CLK_FREQ)

# Initialize a new atecc object
atecc = ATECC(i2c)

print("ATECC Serial Number: ", atecc.serial_number)

if not atecc.locked:
    if not LOCK_ATECC:
        raise RuntimeError(
            "The ATECC is not locked, set LOCK_ATECC to True in code.py."
        )
    print("Writing default configuration to the device...")
    atecc.write_config(CFG_TLS)
    print("Wrote configuration, locking ATECC module...")
    # Lock ATECC config, data, and otp zones
    atecc.lock_all_zones()
    print("ATECC locked!")

print("Generating Certificate Signing Request...")
# Initialize a certificate signing request with provided info
csr = cert_utils.CSR(
    atecc,
    ATECC_SLOT,
    GENERATE_PRIVATE_KEY,
    MY_COUNTRY,
    MY_STATE,
    MY_CITY,
    MY_ORG,
    MY_SECTION,
)
# Generate CSR
my_csr = csr.generate_csr()
print("-----BEGIN CERTIFICATE REQUEST-----\n")
print(my_csr.decode("utf-8"))
print("-----END CERTIFICATE REQUEST-----")

Basic tests like accessing the serial, the random number generator or the counter were successful.
However generating the CSR failed:

ATECC Serial Number:  01235AC6DE96E396EE
Writing default configuration to the device...
Wrote configuration, locking ATECC module...
ATECC locked!
Generating Certificate Signing Request...
Traceback (most recent call last):
  File "code.py", line 61, in <module>
    my_csr = csr.generate_csr()
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc_cert_util.py", line 91, in generate_csr
    self._csr_begin()
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc_cert_util.py", line 101, in _csr_begin
    self._atecc.gen_key(self._key, self._slot, self.private_key)
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 424, in gen_key
    self._get_response(key)
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 542, in _get_response
    raise RuntimeError("CRC Mismatch")
RuntimeError: CRC Mismatch

Since then I have no connection to the module anymore. i2cdetect recognizes an i2c device on 0x10 but every other call the device disappears again. Before the device was recognized as 0x60. There are no other I2C devices connected.

(venv) pi@raspberrypi:~/atecc608a $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
(venv) pi@raspberrypi:~/atecc608a $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- --
10: 10 -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --

Does anyone have an idea what happened here? Is there any chance to revive the ATECC608A?

@rrottmann
Copy link
Contributor Author

TLDR: Chip is working. I needed to modify adafruit_atecc.py:150 from 0x60 to 0x10 so that the ATECC608a gets detected again,

After a lengthy but insightful troubleshooting session I've got access to the ATECC608a. IMHO hours well spent as getting familiar with these secure elements was the main idea of tinkering with them.

Steps I did:

  • Checking SOIC8 solder joints for any error
  • Testing on Raspberry Pi Zero W and Pi 2B to rule out defective Raspberry Pi
  • Using a second ATECC608A to rule out defective chip
  • Using i2cdetect -y 1 to scan for the ATECC608A -> got disappearing device at address 0x10
  • Compiled and tested with https://github.com/wirenboard/atecc-util.git to rule out a Python issue
  • Debugging the Python code

Findings:

  • Most crypto chips are really hard to troubleshoot as info is sparse or locked behind NDAs
  • i2cdetect does not really work with ATECC608A as the chip uses power saving and after locking the chip, reads and writes result is intended errors
  • Personalisation of the ATECC608A by writing the CFG_TLS to the chip immediately changes the I2C slave address (to a random address??). In my case it was changed to 0x10 or 16dec. It is not 0x60 or 96dec anymore.
  • According to the datasheet, it should be possible to change this address even when configuration is locked.
  • I needed to modify adafruit_atecc.py:150 from 0x60 to 0x10 so that the ATECC608a gets detected again
  • It is important to verify the I2C frequencies on Raspberry Pi as I2C config is a mess
    • It may need to be adjusted according to the clock rate of the cpu and cpu frequency scaling can change it during runtime! /boot/config.txt needed the following settings: core_freq=250 and dtparam=i2c_arm=on,i2c_arm_baudrate=100000
 cat ../../i2cspeed.sh
#!/bin/bash
var="$(xxd -ps /sys/class/i2c-adapter/i2c-1/of_node/clock-frequency)"
var=${var//[[:blank:].\}]/}
printf "%d\n" 0x$var

@rrottmann
Copy link
Contributor Author

csr.generate_csr() still fails. Here is a debug output of the communication with the ATECC608a:

 python code.py
16 [16]  # <-- added by me. configured adress and list of detected i2c adresses
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x30', '0x0', '0x0', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0x0', '0x0', '0x60', '0x2', '0x80', '0x38']
14464 14464  # <-- added by me. crc, crc2 values
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x0', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0x1', '0x23', '0x11', '0xe2', '0xf7', '0x93']
37879 37879
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x2', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0xb5', '0x18', '0x20', '0x2a', '0x2b', '0xd1']
53547 53547
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x3', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0xee', '0x1', '0x61', '0x0', '0x1d', '0x1f']
7965 7965
ATECC Serial Number:  012311E2B518202AEE
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x15', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0x0', '0x0', '0x0', '0x0', '0x3', '0xad']
44291 44291
Generating Certificate Signing Request...
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x0', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0x1', '0x23', '0x11', '0xe2', '0xf7', '0x93']
37879 37879
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x2', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0xb5', '0x18', '0x20', '0x2a', '0x2b', '0xd1']
53547 53547
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x2', '0x0', '0x3', '0x0', '0x0', '0x0']
        Received:  ['0x7', '0xee', '0x1', '0x61', '0x0', '0x1d', '0x1f']
7965 7965
Command Packet Sz:  8
        Sending: ['0x3', '0x7', '0x40', '0x4', '0x0', '0x0', '0x0', '0x0']
        Received:  ['0x4', '0xf', '0x23', '0x42', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff', '0xff']
65535 4217
Traceback (most recent call last):
  File "code2.py", line 61, in <module>
    my_csr = csr.generate_csr()
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc_cert_util.py", line 91, in generate_csr
    self._csr_begin()
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc_cert_util.py", line 101, in _csr_begin
    self._atecc.gen_key(self._key, self._slot, self.private_key)
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 430, in gen_key
    self._get_response(key)
  File "/home/pi/atecc608a/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 550, in _get_response
    raise RuntimeError("CRC Mismatch")
RuntimeError: CRC Mismatch

@rrottmann
Copy link
Contributor Author

Reproduced the same output with latest Circuit Python, Adafruit Metro M4 Airlift board and a fresh ATECC608A (I2C version!) from another,reputable microchip vendor. As the I2C connection is now made with a microcontroller that has no pullup resistors on the board, I use 2x 4k7 R to pullup SCL and SDA lines to 3V3.

Using code from https://learn.adafruit.com/adafruit-atecc608-breakout/python-circuitpython

Excact same behaviour :-(

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ATECC Serial:  012363E9326927E4EE
Random Value:  1016
ATECC Counter #1 Value:  bytearray(b'\x01\x00\x00\x00')
Appending to the digest...
Appending to the digest...
SHA Digest:  bytearray(b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06')


Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ATECC Serial:  012363E9326927E4EE
Random Value:  1016
ATECC Counter #1 Value:  bytearray(b'\x04\x00\x00\x00')
Appending to the digest...
Appending to the digest...
SHA Digest:  bytearray(b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06')


code.py output:
ATECC Serial Number:  012363E9326927E4EE
Traceback (most recent call last):
  File "code.py", line 39, in <module>
RuntimeError: The ATECC is not locked, set LOCK_ATECC to True in code.py.



Press any key to enter the REPL. Use CTRL-D to reload.



Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ATECC Serial Number:  012363E9326927E4EE
Writing default configuration to the device...
Wrote configuration, locking ATECC module...
ATECC locked!
Generating Certificate Signing Request...
Traceback (most recent call last):
  File "code.py", line 61, in <module>
  File "adafruit_atecc/adafruit_atecc_cert_util.py", line 91, in generate_csr
  File "adafruit_atecc/adafruit_atecc_cert_util.py", line 101, in _csr_begin
  File "adafruit_atecc/adafruit_atecc.py", line 424, in gen_key
  File "adafruit_atecc/adafruit_atecc.py", line 542, in _get_response
RuntimeError: CRC Mismatch

After reboot of the Metro M4, the ATECC608A is not recognizeable anymore...

Press any key to enter the REPL. Use CTRL-D to reload.soft reboot

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
Traceback (most recent call last):
  File "code.py", line 9, in <module>
  File "adafruit_atecc/adafruit_atecc.py", line 134, in __init__
  File "adafruit_atecc/adafruit_atecc.py", line 165, in wakeup
IndexError: ATECCx08 not found - please check your wiring!

Running CircuitPython demo - I2C scan yields no devices on the I2C bus.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
I2C addresses found: []
I2C addresses found: []

@rrottmann
Copy link
Contributor Author

As with the tests on Raspberry Pi 2, I needed to change the address of my ATECC608A device in adafruit_atecc.py

-_REG_ATECC_ADDR = const(0xC0)
+_REG_ATECC_ADDR = const(0x90)
-_REG_ATECC_DEVICE_ADDR = _REG_ATECC_ADDR >> 1
+_REG_ATECC_DEVICE_ADDR = 0x10

Looking into the ATECC508A Complete Datasheet, I found the information of how to determine the device address and the configuration byte:

1100 0000 0xc0 192dec 8bit with ic2 on cfg byte
0110 0000 0x60 96dec  7bit with i2c on addr (default)

1001 0000 0x90 8bit 144dec with i2c on cfg byte
0001 0000 0x10 7bit 16dec  with i2c on addr (my chip)

Now the devices also get recognized on my Adafruit METRO M4 Airlift Lite.

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ATECC Serial Number:  01235AC6DE96E396EE
Generating Certificate Signing Request...
Traceback (most recent call last):
  File "code.py", line 61, in <module>
  File "adafruit_atecc/adafruit_atecc_cert_util.py", line 91, in generate_csr
  File "adafruit_atecc/adafruit_atecc_cert_util.py", line 101, in _csr_begin
  File "/lib/adafruit_atecc/adafruit_atecc.py", line 430, in gen_key
  File "/lib/adafruit_atecc/adafruit_atecc.py", line 548, in _get_response
RuntimeError: CRC Mismatch

And the second device behaves the same:

Auto-reload is on. Simply save files over USB to run them or enter REPL to disable.
code.py output:
ATECC Serial Number:  012363E9326927E4EE
Generating Certificate Signing Request...
Traceback (most recent call last):
  File "code.py", line 61, in <module>
  File "adafruit_atecc/adafruit_atecc_cert_util.py", line 91, in generate_csr
  File "adafruit_atecc/adafruit_atecc_cert_util.py", line 101, in _csr_begin
  File "/lib/adafruit_atecc/adafruit_atecc.py", line 430, in gen_key
  File "/lib/adafruit_atecc/adafruit_atecc.py", line 548, in _get_response
RuntimeError: CRC Mismatch

The CRC error when reading the certificate signing request from the microchip however remains...

@rrottmann
Copy link
Contributor Author

I've initialized yet another ATECC608A (running short now) with the sample config from
https://github.com/MicrochipTech/cryptoauthtools/blob/master/python/examples/config.py

With that config, I can work with the Adafruit CircuitPython ATECC library and atecc-util without any isues.

I've dumped both configs for easy comparison. According to the datasheet, byte 16 is the I2C address.

16 I2C_Address When I2C_Enable<0> is one, this field is the I2C_Address with a
default value of 0xC0.

It seems that this adress can be changed one more time also when configuration is locked. The bitwise notation is a little bit cumbersome to understand. Only 7 bits are the address. The write is done with the UpdateExtra command.

I think that I can update my other chips with the default I2C address and they might work.

MSB/LSB notation, Hex, dec and bin notation in different source codes and reference of the actual byte in the cfg zone or only
the 7bits that are for the address code make it difficult to assemble the UpdateExtra command to send to the chip...

Working Config zone dumped with atecc-util:
git/atecc-util/atecc -b 1 -c "dump-config -" | head -34
============= Config zone dump: =============

000: 01         001: 23         002: 46         003: 2B
004: 00         005: 00         006: 60         007: 02
008: 28         009: 24         010: 88         011: 21
012: EE         013: 01         014: 55         015: 00
016: C0         017: 00         018: 00         019: 01
020: 85         021: 00         022: 82         023: 00
024: 85         025: 20         026: 85         027: 20
028: 85         029: 20         030: C6         031: 46
032: 8F         033: 0F         034: 9F         035: 8F
036: 0F         037: 0F         038: 8F         039: 0F
040: 0F         041: 0F         042: 0F         043: 0F
044: 0F         045: 0F         046: 0F         047: 0F
048: 0D         049: 1F         050: 0F         051: 0F
052: FF         053: FF         054: FF         055: FF
056: 00         057: 00         058: 00         059: 00
060: FF         061: FF         062: FF         063: FF
064: 00         065: 00         066: 00         067: 00
068: 00         069: 00         070: 03         071: F7
072: 00         073: 69         074: 76         075: 00
076: 00         077: 00         078: 00         079: 00
080: 00         081: 00         082: 00         083: 00
084: 00         085: 00         086: 00         087: 00
088: FF         089: FF         090: 0E         091: 60
092: 00         093: 00         094: 00         095: 00
096: 53         097: 00         098: 53         099: 00
100: 73         101: 00         102: 73         103: 00
104: 73         105: 00         106: 38         107: 00
108: 7C         109: 00         110: 1C         111: 00
112: 3C         113: 00         114: 1A         115: 00
116: 3C         117: 00         118: 30         119: 00
120: 3C         121: 00         122: 30         123: 00
124: 12         125: 00         126: 30         127: 00


Converted TLS_CFG
=============       TLS_CFG     =============

000: 01         001: 23         002: 00         003: 00
004: 00         005: 00         006: 50         007: 00
008: 00         009: 00         010: 00         011: 00
012: 00         013: c0         014: 71         015: 00
016: 20         017: 20         018: 20         019: 20
020: 20         021: 20         022: 20         023: 20
024: 20         025: 20         026: 20         027: 20
028: 20         029: c0         030: 00         031: 55
032: 00         033: 83         034: 20         035: 87
036: 20         037: 87         038: 20         039: 87
040: 2f         041: 87         042: 2f         043: 8f
044: 8f         045: 9f         046: 8f         047: af
048: 20         049: 20         050: 20         051: 20
052: 20         053: 20         054: 20         055: 20
056: 20         057: 20         058: 20         059: 20
060: 20         061: 8f         062: 00         063: 00
064: 00         065: 00         066: 00         067: 00
068: 00         069: 00         070: 00         071: 00
072: 00         073: 00         074: 00         075: 00
076: 20         077: 20         078: 20         079: 20
080: 20         081: 20         082: 20         083: 20
084: 20         085: 20         086: 20         087: 20
088: 20         089: af         090: 8f         091: ff
092: ff         093: ff         094: ff         095: 00
096: 00         097: 00         098: 00         099: ff
100: ff         101: ff         102: ff         103: 00
104: 20         105: 20         106: 20         107: 20
108: 20         109: 20         110: 20         111: 20
112: 20         113: 20         114: 20         115: 20
116: 20         117: 00         118: 00         119: 00
120: ff         121: ff         122: ff         123: ff
124: ff         125: ff         126: ff         127: ff

@evaherrada evaherrada added the bug Something isn't working label Jun 30, 2020
@evaherrada
Copy link
Collaborator

@rrottmann You figured out a fix, right? If so, a PR would be greatly appreciated. Thanks!

@rrottmann
Copy link
Contributor Author

Unfortunately, my "fix" right now is just the initialization with the help of another tool. I am not sure of the meaning of all the configuration bytes. So I cannot provide valid settings that should be used instead. I assume that either the Adafruit module has been initialized during manufacturing and maybe already contains settings that are not set via the Python code. I used factory-default modules from different sources and "bricked" them. I was not able/did not try to change the I2C address. This needs the help of a bus pirate and raw commands and you only have one try to set it correctly.

@rrottmann
Copy link
Contributor Author

I wrote a small gist with notes of how I configure an ATECC608A for tinkering/development purposes and to document what I learned about the I2C configuration: https://gist.github.com/rrottmann/292d1fb7f30b448e96cb4dedadcaa213

@rrottmann
Copy link
Contributor Author

I just tested the latest code with a Raspberry Pi Zero and an ATECC608A module on i2c 0x35. I've documented what I did to change the code to work with this device:

Test with Raspberry Pi Zero

# raspbian buster lite
sudo apt update
sudo apt install -y i2c-tools python3-venv python3-pip build-essential cmake git libusbhid-common libusb-1.0-0-dev libhidapi-dev
cd
mkdir git
cd git
git clone https://github.com/adafruit/Adafruit_CircuitPython_ATECC.git
cd Adafruit_CircuitPython_ATECC/
python3 -m venv venv
source venv/bin/activate
pip3 install wheel
pip3 install -r requirements.txt
i2cdetect -y 1
#     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
#00:          -- -- -- -- -- -- -- -- -- -- -- -- --
#10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#30: -- -- -- -- -- 35 -- -- -- -- -- -- -- -- -- --
#40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
#70: -- -- -- -- -- -- -- --
pip3 freeze
#Adafruit-Blinka==6.3.0
#adafruit-circuitpython-binascii==1.2.5
#adafruit-circuitpython-busdevice==5.0.6
#Adafruit-PlatformDetect==3.2.0
#Adafruit-PureIO==1.1.8
#pkg-resources==0.0.0
#pyftdi==0.52.9
#pyserial==3.5
#pyusb==1.1.1
#rpi-ws281x==4.2.5
#RPi.GPIO==0.7.0
#sysv-ipc==1.1.0
python3 setup.py bdist_wheel
# patch the i2c address if yours is different to 0x60 (in this case 0x35)
cat > atecc.patch <<"PATCH"
--- atecc.patch 2021-03-04 21:21:07.229852937 +0000
+++ atecc.patch2        2021-03-04 21:20:52.339761410 +0000
@@ -3,13 +3,13 @@
 --- a/adafruit_atecc/adafruit_atecc.py
 +++ b/adafruit_atecc/adafruit_atecc.py
 @@ -59,7 +59,7 @@ def _convert_i2c_addr_to_atecc_addr(i2c_addr=0x60):
-
-
+
+
  # Device Address
 -_I2C_ADDR = 0x60
 +_I2C_ADDR = 0x35
  _REG_ATECC_ADDR = _convert_i2c_addr_to_atecc_addr(i2c_addr=_I2C_ADDR)
-
+
  _REG_ATECC_DEVICE_ADDR = _REG_ATECC_ADDR >> 1
 diff --git a/examples/atecc_simpletest.py b/examples/atecc_simpletest.py
 index 088f48e..403e179 100644
@@ -17,10 +17,10 @@
 +++ b/examples/atecc_simpletest.py
 @@ -9,7 +9,7 @@ from adafruit_atecc.adafruit_atecc import ATECC, _WAKE_CLK_FREQ
  i2c = busio.I2C(board.SCL, board.SDA, frequency=_WAKE_CLK_FREQ)
-
+
  # Initialize a new atecc object
 -atecc = ATECC(i2c)
 +atecc = ATECC(i2c, address=0x35, debug=False)
-
+
  print("ATECC Serial: ", atecc.serial_number)
-
+
PATCH

# force reinstall in case original wheel already installed during testing
pip3 install --upgrade --force-reinstall dist/adafruit_circuitpython_atecc-1.2.6-py3-none-any.whl
 python3 examples/atecc_simpletest.py
ATECC Serial:  0123E*************
Random Value:  525
ATECC Counter #1 Value:  bytearray(b'\t\x00\x00\x00')
Appending to the digest...
Appending to the digest...
SHA Digest:  bytearray(b'\x03\x1e\xdd}Ae\x15\x93\xc5\xfe\\\x00o\xa5u+7\xfd\xdf\xf7\xbcN\x84:\xa6\xaf\x0c\x95\x0fK\x94\x06')

@dexterac
Copy link

dexterac commented Feb 8, 2022

I tried this and still unable to create CSR and run the simpletest after first time CSR creation failed


(venv) pi@anupi01:~/git/Adafruit_CircuitPython_ATECC $ pip list -v
Package                          Version   Location                                                                   Installer
-------------------------------- --------- -------------------------------------------------------------------------- ---------
Adafruit-Blinka                  6.20.1    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
adafruit-circuitpython-atecc     1.2.9     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
adafruit-circuitpython-binascii  1.2.8     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
adafruit-circuitpython-busdevice 5.1.2     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
adafruit-circuitpython-lis3dh    5.1.13    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
Adafruit-PlatformDetect          3.19.5    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
Adafruit-PureIO                  1.1.9     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
certifi                          2021.10.8 /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
charset-normalizer               2.0.11    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
circuitpython-build-tools        1.9.0     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
click                            8.0.3     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
idna                             3.3       /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
importlib-metadata               4.10.1    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
pip                              22.0.3    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
pkg_resources                    0.0.0     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
pyftdi                           0.53.3    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
pyserial                         3.5       /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
pyusb                            1.2.1     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
requests                         2.27.1    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
RPi.GPIO                         0.7.1     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
rpi-ws281x                       4.3.1     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
semver                           2.13.0    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
setuptools                       60.8.1    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
sysv-ipc                         1.1.0     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
typing_extensions                4.0.1     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
urllib3                          1.26.8    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
wheel                            0.37.1    /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
zipp                             3.7.0     /home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages pip
(venv) pi@anupi01:~/git/Adafruit_CircuitPython_ATECC $ python examples/atecc_simpletest.py 
Traceback (most recent call last):
  File "examples/atecc_simpletest.py", line 12, in <module>
    atecc = ATECC(i2c)
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 169, in __init__
    if (self.version() >> 8) not in (_ATECC_508_VER, _ATECC_608_VER):
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 241, in version
    self.idle()
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/adafruit_atecc/adafruit_atecc.py", line 195, in idle
    i2c.write(self._i2cbuf, end=1)
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/adafruit_bus_device/i2c_device.py", line 84, in write
    self.i2c.writeto(self.device_address, buf, start=start, end=end)
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/busio.py", line 165, in writeto
    return self._i2c.writeto(address, memoryview(buffer)[start:end], stop=stop)
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/adafruit_blinka/microcontroller/generic_linux/i2c.py", line 49, in writeto
    self._i2c_bus.write_bytes(address, buffer[start:end])
  File "/home/pi/git/Adafruit_CircuitPython_ATECC/venv/lib/python3.7/site-packages/Adafruit_PureIO/smbus.py", line 314, in write_bytes
    self._device.write(buf)
OSError: [Errno 121] Remote I/O error
(venv) pi@anupi01:~/git/Adafruit_CircuitPython_ATECC $ 

(venv) pi@anupi01:~/git/Adafruit_CircuitPython_ATECC $ i2cdetect -y 1
     0  1  2  3  4  5  6  7  8  9  a  b  c  d  e  f
00:          -- -- -- -- -- -- -- -- -- -- -- -- -- 
10: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
20: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
30: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
40: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
50: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
60: 60 -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- 
70: -- -- -- -- -- -- -- --                         

@smalltoe
Copy link

smalltoe commented Apr 23, 2022

@dexterac were you able to solve this? I have exatctly the same issue with raspberry and feather M4.

@dexterac
Copy link

dexterac commented Apr 30, 2022 via email

@smalltoe
Copy link

smalltoe commented May 4, 2022

Many Thanks for this but unfortunately so far I was not very lucky. Two questions - if I may:

  • which Python are you using?
  • do you run the script against a new chip or one that is already locked?

@dexterac
Copy link

dexterac commented May 5, 2022

Python 3.8 and 3.9 both worked for me.

This worked with already locked chip. To configure a new chip I used this


cat << EOF > /var/lib/cryptoauthlib/0.conf 
interface = i2c,0x6A,1

# Configure the device type - base part number (optional)
#device = ATECC608A-TFLXTLS

#Configure open slots for additional pkcs11 objects (optional)
freeslots = 1,2,3

# Manually configure keys into device locations (slots/handles)

# Slot 0 is the primary private key
object = private,device,0

# Slot 15 is a public key
#object = public,root,15
EOF

mkdir -p /etc/pkcs11
sudo mkdir /etc/pkcs11
sudo bash -c 'cat << EOF > /etc/pkcs11/pkcs11.conf
# This setting controls whether to load user configuration from the
# ~/.config/pkcs11 directory. Possible values:
#    none: No user configuration
#    merge: Merge the user config over the system configuration (default)
#    only: Only user configuration, ignore system configuration
user-config: merge
EOF'


p11tool --list-all 'pkcs11:model=ATECC608A'
ls /usr/lib/libcryptoauth.so
sudo updatedb
locate libcryptoauth.so
sudo bash -c 'cat << EOF > /usr/share/p11-kit/modules/cryptoauthlib.module
module: /usr/lib/aarch64-linux-gnu/libcryptoauth.so
critical: yes
trust-policy: yes
managed: yes
log-calls: no
EOF
'
p11tool --list-all 'pkcs11:model=ATECC608A'

@dexterac
Copy link

dexterac commented May 5, 2022

sudo apt install -y cmake libudev-dev p11-kit bc
mkdir secure_elements
cd secure_elements/
pip install cryptoauthlib
git clone https://github.com/MicrochipTech/cryptoauthlib.git
git clone https://github.com/MicrochipTech/cryptoauthtools.git
cd cryptoauthtools/python/examples/
python info.py -i i2c
python info.py -i i2c -p address=6a

cd ~/secure_elements/cryptoauthlib/
mkdir build
cd build
cmake -DATCA_PKCS11:STRING=ON -DATCA_HAL_I2C=ON ..
cmake -DATCA_ATECC608A_SUPPORT=ON -DATCA_TFLEX_SUPPORT=on -DATCA_OPENSSL=ON -DATCA_HAL_KIT_HID=ON -DATCA_PKCS11:STRING=ON -DATCA_HAL_I2C=ON ../
cmake -LAH -B build .|grep -i i2c
cmake --build .
cmake .
make
sudo make install


echo "0x$(bc <<< 'obase = 16; ibase = 16; 35 * 2')"

cat << EOF > /var/lib/cryptoauthlib/0.conf 
interface = i2c,0x6A,1

# Configure the device type - base part number (optional)
#device = ATECC608A-TFLXTLS

#Configure open slots for additional pkcs11 objects (optional)
freeslots = 1,2,3

# Manually configure keys into device locations (slots/handles)

# Slot 0 is the primary private key
object = private,device,0

# Slot 15 is a public key
#object = public,root,15
EOF

mkdir -p /etc/pkcs11
sudo mkdir /etc/pkcs11
sudo bash -c 'cat << EOF > /etc/pkcs11/pkcs11.conf
# This setting controls whether to load user configuration from the
# ~/.config/pkcs11 directory. Possible values:
#    none: No user configuration
#    merge: Merge the user config over the system configuration (default)
#    only: Only user configuration, ignore system configuration
user-config: merge
EOF'


p11tool --list-all 'pkcs11:model=ATECC608A'
ls /usr/lib/libcryptoauth.so
sudo updatedb
locate libcryptoauth.so
sudo bash -c 'cat << EOF > /usr/share/p11-kit/modules/cryptoauthlib.module
module: /usr/lib/aarch64-linux-gnu/libcryptoauth.so
critical: yes
trust-policy: yes
managed: yes
log-calls: no
EOF
'
p11tool --list-all 'pkcs11:model=ATECC608A'

Finally to export public key, CSR, and new keys for sign/verify



#/bin/bash

# https://github.com/MicrochipTech/cryptoauthlib/wiki/PKCS11-Linux-Setup
# View public key
sudo p11tool --export-pubkey "pkcs11:model=ATECC608A;manufacturer=Microchip%20Technology%20Inc;serial=23B201E36871FFEE;object=device;type=private;token=00ABC"

# Export the public key
sudo p11tool --export-pubkey "pkcs11:model=ATECC608A;manufacturer=Microchip%20Technology%20Inc;serial=23B201E36871FFEE;object=device;type=private;token=00ABC"|openssl pkey -pubin -out atecc608a.pubkey

# Create a new CSR
sudo openssl req -engine pkcs11 -key "pkcs11:token=00ABC;object=device;type=private" -keyform engine -new -out new_device.csr -subj "/CN=minimulus cloud csr for anupi01"


# https://github.com/MicrochipTech/cryptoauthlib/wiki/OpenSSL-and-ATECC-Sign-Verify
sudo openssl req -in new_device.csr -verify -text -noout

sudo openssl ecparam -genkey -out key.pem -name prime256v1

sudo openssl ec -in key.pem -noout -text

sudo openssl dgst -sha256 -sign key.pem -out signature.der 01_update_routemetric.sh

sudo openssl ec -in key.pem -pubout -out pub-key.pem

sudo openssl dgst -sha256 -verify pub-key.pem -signature signature.der 01_update_routemetric.sh

sudo openssl dgst -sha256 -out message-digest.txt 01_update_routemetric.sh

sudo openssl asn1parse -inform DER -in signature.der

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

4 participants