|
| 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