配置加密芯片
设备内部集成加密芯片(Microchip ATECC608A),加密芯片的具体使用和用户实际的上层应用有关;因为每个客户的需求不同,故下文仅介绍加密芯片对应的加密库源码下载/编译和加密芯片的测试示例。
前提条件:
设备已正常启动。
操作步骤:
修改
config.txt配置文件,使能I2C和加密芯片。a. 设备正常启动后,打开命令窗格,执行如下命令,打开
config.txt配置文件。sudo nano /boot/firmware/config.txtb. 在配置文件中添加如下内容。
dtparam=i2c_arm=on

c. 输入ctrl+o保存文件,再按Enter,最后输入ctrl+x退出文件编辑模式。
d. 执行如下命令重启设备,使配置生效。
sudo reboot
(可选)在GitHub上获取CryptoAuthLib v3.7.0版本的加密库源码,路径为:CryptoAuthLib v3.7.0。
(可选)在设备上对下载的加密库源码进行编译。
mkdir build && cd build
cmake .. -DATCA_HAL_I2C=ON -DATCA_BUILD_SHARED_LIBS=on -DATCA_USE_ATCAB_FUNCTIONS=ON
make -j4
sudo make install
- 安装Python,使Python能导入cryptoauthlib。
cd ~/cryptoauthlib-3.7.0/python
sudo cp -r cryptoauthlib/ $(python3 -c "import site; print(site.getsitepackages()[0])")
- 通过示例代码来测试加密芯片是否可以正常使用。
#!/usr/bin/env python3
"""
CryptoAuthLib 3.7.0 + ATECC608A @ 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_ateccx08a_i2c_default
cfg = cfg_ateccx08a_i2c_default()
cfg.cfg.atcai2c.u.slave_address = 0x60 << 1
cfg.cfg.atcai2c.bus = 10
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("ATECC608A@0x60 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.")
提示
- CryptoAuthLib v3.7.0对ATECC608A的支持较稳定,且功能完整,故建议使用CryptoAuthLib v3.7.0版本的加密库源码。
- 若使用的是我司预安装的操作系统,则默认已安装CryptoAuthLib v3.7.0版本的加密库源码并完成源码编译,故可跳过步骤1和步骤2。
- 若用户自行安装标准的Raspberry Pi OS,则需要执行步骤1~步骤4。
