Configuring the Security Chip

The device integrates a security chip (Microchip ATSHA204A). The specific use of the security chip depends on the user's actual upper-layer application. Since each customer's requirements differ, the following content only covers downloading/compiling the crypto library source code and a test example for the security chip.

Preparation:

The device has started up normally.

Steps:

  1. Modify the config.txt configuration file to enable I2C and the security chip.

a. After the device starts up normally, open a terminal window and execute the following command to open the config.txt configuration file.

sudo nano /boot/firmware/config.txt

b. Add the following content to the configuration file.

dtparam=i2c_arm=on

c. Press Ctrl+O to save the file, then press Enter, and finally press Ctrl+X to exit the file editing mode.

d. Execute the following command to restart the device and apply the configuration.

 ```sh
 sudo reboot
 ```
  1. (Optional) Obtain the CryptoAuthLib v3.7.0 crypto library source code from GitHub at the following path: CryptoAuthLib v3.7.0.

  2. (Optional) Compile the downloaded crypto library source code on the device.

mkdir build && cd build
cmake .. -DATCA_HAL_I2C=ON -DATCA_BUILD_SHARED_LIBS=on -DATCA_USE_ATCAB_FUNCTIONS=ON 
make -j4
sudo make install
  1. Install Python so that it can import cryptoauthlib.
cd ~/cryptoauthlib-3.7.0/python
sudo cp -r cryptoauthlib/ $(python3 -c "import site; print(site.getsitepackages()[0])")
  1. Test whether the security chip is functioning properly using the sample code.
#!/usr/bin/env python3
"""
CryptoAuthLib 3.7.0 + ATSHA204A @ i2c-1 0x64
"""
import ctypes

from cryptoauthlib.library import load_cryptoauthlib,AtcaReference
load_cryptoauthlib()

from cryptoauthlib.atcab import (
    atcab_init, atcab_release,
    atcab_info, atcab_read_serial_number,
    atcab_random, atcab_is_locked
)
# from cryptoauthlib.iface import cfg_ateccx08a_i2c_default
from cryptoauthlib.iface import cfg_atsha20xa_i2c_default   

cfg = cfg_atsha20xa_i2c_default()

cfg.cfg.atcai2c.u.slave_address = 0x64 << 1 
cfg.cfg.atcai2c.bus = 1  
print(f"slave_address = 0x{cfg.cfg.atcai2c.u.slave_address:02X}")

st = atcab_init(cfg)
if st != 0:
    print(f"❌ atcab_init failed: {st}")
    exit(1)

print("ATSHA204A@0x64 initialized ok.")

rev = bytearray(4)
atcab_info(rev)
print(f"Rev : {rev.hex()}")

sn = bytearray(9)
atcab_read_serial_number(sn)
print(f"SN  : {sn.hex()}")

rnd = bytearray(32)
atcab_random(rnd)
print(f"RND : {rnd[:8].hex()}...")

is_locked_cfg = AtcaReference(ctypes.c_bool(False))

status1 = atcab_is_locked(0x00, is_locked_cfg)
if status1 == 0:
    cfg_lock = is_locked_cfg.value
    print(f"Config localed: {cfg_lock}")
else:
    print(f"Config lock status read failed: {status1}")

atcab_release()
print("Test completed.")

TIP

  • CryptoAuthLib v3.7.0 provides stable support for ATSHA204A with complete functionality, so it is recommended to use the CryptoAuthLib v3.7.0 version of the crypto library source code.
  • If using our company's pre-installed operating system, the CryptoAuthLib v3.7.0 crypto library source code is already installed and compiled by default, so steps 1 and 2 can be skipped.
  • If users install the standard Raspberry Pi OS themselves, they need to perform steps 1 through 4.