【随笔记】ATECC608 加密芯片调试记录
芯片通信测试
根据芯片手册,ATECC608B 的 7bit 器件地址是:0x35
root@linux:/usr/bin# 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: -- -- -- -- -- -- -- -- -- -- -- -- -- UU -- --
50: -- UU 52 53 -- -- -- -- -- -- -- -- -- -- -- --
60: -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- --
70: -- -- -- -- -- -- -- --
下载必要文件
大多数加密芯片都是由原厂提供库文件,便于降低开发难度。
官网下载:CryptoAuthLib
https://www.microchip.com/en-us/software-library/cryptoauthlib
https://codeload.github.com/MicrochipTech/cryptoauthlib/zip/refs/tags/v3.3.3
编译库文件的版本只需要最低 cmake 2.6.4 版本,但是测试程序最低 cmake 3.10
官网下载:
https://cmake.org/files/v3.10/cmake-3.10.3-Linux-x86_64.tar.gz
解压并设置好环境变量:
export PATH=/home/lmx/work/tmp/cmake-3.10.3-Linux-x86_64/bin:$PATH
移植编译源码
配置编译工具链
建立新的配置文件,指定编译工具链的位置:xxx_linux_toolchain.cmake
set(CMAKE_SYSTEM_NAME Linux)
set(CMAKE_SYSTEM_PROCESSOR arm)
set(CMAKE_TOOLCHAIN_PATH /home/lmx/work/xxx/lichee/out/gcc-linaro-5.3.1-2016.05/gcc-aarch64)
set(CMAKE_C_COMPILER ${CMAKE_TOOLCHAIN_PATH}/bin/aarch64-linux-gnu-gcc)
set(CMAKE_CXX_COMPILER ${CMAKE_TOOLCHAIN_PATH}/bin/arm-linux-gnueabihf-g++)
编译动态链接库
通过生成 Makefile:
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3$ mkdir build && cd build
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3/build$ cmake -DCMAKE_TOOLCHAIN_FILE=xxx_linux_toolchain.cmake -DATCA_HAL_I2C=ON ../
-- The C compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/lmx/work/xxx/lichee/out/gcc-linaro-5.3.1-2016.05/gcc-aarch64/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /home/lmx/work/xxx/lichee/out/gcc-linaro-5.3.1-2016.05/gcc-aarch64/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Looking for malloc
-- Looking for malloc - found
-- Looking for free
-- Looking for free - found
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lmx/work/xxx/application/cryptoauthlib-3.3.3/build
编译动态库文件:
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3/build$ make
Scanning dependencies of target cryptoauth
[ 1%] Building C object lib/CMakeFiles/cryptoauth.dir/atca_basic.c.o
[ 2%] Building C object lib/CMakeFiles/cryptoauth.dir/atca_cfgs.c.o
......
[ 95%] Building C object lib/CMakeFiles/cryptoauth.dir/hal/atca_hal.c.o
[ 97%] Building C object lib/CMakeFiles/cryptoauth.dir/hal/hal_linux.c.o
[ 98%] Building C object lib/CMakeFiles/cryptoauth.dir/hal/hal_linux_i2c_userspace.c.o
[100%] Linking C shared library libcryptoauth.so
[100%] Built target cryptoauth
得到两个关键文件:
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3/build$ ls -l lib/*.h lib/*.so
-rw-r--r-- 1 lmx lmx 2923 11月 4 11:20 lib/atca_config.h
-rwxrwxr-x 1 lmx lmx 927371 11月 4 14:02 lib/libcryptoauth.so
编译可执行程序
指定动态链接库的位置:
--- cryptoauthlib-3.3.3\test\CMakeLists.txt
+++ cryptoauthlib-3.3.3\test\CMakeLists.txt
@@ -52,13 +52,13 @@
${CMAKE_CURRENT_SOURCE_DIR}/../lib
${CMAKE_CURRENT_SOURCE_DIR}/../third_party
${CMAKE_CURRENT_SOURCE_DIR}/../third_party/mbedtls/include
${CMAKE_CURRENT_SOURCE_DIR}/../third_party/wolfssl
${CMAKE_CURRENT_BINARY_DIR}/../lib)
-target_link_libraries(cryptoauth_test cryptoauth)
+target_link_libraries(cryptoauth_test ${CMAKE_CURRENT_BINARY_DIR}/../lib/libcryptoauth.so)
if(UNIX)
target_link_libraries(cryptoauth_test pthread)
endif()
if(ATCA_BUILD_SHARED_LIBS)
编译可执行程序:
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3/build$ mkdir bin && cd bin
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3/build/bin$ cmake -DCMAKE_TOOLCHAIN_FILE=../xxx_linux_toolchain.cmake ../../test/
-- The C compiler identification is GNU 4.9.3
-- Check for working C compiler: /home/lmx/work/xxx/lichee/out/gcc-linaro-5.3.1-2016.05/gcc-aarch64/bin/aarch64-linux-gnu-gcc
-- Check for working C compiler: /home/lmx/work/xxx/lichee/out/gcc-linaro-5.3.1-2016.05/gcc-aarch64/bin/aarch64-linux-gnu-gcc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Configuring done
-- Generating done
-- Build files have been written to: /home/lmx/work/xxx/application/cryptoauthlib-3.3.3/build/bin
lmx@lmx:~/work/xxx/application/cryptoauthlib-3.3.3/build/bin$ make
Scanning dependencies of target cryptoauth_test
[ 1%] Building C object CMakeFiles/cryptoauth_test.dir/atca_crypto_sw_tests.c.o
[ 2%] Building C object CMakeFiles/cryptoauth_test.dir/atca_test.c.o
......
[ 98%] Building C object CMakeFiles/cryptoauth_test.dir/home/lmx/work/xxx/application/cryptoauthlib-3.3.3/third_party/unity/unity_memory.c.o
[100%] Linking C executable cryptoauth_test
[100%] Built target cryptoauth_test
测试芯片功能:
执行测试程序(器件地址是 0x35,传给测试程序需要左移一位得到 0x6a):
root@linux:~# ./cryptoauth_test info -d ecc608 -i i2c 1 -a 0x6a
revision:
00 00 60 03
root@linux:~# ./cryptoauth_test sernum -d ecc608 -i i2c 1 -a 0x6a
serial number:
01 23 1A 41 5D 5C E7 AF 01