Skip to content

Commit 858cf82

Browse files
committed
initial commit
1 parent f8a9c11 commit 858cf82

File tree

5 files changed

+675
-0
lines changed

5 files changed

+675
-0
lines changed

CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 3.10)
2+
3+
# Project name
4+
project(ATECC608_RPI_Handler)
5+
6+
# Set C++ standard
7+
set(CMAKE_CXX_STANDARD 11)
8+
9+
# Find the cryptoauthlib library
10+
find_library(CRYPTOAUTH_LIB cryptoauth HINTS /usr/lib /usr/local/lib)
11+
12+
# Find the cryptoauthlib include directory
13+
find_path(CRYPTOAUTH_INCLUDE_DIR
14+
NAMES atca_basic.h
15+
HINTS /usr/include /usr/local/include /usr/include/cryptoauthlib /usr/local/include/cryptoauthlib
16+
)
17+
18+
# If the library or include directory is not found, display an error message
19+
if(NOT CRYPTOAUTH_LIB)
20+
message(FATAL_ERROR "cryptoauth library not found")
21+
endif()
22+
23+
if(NOT CRYPTOAUTH_INCLUDE_DIR)
24+
message(FATAL_ERROR "cryptoauth include directory not found")
25+
endif()
26+
27+
# Include directories
28+
include_directories(${CRYPTOAUTH_INCLUDE_DIR} ${PROJECT_SOURCE_DIR}/include)
29+
30+
# Add the library
31+
add_library(atecc608_handler SHARED src/atecc608_handler.cpp)
32+
33+
# Link the library with cryptoauthlib
34+
target_link_libraries(atecc608_handler ${CRYPTOAUTH_LIB})
35+
36+
# Add the test executable
37+
add_executable(test_atecc608 test/test_atecc608.cpp)
38+
39+
# Link the test executable with the atecc608_handler library
40+
target_link_libraries(test_atecc608 atecc608_handler)
41+

include/atecc608_handler.h

Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
#include <iostream>
2+
#include <vector>
3+
#include <bitset>
4+
5+
6+
7+
// Exemple of configuration for ATECC608A. Look at page 8 on the datasheet for ATECC608A-TFLXTLS
8+
const uint8_t ECCX08_DEFAULT_CONFIGURATION_VALS[112] = {
9+
// Read only - end
10+
// I2C_Address
11+
0xC0,
12+
// Reserved
13+
0x00,
14+
// OTPmode
15+
0x55,
16+
// ChipMode
17+
0x00,
18+
// SlotConfig
19+
0x83, 0x60,
20+
0x87, 0x60,
21+
0x8F, 0x60,
22+
0x83, 0x60,
23+
0x83, 0x60,
24+
0x8F, 0x8F,
25+
0x9F, 0x8F,
26+
0xAF, 0x8F,
27+
0x00, 0x00,
28+
0x00, 0x00,
29+
0x00, 0x00,
30+
0x00, 0x00,
31+
0x00, 0x00,
32+
0x00, 0x00,
33+
0x00, 0x00,
34+
0xAF, 0x8F,
35+
// Counter[0]
36+
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
37+
// Counter[1]
38+
0xFF, 0xFF, 0xFF, 0xFF, 0x00, 0x00, 0x00, 0x00,
39+
// LastKeyUse
40+
0xFF, 0xFF, 0xFF, 0xFF,
41+
0xFF, 0xFF, 0xFF, 0xFF,
42+
0xFF, 0xFF, 0xFF, 0xFF,
43+
0xFF, 0xFF, 0xFF, 0xFF,
44+
// Write via commands only - start
45+
// UserExtra
46+
0x00,
47+
// Selector
48+
0x00,
49+
// LockValue
50+
0x55,
51+
// LockConfig
52+
0x55,
53+
// SlotLocked
54+
0xFF, 0xFF,
55+
// Write via commands only - end
56+
// RFU
57+
0x00, 0x00,
58+
// X509format
59+
0x00, 0x00, 0x00, 0x00,
60+
// KeyConfig
61+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
62+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
63+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x33, 0x00,
64+
0x1C, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
65+
0x33, 0x00, // Private | Public | P256 NIST ECC key, Default: 0x1C, 0x00,
66+
0x1C, 0x00,
67+
0x1C, 0x00,
68+
0x1C, 0x00,
69+
0x3C, 0x00,
70+
0x3C, 0x00,
71+
0x3C, 0x00,
72+
0x3C, 0x00,
73+
0x3C, 0x00,
74+
0x3C, 0x00,
75+
0x3C, 0x00,
76+
0x1C, 0x00
77+
};
78+
79+
extern "C" int atecc_handler_write_configuration(const uint8_t *config, size_t len);
80+
extern "C" int atecc_handler_lock_zone(uint8_t zone);
81+
extern "C" int atecc_handler_init(int i2cAddr);
82+
extern "C" int atecc_handler_read_configuration(uint8_t *config_data);
83+
extern "C" int atecc_handler_inject_priv_key(int slot, uint8_t * priv_key);
84+
extern "C" int atecc_handler_get_public_key(int slot, uint8_t * pub_key);
85+
extern "C" int atecc_handler_sign(int slot, const uint8_t * msg, uint8_t * signature);
86+
extern "C" int atecc_handler_verify(int slot, const uint8_t * msg, const uint8_t * signature, const uint8_t * pub_key);
87+
extern "C" int atecc_handler_genkey(int slot, uint8_t * pub_key);
88+
extern "C" int atecc_handler_lock_slot(int slot);

python/test_atecc608.py

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,114 @@
1+
import ctypes
2+
from ctypes import c_int, c_uint8, c_size_t, POINTER, byref, create_string_buffer
3+
import sys
4+
5+
# Load the shared library
6+
atecc608_lib = ctypes.CDLL('../build/libatecc608_handler.so')
7+
8+
# Define argument and return types for the functions
9+
atecc608_lib.atecc_handler_init.argtypes = [c_int]
10+
atecc608_lib.atecc_handler_init.restype = c_int
11+
12+
atecc608_lib.atecc_handler_write_configuration.argtypes = [POINTER(c_uint8), c_size_t]
13+
atecc608_lib.atecc_handler_write_configuration.restype = c_int
14+
15+
atecc608_lib.atecc_handler_lock_zone.argtypes = [c_uint8]
16+
atecc608_lib.atecc_handler_lock_zone.restype = c_int
17+
18+
atecc608_lib.atecc_handler_genkey.argtypes = [c_int, POINTER(c_uint8)]
19+
atecc608_lib.atecc_handler_genkey.restype = c_int
20+
21+
atecc608_lib.atecc_handler_inject_priv_key.argtypes = [c_int, POINTER(c_uint8)]
22+
atecc608_lib.atecc_handler_inject_priv_key.restype = c_int
23+
24+
atecc608_lib.atecc_handler_get_public_key.argtypes = [c_int, POINTER(c_uint8)]
25+
atecc608_lib.atecc_handler_get_public_key.restype = c_int
26+
27+
atecc608_lib.atecc_handler_sign.argtypes = [c_int, POINTER(c_uint8), POINTER(c_uint8)]
28+
atecc608_lib.atecc_handler_sign.restype = c_int
29+
30+
atecc608_lib.atecc_handler_verify.argtypes = [c_int, POINTER(c_uint8), POINTER(c_uint8), POINTER(c_uint8)]
31+
atecc608_lib.atecc_handler_verify.restype = c_int
32+
33+
def print_hex_buffer(input):
34+
print(" ".join(f"{x:02X}" for x in input))
35+
36+
def write_atecc_config():
37+
status = atecc608_lib.atecc_handler_init(0xC0)
38+
if status:
39+
print(f"atecc_handler_init Fail! {status}")
40+
return
41+
42+
ECCX08_DEFAULT_CONFIGURATION_VALS = (c_uint8 * 112)() # Adjust the size accordingly
43+
status = atecc608_lib.atecc_handler_write_configuration(ECCX08_DEFAULT_CONFIGURATION_VALS, 112)
44+
if status:
45+
print(f"atecc_handler_write_configuration Fail! {status}")
46+
return
47+
48+
status = atecc608_lib.atecc_handler_lock_zone(0)
49+
if status:
50+
print(f"atecc_handler_lock_zone Fail! {status}")
51+
return
52+
53+
def general_test():
54+
status = atecc608_lib.atecc_handler_init(0xC0)
55+
if status:
56+
print(f"atecc_handler_init Fail! {status}")
57+
return 0
58+
59+
slotID = 1
60+
pub_key = (c_uint8 * 64)()
61+
status = atecc608_lib.atecc_handler_genkey(slotID, pub_key)
62+
if status:
63+
print(f"atecc_handler_genkey Fail! {status}")
64+
return 0
65+
print("Pub key generated:")
66+
print_hex_buffer(pub_key)
67+
68+
priv_key = (c_uint8 * 36)(
69+
0x00, 0x00, 0x00, 0x00,
70+
0x39, 0xac, 0x9b, 0xf9, 0x17, 0x1d, 0xe8, 0x6f, 0xfa, 0x77, 0xe0, 0xb9, 0x05, 0x0b, 0xf6, 0xe0,
71+
0x6a, 0x2c, 0x1b, 0xc1, 0x76, 0x79, 0x36, 0xe6, 0xc7, 0x45, 0x79, 0xe4, 0x26, 0xa4, 0x47, 0x5f
72+
)
73+
74+
status = atecc608_lib.atecc_handler_inject_priv_key(slotID, priv_key)
75+
if status:
76+
print(f"atecc_handler_inject_priv_key Fail! {status}")
77+
return 0
78+
79+
status = atecc608_lib.atecc_handler_get_public_key(slotID, pub_key)
80+
if status:
81+
print(f"atecc_handler_get_public_key Fail! {status}")
82+
return 0
83+
print("Pub key:")
84+
print_hex_buffer(pub_key)
85+
86+
signature = (c_uint8 * 64)()
87+
msg = (c_uint8 * 32)(
88+
0xc8, 0x90, 0xf8, 0x65, 0xf3, 0xb0, 0x5f, 0x78, 0x27, 0x03, 0x4a, 0xae, 0x6a, 0xc2, 0x5c, 0xd5,
89+
0xcb, 0xca, 0x5d, 0x25, 0xeb, 0x0f, 0x0c, 0x35, 0xdf, 0x5d, 0x33, 0x90, 0x3e, 0x08, 0xfa, 0xbe
90+
)
91+
92+
status = atecc608_lib.atecc_handler_sign(slotID, msg, signature)
93+
if status:
94+
print(f"atecc_handler_sign Fail! {status}")
95+
return 0
96+
print("Signature:")
97+
print_hex_buffer(signature)
98+
99+
status = atecc608_lib.atecc_handler_verify(slotID, msg, signature, pub_key)
100+
if status:
101+
print(f"atecc_handler_verify Fail! {status}")
102+
return 0
103+
104+
print("TEST ENDED SUCCESSFULLY!")
105+
return 0
106+
107+
108+
if __name__ == "__main__":
109+
# write_atecc_config()
110+
# Uncomment the following line to run the general test
111+
general_test()
112+
113+
114+

0 commit comments

Comments
 (0)