├── .gitignore ├── .travis.yml ├── .vscode └── extensions.json ├── README.md ├── include └── README ├── lib └── README ├── platformio.ini ├── src ├── ATECCX08A_Arduino │ ├── CMakeLists.txt │ ├── Configuration chip.txt │ ├── atca_bool.h │ ├── atca_cfgs.c │ ├── atca_cfgs.h │ ├── atca_command.c │ ├── atca_command.h │ ├── atca_compiler.h │ ├── atca_config.h │ ├── atca_config.h.in │ ├── atca_device.c │ ├── atca_device.h │ ├── atca_devtypes.h │ ├── atca_execution.c │ ├── atca_execution.h │ ├── atca_iface.c │ ├── atca_iface.h │ ├── atca_status.h │ ├── atca_version.h │ ├── atcacert │ │ ├── atcacert.h │ │ ├── atcacert_client.c │ │ ├── atcacert_client.h │ │ ├── atcacert_date.c │ │ ├── atcacert_date.h │ │ ├── atcacert_def.c │ │ ├── atcacert_def.h │ │ ├── atcacert_der.c │ │ ├── atcacert_der.h │ │ ├── atcacert_host_hw.c │ │ ├── atcacert_host_hw.h │ │ ├── atcacert_host_sw.c │ │ ├── atcacert_host_sw.h │ │ ├── atcacert_pem.c │ │ └── atcacert_pem.h │ ├── basic │ │ ├── README.md │ │ ├── atca_basic.c │ │ ├── atca_basic.h │ │ ├── atca_basic_aes.c │ │ ├── atca_basic_aes_cbc.c │ │ ├── atca_basic_aes_cmac.c │ │ ├── atca_basic_aes_ctr.c │ │ ├── atca_basic_aes_gcm.c │ │ ├── atca_basic_aes_gcm.h │ │ ├── atca_basic_checkmac.c │ │ ├── atca_basic_counter.c │ │ ├── atca_basic_derivekey.c │ │ ├── atca_basic_ecdh.c │ │ ├── atca_basic_gendig.c │ │ ├── atca_basic_genkey.c │ │ ├── atca_basic_hmac.c │ │ ├── atca_basic_info.c │ │ ├── atca_basic_kdf.c │ │ ├── atca_basic_lock.c │ │ ├── atca_basic_mac.c │ │ ├── atca_basic_nonce.c │ │ ├── atca_basic_privwrite.c │ │ ├── atca_basic_random.c │ │ ├── atca_basic_read.c │ │ ├── atca_basic_secureboot.c │ │ ├── atca_basic_selftest.c │ │ ├── atca_basic_sha.c │ │ ├── atca_basic_sign.c │ │ ├── atca_basic_updateextra.c │ │ ├── atca_basic_verify.c │ │ ├── atca_basic_write.c │ │ ├── atca_helpers.c │ │ └── atca_helpers.h │ ├── crypto │ │ ├── README.md │ │ ├── atca_crypto_sw.h │ │ ├── atca_crypto_sw_ecdsa.c │ │ ├── atca_crypto_sw_ecdsa.h │ │ ├── atca_crypto_sw_rand.c │ │ ├── atca_crypto_sw_rand.h │ │ ├── atca_crypto_sw_sha1.c │ │ ├── atca_crypto_sw_sha1.h │ │ ├── atca_crypto_sw_sha2.c │ │ ├── atca_crypto_sw_sha2.h │ │ └── hashes │ │ │ ├── sha1_routines.c │ │ │ ├── sha1_routines.h │ │ │ ├── sha2_routines.c │ │ │ └── sha2_routines.h │ ├── cryptoauthlib.h │ ├── hal │ │ ├── 90-cryptohid.rules │ │ ├── README.md │ │ ├── TWI │ │ │ ├── hal_arduino_twi.cpp │ │ │ └── hal_arduino_twi.h │ │ ├── atca_hal.c │ │ ├── atca_hal.h │ │ ├── atca_start_config.h │ │ ├── atca_start_iface.h │ │ ├── hal_arduino.cpp │ │ ├── hal_arduino.h │ │ ├── kit_phy.h │ │ ├── kit_protocol.c │ │ └── kit_protocol.h │ ├── host │ │ ├── atca_host.c │ │ └── atca_host.h │ ├── jwt │ │ ├── atca_jwt.c │ │ └── atca_jwt.h │ └── python │ │ ├── .pylintrc │ │ ├── LICENSE.txt │ │ ├── MANIFEST.in │ │ ├── README.md │ │ ├── cryptoauthlib │ │ ├── __init__.py │ │ ├── atcab.py │ │ ├── atcacert.py │ │ ├── atcaenum.py │ │ ├── atjwt.py │ │ ├── device.py │ │ ├── exceptions.py │ │ ├── iface.py │ │ ├── library.py │ │ ├── status.py │ │ └── tng.py │ │ ├── setup.py │ │ ├── tests │ │ ├── README.md │ │ ├── conftest.py │ │ ├── cryptoauthlib_mock.py │ │ ├── test_atcab.py │ │ ├── test_atcacert.py │ │ ├── test_device.py │ │ ├── test_iface.py │ │ ├── test_jwt.py │ │ ├── test_library.py │ │ ├── test_status.py │ │ └── test_tng.py │ │ └── tox.ini ├── Examples │ ├── AES CBC │ │ ├── aes_cbc.cpp │ │ └── aes_cbc.h │ ├── AES_crypto_example.ino │ ├── Configuration │ │ ├── Configuration.cpp │ │ └── Configuration.h │ └── configuration_example.ino └── main.cpp └── test └── README /.gitignore: -------------------------------------------------------------------------------- 1 | .pio 2 | .vscode/.browse.c_cpp.db* 3 | .vscode/c_cpp_properties.json 4 | .vscode/launch.json 5 | .vscode/ipch 6 | Zone.sav 7 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | # Continuous Integration (CI) is the practice, in software 2 | # engineering, of merging all developer working copies with a shared mainline 3 | # several times a day < https://docs.platformio.org/page/ci/index.html > 4 | # 5 | # Documentation: 6 | # 7 | # * Travis CI Embedded Builds with PlatformIO 8 | # < https://docs.travis-ci.com/user/integration/platformio/ > 9 | # 10 | # * PlatformIO integration with Travis CI 11 | # < https://docs.platformio.org/page/ci/travis.html > 12 | # 13 | # * User Guide for `platformio ci` command 14 | # < https://docs.platformio.org/page/userguide/cmd_ci.html > 15 | # 16 | # 17 | # Please choose one of the following templates (proposed below) and uncomment 18 | # it (remove "# " before each line) or use own configuration according to the 19 | # Travis CI documentation (see above). 20 | # 21 | 22 | 23 | # 24 | # Template #1: General project. Test it using existing `platformio.ini`. 25 | # 26 | 27 | # language: python 28 | # python: 29 | # - "2.7" 30 | # 31 | # sudo: false 32 | # cache: 33 | # directories: 34 | # - "~/.platformio" 35 | # 36 | # install: 37 | # - pip install -U platformio 38 | # - platformio update 39 | # 40 | # script: 41 | # - platformio run 42 | 43 | 44 | # 45 | # Template #2: The project is intended to be used as a library with examples. 46 | # 47 | 48 | # language: python 49 | # python: 50 | # - "2.7" 51 | # 52 | # sudo: false 53 | # cache: 54 | # directories: 55 | # - "~/.platformio" 56 | # 57 | # env: 58 | # - PLATFORMIO_CI_SRC=path/to/test/file.c 59 | # - PLATFORMIO_CI_SRC=examples/file.ino 60 | # - PLATFORMIO_CI_SRC=path/to/test/directory 61 | # 62 | # install: 63 | # - pip install -U platformio 64 | # - platformio update 65 | # 66 | # script: 67 | # - platformio ci --lib="." --board=ID_1 --board=ID_2 --board=ID_N 68 | -------------------------------------------------------------------------------- /.vscode/extensions.json: -------------------------------------------------------------------------------- 1 | { 2 | // See http://go.microsoft.com/fwlink/?LinkId=827846 3 | // for the documentation about the extensions.json format 4 | "recommendations": [ 5 | "platformio.platformio-ide" 6 | ] 7 | } 8 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Arduino-Atecc608A 2 | Use of the MicroChip Atecc608a with a Arduino UNO 3 | # ArduinoUno-Ateccx08a 4 | 5 | This library allows to use the Arduino UNO with the Atecc508a and Atecc608a. 6 | This is a fork of the MicroChip library called "Cryptoauthlib" 7 | 8 | # Example 9 | 10 | Example of AES CBC and Configuration are available in the folder "src/Examples/" 11 | -------------------------------------------------------------------------------- /include/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project header files. 3 | 4 | A header file is a file containing C declarations and macro definitions 5 | to be shared between several project source files. You request the use of a 6 | header file in your project source file (C, C++, etc) located in `src` folder 7 | by including it, with the C preprocessing directive `#include'. 8 | 9 | ```src/main.c 10 | 11 | #include "header.h" 12 | 13 | int main (void) 14 | { 15 | ... 16 | } 17 | ``` 18 | 19 | Including a header file produces the same results as copying the header file 20 | into each source file that needs it. Such copying would be time-consuming 21 | and error-prone. With a header file, the related declarations appear 22 | in only one place. If they need to be changed, they can be changed in one 23 | place, and programs that include the header file will automatically use the 24 | new version when next recompiled. The header file eliminates the labor of 25 | finding and changing all the copies as well as the risk that a failure to 26 | find one copy will result in inconsistencies within a program. 27 | 28 | In C, the usual convention is to give header files names that end with `.h'. 29 | It is most portable to use only letters, digits, dashes, and underscores in 30 | header file names, and at most one dot. 31 | 32 | Read more about using header files in official GCC documentation: 33 | 34 | * Include Syntax 35 | * Include Operation 36 | * Once-Only Headers 37 | * Computed Includes 38 | 39 | https://gcc.gnu.org/onlinedocs/cpp/Header-Files.html 40 | -------------------------------------------------------------------------------- /lib/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for project specific (private) libraries. 3 | PlatformIO will compile them to static libraries and link into executable file. 4 | 5 | The source code of each library should be placed in a an own separate directory 6 | ("lib/your_library_name/[here are source files]"). 7 | 8 | For example, see a structure of the following two libraries `Foo` and `Bar`: 9 | 10 | |--lib 11 | | | 12 | | |--Bar 13 | | | |--docs 14 | | | |--examples 15 | | | |--src 16 | | | |- Bar.c 17 | | | |- Bar.h 18 | | | |- library.json (optional, custom build options, etc) https://docs.platformio.org/page/librarymanager/config.html 19 | | | 20 | | |--Foo 21 | | | |- Foo.c 22 | | | |- Foo.h 23 | | | 24 | | |- README --> THIS FILE 25 | | 26 | |- platformio.ini 27 | |--src 28 | |- main.c 29 | 30 | and a contents of `src/main.c`: 31 | ``` 32 | #include 33 | #include 34 | 35 | int main (void) 36 | { 37 | ... 38 | } 39 | 40 | ``` 41 | 42 | PlatformIO Library Dependency Finder will find automatically dependent 43 | libraries scanning project source files. 44 | 45 | More information about PlatformIO Library Dependency Finder 46 | - https://docs.platformio.org/page/librarymanager/ldf.html 47 | -------------------------------------------------------------------------------- /platformio.ini: -------------------------------------------------------------------------------- 1 | ; PlatformIO Project Configuration File 2 | ; 3 | ; Build options: build flags, source filter 4 | ; Upload options: custom upload port, speed and extra flags 5 | ; Library options: dependencies, extra library storages 6 | ; Advanced options: extra scripting 7 | ; 8 | ; Please visit documentation for the other options and examples 9 | ; https://docs.platformio.org/page/projectconf.html 10 | 11 | [env:uno] 12 | platform = atmelavr 13 | board = uno 14 | framework = arduino 15 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/CMakeLists.txt: -------------------------------------------------------------------------------- 1 | cmake_minimum_required(VERSION 3.1.0) 2 | project(cryptoauth) 3 | 4 | # Various Options for Build 5 | option(ATCA_HAL_KIT_HID "Include the HID HAL Driver") 6 | option(ATCA_HAL_KIT_CDC "Include the CDC HAL Driver (Legacy)" OFF) 7 | option(ATCA_HAL_I2C "Include the I2C Hal Driver - Linux & MCU only") 8 | option(ATCA_HAL_CUSTOM "Include support for Custom/Plug-in Hal Driver") 9 | option(ATCA_PRINTF "Enable Debug print statements in library") 10 | option(ATCA_PKCS11 "Build PKCS11 Library") 11 | option(ATCA_MBEDTLS "Integrate with mbedtls") 12 | option(ATCA_BUILD_SHARED_LIBS "Build CryptoAuthLib as shared library" ON) 13 | set(BUILD_SHARED_LIBS ${ATCA_BUILD_SHARED_LIBS}) 14 | 15 | # Collect Library Sources 16 | file(GLOB LIB_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "*.c") 17 | file(GLOB ATCACERT_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "atcacert/*.c") 18 | file(GLOB BASIC_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "basic/*.c") 19 | file(GLOB_RECURSE CRYPTO_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "crypto/*.c") 20 | file(GLOB HOST_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "host/*.c") 21 | file(GLOB JWT_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "jwt/*.c") 22 | file(GLOB PKCS11_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "pkcs11/*.c") 23 | file(GLOB MBEDTLS_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "mbedtls/*.c") 24 | file(GLOB TNG_SRC RELATIVE ${CMAKE_CURRENT_SOURCE_DIR} "../app/tng/*.c") 25 | 26 | # Add the basic sources to the library 27 | set(CRYPTOAUTH_SRC ${LIB_SRC} ${ATCACERT_SRC} ${BASIC_SRC} ${CRYPTO_SRC} ${HOST_SRC} ${JWT_SRC} hal/atca_hal.c ${TNG_SRC}) 28 | 29 | if(ATCA_HAL_KIT_HID OR ATCA_HAL_KIT_CDC) 30 | set(NEED_USB true) 31 | endif() 32 | 33 | if(WIN32) 34 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} hal/hal_win_timer.c) 35 | set(HID_SRC ../third_party/hidapi/windows/hid.c) 36 | set(CDC_SRC hal/hal_win_kit_cdc.c) 37 | elseif(APPLE) 38 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} hal/hal_linux_timer.c) 39 | set(HID_SRC ../third_party/hidapi/mac/hid.c) 40 | elseif(UNIX) 41 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} hal/hal_linux_timer.c) 42 | set(TWI_SRC hal/hal_linux_i2c_userspace.c) 43 | set(LINUX TRUE) 44 | endif() 45 | 46 | if(LINUX AND NEED_USB) 47 | find_path(LIBUSB_INCLUDE_DIR NAMES libusb.h PATH_SUFFIXES "include" "libusb" "libusb-1.0") 48 | find_path(LIBUDEV_INCLUDE_DIR NAMES libudev.h PATH_SUFFIXES "include") 49 | find_library(HAS_LIBUSB usb-1.0) 50 | find_library(HAS_LIBUDEV udev) 51 | 52 | if(HAS_LIBUSB AND LIBUSB_INCLUDE_DIR) 53 | set(LIBUSB_GOOD TRUE) 54 | endif() 55 | 56 | if(HAS_LIBUDEV AND LIBUDEV_INCLUDE_DIR) 57 | set(LIBUDEV_GOOD TRUE) 58 | endif() 59 | 60 | if(ATCA_HAL_KIT_CDC AND LIBUDEV_GOOD) 61 | set(USE_UDEV TRUE) 62 | elseif(LIBUSB_GOOD AND NOT ATCA_HAL_KIT_CDC) 63 | set(USE_LIBUSB TRUE) 64 | else() 65 | message(FATAL_ERROR, "Missing Build Dependencies for USB - install libusb-1.0-0-dev or libudev-dev") 66 | endif() 67 | 68 | endif(LINUX AND NEED_USB) 69 | 70 | if(USE_UDEV) 71 | set(USB_INCLUDE_DIR ${LIBUDEV_INCLUDE_DIR}) 72 | set(HID_SRC ../third_party/hidapi/linux/hid.c) 73 | set(CDC_SRC hal/hal_linux_kit_cdc.c) 74 | endif(USE_UDEV) 75 | 76 | if(USE_LIBUSB) 77 | set(USB_INCLUDE_DIR ${LIBUSB_INCLUDE_DIR}) 78 | set(HID_SRC ../third_party/hidapi/libusb/hid.c) 79 | endif(USE_LIBUSB) 80 | 81 | if(NEED_USB) 82 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} hal/kit_protocol.c) 83 | endif() 84 | 85 | if(ATCA_HAL_KIT_HID) 86 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} ${HID_SRC} hal/hal_all_platforms_kit_hidapi.c) 87 | endif(ATCA_HAL_KIT_HID) 88 | 89 | if(ATCA_HAL_KIT_CDC) 90 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} ${CDC_SRC}) 91 | endif(ATCA_HAL_KIT_CDC) 92 | 93 | if(ATCA_HAL_I2C) 94 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} ${TWI_SRC}) 95 | endif(ATCA_HAL_I2C) 96 | 97 | # Add Remaining Sources depending on target library type 98 | if(ATCA_MBEDTLS) 99 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} ${MBEDTLS_SRC}) 100 | endif() 101 | 102 | if(ATCA_PKCS11) 103 | set(CRYPTOAUTH_SRC ${CRYPTOAUTH_SRC} ${PKCS11_SRC}) 104 | endif() 105 | 106 | add_library(cryptoauth ${CRYPTOAUTH_SRC} ${ATCACERT_DEF_SRC}) 107 | 108 | set_property(TARGET cryptoauth PROPERTY C_STANDARD 99) 109 | 110 | # Add Remaining Sources depending on target library type 111 | if(ATCA_PKCS11) 112 | set_target_properties(cryptoauth PROPERTIES OUTPUT_NAME "ateccx08pkcs11") 113 | endif(ATCA_PKCS11) 114 | 115 | configure_file(atca_config.h.in atca_config.h @ONLY) 116 | 117 | include_directories(cryptoauth PUBLIC ${CMAKE_CURRENT_BINARY_DIR} ${CMAKE_CURRENT_SOURCE_DIR} ../third_party/hidapi/hidapi ${USB_INCLUDE_DIR}) 118 | 119 | if(WIN32) 120 | set_target_properties(cryptoauth PROPERTIES WINDOWS_EXPORT_ALL_SYMBOLS true) 121 | target_link_libraries(cryptoauth setupapi.lib) 122 | endif(WIN32) 123 | 124 | if(APPLE) 125 | find_library(IO_KIT_LIB IOKit) 126 | find_library(CORE_LIB CoreFoundation) 127 | target_link_libraries(cryptoauth ${IO_KIT_LIB} ${CORE_LIB}) 128 | endif() 129 | 130 | if(LINUX) 131 | if(USE_LIBUSB) 132 | target_link_libraries(cryptoauth usb-1.0) 133 | elseif(USE_UDEV) 134 | target_link_libraries(cryptoauth udev) 135 | endif() 136 | target_link_libraries(cryptoauth rt) 137 | endif(LINUX) 138 | 139 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/Configuration chip.txt: -------------------------------------------------------------------------------- 1 | // Reseved // 2 | 0: 0x01 0b00000001 3 | 1: 0x23 0b00100011 4 | 2: 0x2E 0b00101110 5 | 3: 0xFF 0b11111111 6 | 4: 0x00 0b00000000 7 | 5: 0x00 0b00000000 8 | 6: 0x60 0b01100000 9 | 7: 0x02 0b00000010 10 | 8: 0x9D 0b10011101 11 | 9: 0x96 0b10010110 12 | 10: 0xB8 0b10111000 13 | 11: 0x9C 0b10011100 14 | 12: 0xEE 0b11101110 15 | 13: 0x01 0b00000001 16 | 14: 0x3D 0b00111101 17 | 15: 0x00 0b00000000 18 | // Reserved // 19 | 20 | 16: 0xC0 0b11000000 // I2C Address 21 | 17: 0x00 0b00000000 22 | 18: 0x00 0b00000000 // CountMatch 23 | 19: 0x00 0b00000000 // ChipMode 24 | 25 | // Slot Config 26 | 0x83 0x20 // Slot 1 27 | 0x87 0x20 // Slot 2 -> 0x85 0x20(Key config) 28 | 0x8F 0x20 // Slot 3 29 | 0xC4 0x8F // Slot 4 30 | 0x8F 0x8F // Slot 5 31 | 0x8F 0x8F // Slot 6 32 | 0x9F 0x8F // Slot 7 33 | 0xAF 0x8F // Slot 8 -> 0x0F 0x0F (Store everything) 34 | 0x00 0x00 // Slot 9 -> 0x8F 0x0F (AES No lockable) 35 | 0x00 0x00 // Slot 10 -> 0x8F 0x0F (AES lockable) 36 | 0x00 0x00 // Slot 11 37 | 0x00 0x00 // Slot 12 38 | 0x00 0x00 // Slot 13 39 | 0x00 0x00 // Slot 14 40 | 0x00 0x00 // Slot 15 41 | 0xAF 0x8F // Slot 16 42 | 43 | // Counter[0] // 44 | 52: 0xFF 0b11111111 45 | 53: 0xFF 0b11111111 46 | 54: 0xFF 0b11111111 47 | 55: 0xFF 0b11111111 48 | 56: 0x00 0b00000000 49 | 57: 0x00 0b00000000 50 | 58: 0x00 0b00000000 51 | 59: 0x00 0b00000000 52 | 53 | // Counter[1] // 54 | 60: 0xFF 0b11111111 55 | 61: 0xFF 0b11111111 56 | 62: 0xFF 0b11111111 57 | 63: 0xFF 0b11111111 58 | 64: 0x00 0b00000000 59 | 65: 0x00 0b00000000 60 | 66: 0x00 0b00000000 61 | 67: 0x00 0b00000000 62 | 63 | 68: 0x00 0b00000000 // UseLock 64 | 69: 0x00 0b00000000 // VolatileKey Permission 65 | 66 | // SecureBoot 67 | 70: 0x00 0b00000000 68 | 71: 0x00 0b00000000 69 | 70 | 71 | 72: 0x00 0b00000000 // KdfIvLoc 72 | 73 | // KdfIvStr // 74 | 73: 0x00 0b00000000 75 | 74: 0x00 0b00000000 76 | 77 | // Reserved (Must be 0) // 78 | 75: 0x00 0b00000000 79 | 76: 0x00 0b00000000 80 | 77: 0x00 0b00000000 81 | 78: 0x00 0b00000000 82 | 79: 0x00 0b00000000 83 | 80: 0x00 0b00000000 84 | 81: 0x00 0b00000000 85 | 82: 0x00 0b00000000 86 | 83: 0x00 0b00000000 87 | 88 | 89 | 84: 0x00 0b00000000 // UserExtra 90 | 85: 0x00 0b00000000 // UserExtraAdd 91 | 86: 0x00 0b00000000 // LockValue 92 | 87: 0x00 0b00000000 // LockConfig 93 | 94 | // SlotLocked // 95 | 88: 0xFE 0b11111110 96 | 89: 0xFF 0b11111111 97 | 98 | // ChipOptions // 99 | 90: 0x00 0b00000000 100 | 91: 0x00 0b00000000 101 | 102 | // X509format // 103 | 92: 0x00 0b00000000 104 | 93: 0x00 0b00000000 105 | 94: 0x00 0b00000000 106 | 95: 0x00 0b00000000 107 | 108 | // KeyConfig // 109 | 0x33 0x00 // Slot 1 110 | 0x33 0x00 // Slot 2 -> 0x73 0x00 (Key config) 111 | 0x33 0x00 // Slot 3 112 | 0x1C 0x00 // Slot 4 113 | 0x1C 0x00 // Slot 5 114 | 0x1C 0x00 // Slot 6 115 | 0x1C 0x00 // Slot 7 116 | 0x1C 0x00 // Slot 8 -> 0x3C 0x00 (Store everything) 117 | 0x3C 0x00 // Slot 9 -> 0x1A 0x00 (AES No lockable) 118 | 0x3C 0x00 // Slot 10 -> 0x3A 0x00 (AES lockable) 119 | 0x3C 0x00 // Slot 11 120 | 0x3C 0x00 // Slot 12 121 | 0x3C 0x00 // Slot 13 122 | 0x3C 0x00 // Slot 14 123 | 0x3C 0x00 // Slot 15 124 | 0x1C 0x00 // Slot 16 125 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_bool.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief bool define for systems that don't have it 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | #ifndef _ATCA_BOOL_H 30 | #define _ATCA_BOOL_H 31 | 32 | #if defined(_MSC_VER) && (_MSC_VER <= 1700) 33 | // VS2012 and earlier don't support stdbool.h 34 | #ifndef __cplusplus 35 | #define bool unsigned char 36 | #define false 0 37 | #define true 1 38 | #endif 39 | #else 40 | #include 41 | #endif 42 | 43 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_cfgs.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief a set of default configurations for various ATCA devices and interfaces 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | 29 | #ifndef ATCA_CFGS_H_ 30 | #define ATCA_CFGS_H_ 31 | 32 | #include "atca_iface.h" 33 | 34 | #ifdef __cplusplus 35 | extern "C" { 36 | #endif 37 | 38 | 39 | /** \brief default configuration for an ECCx08A device on the first logical I2C bus */ 40 | extern ATCAIfaceCfg cfg_ateccx08a_i2c_default; 41 | 42 | /** \brief default configuration for an ECCx08A device on the logical SWI bus over UART*/ 43 | extern ATCAIfaceCfg cfg_ateccx08a_swi_default; 44 | 45 | /** \brief default configuration for Kit protocol over a CDC interface */ 46 | extern ATCAIfaceCfg cfg_ateccx08a_kitcdc_default; 47 | 48 | /** \brief default configuration for Kit protocol over a HID interface */ 49 | extern ATCAIfaceCfg cfg_ateccx08a_kithid_default; 50 | 51 | 52 | /** \brief default configuration for a SHA204A device on the first logical I2C bus */ 53 | extern ATCAIfaceCfg cfg_atsha20xa_i2c_default; 54 | 55 | /** \brief default configuration for an SHA20xA device on the logical SWI bus over UART*/ 56 | extern ATCAIfaceCfg cfg_atsha20xa_swi_default; 57 | 58 | /** \brief default configuration for Kit protocol over a CDC interface */ 59 | extern ATCAIfaceCfg cfg_atsha20xa_kitcdc_default; 60 | 61 | /** \brief default configuration for Kit protocol over a HID interface for SHA204 */ 62 | extern ATCAIfaceCfg cfg_atsha20xa_kithid_default; 63 | 64 | #ifdef __cplusplus 65 | } 66 | #endif 67 | #endif /* ATCA_CFGS_H_ */ -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_config.h: -------------------------------------------------------------------------------- 1 | #ifndef ATCA_CONFIG_H 2 | #define ATCA_CONFIG_H 3 | 4 | #define ATCA_HAL_I2C 5 | 6 | 7 | /* Include HALS */ 8 | #include "hal\atca_hal.h" 9 | 10 | /* Included device support */ 11 | #define ATCA_ATSHA204A_SUPPORT 12 | #define ATCA_ATSHA206A_SUPPORT 13 | #define ATCA_ATECC108A_SUPPORT 14 | #define ATCA_ATECC508A_SUPPORT 15 | #define ATCA_ATECC608A_SUPPORT 16 | 17 | // Print function 18 | #define ATCAPRINTF 19 | 20 | #endif // ATCA_CONFIG_H 21 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_config.h.in: -------------------------------------------------------------------------------- 1 | /* Auto-generated config file atca_config.h */ 2 | #ifndef ATCA_CONFIG_H 3 | #define ATCA_CONFIG_H 4 | 5 | /* Include HALS */ 6 | #cmakedefine ATCA_HAL_KIT_HID 7 | #cmakedefine ATCA_HAL_KIT_CDC 8 | #cmakedefine ATCA_HAL_I2C 9 | #cmakedefine ATCA_HAL_CUSTOM 10 | 11 | /* Included device support */ 12 | #define ATCA_ATSHA204A_SUPPORT 13 | #define ATCA_ATSHA206A_SUPPORT 14 | #define ATCA_ATECC108A_SUPPORT 15 | #define ATCA_ATECC508A_SUPPORT 16 | #define ATCA_ATECC608A_SUPPORT 17 | 18 | /** Define if cryptoauthlib is to use the maximum execution time method */ 19 | #cmakedefine ATCA_NO_POLL 20 | 21 | /** Define if the library is not to use malloc/free */ 22 | #cmakedefine ATCA_NO_HEAP 23 | 24 | /* \brief How long to wait after an initial wake failure for the POST to 25 | * complete. 26 | * If Power-on self test (POST) is enabled, the self test will run on waking 27 | * from sleep or during power-on, which delays the wake reply. 28 | */ 29 | #ifndef ATCA_POST_DELAY_MSEC 30 | #define ATCA_POST_DELAY_MSEC 25 31 | #endif 32 | 33 | #cmakedefine ATCA_PRINTF 34 | 35 | #endif // ATCA_CONFIG_H 36 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_device.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Microchip CryptoAuth device object 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #include 29 | #include "atca_device.h" 30 | #include "atca_config.h" 31 | 32 | /** \defgroup device ATCADevice (atca_) 33 | * \brief ATCADevice object - composite of command and interface objects 34 | @{ */ 35 | 36 | 37 | #ifndef ATCA_NO_HEAP 38 | /** \brief constructor for a Microchip CryptoAuth device 39 | * \param[in] cfg Interface configuration object 40 | * \return Reference to a new ATCADevice on success. NULL on failure. 41 | */ 42 | ATCADevice newATCADevice(ATCAIfaceCfg *cfg) 43 | { 44 | ATCADevice ca_dev = NULL; 45 | ATCA_STATUS status; 46 | 47 | if (cfg == NULL) 48 | { 49 | return NULL; 50 | } 51 | 52 | ca_dev = (ATCADevice)malloc(sizeof(*ca_dev)); 53 | if (ca_dev == NULL) 54 | { 55 | return NULL; 56 | } 57 | 58 | ca_dev->mCommands = (ATCACommand)malloc(sizeof(*(ca_dev->mCommands))); 59 | if (ca_dev->mCommands == NULL) 60 | { 61 | free(ca_dev); 62 | ca_dev = NULL; 63 | return NULL; 64 | } 65 | 66 | ca_dev->mIface = (ATCAIface)malloc(sizeof(*(ca_dev->mIface))); 67 | if (ca_dev->mIface == NULL) 68 | { 69 | free(ca_dev->mCommands); 70 | free(ca_dev); 71 | ca_dev = NULL; 72 | return NULL; 73 | } 74 | 75 | status = initATCADevice(cfg, ca_dev); 76 | if (status != ATCA_SUCCESS) 77 | { 78 | free(ca_dev->mIface); 79 | free(ca_dev->mCommands); 80 | free(ca_dev); 81 | ca_dev = NULL; 82 | return NULL; 83 | } 84 | 85 | return ca_dev; 86 | } 87 | 88 | /** \brief destructor for a device NULLs reference after object is freed 89 | * \param[in] ca_dev pointer to a reference to a device 90 | */ 91 | void deleteATCADevice(ATCADevice *ca_dev) 92 | { 93 | if (ca_dev == NULL) 94 | { 95 | return; 96 | } 97 | 98 | releaseATCADevice(*ca_dev); 99 | deleteATCACommand(&(*ca_dev)->mCommands); 100 | // Free iface manually as we don't want to call releaseATCAIface twice 101 | if ((*ca_dev)->mIface) 102 | { 103 | free((*ca_dev)->mIface); 104 | (*ca_dev)->mIface = NULL; 105 | } 106 | 107 | free(*ca_dev); 108 | *ca_dev = NULL; 109 | } 110 | #endif 111 | 112 | /** \brief Initializer for an Microchip CryptoAuth device 113 | * \param[in] cfg pointer to an interface configuration object 114 | * \param[inout] ca_dev As input, pre-allocated structure to be initialized. 115 | * mCommands and mIface members should point to existing 116 | * structures to be initialized. 117 | * \return ATCA_SUCCESS on success, otherwise an error code. 118 | */ 119 | ATCA_STATUS initATCADevice(ATCAIfaceCfg *cfg, ATCADevice ca_dev) 120 | { 121 | ATCA_STATUS status; 122 | 123 | if (cfg == NULL || ca_dev == NULL || ca_dev->mCommands == NULL || ca_dev->mIface == NULL) 124 | { 125 | return ATCA_BAD_PARAM; 126 | } 127 | 128 | status = initATCACommand(cfg->devtype, ca_dev->mCommands); 129 | if (status != ATCA_SUCCESS) 130 | { 131 | return status; 132 | } 133 | 134 | status = initATCAIface(cfg, ca_dev->mIface); 135 | if (status != ATCA_SUCCESS) 136 | { 137 | return status; 138 | } 139 | 140 | return ATCA_SUCCESS; 141 | } 142 | 143 | /** \brief returns a reference to the ATCACommand object for the device 144 | * \param[in] dev reference to a device 145 | * \return reference to the ATCACommand object for the device 146 | */ 147 | ATCACommand atGetCommands(ATCADevice dev) 148 | { 149 | return dev->mCommands; 150 | } 151 | 152 | /** \brief returns a reference to the ATCAIface interface object for the device 153 | * \param[in] dev reference to a device 154 | * \return reference to the ATCAIface object for the device 155 | */ 156 | ATCAIface atGetIFace(ATCADevice dev) 157 | { 158 | return dev->mIface; 159 | } 160 | 161 | /** \brief Release any resources associated with the device. 162 | * \param[in] ca_dev Device to release 163 | * \return ATCA_SUCCESS on success, otherwise an error code. 164 | */ 165 | ATCA_STATUS releaseATCADevice(ATCADevice ca_dev) 166 | { 167 | if (ca_dev == NULL) 168 | { 169 | return ATCA_BAD_PARAM; 170 | } 171 | 172 | return releaseATCAIface(ca_dev->mIface); 173 | } 174 | 175 | /** @} */ 176 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_device.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief Microchip Crypto Auth device object 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | #ifndef ATCA_DEVICE_H 30 | #define ATCA_DEVICE_H 31 | 32 | #include "atca_command.h" 33 | #include "atca_iface.h" 34 | /** \defgroup device ATCADevice (atca_) 35 | @{ */ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /** \brief atca_device is the C object backing ATCADevice. See the 42 | * atca_device.h file for details on the ATCADevice methods. 43 | */ 44 | struct atca_device 45 | { 46 | ATCACommand mCommands; //!< Command set for a given CryptoAuth device 47 | ATCAIface mIface; //!< Physical interface 48 | }; 49 | 50 | typedef struct atca_device * ATCADevice; 51 | 52 | ATCA_STATUS initATCADevice(ATCAIfaceCfg* cfg, ATCADevice cadev); 53 | ATCADevice newATCADevice(ATCAIfaceCfg *cfg); 54 | ATCA_STATUS releaseATCADevice(ATCADevice ca_dev); 55 | void deleteATCADevice(ATCADevice *ca_dev); 56 | 57 | ATCACommand atGetCommands(ATCADevice dev); 58 | ATCAIface atGetIFace(ATCADevice dev); 59 | 60 | 61 | 62 | #ifdef __cplusplus 63 | } 64 | #endif 65 | /** @} */ 66 | #endif 67 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_devtypes.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Microchip Crypto Auth 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | 29 | #ifndef ATCA_DEVTYPES_H_ 30 | #define ATCA_DEVTYPES_H_ 31 | 32 | /** \defgroup device ATCADevice (atca_) 33 | @{ */ 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | 40 | /** \brief The supported Device type in Cryptoauthlib library */ 41 | typedef enum 42 | { 43 | ATSHA204A, 44 | ATECC108A, 45 | ATECC508A, 46 | ATECC608A, 47 | ATSHA206A, 48 | ATCA_DEV_UNKNOWN = 0x20 49 | } ATCADeviceType; 50 | 51 | #ifdef __cplusplus 52 | } 53 | #endif 54 | /** @} */ 55 | #endif /* ATCA_DEVTYPES_H_ */ -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_execution.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Defines an execution handler that executes a given command on a 4 | * device and returns the results. 5 | * 6 | * The basic flow is to wake the device, send the command, wait/poll for 7 | * completion, and finally receives the response from the device and does 8 | * basic checks before returning to caller. 9 | * 10 | * This handler supports the ATSHA and ATECC device family. 11 | * 12 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 13 | * 14 | * \page License 15 | * 16 | * Subject to your compliance with these terms, you may use Microchip software 17 | * and any derivatives exclusively with Microchip products. It is your 18 | * responsibility to comply with third party license terms applicable to your 19 | * use of third party software (including open source software) that may 20 | * accompany Microchip software. 21 | * 22 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 23 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 24 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 25 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 26 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 27 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 28 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 29 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 30 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 31 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 32 | * THIS SOFTWARE. 33 | */ 34 | 35 | 36 | #ifndef ATCA_EXECUTION_H 37 | #define ATCA_EXECUTION_H 38 | 39 | #include "atca_status.h" 40 | #include "atca_command.h" 41 | #include "atca_device.h" 42 | #include "atca_config.h" 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | #define ATCA_UNSUPPORTED_CMD ((uint16_t)0xFFFF) 49 | 50 | #ifdef ATCA_NO_POLL 51 | /** \brief Structure to hold the device execution time and the opcode for the 52 | * corresponding command 53 | */ 54 | typedef struct 55 | { 56 | uint8_t opcode; 57 | uint16_t execution_time_msec; 58 | }device_execution_time_t; 59 | 60 | ATCA_STATUS atGetExecTime(uint8_t opcode, ATCACommand ca_cmd); 61 | #endif 62 | 63 | ATCA_STATUS atca_execute_command(ATCAPacket* packet, ATCADevice device); 64 | 65 | #ifdef __cplusplus 66 | } 67 | #endif 68 | #endif 69 | 70 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_status.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief Microchip Crypto Auth status codes 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | #ifndef _ATCA_STATUS_H 30 | #define _ATCA_STATUS_H 31 | 32 | #include 33 | #include "atca_bool.h" 34 | 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /* all status codes for the ATCA lib are defined here */ 40 | 41 | typedef enum 42 | { 43 | ATCA_SUCCESS = 0x00, //!< Function succeeded. 44 | ATCA_CONFIG_ZONE_LOCKED = 0x01, 45 | ATCA_DATA_ZONE_LOCKED = 0x02, 46 | ATCA_WAKE_FAILED = 0xD0, //!< response status byte indicates CheckMac failure (status byte = 0x01) 47 | ATCA_CHECKMAC_VERIFY_FAILED = 0xD1, //!< response status byte indicates CheckMac failure (status byte = 0x01) 48 | ATCA_PARSE_ERROR = 0xD2, //!< response status byte indicates parsing error (status byte = 0x03) 49 | ATCA_STATUS_CRC = 0xD4, //!< response status byte indicates DEVICE did not receive data properly (status byte = 0xFF) 50 | ATCA_STATUS_UNKNOWN = 0xD5, //!< response status byte is unknown 51 | ATCA_STATUS_ECC = 0xD6, //!< response status byte is ECC fault (status byte = 0x05) 52 | ATCA_STATUS_SELFTEST_ERROR = 0xD7, //!< response status byte is Self Test Error, chip in failure mode (status byte = 0x07) 53 | ATCA_FUNC_FAIL = 0xE0, //!< Function could not execute due to incorrect condition / state. 54 | ATCA_GEN_FAIL = 0xE1, //!< unspecified error 55 | ATCA_BAD_PARAM = 0xE2, //!< bad argument (out of range, null pointer, etc.) 56 | ATCA_INVALID_ID = 0xE3, //!< invalid device id, id not set 57 | ATCA_INVALID_SIZE = 0xE4, //!< Count value is out of range or greater than buffer size. 58 | ATCA_RX_CRC_ERROR = 0xE5, //!< CRC error in data received from device 59 | ATCA_RX_FAIL = 0xE6, //!< Timed out while waiting for response. Number of bytes received is > 0. 60 | ATCA_RX_NO_RESPONSE = 0xE7, //!< Not an error while the Command layer is polling for a command response. 61 | ATCA_RESYNC_WITH_WAKEUP = 0xE8, //!< Re-synchronization succeeded, but only after generating a Wake-up 62 | ATCA_PARITY_ERROR = 0xE9, //!< for protocols needing parity 63 | ATCA_TX_TIMEOUT = 0xEA, //!< for Microchip PHY protocol, timeout on transmission waiting for master 64 | ATCA_RX_TIMEOUT = 0xEB, //!< for Microchip PHY protocol, timeout on receipt waiting for master 65 | ATCA_TOO_MANY_COMM_RETRIES = 0xEC, //!< Device did not respond too many times during a transmission. Could indicate no device present. 66 | ATCA_SMALL_BUFFER = 0xED, //!< Supplied buffer is too small for data required 67 | ATCA_COMM_FAIL = 0xF0, //!< Communication with device failed. Same as in hardware dependent modules. 68 | ATCA_TIMEOUT = 0xF1, //!< Timed out while waiting for response. Number of bytes received is 0. 69 | ATCA_BAD_OPCODE = 0xF2, //!< opcode is not supported by the device 70 | ATCA_WAKE_SUCCESS = 0xF3, //!< received proper wake token 71 | ATCA_EXECUTION_ERROR = 0xF4, //!< chip was in a state where it could not execute the command, response status byte indicates command execution error (status byte = 0x0F) 72 | ATCA_UNIMPLEMENTED = 0xF5, //!< Function or some element of it hasn't been implemented yet 73 | ATCA_ASSERT_FAILURE = 0xF6, //!< Code failed run-time consistency check 74 | ATCA_TX_FAIL = 0xF7, //!< Failed to write 75 | ATCA_NOT_LOCKED = 0xF8, //!< required zone was not locked 76 | ATCA_NO_DEVICES = 0xF9, //!< For protocols that support device discovery (kit protocol), no devices were found 77 | ATCA_HEALTH_TEST_ERROR = 0xFA, //!< random number generator health test error 78 | ATCA_ALLOC_FAILURE = 0xFB, //!< Couldn't allocate required memory 79 | ATCA_USE_FLAGS_CONSUMED = 0xFC, //!< Use flags on the device indicates its consumed fully 80 | } ATCA_STATUS; 81 | 82 | #ifdef __cplusplus 83 | } 84 | #endif 85 | #endif 86 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atca_version.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief Microchip CryptoAuth Library Version 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | #ifndef _ATCA_VERSION_H 30 | #define _ATCA_VERSION_H 31 | 32 | // Version format yyyymmdd 33 | #define ATCA_LIBRARY_VERSION_DATE "20200203" 34 | #define ATCA_LIBRARY_VERSION_MAJOR 3 35 | #define ATCA_LIBRARY_VERSION_MINOR 1 36 | #define ATCA_LIBRARY_VERSION_BUILD 0 37 | 38 | #endif /* _ATCA_VERSION_H */ 39 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atcacert/atcacert.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Declarations common to all atcacert code. 4 | * 5 | * These are common definitions used by all the atcacert code. 6 | * 7 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 8 | * 9 | * \page License 10 | * 11 | * Subject to your compliance with these terms, you may use Microchip software 12 | * and any derivatives exclusively with Microchip products. It is your 13 | * responsibility to comply with third party license terms applicable to your 14 | * use of third party software (including open source software) that may 15 | * accompany Microchip software. 16 | * 17 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 18 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 19 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 20 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 21 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 22 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 23 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 24 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 25 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 26 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 27 | * THIS SOFTWARE. 28 | */ 29 | 30 | #ifndef ATCACERT_H 31 | #define ATCACERT_H 32 | 33 | #include 34 | #include 35 | 36 | /** \defgroup atcacert_ Certificate manipulation methods (atcacert_) 37 | * 38 | * \brief 39 | * These methods provide convenient ways to perform certification I/O with 40 | * CryptoAuth chips and perform certificate manipulation in memory 41 | * 42 | @{ */ 43 | #ifndef FALSE 44 | #define FALSE (0) 45 | #endif 46 | #ifndef TRUE 47 | #define TRUE (1) 48 | #endif 49 | 50 | #define ATCACERT_E_SUCCESS 0 //!< Operation completed successfully. 51 | #define ATCACERT_E_ERROR 1 //!< General error. 52 | #define ATCACERT_E_BAD_PARAMS 2 //!< Invalid/bad parameter passed to function. 53 | #define ATCACERT_E_BUFFER_TOO_SMALL 3 //!< Supplied buffer for output is too small to hold the result. 54 | #define ATCACERT_E_DECODING_ERROR 4 //!< Data being decoded/parsed has an invalid format. 55 | #define ATCACERT_E_INVALID_DATE 5 //!< Date is invalid. 56 | #define ATCACERT_E_UNIMPLEMENTED 6 //!< Function is unimplemented for the current configuration. 57 | #define ATCACERT_E_UNEXPECTED_ELEM_SIZE 7 //!< A certificate element size was not what was expected. 58 | #define ATCACERT_E_ELEM_MISSING 8 //!< The certificate element isn't defined for the certificate definition. 59 | #define ATCACERT_E_ELEM_OUT_OF_BOUNDS 9 //!< Certificate element is out of bounds for the given certificate. 60 | #define ATCACERT_E_BAD_CERT 10 //!< Certificate structure is bad in some way. 61 | #define ATCACERT_E_WRONG_CERT_DEF 11 62 | #define ATCACERT_E_VERIFY_FAILED 12 //!< Certificate or challenge/response verification failed. 63 | #define ATCACERT_E_INVALID_TRANSFORM 13 //!< Invalid transform passed to function. 64 | 65 | /** @} */ 66 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atcacert/atcacert_host_hw.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief host side methods using CryptoAuth hardware 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #include "atcacert_host_hw.h" 29 | #include "../basic/atca_basic.h" 30 | #include "../crypto/atca_crypto_sw_sha2.h" 31 | 32 | 33 | 34 | 35 | 36 | int atcacert_verify_cert_hw(const atcacert_def_t* cert_def, 37 | const uint8_t* cert, 38 | size_t cert_size, 39 | const uint8_t ca_public_key[64]) 40 | { 41 | int ret = 0; 42 | uint8_t tbs_digest[32]; 43 | uint8_t signature[64]; 44 | bool is_verified = false; 45 | 46 | if (cert_def == NULL || ca_public_key == NULL || cert == NULL) 47 | { 48 | return ATCACERT_E_BAD_PARAMS; 49 | } 50 | 51 | ret = atcacert_get_tbs_digest(cert_def, cert, cert_size, tbs_digest); 52 | if (ret != ATCACERT_E_SUCCESS) 53 | { 54 | return ret; 55 | } 56 | 57 | ret = atcacert_get_signature(cert_def, cert, cert_size, signature); 58 | if (ret != ATCACERT_E_SUCCESS) 59 | { 60 | return ret; 61 | } 62 | 63 | ret = atcab_verify_extern(tbs_digest, signature, ca_public_key, &is_verified); 64 | if (ret != ATCA_SUCCESS) 65 | { 66 | return ret; 67 | } 68 | 69 | return is_verified ? ATCACERT_E_SUCCESS : ATCACERT_E_VERIFY_FAILED; 70 | } 71 | 72 | 73 | 74 | 75 | int atcacert_gen_challenge_hw(uint8_t challenge[32]) 76 | { 77 | if (challenge == NULL) 78 | { 79 | return ATCACERT_E_BAD_PARAMS; 80 | } 81 | 82 | return atcab_random(challenge); 83 | } 84 | 85 | 86 | int atcacert_verify_response_hw(const uint8_t device_public_key[64], 87 | const uint8_t challenge[32], 88 | const uint8_t response[64]) 89 | { 90 | int ret = 0; 91 | bool is_verified = false; 92 | 93 | if (device_public_key == NULL || challenge == NULL || response == NULL) 94 | { 95 | return ATCACERT_E_BAD_PARAMS; 96 | } 97 | 98 | ret = atcab_verify_extern(challenge, response, device_public_key, &is_verified); 99 | if (ret != ATCA_SUCCESS) 100 | { 101 | return ret; 102 | } 103 | 104 | return is_verified ? ATCACERT_E_SUCCESS : ATCACERT_E_VERIFY_FAILED; 105 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atcacert/atcacert_host_hw.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief host side methods using CryptoAuth hardware 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | #ifndef ATCACERT_HOST_HA_H 28 | #define ATCACERT_HOST_HA_H 29 | 30 | #include 31 | #include 32 | #include "atcacert_def.h" 33 | 34 | // Inform function naming when compiling in C++ 35 | #ifdef __cplusplus 36 | extern "C" { 37 | #endif 38 | 39 | /** \defgroup atcacert_ Certificate manipulation methods (atcacert_) 40 | * 41 | * \brief 42 | * These methods provide convenient ways to perform certification I/O with 43 | * CryptoAuth chips and perform certificate manipulation in memory 44 | * 45 | @{ */ 46 | 47 | /** 48 | * \brief Verify a certificate against its certificate authority's public key using the host's ATECC 49 | * device for crypto functions. 50 | * 51 | * \param[in] cert_def Certificate definition describing how to extract the TBS and signature 52 | * components from the certificate specified. 53 | * \param[in] cert Certificate to verify. 54 | * \param[in] cert_size Size of the certificate (cert) in bytes. 55 | * \param[in] ca_public_key The ECC P256 public key of the certificate authority that signed this 56 | * certificate. Formatted as the 32 byte X and Y integers concatenated 57 | * together (64 bytes total). 58 | * 59 | * \return ATCACERT_E_SUCCESS if the verify succeeds, ATCACERT_VERIFY_FAILED or ATCA_EXECUTION_ERROR if it fails to 60 | * verify. ATCA_EXECUTION_ERROR may occur when the public key is invalid and doesn't fall 61 | * on the P256 curve. 62 | */ 63 | int atcacert_verify_cert_hw(const atcacert_def_t* cert_def, 64 | const uint8_t* cert, 65 | size_t cert_size, 66 | const uint8_t ca_public_key[64]); 67 | 68 | 69 | /** 70 | * \brief Generate a random challenge to be sent to the client using the RNG on the host's ATECC 71 | * device. 72 | * 73 | * \param[out] challenge Random challenge is return here. 32 bytes. 74 | * 75 | * \return ATCACERT_E_SUCCESS on success, otherwise an error code. 76 | */ 77 | int atcacert_gen_challenge_hw(uint8_t challenge[32]); 78 | 79 | 80 | /** 81 | * \brief Verify a client's response to a challenge using the host's ATECC device for crypto 82 | * functions. 83 | * 84 | * The challenge-response protocol is an ECDSA Sign and Verify. This performs an ECDSA verify on the 85 | * response returned by the client, verifying the client has the private key counter-part to the 86 | * public key returned in its certificate. 87 | * 88 | * \param[in] device_public_key Device public key as read from its certificate. Formatted as the X 89 | * and Y integers concatenated together. 64 bytes. 90 | * \param[in] challenge Challenge that was sent to the client. 32 bytes. 91 | * \param[in] response Response returned from the client to be verified. 64 bytes. 92 | * 93 | * \return ATCACERT_E_SUCCESS if the verify succeeds, ATCACERT_VERIFY_FAILED or ATCA_EXECUTION_ERROR if it fails to 94 | * verify. ATCA_EXECUTION_ERROR may occur when the public key is invalid and doesn't fall 95 | * on the P256 curve. 96 | */ 97 | int atcacert_verify_response_hw(const uint8_t device_public_key[64], 98 | const uint8_t challenge[32], 99 | const uint8_t response[64]); 100 | 101 | /** @} */ 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | 106 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atcacert/atcacert_host_sw.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief host side methods using software implementations 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #include "atcacert_host_sw.h" 29 | #include "../crypto/atca_crypto_sw_sha2.h" 30 | #include "../crypto/atca_crypto_sw_ecdsa.h" 31 | #include "../crypto/atca_crypto_sw_rand.h" 32 | 33 | 34 | 35 | 36 | 37 | 38 | int atcacert_verify_cert_sw(const atcacert_def_t* cert_def, 39 | const uint8_t* cert, 40 | size_t cert_size, 41 | const uint8_t ca_public_key[64]) 42 | { 43 | int ret = 0; 44 | uint8_t tbs_digest[32]; 45 | uint8_t signature[64]; 46 | 47 | if (cert_def == NULL || ca_public_key == NULL || cert == NULL) 48 | { 49 | return ATCACERT_E_BAD_PARAMS; 50 | } 51 | 52 | ret = atcacert_get_tbs_digest(cert_def, cert, cert_size, tbs_digest); 53 | if (ret != ATCACERT_E_SUCCESS) 54 | { 55 | return ret; 56 | } 57 | 58 | ret = atcacert_get_signature(cert_def, cert, cert_size, signature); 59 | if (ret != ATCACERT_E_SUCCESS) 60 | { 61 | return ret; 62 | } 63 | 64 | ret = atcac_sw_ecdsa_verify_p256(tbs_digest, signature, ca_public_key); 65 | if (ret != ATCACERT_E_SUCCESS) 66 | { 67 | return ret; 68 | } 69 | 70 | return ATCACERT_E_SUCCESS; 71 | } 72 | 73 | 74 | 75 | int atcacert_gen_challenge_sw(uint8_t challenge[32]) 76 | { 77 | if (challenge == NULL) 78 | { 79 | return ATCACERT_E_BAD_PARAMS; 80 | } 81 | 82 | return atcac_sw_random(challenge, 32); 83 | } 84 | 85 | 86 | 87 | int atcacert_verify_response_sw(const uint8_t device_public_key[64], 88 | const uint8_t challenge[32], 89 | const uint8_t response[64]) 90 | { 91 | if (device_public_key == NULL || challenge == NULL || response == NULL) 92 | { 93 | return ATCACERT_E_BAD_PARAMS; 94 | } 95 | 96 | return atcac_sw_ecdsa_verify_p256(challenge, response, device_public_key); 97 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atcacert/atcacert_host_sw.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Host side methods using software implementations. host-side, the one authenticating 4 | * a client, of the authentication process. Crypto functions are performed using a software library. 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | #ifndef ATCACERT_HOST_SOFT_H 30 | #define ATCACERT_HOST_SOFT_H 31 | 32 | #include 33 | #include 34 | #include "atcacert_def.h" 35 | 36 | // Inform function naming when compiling in C++ 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | 42 | /** \defgroup atcacert_ Certificate manipulation methods (atcacert_) 43 | * 44 | * \brief 45 | * These methods provide convenient ways to perform certification I/O with 46 | * CryptoAuth chips and perform certificate manipulation in memory 47 | * 48 | @{ */ 49 | 50 | /** 51 | * \brief Verify a certificate against its certificate authority's public key using software crypto 52 | * functions.The function is currently not implemented. 53 | * 54 | * \param[in] cert_def Certificate definition describing how to extract the TBS and signature 55 | * components from the certificate specified. 56 | * \param[in] cert Certificate to verify. 57 | * \param[in] cert_size Size of the certificate (cert) in bytes. 58 | * \param[in] ca_public_key The ECC P256 public key of the certificate authority that signed this 59 | * certificate. Formatted as the 32 byte X and Y integers concatenated 60 | * together (64 bytes total). 61 | * 62 | * \return ATCA_UNIMPLEMENTED , as the function is currently not implemented. 63 | */ 64 | int atcacert_verify_cert_sw(const atcacert_def_t* cert_def, 65 | const uint8_t* cert, 66 | size_t cert_size, 67 | const uint8_t ca_public_key[64]); 68 | 69 | 70 | 71 | /** 72 | * \brief Generate a random challenge to be sent to the client using a software PRNG.The function is currently not implemented. 73 | * 74 | * \param[out] challenge Random challenge is return here. 32 bytes. 75 | * 76 | * \return ATCA_UNIMPLEMENTED , as the function is currently not implemented. 77 | */ 78 | int atcacert_gen_challenge_sw(uint8_t challenge[32]); 79 | 80 | 81 | 82 | 83 | /** 84 | * \brief Verify a client's response to a challenge using software crypto functions.The function is currently not implemented. 85 | * 86 | * The challenge-response protocol is an ECDSA Sign and Verify. This performs an ECDSA verify on the 87 | * response returned by the client, verifying the client has the private key counter-part to the 88 | * public key returned in its certificate. 89 | * 90 | * \param[in] device_public_key Device public key as read from its certificate. Formatted as the X 91 | * and Y integers concatenated together. 64 bytes. 92 | * \param[in] challenge Challenge that was sent to the client. 32 bytes. 93 | * \param[in] response Response returned from the client to be verified. 64 bytes. 94 | * 95 | * \return ATCA_UNIMPLEMENTED , as the function is currently not implemented. 96 | */ 97 | int atcacert_verify_response_sw(const uint8_t device_public_key[64], 98 | const uint8_t challenge[32], 99 | const uint8_t response[64]); 100 | 101 | /** @} */ 102 | #ifdef __cplusplus 103 | } 104 | #endif 105 | 106 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/atcacert/atcacert_pem.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Functions for converting between DER and PEM formats. 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCACERT_PEM_H 29 | #define ATCACERT_PEM_H 30 | 31 | #include 32 | 33 | #ifdef __cplusplus 34 | extern "C" { 35 | #endif 36 | 37 | #define PEM_CERT_BEGIN "-----BEGIN CERTIFICATE-----" 38 | #define PEM_CERT_END "-----END CERTIFICATE-----" 39 | #define PEM_CSR_BEGIN "-----BEGIN CERTIFICATE REQUEST-----" 40 | #define PEM_CSR_END "-----END CERTIFICATE REQUEST-----" 41 | 42 | /** 43 | * \brief Encode a DER data in PEM format. 44 | * \param[in] der DER data to be encoded as PEM. 45 | * \param[out] der_size DER data size in bytes. 46 | * \param[out] pem PEM encoded data is returned here. 47 | * \param[inout] pem_size As input, the size of the pem buffer. 48 | * As output, the size of the PEM data. 49 | * \param[in] header Header to place at the beginning of the PEM data. 50 | * \param[in] footer Footer to place at the end of the PEM data. 51 | * \return ATCA_SUCCESS on success, otherwise an error code. 52 | */ 53 | int atcacert_encode_pem(const uint8_t* der, 54 | size_t der_size, 55 | char* pem, 56 | size_t* pem_size, 57 | const char* header, 58 | const char* footer); 59 | 60 | /** 61 | * \brief Decode PEM data into DER format. 62 | * \param[in] pem PEM data to decode to DER. 63 | * \param[in] pem_size PEM data size in bytes. 64 | * \param[out] der DER data is returned here. 65 | * \param[inout] der_size As input, the size of the der buffer. 66 | * As output, the size of the DER data. 67 | * \param[in] header Header to find the beginning of the PEM data. 68 | * \param[in] footer Footer to find the end of the PEM data. 69 | * \return ATCA_SUCCESS on success, otherwise an error code. 70 | */ 71 | int atcacert_decode_pem(const char* pem, 72 | size_t pem_size, 73 | uint8_t* der, 74 | size_t* der_size, 75 | const char* header, 76 | const char* footer); 77 | 78 | /** 79 | * \brief Encode a DER certificate in PEM format. 80 | * \param[in] der_cert DER certificate to be encoded as PEM. 81 | * \param[out] der_cert_size DER certificate size in bytes. 82 | * \param[out] pem_cert PEM encoded certificate is returned here. 83 | * \param[inout] pem_cert_size As input, the size of the pem_cert buffer. 84 | * As output, the size of the PEM certificate. 85 | * \return ATCA_SUCCESS on success, otherwise an error code. 86 | */ 87 | int atcacert_encode_pem_cert(const uint8_t* der_cert, size_t der_cert_size, char* pem_cert, size_t* pem_cert_size); 88 | 89 | /** 90 | * \brief Decode a PEM certificate into DER format. 91 | * \param[in] pem_cert PEM certificate to decode to DER. 92 | * \param[in] pem_cert_size PEM certificate size in bytes. 93 | * \param[out] der_cert DER certificate is returned here. 94 | * \param[inout] der_cert_size As input, the size of the der_cert buffer. 95 | * As output, the size of the DER certificate. 96 | * \return ATCA_SUCCESS on success, otherwise an error code. 97 | */ 98 | int atcacert_decode_pem_cert(const char* pem_cert, size_t pem_cert_size, uint8_t* der_cert, size_t* der_cert_size); 99 | 100 | /** 101 | * \brief Encode a DER CSR in PEM format. 102 | * \param[in] der_csr DER CSR to be encoded as PEM. 103 | * \param[out] der_csr_size DER CSR size in bytes. 104 | * \param[out] pem_csr PEM encoded CSR is returned here. 105 | * \param[inout] pem_csr_size As input, the size of the pem_csr buffer. 106 | * As output, the size of the PEM CSR. 107 | * \return ATCA_SUCCESS on success, otherwise an error code. 108 | */ 109 | int atcacert_encode_pem_csr(const uint8_t* der_csr, size_t der_csr_size, char* pem_csr, size_t* pem_csr_size); 110 | 111 | /** 112 | * \brief Extract the CSR certificate bytes from a PEM encoded CSR certificate 113 | * \param[in] pem_csr PEM CSR to decode to DER. 114 | * \param[in] pem_csr_size PEM CSR size in bytes. 115 | * \param[out] der_csr DER CSR is returned here. 116 | * \param[inout] der_csr_size As input, the size of the der_csr buffer. 117 | * As output, the size of the DER CSR. 118 | * \return ATCA_SUCCESS on success, otherwise an error code. 119 | */ 120 | int atcacert_decode_pem_csr(const char* pem_csr, size_t pem_csr_size, uint8_t* der_csr, size_t* der_csr_size); 121 | 122 | #ifdef __cplusplus 123 | } 124 | #endif 125 | 126 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/README.md: -------------------------------------------------------------------------------- 1 | basic directory - Purpose 2 | ========================= 3 | The purpose of this directory is to contain the files implementing the APIs for 4 | a basic interface to the core CryptoAuthLib library. 5 | 6 | High-level functions like these make it very convenient to use the library when 7 | standard configurations and defaults are in play. They are the easiest to use 8 | when developing examples or trying to understand the "flow" of an 9 | authentication operation without getting overwhelmed by the details. 10 | 11 | This makes simple jobs easy and if you need more sophistication and power, you 12 | can employ the full power of the CryptoAuthLib object model. 13 | 14 | See the Doxygen documentation in cryptoauthlib/docs for details on the API of 15 | the Basic commands. 16 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_aes_cbc.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for AES CBC mode. 4 | * 5 | * The AES command supports 128-bit AES encryption or decryption of small 6 | * messages or data packets in ECB mode. Also can perform GFM (Galois Field 7 | * Multiply) calculation in support of AES-GCM. 8 | * 9 | * \note List of devices that support this command - ATECC608A. Refer to device 10 | * datasheet for full details. 11 | * 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | #include "atca_basic.h" 36 | #ifdef _WIN32 37 | #include 38 | #endif 39 | 40 | /** \brief Initialize context for AES CBC operation. 41 | * 42 | * \param[in] ctx AES CBC context to be initialized 43 | * \param[in] key_id Key location. Can either be a slot number or 44 | * ATCA_TEMPKEY_KEYID for TempKey. 45 | * \param[in] key_block Index of the 16-byte block to use within the key 46 | * location for the actual key. 47 | * \param[in] iv Initialization vector (16 bytes). 48 | * 49 | * \return ATCA_SUCCESS on success, otherwise an error code. 50 | */ 51 | ATCA_STATUS atcab_aes_cbc_init(atca_aes_cbc_ctx_t* ctx, uint16_t key_id, uint8_t key_block, const uint8_t* iv) 52 | { 53 | if (ctx == NULL || iv == NULL) 54 | { 55 | return ATCA_BAD_PARAM; 56 | } 57 | 58 | memset(ctx, 0, sizeof(*ctx)); 59 | ctx->key_id = key_id; 60 | ctx->key_block = key_block; 61 | memcpy(ctx->ciphertext, iv, sizeof(ctx->ciphertext)); 62 | 63 | return ATCA_SUCCESS; 64 | } 65 | 66 | /** \brief Encrypt a block of data using CBC mode and a key within the 67 | * ATECC608A. atcab_aes_cbc_init() should be called before the 68 | * first use of this function. 69 | * 70 | * \param[in] ctx AES CBC context. 71 | * \param[in] plaintext Plaintext to be encrypted (16 bytes). 72 | * \param[out] ciphertext Encrypted data is returned here (16 bytes). 73 | * 74 | * \return ATCA_SUCCESS on success, otherwise an error code. 75 | */ 76 | ATCA_STATUS atcab_aes_cbc_encrypt_block(atca_aes_cbc_ctx_t* ctx, const uint8_t* plaintext, uint8_t* ciphertext) 77 | { 78 | uint8_t input[AES_DATA_SIZE]; 79 | int i; 80 | ATCA_STATUS status = ATCA_SUCCESS; 81 | 82 | if (ctx == NULL || plaintext == NULL || ciphertext == NULL) 83 | { 84 | return ATCA_BAD_PARAM; 85 | } 86 | 87 | // XOR plaintext with previous block's ciphertext to get input value to block encrypt 88 | for (i = 0; i < AES_DATA_SIZE; i++) 89 | { 90 | input[i] = plaintext[i] ^ ctx->ciphertext[i]; 91 | } 92 | 93 | // Block encrypt of input data 94 | status = atcab_aes_encrypt(ctx->key_id, ctx->key_block, input, ciphertext); 95 | if (status != ATCA_SUCCESS) 96 | { 97 | return status; 98 | } 99 | 100 | // Save copy of ciphertext for next block operation 101 | memcpy(ctx->ciphertext, ciphertext, AES_DATA_SIZE); 102 | 103 | return status; 104 | } 105 | 106 | /** \brief Decrypt a block of data using CBC mode and a key within the 107 | * ATECC608A. atcab_aes_cbc_init() should be called before the 108 | * first use of this function. 109 | * 110 | * \param[in] ctx AES CBC context. 111 | * \param[in] ciphertext Ciphertext to be decrypted (16 bytes). 112 | * \param[out] plaintext Decrypted data is returned here (16 bytes). 113 | * 114 | * \return ATCA_SUCCESS on success, otherwise an error code. 115 | */ 116 | ATCA_STATUS atcab_aes_cbc_decrypt_block(atca_aes_cbc_ctx_t* ctx, const uint8_t* ciphertext, uint8_t* plaintext) 117 | { 118 | uint8_t output[AES_DATA_SIZE]; 119 | int i; 120 | ATCA_STATUS status = ATCA_SUCCESS; 121 | 122 | if (ctx == NULL || ciphertext == NULL || plaintext == NULL) 123 | { 124 | return ATCA_BAD_PARAM; 125 | } 126 | 127 | // Block decrypt of ciphertext 128 | status = atcab_aes_decrypt(ctx->key_id, ctx->key_block, ciphertext, output); 129 | if (status != ATCA_SUCCESS) 130 | { 131 | return status; 132 | } 133 | 134 | // XOR output with previous block's ciphertext to get plaintext 135 | for (i = 0; i < AES_DATA_SIZE; i++) 136 | { 137 | plaintext[i] = output[i] ^ ctx->ciphertext[i]; 138 | } 139 | 140 | // Save copy of ciphertext for next block operation 141 | memcpy(ctx->ciphertext, ciphertext, AES_DATA_SIZE); 142 | 143 | return status; 144 | } 145 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_aes_gcm.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Unity tests for the cryptoauthlib AES GCM functions. 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | #ifndef ATCA_BASIC_AES_GCM_H_ 28 | #define ATCA_BASIC_AES_GCM_H_ 29 | 30 | #include "ATECCX08A_Arduino/cryptoauthlib.h" 31 | 32 | /** \ingroup atcab_ 33 | * @{ 34 | */ 35 | 36 | extern const char* atca_basic_aes_gcm_version; 37 | 38 | /** Context structure for AES GCM operations. 39 | */ 40 | typedef struct atca_aes_gcm_ctx 41 | { 42 | uint16_t key_id; //!< Key location. Can either be a slot number or ATCA_TEMPKEY_KEYID for TempKey. 43 | uint8_t key_block; //!< Index of the 16-byte block to use within the key location for the actual key. 44 | uint8_t cb[AES_DATA_SIZE]; //!< Counter block, comprises of nonce + count value (16 bytes). 45 | uint32_t data_size; //!< Size of the data being encrypted/decrypted in bytes. 46 | uint32_t aad_size; //!< Size of the additional authenticated data in bytes. 47 | uint8_t h[AES_DATA_SIZE]; //!< Subkey for ghash functions in GCM. 48 | uint8_t j0[AES_DATA_SIZE]; //!< Precounter block generated from IV. 49 | uint8_t y[AES_DATA_SIZE]; //!< Current GHASH output 50 | uint8_t partial_aad[AES_DATA_SIZE]; //!< Partial blocks of data waiting to be processed 51 | uint32_t partial_aad_size; //!< Amount of data in the partial block buffer 52 | uint8_t enc_cb[AES_DATA_SIZE]; //!< Last encrypted counter block 53 | uint8_t ciphertext_block[AES_DATA_SIZE]; //!< Last ciphertext block 54 | } atca_aes_gcm_ctx_t; 55 | 56 | ATCA_STATUS atcab_aes_gcm_init(atca_aes_gcm_ctx_t* ctx, uint16_t key_id, uint8_t key_block, const uint8_t* iv, size_t iv_size); 57 | ATCA_STATUS atcab_aes_gcm_init_rand(atca_aes_gcm_ctx_t* ctx, uint16_t key_id, uint8_t key_block, size_t rand_size, 58 | const uint8_t* free_field, size_t free_field_size, uint8_t* iv); 59 | ATCA_STATUS atcab_aes_gcm_aad_update(atca_aes_gcm_ctx_t* ctx, const uint8_t* aad, uint32_t aad_size); 60 | ATCA_STATUS atcab_aes_gcm_encrypt_update(atca_aes_gcm_ctx_t* ctx, const uint8_t* plaintext, uint32_t plaintext_size, uint8_t* ciphertext); 61 | ATCA_STATUS atcab_aes_gcm_encrypt_finish(atca_aes_gcm_ctx_t* ctx, uint8_t* tag, size_t tag_size); 62 | ATCA_STATUS atcab_aes_gcm_decrypt_update(atca_aes_gcm_ctx_t* ctx, const uint8_t* ciphertext, uint32_t ciphertext_size, uint8_t* plaintext); 63 | ATCA_STATUS atcab_aes_gcm_decrypt_finish(atca_aes_gcm_ctx_t* ctx, const uint8_t* tag, size_t tag_size, bool* is_verified); 64 | 65 | /** @} */ 66 | 67 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_checkmac.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for CheckMAC command. 4 | * 5 | * The CheckMac command calculates a MAC response that would have been 6 | * generated on a different CryptoAuthentication device and then compares the 7 | * result with input value. 8 | * 9 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 10 | * ATECC508A, and ATECC608A. There are differences in the modes that they 11 | * support. Refer to device datasheets for full details. 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | 36 | #include "atca_basic.h" 37 | #include "../atca_execution.h" 38 | 39 | /** \brief Compares a MAC response with input values 40 | * \param[in] mode Controls which fields within the device are used in 41 | * the message 42 | * \param[in] key_id Key location in the CryptoAuth device to use for the 43 | * MAC 44 | * \param[in] challenge Challenge data (32 bytes) 45 | * \param[in] response MAC response data (32 bytes) 46 | * \param[in] other_data OtherData parameter (13 bytes) 47 | * \return ATCA_SUCCESS on success, otherwise an error code. 48 | */ 49 | ATCA_STATUS atcab_checkmac(uint8_t mode, uint16_t key_id, const uint8_t *challenge, const uint8_t *response, const uint8_t *other_data) 50 | { 51 | ATCAPacket packet; 52 | ATCACommand ca_cmd = _gDevice->mCommands; 53 | ATCA_STATUS status = ATCA_GEN_FAIL; 54 | 55 | // Verify the inputs 56 | if (response == NULL || other_data == NULL) 57 | { 58 | return ATCA_BAD_PARAM; 59 | } 60 | if (!(mode & CHECKMAC_MODE_BLOCK2_TEMPKEY) && challenge == NULL) 61 | { 62 | return ATCA_BAD_PARAM; 63 | } 64 | 65 | do 66 | { 67 | // build Check MAC command 68 | packet.param1 = mode; 69 | packet.param2 = key_id; 70 | if (challenge != NULL) 71 | { 72 | memcpy(&packet.data[0], challenge, CHECKMAC_CLIENT_CHALLENGE_SIZE); 73 | } 74 | else 75 | { 76 | memset(&packet.data[0], 0, CHECKMAC_CLIENT_CHALLENGE_SIZE); 77 | } 78 | memcpy(&packet.data[32], response, CHECKMAC_CLIENT_RESPONSE_SIZE); 79 | memcpy(&packet.data[64], other_data, CHECKMAC_OTHER_DATA_SIZE); 80 | 81 | if ((status = atCheckMAC(ca_cmd, &packet)) != ATCA_SUCCESS) 82 | { 83 | break; 84 | } 85 | 86 | if ((status = atca_execute_command( (void*)&packet, _gDevice)) != ATCA_SUCCESS) 87 | { 88 | break; 89 | } 90 | } 91 | while (0); 92 | 93 | return status; 94 | } 95 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_counter.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for Counter command. 4 | * 5 | * The Counter command reads or increments the binary count value for one of the 6 | * two monotonic counters 7 | * 8 | * \note List of devices that support this command - ATECC508A and ATECC608A. 9 | * There are differences in the modes that they support. Refer to device 10 | * datasheets for full details. 11 | * 12 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 13 | * 14 | * \page License 15 | * 16 | * Subject to your compliance with these terms, you may use Microchip software 17 | * and any derivatives exclusively with Microchip products. It is your 18 | * responsibility to comply with third party license terms applicable to your 19 | * use of third party software (including open source software) that may 20 | * accompany Microchip software. 21 | * 22 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 23 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 24 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 25 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 26 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 27 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 28 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 29 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 30 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 31 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 32 | * THIS SOFTWARE. 33 | */ 34 | #include "atca_basic.h" 35 | #include "../atca_execution.h" 36 | 37 | 38 | /** \brief Compute the Counter functions 39 | * \param[in] mode the mode used for the counter 40 | * \param[in] counter_id The counter to be used 41 | * \param[out] counter_value pointer to the counter value returned from device 42 | * \return ATCA_SUCCESS on success, otherwise an error code. 43 | */ 44 | 45 | ATCA_STATUS atcab_counter(uint8_t mode, uint16_t counter_id, uint32_t *counter_value) 46 | { 47 | ATCAPacket packet; 48 | ATCACommand ca_cmd = _gDevice->mCommands; 49 | ATCA_STATUS status = ATCA_GEN_FAIL; 50 | 51 | do 52 | { 53 | if (counter_id > 1) 54 | { 55 | return ATCA_BAD_PARAM; 56 | } 57 | 58 | // build a Counter command 59 | packet.param1 = mode; 60 | packet.param2 = counter_id; 61 | 62 | if ((status = atCounter(ca_cmd, &packet)) != ATCA_SUCCESS) 63 | { 64 | break; 65 | } 66 | 67 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 68 | { 69 | break; 70 | } 71 | 72 | if (counter_value != NULL) 73 | { 74 | if (packet.data[ATCA_COUNT_IDX] == 7) 75 | { 76 | *counter_value = ((uint32_t)packet.data[ATCA_RSP_DATA_IDX + 0] << 0) | 77 | ((uint32_t)packet.data[ATCA_RSP_DATA_IDX + 1] << 8) | 78 | ((uint32_t)packet.data[ATCA_RSP_DATA_IDX + 2] << 16) | 79 | ((uint32_t)packet.data[ATCA_RSP_DATA_IDX + 3] << 24); 80 | } 81 | else 82 | { 83 | status = ATCA_RX_FAIL; 84 | } 85 | 86 | } 87 | } 88 | while (0); 89 | 90 | return status; 91 | } 92 | 93 | /** \brief Increments one of the device's monotonic counters 94 | * \param[in] counter_id Counter to be incremented 95 | * \param[out] counter_value New value of the counter is returned here. Can be 96 | * NULL if not needed. 97 | * \return ATCA_SUCCESS on success, otherwise an error code. 98 | */ 99 | ATCA_STATUS atcab_counter_increment(uint16_t counter_id, uint32_t* counter_value) 100 | { 101 | return atcab_counter(COUNTER_MODE_INCREMENT, counter_id, counter_value); 102 | } 103 | 104 | /** \brief Read one of the device's monotonic counters 105 | * \param[in] counter_id Counter to be read 106 | * \param[out] counter_value Counter value is returned here. 107 | * \return ATCA_SUCCESS on success, otherwise an error code. 108 | */ 109 | ATCA_STATUS atcab_counter_read(uint16_t counter_id, uint32_t* counter_value) 110 | { 111 | return atcab_counter(COUNTER_MODE_READ, counter_id, counter_value); 112 | } 113 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_derivekey.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for DeriveKey command. 4 | * 5 | * The DeriveKey command combines the current value of a key with the nonce 6 | * stored in TempKey using SHA-256 and derives a new key. 7 | * 8 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 9 | * ATECC508A, and ATECC608A. There are differences in the modes that they 10 | * support. Refer to device datasheets for full details. 11 | * 12 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 13 | * 14 | * \page License 15 | * 16 | * Subject to your compliance with these terms, you may use Microchip software 17 | * and any derivatives exclusively with Microchip products. It is your 18 | * responsibility to comply with third party license terms applicable to your 19 | * use of third party software (including open source software) that may 20 | * accompany Microchip software. 21 | * 22 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 23 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 24 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 25 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 26 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 27 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 28 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 29 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 30 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 31 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 32 | * THIS SOFTWARE. 33 | */ 34 | 35 | #include "atca_basic.h" 36 | #include "../atca_execution.h" 37 | 38 | /** \brief Executes the DeviveKey command for deriving a new key from a 39 | * nonce (TempKey) and an existing key. 40 | * 41 | * \param[in] mode Bit 2 must match the value in TempKey.SourceFlag 42 | * \param[in] target_key Key slot to be written 43 | * \param[in] mac Optional 32 byte MAC used to validate operation. NULL 44 | * if not required. 45 | * 46 | * \return ATCA_SUCCESS on success, otherwise an error code. 47 | */ 48 | ATCA_STATUS atcab_derivekey(uint8_t mode, uint16_t target_key, const uint8_t* mac) 49 | { 50 | ATCAPacket packet; 51 | ATCACommand ca_cmd = _gDevice->mCommands; 52 | ATCA_STATUS status = ATCA_GEN_FAIL; 53 | 54 | do 55 | { 56 | // build a deriveKey command (pass through mode) 57 | packet.param1 = mode; 58 | packet.param2 = target_key; 59 | 60 | if (mac != NULL) 61 | { 62 | memcpy(packet.data, mac, MAC_SIZE); 63 | } 64 | 65 | if ((status = atDeriveKey(ca_cmd, &packet, mac != NULL)) != ATCA_SUCCESS) 66 | { 67 | break; 68 | } 69 | 70 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 71 | { 72 | break; 73 | } 74 | 75 | } 76 | while (0); 77 | 78 | return status; 79 | } 80 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_gendig.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for GenDig command. 4 | * 5 | * The GenDig command uses SHA-256 to combine a stored value with the contents 6 | * of TempKey, which must have been valid prior to the execution of this 7 | * command. 8 | * 9 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 10 | * ATECC508A, and ATECC608A. There are differences in the modes that 11 | * they support. Refer to device datasheets for full details. 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | 36 | #include "atca_basic.h" 37 | #include "../atca_execution.h" 38 | 39 | /** \brief Issues a GenDig command, which performs a SHA256 hash on the source data indicated by zone with the 40 | * contents of TempKey. See the CryptoAuth datasheet for your chip to see what the values of zone 41 | * correspond to. 42 | * \param[in] zone Designates the source of the data to hash with TempKey. 43 | * \param[in] key_id Indicates the key, OTP block, or message order for shared nonce mode. 44 | * \param[in] other_data Four bytes of data for SHA calculation when using a NoMac key, 32 bytes for 45 | * "Shared Nonce" mode, otherwise ignored (can be NULL). 46 | * \param[in] other_data_size Size of other_data in bytes. 47 | * \return ATCA_SUCCESS on success, otherwise an error code. 48 | */ 49 | ATCA_STATUS atcab_gendig(uint8_t zone, uint16_t key_id, const uint8_t *other_data, uint8_t other_data_size) 50 | { 51 | ATCAPacket packet; 52 | ATCACommand ca_cmd = _gDevice->mCommands; 53 | ATCA_STATUS status = ATCA_GEN_FAIL; 54 | bool is_no_mac_key = false; 55 | 56 | if (other_data_size > 0 && other_data == NULL) 57 | { 58 | return ATCA_BAD_PARAM; 59 | } 60 | 61 | do 62 | { 63 | // build gendig command 64 | packet.param1 = zone; 65 | packet.param2 = key_id; 66 | 67 | if (packet.param1 == GENDIG_ZONE_SHARED_NONCE && other_data_size >= ATCA_BLOCK_SIZE) 68 | { 69 | memcpy(&packet.data[0], &other_data[0], ATCA_BLOCK_SIZE); 70 | } 71 | else if (packet.param1 == GENDIG_ZONE_DATA && other_data_size >= ATCA_WORD_SIZE) 72 | { 73 | memcpy(&packet.data[0], &other_data[0], ATCA_WORD_SIZE); 74 | is_no_mac_key = true; 75 | } 76 | 77 | if ((status = atGenDig(ca_cmd, &packet, is_no_mac_key)) != ATCA_SUCCESS) 78 | { 79 | break; 80 | } 81 | 82 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 83 | { 84 | break; 85 | } 86 | 87 | } 88 | while (0); 89 | 90 | return status; 91 | } 92 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_genkey.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for GenKey command. 4 | * 5 | * The GenKey command is used for creating ECC private keys, generating ECC 6 | * public keys, and for digest calculations involving public keys. 7 | * 8 | * \note List of devices that support this command - ATECC108A, ATECC508A, 9 | * ATECC608A. There are differences in the modes that they support. Refer 10 | * to device datasheets for full details. 11 | * 12 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 13 | * 14 | * \page License 15 | * 16 | * Subject to your compliance with these terms, you may use Microchip software 17 | * and any derivatives exclusively with Microchip products. It is your 18 | * responsibility to comply with third party license terms applicable to your 19 | * use of third party software (including open source software) that may 20 | * accompany Microchip software. 21 | * 22 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 23 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 24 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 25 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 26 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 27 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 28 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 29 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 30 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 31 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 32 | * THIS SOFTWARE. 33 | */ 34 | 35 | #include "atca_basic.h" 36 | #include "../atca_execution.h" 37 | 38 | /** \brief Issues GenKey command, which can generate a private key, compute a 39 | * public key, nd/or compute a digest of a public key. 40 | * 41 | * \param[in] mode Mode determines what operations the GenKey 42 | * command performs. 43 | * \param[in] key_id Slot to perform the GenKey command on. 44 | * \param[in] other_data OtherData for PubKey digest calculation. Can be set 45 | * to NULL otherwise. 46 | * \param[out] public_key If the mode indicates a public key will be 47 | * calculated, it will be returned here. Format will 48 | * be the X and Y integers in big-endian format. 49 | * 64 bytes for P256 curve. Set to NULL if public key 50 | * isn't required. 51 | * 52 | * \return ATCA_SUCCESS on success, otherwise an error code. 53 | */ 54 | ATCA_STATUS atcab_genkey_base(uint8_t mode, uint16_t key_id, const uint8_t* other_data, uint8_t* public_key) 55 | { 56 | ATCAPacket packet; 57 | ATCACommand ca_cmd = _gDevice->mCommands; 58 | ATCA_STATUS status = ATCA_GEN_FAIL; 59 | 60 | do 61 | { 62 | // Build GenKey command 63 | packet.param1 = mode; 64 | packet.param2 = key_id; 65 | if (other_data) 66 | { 67 | memcpy(packet.data, other_data, GENKEY_OTHER_DATA_SIZE); 68 | } 69 | 70 | if ((status = atGenKey(ca_cmd, &packet)) != ATCA_SUCCESS) 71 | { 72 | break; 73 | } 74 | 75 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 76 | { 77 | break; 78 | } 79 | 80 | if (public_key != NULL) 81 | { 82 | if (packet.data[ATCA_COUNT_IDX] == (ATCA_PUB_KEY_SIZE + ATCA_PACKET_OVERHEAD)) 83 | { 84 | memcpy(public_key, &packet.data[ATCA_RSP_DATA_IDX], ATCA_PUB_KEY_SIZE); 85 | } 86 | else 87 | { 88 | status = ATCA_RX_FAIL; 89 | } 90 | } 91 | } 92 | while (0); 93 | 94 | return status; 95 | } 96 | 97 | /** \brief Issues GenKey command, which generates a new random private key in 98 | * slot and returns the public key. 99 | * 100 | * \param[in] key_id Slot number where an ECC private key is configured. 101 | * Can also be ATCA_TEMPKEY_KEYID to generate a private 102 | * key in TempKey. 103 | * \param[out] public_key Public key will be returned here. Format will be 104 | * the X and Y integers in big-endian format. 105 | * 64 bytes for P256 curve. Set to NULL if public key 106 | * isn't required. 107 | * 108 | * \return ATCA_SUCCESS on success, otherwise an error code. 109 | */ 110 | ATCA_STATUS atcab_genkey(uint16_t key_id, uint8_t *public_key) 111 | { 112 | return atcab_genkey_base(GENKEY_MODE_PRIVATE, key_id, NULL, public_key); 113 | } 114 | 115 | /** \brief Uses GenKey command to calculate the public key from an existing 116 | * private key in a slot. 117 | * 118 | * \param[in] key_id Slot number of the private key. 119 | * \param[out] public_key Public key will be returned here. Format will be 120 | * the X and Y integers in big-endian format. 121 | * 64 bytes for P256 curve. Set to NULL if public key 122 | * isn't required. 123 | * 124 | * \return ATCA_SUCCESS on success, otherwise an error code. 125 | */ 126 | ATCA_STATUS atcab_get_pubkey(uint16_t key_id, uint8_t *public_key) 127 | { 128 | return atcab_genkey_base(GENKEY_MODE_PUBLIC, key_id, NULL, public_key); 129 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_hmac.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for HMAC command. 4 | * 5 | * The HMAC command computes an HMAC/SHA-256 digest using a key stored in the 6 | * device over a challenge stored in the TempKey register, and/or other 7 | * information stored within the device. 8 | * 9 | * \note List of devices that support this command - ATSHA204A, ATECC108A, and 10 | * ATECC508A . There are differences in the modes that they support. 11 | * Refer to device datasheets for full details. 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | 36 | 37 | #include "atca_basic.h" 38 | #include "../atca_execution.h" 39 | 40 | /** \brief Issues a HMAC command, which computes an HMAC/SHA-256 digest of a 41 | * key stored in the device, a challenge, and other information on the 42 | * device. 43 | * 44 | * \param[in] mode Controls which fields within the device are used in the 45 | * message. 46 | * \param[in] key_id Which key is to be used to generate the response. 47 | * Bits 0:3 only are used to select a slot but all 16 bits 48 | * are used in the HMAC message. 49 | * \param[out] digest HMAC digest is returned in this buffer (32 bytes). 50 | * 51 | * \return ATCA_SUCCESS on success, otherwise an error code. 52 | */ 53 | ATCA_STATUS atcab_hmac(uint8_t mode, uint16_t key_id, uint8_t* digest) 54 | { 55 | ATCAPacket packet; 56 | ATCACommand ca_cmd = _gDevice->mCommands; 57 | ATCA_STATUS status = ATCA_GEN_FAIL; 58 | 59 | do 60 | { 61 | if (digest == NULL) 62 | { 63 | status = ATCA_BAD_PARAM; 64 | break; 65 | } 66 | // build HMAC command 67 | packet.param1 = mode; 68 | packet.param2 = key_id; 69 | 70 | if ((status = atHMAC(ca_cmd, &packet)) != ATCA_SUCCESS) 71 | { 72 | break; 73 | } 74 | 75 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 76 | { 77 | break; 78 | } 79 | 80 | if (packet.data[ATCA_COUNT_IDX] != HMAC_DIGEST_SIZE + 3) 81 | { 82 | status = ATCA_RX_FAIL; // Unexpected response size 83 | break; 84 | } 85 | 86 | memcpy(digest, &packet.data[ATCA_RSP_DATA_IDX], HMAC_DIGEST_SIZE); 87 | 88 | } 89 | while (0); 90 | 91 | return status; 92 | } 93 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_info.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for Info command. 4 | * 5 | * Info command returns a variety of static and dynamic information about the 6 | * device and its state. Also is used to control the GPIO pin and the persistent 7 | * latch. 8 | * 9 | * \note The ATSHA204A refers to this command as DevRev instead of Info, 10 | * however, the OpCode and operation is the same. 11 | * 12 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 13 | * ATECC508A & ATECC608A. There are differences in the modes that they 14 | * support. Refer to device datasheets for full details. 15 | * 16 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 17 | * 18 | * \page License 19 | * 20 | * Subject to your compliance with these terms, you may use Microchip software 21 | * and any derivatives exclusively with Microchip products. It is your 22 | * responsibility to comply with third party license terms applicable to your 23 | * use of third party software (including open source software) that may 24 | * accompany Microchip software. 25 | * 26 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 27 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 28 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 29 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 30 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 31 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 32 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 33 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 34 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 35 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 36 | * THIS SOFTWARE. 37 | */ 38 | #include "atca_basic.h" 39 | #include "../atca_execution.h" 40 | 41 | /** \brief Issues an Info command, which return internal device information and 42 | * can control GPIO and the persistent latch. 43 | * 44 | * \param[in] mode Selects which mode to be used for info command. 45 | * \param[in] param2 Selects the particular fields for the mode. 46 | * \param[out] out_data Response from info command (4 bytes). Can be set to 47 | * NULL if not required. 48 | * \return ATCA_SUCCESS on success, otherwise an error code. 49 | */ 50 | ATCA_STATUS atcab_info_base(uint8_t mode, uint16_t param2, uint8_t* out_data) 51 | { 52 | ATCAPacket packet; 53 | ATCACommand ca_cmd = _gDevice->mCommands; 54 | ATCA_STATUS status = ATCA_GEN_FAIL; 55 | 56 | // build an info command 57 | packet.param1 = mode; 58 | packet.param2 = param2; 59 | 60 | do 61 | { 62 | 63 | if ((status = atInfo(ca_cmd, &packet)) != ATCA_SUCCESS) 64 | { 65 | break; 66 | } 67 | 68 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 69 | { 70 | break; 71 | } 72 | 73 | if (out_data != NULL && packet.data[ATCA_COUNT_IDX] >= 7) 74 | { 75 | memcpy(out_data, &packet.data[ATCA_RSP_DATA_IDX], 4); 76 | } 77 | } 78 | while (0); 79 | 80 | return status; 81 | } 82 | 83 | /** \brief Use the Info command to get the device revision (DevRev). 84 | * \param[out] revision Device revision is returned here (4 bytes). 85 | * \return ATCA_SUCCESS on success, otherwise an error code. 86 | */ 87 | ATCA_STATUS atcab_info(uint8_t* revision) 88 | { 89 | if (revision == NULL) 90 | { 91 | return ATCA_BAD_PARAM; 92 | } 93 | 94 | return atcab_info_base(INFO_MODE_REVISION, 0, revision); 95 | } 96 | 97 | /** \brief Use the Info command to get the persistent latch current state for 98 | * an ATECC608A device. 99 | * 100 | * \param[out] state The state is returned here. Set (true) or Cler (false). 101 | * 102 | * \return ATCA_SUCCESS on success, otherwise an error code. 103 | */ 104 | 105 | ATCA_STATUS atcab_info_get_latch(bool* state) 106 | { 107 | ATCA_STATUS status = ATCA_GEN_FAIL; 108 | uint8_t out_data[4]; 109 | 110 | if (state == NULL) 111 | { 112 | return ATCA_BAD_PARAM; 113 | } 114 | 115 | status = atcab_info_base(INFO_MODE_VOL_KEY_PERMIT, 0, out_data); 116 | if (status != ATCA_SUCCESS) 117 | { 118 | return status; 119 | } 120 | 121 | *state = (out_data[0] == 1); 122 | 123 | return status; 124 | } 125 | 126 | /** \brief Use the Info command to set the persistent latch state for an 127 | * ATECC608A device. 128 | * 129 | * \param[out] state Persistent latch state. Set (true) or clear (false). 130 | * 131 | * \return ATCA_SUCCESS on success, otherwise an error code. 132 | */ 133 | ATCA_STATUS atcab_info_set_latch(bool state) 134 | { 135 | uint16_t param2 = INFO_PARAM2_SET_LATCH_STATE; 136 | 137 | param2 |= state ? INFO_PARAM2_LATCH_SET : INFO_PARAM2_LATCH_CLEAR; 138 | return atcab_info_base(INFO_MODE_VOL_KEY_PERMIT, param2, NULL); 139 | } 140 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_kdf.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for KDF command. 4 | * 5 | * The KDF command implements one of a number of Key Derivation Functions (KDF). 6 | * Generally this function combines a source key with an input string and 7 | * creates a result key/digest/array. Three algorithms are currently supported: 8 | * PRF, HKDF and AES. 9 | * 10 | * \note List of devices that support this command - ATECC608A. Refer to device 11 | * datasheet for full details. 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | 36 | #include "atca_basic.h" 37 | #include "../atca_execution.h" 38 | 39 | /** \brief Executes the KDF command, which derives a new key in PRF, AES, or 40 | * HKDF modes. 41 | * 42 | * Generally this function combines a source key with an input string and 43 | * creates a result key/digest/array. 44 | * 45 | * \param[in] mode Mode determines KDF algorithm (PRF,AES,HKDF), source 46 | * key location, and target key locations. 47 | * \param[in] key_id Source and target key slots if locations are in the 48 | * EEPROM. Source key slot is the LSB and target key 49 | * slot is the MSB. 50 | * \param[in] details Further information about the computation, depending 51 | * on the algorithm (4 bytes). 52 | * \param[in] message Input value from system (up to 128 bytes). Actual size 53 | * of message is 16 bytes for AES algorithm or is encoded 54 | * in the MSB of the details parameter for other 55 | * algorithms. 56 | * \param[out] out_data Output of the KDF function is returned here. If the 57 | * result remains in the device, this can be NULL. 58 | * \param[out] out_nonce If the output is encrypted, a 32 byte random nonce 59 | * generated by the device is returned here. If output 60 | * encryption is not used, this can be NULL. 61 | * 62 | * \return ATCA_SUCCESS on success, otherwise an error code. 63 | */ 64 | ATCA_STATUS atcab_kdf(uint8_t mode, uint16_t key_id, const uint32_t details, const uint8_t* message, uint8_t* out_data, uint8_t* out_nonce) 65 | { 66 | ATCAPacket packet; 67 | ATCACommand ca_cmd = _gDevice->mCommands; 68 | ATCA_STATUS status = ATCA_GEN_FAIL; 69 | uint16_t out_data_size = 0; 70 | 71 | do 72 | { 73 | if (message == NULL) 74 | { 75 | return ATCA_BAD_PARAM; 76 | } 77 | 78 | // Build the KDF command 79 | packet.param1 = mode; 80 | packet.param2 = key_id; 81 | 82 | // Add details parameter 83 | packet.data[0] = details; 84 | packet.data[1] = details >> 8; 85 | packet.data[2] = details >> 16; 86 | packet.data[3] = details >> 24; 87 | 88 | // Add input message 89 | if ((mode & KDF_MODE_ALG_MASK) == KDF_MODE_ALG_AES) 90 | { 91 | // AES algorithm has a fixed message size 92 | memcpy(&packet.data[KDF_DETAILS_SIZE], message, AES_DATA_SIZE); 93 | } 94 | else 95 | { 96 | // All other algorithms encode message size in the last byte of details 97 | memcpy(&packet.data[KDF_DETAILS_SIZE], message, packet.data[3]); 98 | } 99 | 100 | // Build command 101 | if ((status = atKDF(ca_cmd, &packet)) != ATCA_SUCCESS) 102 | { 103 | break; 104 | } 105 | 106 | // Run command 107 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 108 | { 109 | break; 110 | } 111 | 112 | if (((mode & KDF_MODE_ALG_MASK) == KDF_MODE_ALG_PRF) && (details & KDF_DETAILS_PRF_TARGET_LEN_64)) 113 | { 114 | out_data_size = 64; 115 | } 116 | else 117 | { 118 | out_data_size = 32; 119 | } 120 | 121 | // Return OutData if possible 122 | if (out_data != NULL && packet.data[ATCA_COUNT_IDX] >= (ATCA_PACKET_OVERHEAD + out_data_size)) 123 | { 124 | memcpy(out_data, &packet.data[ATCA_RSP_DATA_IDX], out_data_size); 125 | } 126 | 127 | // return OutNonce if possible 128 | if (out_nonce != NULL && packet.data[ATCA_COUNT_IDX] >= (ATCA_PACKET_OVERHEAD + out_data_size + 32)) 129 | { 130 | memcpy(out_nonce, &packet.data[ATCA_RSP_DATA_IDX + out_data_size], 32); 131 | } 132 | } 133 | while (false); 134 | 135 | return status; 136 | } 137 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_lock.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for Lock command. 4 | * 5 | * The Lock command prevents future modifications of the Configuration zone, 6 | * enables configured policies for Data and OTP zones, and can render 7 | * individual slots read-only regardless of configuration. 8 | * 9 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 10 | * ATECC508A, ATECC608A. There are differences in the modes that they 11 | * support. Refer to device datasheets for full details. 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | 36 | #include "atca_basic.h" 37 | #include "../atca_execution.h" 38 | 39 | /** \brief The Lock command prevents future modifications of the Configuration 40 | * and/or Data and OTP zones. If the device is so configured, then 41 | * this command can be used to lock individual data slots. This 42 | * command fails if the designated area is already locked. 43 | * 44 | * \param[in] mode Zone, and/or slot, and summary check (bit 7). 45 | * \param[in] summary_crc CRC of the config or data zones. Ignored for 46 | * slot locks or when mode bit 7 is set. 47 | * 48 | * \return ATCA_SUCCESS on success, otherwise an error code. 49 | */ 50 | ATCA_STATUS atcab_lock(uint8_t mode, uint16_t summary_crc) 51 | { 52 | ATCAPacket packet; 53 | ATCACommand ca_cmd = _gDevice->mCommands; 54 | ATCA_STATUS status = ATCA_GEN_FAIL; 55 | 56 | // build command for lock zone and send 57 | memset(&packet, 0, sizeof(packet)); 58 | packet.param1 = mode; 59 | packet.param2 = summary_crc; 60 | 61 | do 62 | { 63 | if ((status = atLock(ca_cmd, &packet)) != ATCA_SUCCESS) 64 | { 65 | break; 66 | } 67 | 68 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 69 | { 70 | break; 71 | } 72 | 73 | } 74 | while (0); 75 | 76 | return status; 77 | } 78 | 79 | /** \brief Unconditionally (no CRC required) lock the config zone. 80 | * 81 | * \return ATCA_SUCCESS on success, otherwise an error code. 82 | */ 83 | ATCA_STATUS atcab_lock_config_zone(void) 84 | { 85 | return atcab_lock(LOCK_ZONE_NO_CRC | LOCK_ZONE_CONFIG, 0); 86 | } 87 | 88 | /** \brief Lock the config zone with summary CRC. 89 | * 90 | * The CRC is calculated over the entire config zone contents. 88 bytes for 91 | * ATSHA devices, 128 bytes for ATECC devices. Lock will fail if the provided 92 | * CRC doesn't match the internally calculated one. 93 | * 94 | * \param[in] summary_crc Expected CRC over the config zone. 95 | * 96 | * \return ATCA_SUCCESS on success, otherwise an error code. 97 | */ 98 | ATCA_STATUS atcab_lock_config_zone_crc(uint16_t summary_crc) 99 | { 100 | return atcab_lock(LOCK_ZONE_CONFIG, summary_crc); 101 | } 102 | 103 | /** \brief Unconditionally (no CRC required) lock the data zone (slots and OTP). 104 | * 105 | * ConfigZone must be locked and DataZone must be unlocked for the zone to be successfully locked. 106 | * 107 | * \return ATCA_SUCCESS on success, otherwise an error code. 108 | */ 109 | ATCA_STATUS atcab_lock_data_zone(void) 110 | { 111 | return atcab_lock(LOCK_ZONE_NO_CRC | LOCK_ZONE_DATA, 0); 112 | } 113 | 114 | /** \brief Lock the data zone (slots and OTP) with summary CRC. 115 | * 116 | * The CRC is calculated over the concatenated contents of all the slots and 117 | * OTP at the end. Private keys (KeyConfig.Private=1) are skipped. Lock will 118 | * fail if the provided CRC doesn't match the internally calculated one. 119 | * 120 | * \param[in] summary_crc Expected CRC over the data zone. 121 | * 122 | * \return ATCA_SUCCESS on success, otherwise an error code. 123 | */ 124 | ATCA_STATUS atcab_lock_data_zone_crc(uint16_t summary_crc) 125 | { 126 | return atcab_lock(LOCK_ZONE_DATA, summary_crc); 127 | } 128 | 129 | /** \brief Lock an individual slot in the data zone on an ATECC device. Not 130 | * available for ATSHA devices. Slot must be configured to be slot 131 | * lockable (KeyConfig.Lockable=1). 132 | * 133 | * \param[in] slot Slot to be locked in data zone. 134 | * 135 | * \return ATCA_SUCCESS on success, otherwise an error code. 136 | */ 137 | ATCA_STATUS atcab_lock_data_slot(uint16_t slot) 138 | { 139 | return atcab_lock(((uint8_t)slot << 2) | LOCK_ZONE_DATA_SLOT, 0); 140 | } 141 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_mac.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for MAC command. 4 | * 5 | * The MAC command computes a SHA-256 digest of a key stored in the device, a 6 | * challenge, and other information on the device. The output of this command 7 | * is the digest of this message. 8 | * 9 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 10 | * ATECC508A, and ATECC608A. There are differences in the modes that they 11 | * support. Refer to device datasheets for full details. 12 | * 13 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 14 | * 15 | * \page License 16 | * 17 | * Subject to your compliance with these terms, you may use Microchip software 18 | * and any derivatives exclusively with Microchip products. It is your 19 | * responsibility to comply with third party license terms applicable to your 20 | * use of third party software (including open source software) that may 21 | * accompany Microchip software. 22 | * 23 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 24 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 25 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 26 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 27 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 28 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 29 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 30 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 31 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 32 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 33 | * THIS SOFTWARE. 34 | */ 35 | 36 | #include "atca_basic.h" 37 | #include "../atca_execution.h" 38 | 39 | /** \brief Executes MAC command, which computes a SHA-256 digest of a key 40 | * stored in the device, a challenge, and other information on the 41 | * device. 42 | * 43 | * \param[in] mode Controls which fields within the device are used in 44 | * the message 45 | * \param[in] key_id Key in the CryptoAuth device to use for the MAC 46 | * \param[in] challenge Challenge message (32 bytes). May be NULL if mode 47 | * indicates a challenge isn't required. 48 | * \param[out] digest MAC response is returned here (32 bytes). 49 | * 50 | * \return ATCA_SUCCESS on success, otherwise an error code. 51 | */ 52 | ATCA_STATUS atcab_mac(uint8_t mode, uint16_t key_id, const uint8_t* challenge, uint8_t* digest) 53 | { 54 | ATCAPacket packet; 55 | ATCACommand ca_cmd = _gDevice->mCommands; 56 | ATCA_STATUS status = ATCA_GEN_FAIL; 57 | 58 | do 59 | { 60 | if (digest == NULL) 61 | { 62 | return ATCA_BAD_PARAM; 63 | } 64 | 65 | // build mac command 66 | packet.param1 = mode; 67 | packet.param2 = key_id; 68 | if (!(mode & MAC_MODE_BLOCK2_TEMPKEY)) 69 | { 70 | if (challenge == NULL) 71 | { 72 | return ATCA_BAD_PARAM; 73 | } 74 | memcpy(&packet.data[0], challenge, 32); // a 32-byte challenge 75 | } 76 | 77 | if ((status = atMAC(ca_cmd, &packet)) != ATCA_SUCCESS) 78 | { 79 | break; 80 | } 81 | 82 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 83 | { 84 | break; 85 | } 86 | 87 | memcpy(digest, &packet.data[ATCA_RSP_DATA_IDX], MAC_SIZE); 88 | 89 | } 90 | while (0); 91 | 92 | return status; 93 | } 94 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_random.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for Random command. 4 | * 5 | * The Random command generates a random number for use by the system. 6 | * 7 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 8 | * ATECC508A, ATECC608A. There are differences in the modes that they 9 | * support. Refer to device datasheets for full details. 10 | * 11 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 12 | * 13 | * \page License 14 | * 15 | * Subject to your compliance with these terms, you may use Microchip software 16 | * and any derivatives exclusively with Microchip products. It is your 17 | * responsibility to comply with third party license terms applicable to your 18 | * use of third party software (including open source software) that may 19 | * accompany Microchip software. 20 | * 21 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 22 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 23 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 24 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 25 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 26 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 27 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 28 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 29 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 30 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 31 | * THIS SOFTWARE. 32 | */ 33 | #include "atca_basic.h" 34 | #include "../atca_execution.h" 35 | 36 | /** \brief Executes Random command, which generates a 32 byte random number 37 | * from the CryptoAuth device. 38 | * 39 | * \param[out] rand_out 32 bytes of random data is returned here. 40 | * 41 | * \return ATCA_SUCCESS on success, otherwise an error code. 42 | */ 43 | ATCA_STATUS atcab_random(uint8_t *rand_out) 44 | { 45 | ATCAPacket packet; 46 | ATCACommand ca_cmd = _gDevice->mCommands; 47 | ATCA_STATUS status = ATCA_GEN_FAIL; 48 | 49 | do 50 | { 51 | // build an random command 52 | packet.param1 = RANDOM_SEED_UPDATE; 53 | packet.param2 = 0x0000; 54 | 55 | if ((status = atRandom(ca_cmd, &packet)) != ATCA_SUCCESS) 56 | { 57 | break; 58 | } 59 | 60 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 61 | { 62 | break; 63 | } 64 | 65 | if (packet.data[ATCA_COUNT_IDX] != RANDOM_RSP_SIZE) 66 | { 67 | status = ATCA_RX_FAIL; 68 | break; 69 | } 70 | 71 | if (rand_out) 72 | { 73 | memcpy(rand_out, &packet.data[ATCA_RSP_DATA_IDX], RANDOM_NUM_SIZE); 74 | } 75 | } 76 | while (0); 77 | 78 | 79 | return status; 80 | } 81 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_selftest.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for SelfTest command. 4 | * 5 | * The SelfTest command performs a test of one or more of the cryptographic 6 | * engines within the device. 7 | * 8 | * \note List of devices that support this command - ATECC608A. Refer to device 9 | * datasheet for full details. 10 | * 11 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 12 | * 13 | * \page License 14 | * 15 | * Subject to your compliance with these terms, you may use Microchip software 16 | * and any derivatives exclusively with Microchip products. It is your 17 | * responsibility to comply with third party license terms applicable to your 18 | * use of third party software (including open source software) that may 19 | * accompany Microchip software. 20 | * 21 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 22 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 23 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 24 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 25 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 26 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 27 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 28 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 29 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 30 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 31 | * THIS SOFTWARE. 32 | */ 33 | 34 | #include "atca_basic.h" 35 | #include "../atca_execution.h" 36 | 37 | /** \brief Executes the SelfTest command, which performs a test of one or more 38 | * of the cryptographic engines within the ATECC608A chip. 39 | * 40 | * \param[in] mode Functions to test. Can be a bit field combining any 41 | * of the following: SELFTEST_MODE_RNG, 42 | * SELFTEST_MODE_ECDSA_VERIFY, SELFTEST_MODE_ECDSA_SIGN, 43 | * SELFTEST_MODE_ECDH, SELFTEST_MODE_AES, 44 | * SELFTEST_MODE_SHA, SELFTEST_MODE_ALL. 45 | * \param[in] param2 Currently unused, should be 0. 46 | * \param[out] result Results are returned here as a bit field. 47 | * 48 | * \return ATCA_SUCCESS on success, otherwise an error code. 49 | */ 50 | ATCA_STATUS atcab_selftest(uint8_t mode, uint16_t param2, uint8_t* result) 51 | { 52 | ATCAPacket packet; 53 | ATCACommand ca_cmd = _gDevice->mCommands; 54 | ATCA_STATUS status = ATCA_GEN_FAIL; 55 | uint8_t response = 0; 56 | 57 | do 58 | { 59 | // build a SelfTest command 60 | packet.param1 = mode; 61 | packet.param2 = param2; 62 | 63 | if ((status = atSelfTest(ca_cmd, &packet)) != ATCA_SUCCESS) 64 | { 65 | break; 66 | } 67 | 68 | status = atca_execute_command(&packet, _gDevice); 69 | 70 | // This command is a little awkward, because it returns its status as 71 | // a single byte, which can be hard to differentiate from an actual 72 | // error code. 73 | 74 | response = packet.data[ATCA_RSP_DATA_IDX]; 75 | 76 | if (response & !mode) 77 | { 78 | // The response has bits set outside of the bit field requested by 79 | // the mode. This indicates an actual error rather than a self test 80 | // failure. 81 | return status; // Return the translated status. 82 | } 83 | else 84 | { 85 | // Here, we have the possibility of ambiguous results, where some 86 | // error codes can't be differentiated from self test failures. 87 | // We assume self-test failures. 88 | if (result) 89 | { 90 | *result = response; 91 | } 92 | 93 | // Self tests might have failed, but we returned success because 94 | // the results are returned in result. 95 | return ATCA_SUCCESS; 96 | } 97 | } 98 | while (0); 99 | 100 | return status; 101 | } 102 | 103 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_basic_updateextra.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief CryptoAuthLib Basic API methods for UpdateExtra command. 4 | * 5 | * The UpdateExtra command is used to update the values of the two extra bytes 6 | * within the Configuration zone after the Configuration zone has been locked. 7 | * 8 | * \note List of devices that support this command - ATSHA204A, ATECC108A, 9 | * ATECC508A, and ATECC608A. There are differences in the modes that they 10 | * support. Refer to device datasheets for full details. 11 | * 12 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 13 | * 14 | * \page License 15 | * 16 | * Subject to your compliance with these terms, you may use Microchip software 17 | * and any derivatives exclusively with Microchip products. It is your 18 | * responsibility to comply with third party license terms applicable to your 19 | * use of third party software (including open source software) that may 20 | * accompany Microchip software. 21 | * 22 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 23 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 24 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 25 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 26 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 27 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 28 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 29 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 30 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 31 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 32 | * THIS SOFTWARE. 33 | */ 34 | #include "atca_basic.h" 35 | #include "../atca_execution.h" 36 | 37 | /** \brief Executes UpdateExtra command to update the values of the two 38 | * extra bytes within the Configuration zone (bytes 84 and 85). 39 | * 40 | * Can also be used to decrement the limited use counter associated with the 41 | * key in slot NewValue. 42 | * 43 | * \param[in] mode Mode determines what operations the UpdateExtra 44 | * command performs. 45 | * \param[in] new_value Value to be written. 46 | * 47 | * \return ATCA_SUCCESS on success, otherwise an error code. 48 | */ 49 | ATCA_STATUS atcab_updateextra(uint8_t mode, uint16_t new_value) 50 | { 51 | ATCAPacket packet; 52 | ATCACommand ca_cmd = _gDevice->mCommands; 53 | ATCA_STATUS status = ATCA_GEN_FAIL; 54 | 55 | do 56 | { 57 | // Build command 58 | memset(&packet, 0, sizeof(packet)); 59 | packet.param1 = mode; 60 | packet.param2 = new_value; 61 | 62 | if ((status = atUpdateExtra(ca_cmd, &packet)) != ATCA_SUCCESS) 63 | { 64 | break; 65 | } 66 | 67 | if ((status = atca_execute_command(&packet, _gDevice)) != ATCA_SUCCESS) 68 | { 69 | break; 70 | } 71 | 72 | } 73 | while (0); 74 | 75 | return status; 76 | } 77 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/basic/atca_helpers.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Helpers to support the CryptoAuthLib Basic API methods 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCA_HELPERS_H_ 29 | #define ATCA_HELPERS_H_ 30 | 31 | #include "../cryptoauthlib.h" 32 | 33 | /** \ingroup atcab_ 34 | * @{ 35 | */ 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | ATCA_STATUS atcab_printbin(uint8_t* binary, size_t bin_len, bool add_space); 42 | ATCA_STATUS atcab_bin2hex(const uint8_t* bin, size_t bin_size, char* hex, size_t* hex_size); 43 | ATCA_STATUS atcab_bin2hex_(const uint8_t* bin, size_t bin_size, char* hex, size_t* hex_size, bool is_pretty, bool is_space, bool is_upper); 44 | ATCA_STATUS atcab_hex2bin(const char* ascii_hex, size_t ascii_hex_len, uint8_t* binary, size_t* bin_len); 45 | ATCA_STATUS atcab_hex2bin_(const char* hex, size_t hex_size, uint8_t* bin, size_t* bin_size, bool is_space); 46 | ATCA_STATUS atcab_printbin_sp(uint8_t* binary, size_t bin_len); 47 | ATCA_STATUS atcab_printbin_label(const char* label, uint8_t* binary, size_t bin_len); 48 | 49 | 50 | ATCA_STATUS packHex(const char* ascii_hex, size_t ascii_hex_len, char* packed_hex, size_t* packed_len); 51 | bool isDigit(char c); 52 | bool isWhiteSpace(char c); 53 | bool isAlpha(char c); 54 | bool isHexAlpha(char c); 55 | bool isHex(char c); 56 | bool isHexDigit(char c); 57 | 58 | bool isBase64(char c, const uint8_t * rules); 59 | bool isBase64Digit(char c, const uint8_t * rules); 60 | uint8_t base64Index(char c, const uint8_t * rules); 61 | char base64Char(uint8_t id, const uint8_t * rules); 62 | 63 | extern uint8_t atcab_b64rules_default[4]; 64 | extern uint8_t atcab_b64rules_mime[4]; 65 | extern uint8_t atcab_b64rules_urlsafe[4]; 66 | 67 | ATCA_STATUS atcab_base64decode_(const char* encoded, size_t encoded_size, uint8_t* data, size_t* data_size, const uint8_t * rules); 68 | ATCA_STATUS atcab_base64decode(const char* encoded, size_t encoded_size, uint8_t* data, size_t* data_size); 69 | 70 | ATCA_STATUS atcab_base64encode_(const uint8_t* data, size_t data_size, char* encoded, size_t* encoded_size, const uint8_t * rules); 71 | ATCA_STATUS atcab_base64encode(const uint8_t* data, size_t data_size, char* encoded, size_t* encoded_size); 72 | 73 | 74 | ATCA_STATUS atcab_reversal(const uint8_t* bin, size_t bin_size, uint8_t* dest, size_t* dest_size); 75 | 76 | 77 | #ifdef __cplusplus 78 | } 79 | #endif 80 | 81 | /** @} */ 82 | #endif /* ATCA_HELPERS_H_ */ 83 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/README.md: -------------------------------------------------------------------------------- 1 | crypto directory - Purpose 2 | =========================== 3 | This directory contains software implementations of cryptographic functions. 4 | The functions at the base level are wrappers that will point to the final 5 | implementations of the software crypto functions. 6 | 7 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Common defines for CryptoAuthLib software crypto wrappers. 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCA_CRYPTO_SW_H 29 | #define ATCA_CRYPTO_SW_H 30 | 31 | #include "../atca_status.h" 32 | 33 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_ecdsa.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief API wrapper for software ECDSA verify. Currently unimplemented but could be 4 | * implemented via a 3rd party library such as MicroECC. 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | 30 | #include "atca_crypto_sw_ecdsa.h" 31 | 32 | /** \brief return software generated ECDSA verification result and the function is currently not implemented 33 | * \param[in] msg ptr to message or challenge 34 | * \param[in] signature ptr to the signature to verify 35 | * \param[in] public_key ptr to public key of device which signed the challenge 36 | * return ATCA_UNIMPLEMENTED , as the function is currently not implemented 37 | */ 38 | 39 | int atcac_sw_ecdsa_verify_p256(const uint8_t msg[ATCA_ECC_P256_FIELD_SIZE], 40 | const uint8_t signature[ATCA_ECC_P256_SIGNATURE_SIZE], 41 | const uint8_t public_key[ATCA_ECC_P256_PUBLIC_KEY_SIZE]) 42 | { 43 | return ATCA_UNIMPLEMENTED; 44 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_ecdsa.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | 29 | #ifndef ATCA_CRYPTO_SW_ECDSA_H 30 | #define ATCA_CRYPTO_SW_ECDSA_H 31 | 32 | #include "atca_crypto_sw.h" 33 | #include 34 | #include 35 | 36 | /** \defgroup atcac_ Software crypto methods (atcac_) 37 | * 38 | * \brief 39 | * These methods provide a software implementation of various crypto 40 | * algorithms 41 | * 42 | @{ */ 43 | 44 | #define ATCA_ECC_P256_FIELD_SIZE (256 / 8) 45 | #define ATCA_ECC_P256_PRIVATE_KEY_SIZE (ATCA_ECC_P256_FIELD_SIZE) 46 | #define ATCA_ECC_P256_PUBLIC_KEY_SIZE (ATCA_ECC_P256_FIELD_SIZE * 2) 47 | #define ATCA_ECC_P256_SIGNATURE_SIZE (ATCA_ECC_P256_FIELD_SIZE * 2) 48 | 49 | #ifdef __cplusplus 50 | extern "C" { 51 | #endif 52 | 53 | int atcac_sw_ecdsa_verify_p256(const uint8_t msg[ATCA_ECC_P256_FIELD_SIZE], 54 | const uint8_t signature[ATCA_ECC_P256_SIGNATURE_SIZE], 55 | const uint8_t public_key[ATCA_ECC_P256_PUBLIC_KEY_SIZE]); 56 | 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | /** @} */ 62 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_rand.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief API wrapper for software random 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #include "atca_crypto_sw_rand.h" 29 | 30 | /** \brief return software generated random number and the function is currently not implemented 31 | * \param[out] data ptr to space to receive the random number 32 | * \param[in] data_size size of data buffer 33 | * return ATCA_UNIMPLEMENTED , as the function is not implemented 34 | */ 35 | 36 | int atcac_sw_random(uint8_t* data, size_t data_size) 37 | { 38 | return ATCA_UNIMPLEMENTED; 39 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_rand.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCA_CRYPTO_SW_RAND_H 29 | #define ATCA_CRYPTO_SW_RAND_H 30 | 31 | #include "atca_crypto_sw.h" 32 | #include 33 | #include 34 | 35 | /** \defgroup atcac_ Software crypto methods (atcac_) 36 | * 37 | * \brief 38 | * These methods provide a software implementation of various crypto 39 | * algorithms 40 | * 41 | @{ */ 42 | #ifdef __cplusplus 43 | extern "C" { 44 | #endif 45 | 46 | int atcac_sw_random(uint8_t* data, size_t data_size); 47 | 48 | #ifdef __cplusplus 49 | } 50 | #endif 51 | /** @} */ 52 | 53 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_sha1.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Wrapper API for SHA 1 routines 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | 29 | #include "atca_crypto_sw_sha1.h" 30 | #include "hashes/sha1_routines.h" 31 | 32 | 33 | /** \brief Initialize context for performing SHA1 hash in software. 34 | * \param[in] ctx Hash context 35 | * \return ATCA_SUCCESS on success, otherwise an error code. 36 | */ 37 | 38 | int atcac_sw_sha1_init(atcac_sha1_ctx* ctx) 39 | { 40 | if (sizeof(CL_HashContext) > sizeof(atcac_sha1_ctx)) 41 | { 42 | return ATCA_ASSERT_FAILURE; // atcac_sha1_ctx isn't large enough for this implementation 43 | } 44 | CL_hashInit((CL_HashContext*)ctx); 45 | 46 | return ATCA_SUCCESS; 47 | } 48 | 49 | 50 | /** \brief Add arbitrary data to a SHA1 hash. 51 | \param[in] ctx Hash context 52 | \param[in] data Data to be added to the hash 53 | \param[in] data_size Data size in bytes 54 | \return ATCA_SUCCESS 55 | */ 56 | int atcac_sw_sha1_update(atcac_sha1_ctx* ctx, const uint8_t* data, size_t data_size) 57 | { 58 | CL_hashUpdate((CL_HashContext*)ctx, data, (int)data_size); 59 | 60 | return ATCA_SUCCESS; 61 | } 62 | 63 | /** \brief Complete the SHA1 hash in software and return the digest. 64 | * \param[in] ctx Hash context 65 | * \param[out] digest Digest is returned here (20 bytes) 66 | * \return ATCA_SUCCESS 67 | */ 68 | int atcac_sw_sha1_finish(atcac_sha1_ctx* ctx, uint8_t digest[ATCA_SHA1_DIGEST_SIZE]) 69 | { 70 | CL_hashFinal((CL_HashContext*)ctx, digest); 71 | 72 | return ATCA_SUCCESS; 73 | } 74 | 75 | 76 | /** \brief Perform SHA1 hash of data in software. 77 | * \param[in] data Data to be hashed 78 | * \param[in] data_size Data size in bytes 79 | * \param[out] digest Digest is returned here (20 bytes) 80 | * \return ATCA_SUCCESS on success, otherwise an error code. 81 | */ 82 | int atcac_sw_sha1(const uint8_t* data, size_t data_size, uint8_t digest[ATCA_SHA1_DIGEST_SIZE]) 83 | { 84 | int ret; 85 | atcac_sha1_ctx ctx; 86 | 87 | ret = atcac_sw_sha1_init(&ctx); 88 | if (ret != ATCA_SUCCESS) 89 | { 90 | return ret; 91 | } 92 | 93 | ret = atcac_sw_sha1_update(&ctx, data, data_size); 94 | if (ret != ATCA_SUCCESS) 95 | { 96 | return ret; 97 | } 98 | 99 | ret = atcac_sw_sha1_finish(&ctx, digest); 100 | if (ret != ATCA_SUCCESS) 101 | { 102 | return ret; 103 | } 104 | 105 | return ATCA_SUCCESS; 106 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_sha1.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Wrapper API for SHA 1 routines 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCA_CRYPTO_SW_SHA1_H 29 | #define ATCA_CRYPTO_SW_SHA1_H 30 | 31 | #include "atca_crypto_sw.h" 32 | #include 33 | #include 34 | 35 | /** \defgroup atcac_ Software crypto methods (atcac_) 36 | * 37 | * \brief 38 | * These methods provide a software implementation of various crypto 39 | * algorithms 40 | * 41 | @{ */ 42 | 43 | #define ATCA_SHA1_DIGEST_SIZE (20) 44 | 45 | typedef struct 46 | { 47 | uint32_t pad[32]; //!< Filler value to make sure the actual implementation has enough room to store its context. uint32_t is used to remove some alignment warnings. 48 | } atcac_sha1_ctx; 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | int atcac_sw_sha1_init(atcac_sha1_ctx* ctx); 55 | int atcac_sw_sha1_update(atcac_sha1_ctx* ctx, const uint8_t* data, size_t data_size); 56 | int atcac_sw_sha1_finish(atcac_sha1_ctx * ctx, uint8_t digest[ATCA_SHA1_DIGEST_SIZE]); 57 | int atcac_sw_sha1(const uint8_t * data, size_t data_size, uint8_t digest[ATCA_SHA1_DIGEST_SIZE]); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | /** @} */ 64 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_sha2.c: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Wrapper API for software SHA 256 routines 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #include "atca_crypto_sw_sha2.h" 29 | #include "hashes/sha2_routines.h" 30 | 31 | /** \brief initializes the SHA256 software 32 | * \param[in] ctx ptr to context data structure 33 | * \return ATCA_SUCCESS on success, otherwise an error code. 34 | */ 35 | 36 | int atcac_sw_sha2_256_init(atcac_sha2_256_ctx* ctx) 37 | { 38 | if (sizeof(sw_sha256_ctx) > sizeof(atcac_sha2_256_ctx)) 39 | { 40 | return ATCA_ASSERT_FAILURE; // atcac_sha1_ctx isn't large enough for this implementation 41 | } 42 | sw_sha256_init((sw_sha256_ctx*)ctx); 43 | 44 | return ATCA_SUCCESS; 45 | } 46 | 47 | /** \brief updates the running hash with the next block of data, called iteratively for the entire 48 | stream of data to be hashed using the SHA256 software 49 | \param[in] ctx ptr to SHA context data structure 50 | \param[in] data ptr to next block of data to hash 51 | \param[in] data_size size amount of data to hash in the given block, in bytes 52 | \return ATCA_SUCCESS 53 | */ 54 | 55 | int atcac_sw_sha2_256_update(atcac_sha2_256_ctx* ctx, const uint8_t* data, size_t data_size) 56 | { 57 | sw_sha256_update((sw_sha256_ctx*)ctx, data, (uint32_t)data_size); 58 | 59 | return ATCA_SUCCESS; 60 | } 61 | 62 | /** \brief completes the final SHA256 calculation and returns the final digest/hash 63 | * \param[in] ctx ptr to context data structure 64 | * \param[out] digest receives the computed digest of the SHA 256 65 | * \return ATCA_SUCCESS 66 | */ 67 | 68 | int atcac_sw_sha2_256_finish(atcac_sha2_256_ctx* ctx, uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE]) 69 | { 70 | sw_sha256_final((sw_sha256_ctx*)ctx, digest); 71 | 72 | return ATCA_SUCCESS; 73 | } 74 | 75 | 76 | /** \brief single call convenience function which computes Hash of given data using SHA256 software 77 | * \param[in] data pointer to stream of data to hash 78 | * \param[in] data_size size of data stream to hash 79 | * \param[out] digest result 80 | * \return ATCA_SUCCESS on success, otherwise an error code. 81 | */ 82 | 83 | int atcac_sw_sha2_256(const uint8_t* data, size_t data_size, uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE]) 84 | { 85 | int ret; 86 | atcac_sha2_256_ctx ctx; 87 | 88 | ret = atcac_sw_sha2_256_init(&ctx); 89 | if (ret != ATCA_SUCCESS) 90 | { 91 | return ret; 92 | } 93 | 94 | ret = atcac_sw_sha2_256_update(&ctx, data, data_size); 95 | if (ret != ATCA_SUCCESS) 96 | { 97 | return ret; 98 | } 99 | 100 | ret = atcac_sw_sha2_256_finish(&ctx, digest); 101 | if (ret != ATCA_SUCCESS) 102 | { 103 | return ret; 104 | } 105 | 106 | return ATCA_SUCCESS; 107 | } -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/atca_crypto_sw_sha2.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Wrapper API for software SHA 256 routines 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCA_CRYPTO_SW_SHA2_H 29 | #define ATCA_CRYPTO_SW_SHA2_H 30 | 31 | #include "atca_crypto_sw.h" 32 | #include 33 | #include 34 | 35 | /** \defgroup atcac_ Software crypto methods (atcac_) 36 | * 37 | * \brief 38 | * These methods provide a software implementation of various crypto 39 | * algorithms 40 | * 41 | @{ */ 42 | 43 | #define ATCA_SHA2_256_DIGEST_SIZE (32) 44 | 45 | typedef struct 46 | { 47 | uint32_t pad[48]; //!< Filler value to make sure the actual implementation has enough room to store its context. uint32_t is used to remove some alignment warnings. 48 | } atcac_sha2_256_ctx; 49 | 50 | #ifdef __cplusplus 51 | extern "C" { 52 | #endif 53 | 54 | int atcac_sw_sha2_256_init(atcac_sha2_256_ctx* ctx); 55 | int atcac_sw_sha2_256_update(atcac_sha2_256_ctx* ctx, const uint8_t* data, size_t data_size); 56 | int atcac_sw_sha2_256_finish(atcac_sha2_256_ctx * ctx, uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE]); 57 | int atcac_sw_sha2_256(const uint8_t * data, size_t data_size, uint8_t digest[ATCA_SHA2_256_DIGEST_SIZE]); 58 | 59 | #ifdef __cplusplus 60 | } 61 | #endif 62 | 63 | /** @} */ 64 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/hashes/sha1_routines.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Software implementation of the SHA1 algorithm. 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef __SHA1_ROUTINES_DOT_H__ 29 | #define __SHA1_ROUTINES_DOT_H__ 30 | 31 | #include 32 | #include 33 | #include 34 | 35 | #ifdef WIN32 36 | #include 37 | #include 38 | #endif 39 | 40 | #include 41 | 42 | 43 | #ifndef U8 44 | #define U8 uint8_t 45 | #endif 46 | 47 | #ifndef U16 48 | #define U16 uint16_t 49 | #endif 50 | 51 | #ifndef U32 52 | #define U32 uint32_t 53 | #endif 54 | 55 | 56 | #ifndef memcpy_P 57 | #define memcpy_P memmove 58 | #endif 59 | 60 | #ifndef strcpy_P 61 | #define strcpy_P strcpy 62 | #endif 63 | 64 | #ifndef _WDRESET 65 | #define _WDRESET() 66 | #define _NOP() 67 | #endif 68 | 69 | #ifdef __cplusplus 70 | extern "C" { 71 | #endif 72 | 73 | typedef struct 74 | { 75 | U32 h[20 / 4]; // Ensure it's word aligned 76 | U32 buf[64 / 4]; // Ensure it's word aligned 77 | U32 byteCount; 78 | U32 byteCountHi; 79 | } CL_HashContext; 80 | 81 | #define leftRotate(x, n) (x) = (((x) << (n)) | ((x) >> (32 - (n)))) 82 | 83 | void shaEngine(U32 *buf, U32 *h); 84 | void CL_hashInit(CL_HashContext *ctx); 85 | void CL_hashUpdate(CL_HashContext *ctx, const U8 *src, int nbytes); 86 | void CL_hashFinal(CL_HashContext *ctx, U8 *dest); 87 | void CL_hash(U8 *msg, int msgBytes, U8 *dest); 88 | 89 | #ifdef __cplusplus 90 | } 91 | #endif 92 | 93 | #endif // __SHA1_ROUTINES_DOT_H__ 94 | 95 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/crypto/hashes/sha2_routines.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Software implementation of the SHA256 algorithm. 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef SHA2_ROUTINES_H 29 | #define SHA2_ROUTINES_H 30 | 31 | #include 32 | 33 | #define SHA256_DIGEST_SIZE (32) 34 | #define SHA256_BLOCK_SIZE (64) 35 | 36 | #ifdef __cplusplus 37 | extern "C" { 38 | #endif 39 | 40 | typedef struct 41 | { 42 | uint32_t total_msg_size; //!< Total number of message bytes processed 43 | uint32_t block_size; //!< Number of bytes in current block 44 | uint8_t block[SHA256_BLOCK_SIZE * 2]; //!< Unprocessed message storage 45 | uint32_t hash[8]; //!< Hash state 46 | } sw_sha256_ctx; 47 | 48 | void sw_sha256_init(sw_sha256_ctx* ctx); 49 | 50 | void sw_sha256_update(sw_sha256_ctx* ctx, const uint8_t* message, uint32_t len); 51 | 52 | void sw_sha256_final(sw_sha256_ctx * ctx, uint8_t digest[SHA256_DIGEST_SIZE]); 53 | 54 | void sw_sha256(const uint8_t * message, unsigned int len, uint8_t digest[SHA256_DIGEST_SIZE]); 55 | 56 | #ifdef __cplusplus 57 | } 58 | #endif 59 | 60 | #endif // SHA2_ROUTINES_H 61 | 62 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/cryptoauthlib.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Single aggregation point for all CryptoAuthLib header files 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef _ATCA_LIB_H 29 | #define _ATCA_LIB_H 30 | 31 | #include 32 | #include 33 | 34 | /** Library Configuration File - All build attributes should be included in 35 | atca_config.h */ 36 | #include "atca_config.h" 37 | 38 | /* Configuration Macros */ 39 | #define ATCA_SHA_SUPPORT (defined(ATCA_ATSHA204A_SUPPORT) \ 40 | || defined(ATCA_ATSHA206A_SUPPORT)) 41 | #define ATCA_ECC_SUPPORT (defined(ATCA_ATECC108A_SUPPORT) \ 42 | || defined(ATCA_ATECC508A_SUPPORT) \ 43 | || defined(ATCA_ATECC608A_SUPPORT)) 44 | 45 | #define ATCA_CA_SUPPORT (ATCA_SHA_SUPPORT || ATCA_ECC_SUPPORT) 46 | #define ATCA_TA_SUPPORT defined(ATCA_TA100_SUPPORT) 47 | 48 | 49 | #include "hal/atca_hal.h" 50 | #include "atca_status.h" 51 | #include "atca_device.h" 52 | #include "atca_command.h" 53 | #include "atca_cfgs.h" 54 | #include "basic/atca_basic.h" 55 | #include "basic/atca_helpers.h" 56 | 57 | #include 58 | 59 | #ifdef ATCAPRINTF 60 | #include 61 | //#define BREAK(status, message) {printf(__FUNCTION__": "message" -- Status: %02X\r\n", status); break;} 62 | #define BREAK(status, message) { printf(": "message " -- Status: %02X\r\n", status); break; } 63 | #define RETURN(status, message) { printf(": "message " -- Status: %02X\r\n", status); return status; } 64 | #define PRINTSTAT(status, message) { printf(": "message " -- Status: %02X\r\n", status); } 65 | #define PRINT(message) { printf(": "message "\r\n"); break; } 66 | #define DBGOUT(message) { printf(": "message "\r\n"); break; } 67 | #else 68 | #define BREAK(status, message) { break; } 69 | #define RETURN(status, message) { return status; } 70 | #define PRINT(message) { break; } 71 | #define DBGOUT(message) { break; } 72 | #endif 73 | 74 | #endif 75 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/90-cryptohid.rules: -------------------------------------------------------------------------------- 1 | # HIDAPI/libusb 2 | # ATMEL 3 | SUBSYSTEM=="usb", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2312", MODE="0666", ACTION=="add" 4 | 5 | # Microchip 6 | SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f30", MODE="0666", ACTION=="add" 7 | SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f31", MODE="0666", ACTION=="add" 8 | SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f32", MODE="0666", ACTION=="add" 9 | SUBSYSTEM=="usb", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f33", MODE="0666", ACTION=="add" 10 | 11 | # HIDAPI/hidraw 12 | # ATMEL 13 | KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="03eb", ATTRS{idProduct}=="2312", MODE="0666", GROUP="plugdev" 14 | 15 | # Microchip 16 | KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f30", MODE="0666", GROUP="plugdev" 17 | KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f31", MODE="0666", GROUP="plugdev" 18 | KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f32", MODE="0666", GROUP="plugdev" 19 | KERNEL=="hidraw*", ATTRS{busnum}=="1", ATTRS{idVendor}=="04d8", ATTRS{idProduct}=="0f33", MODE="0666", GROUP="plugdev" 20 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/TWI/hal_arduino_twi.h: -------------------------------------------------------------------------------- 1 | /* 2 | twi.h - TWI/I2C library for Wiring & Arduino 3 | Copyright (c) 2006 Nicholas Zambetti. All right reserved. 4 | This library is free software; you can redistribute it and/or 5 | modify it under the terms of the GNU Lesser General Public 6 | License as published by the Free Software Foundation; either 7 | version 2.1 of the License, or (at your option) any later version. 8 | This library is distributed in the hope that it will be useful, 9 | but WITHOUT ANY WARRANTY; without even the implied warranty of 10 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 11 | Lesser General Public License for more details. 12 | You should have received a copy of the GNU Lesser General Public 13 | License along with this library; if not, write to the Free Software 14 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 15 | */ 16 | 17 | #ifndef twi_h 18 | #define twi_h 19 | 20 | #include 21 | 22 | //#define ATMEGA8 23 | 24 | #ifndef TWI_FREQ 25 | #define TWI_FREQ 100000L 26 | #endif 27 | 28 | // Upgrade TWI BUFFER TO 40 (32 old value) 29 | #ifndef TWI_BUFFER_LENGTH 30 | #define TWI_BUFFER_LENGTH 40 31 | #endif 32 | 33 | #define TWI_READY 0 34 | #define TWI_MRX 1 35 | #define TWI_MTX 2 36 | #define TWI_SRX 3 37 | #define TWI_STX 4 38 | 39 | void twi_init(void); 40 | void twi_disable(void); 41 | void twi_setAddress(uint8_t); 42 | void twi_setFrequency(uint32_t); 43 | uint8_t twi_readFrom(uint8_t, uint8_t*, uint8_t, uint8_t); 44 | uint8_t twi_writeTo(uint8_t, uint8_t*, uint8_t, uint8_t, uint8_t); 45 | uint8_t twi_transmit(const uint8_t*, uint8_t); 46 | void twi_attachSlaveRxEvent(void(*)(uint8_t*, int)); 47 | void twi_attachSlaveTxEvent(void(*)(void)); 48 | void twi_reply(uint8_t); 49 | void twi_stop(void); 50 | void twi_releaseBus(void); 51 | 52 | #endif -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/atca_start_config.h: -------------------------------------------------------------------------------- 1 | /* 2 | this is a placeholder include used to satisfy the include 3 | 4 | when used with Atmel START, this file will be overwritten 5 | with the user configuration generated by Atmel START 6 | */ 7 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/atca_start_iface.h: -------------------------------------------------------------------------------- 1 | /* 2 | this is a placeholder include used to satisfy the include 3 | 4 | when used with Atmel START, this file will be overwritten 5 | with the user configuration generated by Atmel START 6 | */ -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/hal_arduino.h: -------------------------------------------------------------------------------- 1 | #ifndef HAL_ARDUINO_H_ 2 | #define HAL_ARDUINO_H_ 3 | 4 | #include "../atca_command.h" 5 | #include "atca_hal.h" 6 | #include "../atca_device.h" 7 | #include "../basic/atca_basic.h" 8 | 9 | # define I2C_BUSES 1 10 | # define WAKE_WORD 0x00 11 | # define WORD 0x03 12 | # define DELAY_READ_VALUE 100 13 | 14 | 15 | typedef struct atcaI2Cmaster { 16 | int _index_bus; 17 | int ref_ct; 18 | } ATCAI2CMaster_t; 19 | 20 | struct Version 21 | { 22 | uint8_t revision[4]; 23 | ATCADeviceType type; 24 | }; 25 | 26 | void change_i2c_speed(ATCAIface iface, uint32_t speed); 27 | 28 | 29 | #endif /* HAL_ARDUINO_H_*/ -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/kit_phy.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief ATCA Hardware abstraction layer physical send & receive function definitions. 4 | * 5 | * This is included for kit protocol implementations. 6 | * It is included in the kit protocol callback to actually send and recieve bytes. 7 | * 8 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 9 | * 10 | * \page License 11 | * 12 | * Subject to your compliance with these terms, you may use Microchip software 13 | * and any derivatives exclusively with Microchip products. It is your 14 | * responsibility to comply with third party license terms applicable to your 15 | * use of third party software (including open source software) that may 16 | * accompany Microchip software. 17 | * 18 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 19 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 20 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 21 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 22 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 23 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 24 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 25 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 26 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 27 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 28 | * THIS SOFTWARE. 29 | */ 30 | 31 | #ifndef KIT_PHY_H_ 32 | #define KIT_PHY_H_ 33 | 34 | #include "../cryptoauthlib.h" 35 | 36 | 37 | /** \defgroup hal_ Hardware abstraction layer (hal_) 38 | * 39 | * \brief 40 | * These methods define the hardware abstraction layer for communicating with a CryptoAuth device 41 | * 42 | @{ */ 43 | 44 | #ifdef __cplusplus 45 | extern "C" { 46 | #endif 47 | 48 | ATCA_STATUS kit_phy_num_found(int8_t* num_found); 49 | ATCA_STATUS kit_phy_send(ATCAIface iface, const char *txdata, int txlength); 50 | ATCA_STATUS kit_phy_receive(ATCAIface iface, char* rxdata, int* rxlength); 51 | 52 | #ifdef __cplusplus 53 | } 54 | #endif 55 | 56 | /** @} */ 57 | 58 | #endif /* ATCA_HAL_PHY_H_ */ 59 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/hal/kit_protocol.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * 4 | * \brief 5 | * 6 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 7 | * 8 | * \page License 9 | * 10 | * Subject to your compliance with these terms, you may use Microchip software 11 | * and any derivatives exclusively with Microchip products. It is your 12 | * responsibility to comply with third party license terms applicable to your 13 | * use of third party software (including open source software) that may 14 | * accompany Microchip software. 15 | * 16 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 17 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 18 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 19 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 20 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 21 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 22 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 23 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 24 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 25 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 26 | * THIS SOFTWARE. 27 | */ 28 | 29 | #ifndef KIT_PROTOCOL_H_ 30 | #define KIT_PROTOCOL_H_ 31 | 32 | #include "../cryptoauthlib.h" 33 | 34 | // Define this for debugging communication 35 | //#define KIT_DEBUG 36 | 37 | /** \defgroup hal_ Hardware abstraction layer (hal_) 38 | * 39 | * \brief 40 | * These methods define the hardware abstraction layer for communicating with a CryptoAuth device 41 | * 42 | @{ */ 43 | 44 | // The number of bytes to wrap a command in kit protocol. sizeof("s:t()\n") 45 | #define KIT_TX_WRAP_SIZE (7) 46 | 47 | // The number of bytes to wrap a response in kit protocol. sizeof("00()\n") 48 | #define KIT_MSG_SIZE (32) 49 | #define KIT_RX_WRAP_SIZE (KIT_MSG_SIZE + 6) 50 | 51 | #ifdef __cplusplus 52 | extern "C" { 53 | #endif 54 | 55 | ATCA_STATUS kit_init(ATCAIface iface); 56 | 57 | ATCA_STATUS kit_send(ATCAIface iface, const uint8_t* txdata, int txlength); 58 | ATCA_STATUS kit_receive(ATCAIface iface, uint8_t* rxdata, uint16_t* rxsize); 59 | 60 | ATCA_STATUS kit_wrap_cmd(const uint8_t* txdata, int txlength, char* pkitbuf, int* nkitbuf, char target); 61 | ATCA_STATUS kit_parse_rsp(const char* pkitbuf, int nkitbuf, uint8_t* kitstatus, uint8_t* rxdata, int* nrxdata); 62 | 63 | ATCA_STATUS kit_wake(ATCAIface iface); 64 | ATCA_STATUS kit_idle(ATCAIface iface); 65 | ATCA_STATUS kit_sleep(ATCAIface iface); 66 | 67 | #ifdef __cplusplus 68 | } 69 | #endif 70 | 71 | /** @} */ 72 | 73 | #endif // KIT_PROTOCOL_H 74 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/jwt/atca_jwt.h: -------------------------------------------------------------------------------- 1 | /** 2 | * \file 3 | * \brief Utilities to create and verify a JSON Web Token (JWT) 4 | * 5 | * \copyright (c) 2015-2020 Microchip Technology Inc. and its subsidiaries. 6 | * 7 | * \page License 8 | * 9 | * Subject to your compliance with these terms, you may use Microchip software 10 | * and any derivatives exclusively with Microchip products. It is your 11 | * responsibility to comply with third party license terms applicable to your 12 | * use of third party software (including open source software) that may 13 | * accompany Microchip software. 14 | * 15 | * THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 16 | * EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 17 | * WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 18 | * PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 19 | * SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 20 | * OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 21 | * MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 22 | * FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 23 | * LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 24 | * THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 25 | * THIS SOFTWARE. 26 | */ 27 | 28 | #ifndef ATCA_JWT_H_ 29 | #define ATCA_JWT_H_ 30 | 31 | /** \defgroup jwt JSON Web Token (JWT) methods (atca_jwt_) 32 | * \brief Methods for signing and verifying JSON Web Token (JWT) tokens. 33 | @{ */ 34 | 35 | #include "../cryptoauthlib.h" 36 | 37 | #ifdef __cplusplus 38 | extern "C" { 39 | #endif 40 | 41 | /** \brief Structure to hold metadata information about the jwt being built */ 42 | typedef struct 43 | { 44 | char* buf; /* Input buffer */ 45 | uint16_t buflen; /* Total buffer size */ 46 | uint16_t cur; /* Current location in the buffer */ 47 | } atca_jwt_t; 48 | 49 | ATCA_STATUS atca_jwt_init(atca_jwt_t* jwt, char* buf, uint16_t buflen); 50 | ATCA_STATUS atca_jwt_add_claim_string(atca_jwt_t* jwt, const char* claim, const char* value); 51 | ATCA_STATUS atca_jwt_add_claim_numeric(atca_jwt_t* jwt, const char* claim, int32_t value); 52 | ATCA_STATUS atca_jwt_finalize(atca_jwt_t* jwt, uint16_t key_id); 53 | void atca_jwt_check_payload_start(atca_jwt_t* jwt); 54 | ATCA_STATUS atca_jwt_verify(const char* buf, uint16_t buflen, const uint8_t* pubkey); 55 | 56 | /** @} */ 57 | #ifdef __cplusplus 58 | } 59 | #endif 60 | 61 | #endif /* ATCA_JWT_H_ */ 62 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/LICENSE.txt: -------------------------------------------------------------------------------- 1 | (c) 2017 Microchip Technology Inc. and its subsidiaries. You may use this 2 | software and any derivatives exclusively with Microchip products. 3 | 4 | THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 5 | EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 6 | WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A PARTICULAR 7 | PURPOSE, OR ITS INTERACTION WITH MICROCHIP PRODUCTS, COMBINATION WITH ANY OTHER 8 | PRODUCTS, OR USE IN ANY APPLICATION. 9 | 10 | IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, SPECIAL, PUNITIVE, 11 | INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE OF ANY KIND 12 | WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF MICROCHIP HAS BEEN 13 | ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE FORESEEABLE. TO THE FULLEST 14 | EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL LIABILITY ON ALL CLAIMS IN ANY WAY 15 | RELATED TO THIS SOFTWARE WILL NOT EXCEED THE AMOUNT OF FEES, IF ANY, THAT YOU 16 | HAVE PAID DIRECTLY TO MICROCHIP FOR THIS SOFTWARE. 17 | 18 | MICROCHIP PROVIDES THIS SOFTWARE CONDITIONALLY UPON YOUR ACCEPTANCE OF THESE 19 | TERMS. 20 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/MANIFEST.in: -------------------------------------------------------------------------------- 1 | recursive-include lib *.c *.h atca_config.h.in CMakeLists.txt 90-cryptohid.rules 2 | recursive-include third_party *.c *.h 3 | recursive-include app *.c *.h 4 | include MANIFEST.in setup.py setup.cfg README.md VERSION atca_utils_sizes.c 5 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/cryptoauthlib/__init__.py: -------------------------------------------------------------------------------- 1 | """ 2 | Package Definition 3 | """ 4 | 5 | import sys 6 | 7 | # All modules are setup to be import * safe so the following is an exception to 8 | # the python coding standard. Also the library has to be loaded before loading 9 | # the rest of the module to correctly set up the ctypes structures 10 | # pylint: disable-msg=wildcard-import, wrong-import-position 11 | 12 | from .library import * 13 | from .exceptions import * 14 | 15 | try: 16 | load_cryptoauthlib() 17 | except LibraryLoadError as error: 18 | if not hasattr(sys, '_called_from_test'): 19 | raise error 20 | 21 | from .status import * 22 | from .atcab import * 23 | from .atcacert import * 24 | from .atjwt import * 25 | from .iface import * 26 | from .tng import * 27 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/cryptoauthlib/atcaenum.py: -------------------------------------------------------------------------------- 1 | """ 2 | Enum Extension for improved comparisons 3 | """ 4 | from enum import Enum 5 | 6 | # This is because @DynamicClassAttribue isn't exactly @property and pylint doesn't really understand it as the same 7 | # pylint: disable-msg=comparison-with-callable 8 | 9 | 10 | class AtcaEnum(Enum): 11 | """ 12 | Overload of standard python enum for some additional convenience features. Assumes closer alignment to C style enums 13 | where the value is always an integer 14 | """ 15 | def __str__(self): 16 | return self.name 17 | 18 | def __eq__(self, other): 19 | if isinstance(other, str): 20 | answer = (self.name == other) 21 | else: 22 | answer = (self.value == int(other)) 23 | return answer 24 | 25 | def __ne__(self, other): 26 | if isinstance(other, str): 27 | answer = (self.name != other) 28 | else: 29 | answer = (self.value != int(other)) 30 | return answer 31 | 32 | def __int__(self): 33 | return int(self.value) 34 | 35 | 36 | # Make module import * safe - keep at the end of the file 37 | __all__ = ['AtcaEnum'] 38 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/cryptoauthlib/atjwt.py: -------------------------------------------------------------------------------- 1 | """ 2 | JWT: Extension to the jwt module with hardware based security 3 | """ 4 | # (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. 5 | # 6 | # Subject to your compliance with these terms, you may use Microchip software 7 | # and any derivatives exclusively with Microchip products. It is your 8 | # responsibility to comply with third party license terms applicable to your 9 | # use of third party software (including open source software) that may 10 | # accompany Microchip software. 11 | # 12 | # THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 13 | # EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 14 | # WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 15 | # PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 16 | # SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 17 | # OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 18 | # MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 19 | # FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 20 | # LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 21 | # THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 22 | # THIS SOFTWARE. 23 | 24 | # pylint: disable-msg=too-few-public-methods 25 | 26 | try: 27 | import hmac 28 | from jwt import PyJWT as Jwt 29 | from jwt.algorithms import ECAlgorithm, HMACAlgorithm 30 | from cryptography.hazmat.backends import default_backend 31 | from cryptography.hazmat.primitives import hashes 32 | 33 | from .status import check_status 34 | from .atcab import atcab_init, atcab_release, atcab_sign, atcab_sha_hmac, atcab_nonce_rand 35 | 36 | class HwEcAlgorithm(ECAlgorithm): 37 | """ 38 | Extended Algorithm with hardware based elliptic curve support 39 | """ 40 | def __init__(self, hash_alg, slot, iface_cfg): 41 | super(HwEcAlgorithm, self).__init__(hash_alg) 42 | self._cfg = iface_cfg 43 | self._slot = slot 44 | 45 | def sign(self, msg, _): 46 | """ 47 | Return a signature of the JWT with hardware ECDSA 48 | """ 49 | if self._cfg is not None: 50 | check_status(atcab_init(self._cfg)) 51 | 52 | digest = hashes.Hash(self.hash_alg(), backend=default_backend()) 53 | digest.update(msg) 54 | digest = digest.finalize() 55 | 56 | signature = bytearray(64) 57 | check_status(atcab_sign(self._slot, digest, signature)) 58 | 59 | if self._cfg is not None: 60 | check_status(atcab_release()) 61 | 62 | return signature 63 | 64 | 65 | class HwHmacAlgorithm(HMACAlgorithm): 66 | """ 67 | Extended Algorithm with hardware based HMAC support 68 | """ 69 | def __init__(self, hash_alg, slot, iface_cfg): 70 | super(HwHmacAlgorithm, self).__init__(hash_alg) 71 | self._cfg = iface_cfg 72 | self._slot = slot 73 | 74 | def sign(self, msg, _): 75 | """ 76 | Return a signature of the JWT with hardware SHA256 HMAC and stored key 77 | """ 78 | if self._cfg is not None: 79 | check_status(atcab_init(self._cfg)) 80 | 81 | check_status(atcab_nonce_rand(bytearray(20), bytearray(32))) 82 | 83 | digest = bytearray(32) 84 | check_status(atcab_sha_hmac(msg, len(msg), self._slot, digest, 0)) 85 | 86 | if self._cfg is not None: 87 | check_status(atcab_release()) 88 | 89 | return bytes(digest) 90 | 91 | def verify(self, msg, key, sig): 92 | """ 93 | Verify a signature using the software HMAC module 94 | """ 95 | return sig == hmac.new(key, msg, self.hash_alg).digest() 96 | 97 | 98 | class PyJWT(Jwt): 99 | """ 100 | Extended PyJWT class from the pyjwt module 101 | """ 102 | def __init__(self, slot=0, iface_cfg=None, options=None): 103 | super(PyJWT, self).__init__(algorithms=[], options=options) 104 | self.register_algorithm('ES256', HwEcAlgorithm(HwEcAlgorithm.SHA256, slot, iface_cfg)) 105 | self.register_algorithm('HS256', HwHmacAlgorithm(HwHmacAlgorithm.SHA256, slot, iface_cfg)) 106 | 107 | __all__ = ['PyJWT'] 108 | 109 | except ImportError: 110 | pass 111 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/cryptoauthlib/exceptions.py: -------------------------------------------------------------------------------- 1 | """ 2 | Cryptoauthlib Exceptions 3 | """ 4 | # (c) 2015-2018 Microchip Technology Inc. and its subsidiaries. 5 | # 6 | # Subject to your compliance with these terms, you may use Microchip software 7 | # and any derivatives exclusively with Microchip products. It is your 8 | # responsibility to comply with third party license terms applicable to your 9 | # use of third party software (including open source software) that may 10 | # accompany Microchip software. 11 | # 12 | # THIS SOFTWARE IS SUPPLIED BY MICROCHIP "AS IS". NO WARRANTIES, WHETHER 13 | # EXPRESS, IMPLIED OR STATUTORY, APPLY TO THIS SOFTWARE, INCLUDING ANY IMPLIED 14 | # WARRANTIES OF NON-INFRINGEMENT, MERCHANTABILITY, AND FITNESS FOR A 15 | # PARTICULAR PURPOSE. IN NO EVENT WILL MICROCHIP BE LIABLE FOR ANY INDIRECT, 16 | # SPECIAL, PUNITIVE, INCIDENTAL OR CONSEQUENTIAL LOSS, DAMAGE, COST OR EXPENSE 17 | # OF ANY KIND WHATSOEVER RELATED TO THE SOFTWARE, HOWEVER CAUSED, EVEN IF 18 | # MICROCHIP HAS BEEN ADVISED OF THE POSSIBILITY OR THE DAMAGES ARE 19 | # FORESEEABLE. TO THE FULLEST EXTENT ALLOWED BY LAW, MICROCHIP'S TOTAL 20 | # LIABILITY ON ALL CLAIMS IN ANY WAY RELATED TO THIS SOFTWARE WILL NOT EXCEED 21 | # THE AMOUNT OF FEES, IF ANY, THAT YOU HAVE PAID DIRECTLY TO MICROCHIP FOR 22 | # THIS SOFTWARE. 23 | 24 | 25 | class CryptoError(Exception): 26 | """Standard CryptoAuthLib Exceptions""" 27 | 28 | 29 | class LibraryLoadError(CryptoError): 30 | """CryptpAuthLib failed to Load""" 31 | 32 | 33 | class ConfigZoneLockedError(CryptoError): 34 | """Config Zone Locked""" 35 | 36 | 37 | class DataZoneLockedError(CryptoError): 38 | """Configuration Enabled""" 39 | 40 | 41 | class WakeFailedError(CryptoError): 42 | """Device Wake failed""" 43 | 44 | 45 | class CheckmacVerifyFailedError(CryptoError): 46 | """response status byte indicates CheckMac failure (status byte = 0x01)""" 47 | 48 | 49 | class ParseError(CryptoError): 50 | """response status byte indicates parsing error (status byte = 0x03)""" 51 | 52 | 53 | class CrcError(CryptoError): 54 | """response status byte indicates CRC error (status byte = 0xFF)""" 55 | 56 | 57 | class StatusUnknownError(CryptoError): 58 | """Response status byte is unknown""" 59 | 60 | 61 | class EccFaultError(CryptoError): 62 | """response status byte is ECC fault (status byte = 0x05)""" 63 | 64 | 65 | class FunctionError(CryptoError): 66 | """Function could not execute due to incorrect condition / state.""" 67 | 68 | 69 | class GenericError(CryptoError): 70 | """unspecified error""" 71 | 72 | 73 | class BadArgumentError(CryptoError): 74 | """bad argument (out of range, null pointer, etc.)""" 75 | 76 | 77 | class InvalidIdentifierError(CryptoError): 78 | """invalid device id, id not set""" 79 | 80 | 81 | class InvalidSizeError(CryptoError): 82 | """Count value is out of range or greater than buffer size.""" 83 | 84 | 85 | class BadCrcError(CryptoError): 86 | """incorrect CRC received""" 87 | 88 | 89 | class ReceiveError(CryptoError): 90 | """Timed out while waiting for response. Number of bytes received is > 0.""" 91 | 92 | 93 | class NoResponseError(CryptoError): 94 | """error while the Command layer is polling for a command response.""" 95 | 96 | 97 | class ResyncWithWakeupError(CryptoError): 98 | """Re-synchronization succeeded, but only after generating a Wake-up""" 99 | 100 | 101 | class ParityError(CryptoError): 102 | """for protocols needing parity""" 103 | 104 | 105 | class TransmissionTimeoutError(CryptoError): 106 | """for Microchip PHY protocol, timeout on transmission waiting for master""" 107 | 108 | 109 | class ReceiveTimeoutError(CryptoError): 110 | """for Microchip PHY protocol, timeout on receipt waiting for master""" 111 | 112 | 113 | class CommunicationError(CryptoError): 114 | """Communication with device failed. Same as in hardware dependent modules.""" 115 | 116 | 117 | class TimeOutError(CryptoError): 118 | """Timed out while waiting for response. Number of bytes received is 0.""" 119 | 120 | 121 | class BadOpcodeError(CryptoError): 122 | """Opcode is not supported by the device""" 123 | 124 | 125 | class ExecutionError(CryptoError): 126 | """chip was in a state where it could not execute the command, response 127 | status byte indicates command execution error (status byte = 0x0F)""" 128 | 129 | 130 | class UnimplementedError(CryptoError): 131 | """Function or some element of it hasn't been implemented yet""" 132 | 133 | 134 | class AssertionFailure(CryptoError): 135 | """Code failed run-time consistency check""" 136 | 137 | 138 | class TransmissionError(CryptoError): 139 | """Failed to write""" 140 | 141 | 142 | class ZoneNotLockedError(CryptoError): 143 | """required zone was not locked""" 144 | 145 | 146 | class NoDevicesFoundError(CryptoError): 147 | """For protocols that support device discovery (kit protocol), no devices were found""" 148 | 149 | 150 | class HealthTestError(CryptoError): 151 | """Random number generator health test error""" 152 | 153 | 154 | class LibraryMemoryError(CryptoError): 155 | """CryptoAuthLib was unable to allocate memory""" 156 | 157 | 158 | class LibraryNotInitialized(CryptoError): 159 | """Indication that library or context was not initialized prior to an API call""" 160 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/README.md: -------------------------------------------------------------------------------- 1 | # Python CryptoAuthLib Module Testing 2 | 3 | ## Introduction 4 | These tests are designed to only test the python interface to the library and 5 | are not designed to test the library itself which is covered by the main 6 | cryptoauthlib tests 7 | 8 | ### Running 9 | The best way to run the test suite is to use [tox](https://tox.readthedocs.io/en/latest/) 10 | which can be easily installed with pip: 11 | 12 | ``` 13 | $ pip install tox 14 | ``` 15 | 16 | From the python folder: 17 | 18 | ``` 19 | :~/cryptoauthlib/python $ tox 20 | ``` 21 | 22 | It is possible to directly run tests but requires more setup 23 | 24 | 1) Install pytest 25 | 26 | ``` 27 | $ pip install pytest 28 | ``` 29 | 30 | 2) Modify the PYTHONPATH environment variable 31 | 32 | Windows: 33 | ``` 34 | cryptoauthlib/python> set PYTHONPATH=/cryptoauthlib/python 35 | ``` 36 | 37 | Linux: 38 | ``` 39 | $ export PYTHONPATH=${PYTHONPATH}:/cryptoauthlib/python 40 | ``` 41 | 42 | 3) Run the tests 43 | ``` 44 | $ pytest -vv 45 | ``` 46 | 47 | ### Test options 48 | 49 | There are additional options that can be invoked with the tests that define 50 | what tests will be run 51 | 52 | 1) --with-lib will attempt to run tests against the compiled c library. 53 | These tests are good for detecting possible platform incompabilities between 54 | the C compiler and the expectations of python 55 | 56 | 2) --with-device will attempt to invoke some tests with a real attached device 57 | These tests are restricted to only the minimum required to verify the python to 58 | library connectivity and are only meant to detect situations can can not be 59 | determined from the library tests alone. 60 | 61 | 62 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/conftest.py: -------------------------------------------------------------------------------- 1 | # content of conftest.py 2 | import pytest 3 | 4 | 5 | def pytest_addoption(parser): 6 | parser.addoption( 7 | "--with-lib", action="store_true", help="Run Tests against a library instance" 8 | ) 9 | parser.addoption( 10 | "--with-device", action="store_true", help="Run Tests that require a real device" 11 | ) 12 | 13 | 14 | @pytest.fixture 15 | def test_init_with_lib(request): 16 | """ 17 | If a test requires a compiled library skip it if this option is not specified 18 | """ 19 | if not request.config.getoption("--with-lib"): 20 | pytest.skip('--with-lib option was not specified') 21 | 22 | 23 | @pytest.fixture 24 | def test_init_with_device(request): 25 | """ 26 | If a test requires a real device skip the test if this option is not specified 27 | """ 28 | if not request.config.getoption("--with-device"): 29 | pytest.skip('--with-device option was not specified') 30 | 31 | 32 | def pytest_configure(config): 33 | import sys 34 | sys._called_from_test = True 35 | 36 | 37 | def pytest_unconfigure(config): 38 | import sys 39 | del sys._called_from_test 40 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/test_iface.py: -------------------------------------------------------------------------------- 1 | """ 2 | These tests verify the structures match the expectation from what is in atca_cfs.c 3 | If that file has been modified then the tests will fail. If the file has not been 4 | modified then we can reasonably expect that there is a problem with the ctypes 5 | definition or assumptions of the platform build and memory alignment is wrong 6 | """ 7 | import pytest 8 | import ctypes 9 | from cryptoauthlib.iface import * 10 | from cryptoauthlib.library import load_cryptoauthlib, get_size_by_name 11 | 12 | 13 | @pytest.fixture 14 | def test_iface_init(test_init_with_lib): 15 | load_cryptoauthlib() 16 | 17 | 18 | def test_iface_cfg_size(test_iface_init): 19 | assert ctypes.sizeof(ATCAIfaceCfg) == get_size_by_name('ATCAIfaceCfg') 20 | 21 | 22 | def test_iface_cfg_ateccx08a_i2c(test_iface_init): 23 | cfg = cfg_ateccx08a_i2c_default() 24 | assert cfg.iface_type == ATCAIfaceType.ATCA_I2C_IFACE 25 | assert cfg.devtype == ATCADeviceType.ATECC608A 26 | assert cfg.cfg.atcai2c.slave_address == 0xC0 27 | assert cfg.cfg.atcai2c.bus == 2 28 | assert cfg.cfg.atcai2c.baud == 400000 29 | 30 | 31 | def test_iface_cfg_ateccx08a_swi(test_iface_init): 32 | cfg = cfg_ateccx08a_swi_default() 33 | assert cfg.iface_type == ATCAIfaceType.ATCA_SWI_IFACE 34 | assert cfg.devtype == ATCADeviceType.ATECC608A 35 | assert cfg.cfg.atcaswi.bus == 4 36 | 37 | 38 | def test_iface_cfg_ateccx08a_kithid(test_iface_init): 39 | cfg = cfg_ateccx08a_kithid_default() 40 | assert cfg.iface_type == ATCAIfaceType.ATCA_HID_IFACE 41 | assert cfg.devtype == ATCADeviceType.ATECC608A 42 | assert cfg.cfg.atcahid.vid == 0x03EB 43 | assert cfg.cfg.atcahid.pid == 0x2312 44 | 45 | 46 | def test_iface_cfg_atsha204a_i2c(test_iface_init): 47 | cfg = cfg_atsha20xa_i2c_default() 48 | assert cfg.iface_type == ATCAIfaceType.ATCA_I2C_IFACE 49 | assert cfg.devtype == ATCADeviceType.ATSHA204A 50 | assert cfg.cfg.atcai2c.slave_address == 0xC8 51 | assert cfg.cfg.atcai2c.bus == 2 52 | assert cfg.cfg.atcai2c.baud == 400000 53 | 54 | 55 | def test_iface_cfg_atsha204a_swi(test_iface_init): 56 | cfg = cfg_atsha20xa_swi_default() 57 | assert cfg.iface_type == ATCAIfaceType.ATCA_SWI_IFACE 58 | assert cfg.devtype == ATCADeviceType.ATSHA204A 59 | assert cfg.cfg.atcaswi.bus == 4 60 | 61 | 62 | def test_iface_cfg_atsha204a_kithid(test_iface_init): 63 | cfg = cfg_atsha20xa_kithid_default() 64 | assert cfg.iface_type == ATCAIfaceType.ATCA_HID_IFACE 65 | assert cfg.devtype == ATCADeviceType.ATSHA204A 66 | assert cfg.cfg.atcahid.vid == 0x03EB 67 | assert cfg.cfg.atcahid.pid == 0x2312 68 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/test_jwt.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | import datetime 3 | import time 4 | import base64 5 | 6 | from cryptoauthlib import * 7 | from cryptoauthlib.library import load_cryptoauthlib 8 | from cryptoauthlib_mock import atcab_mock 9 | 10 | __config = cfg_ateccx08a_kithid_default() 11 | 12 | def pretty_print_hex(a, l=16, indent=''): 13 | """ 14 | Format a list/bytes/bytearray object into a formatted ascii hex string 15 | """ 16 | s = '' 17 | a = bytearray(a) 18 | for x in range(0, len(a), l): 19 | s += indent + ''.join(['%02X ' % y for y in a[x:x+l]]) + '\n' 20 | return s 21 | 22 | 23 | def pubnums_to_bytes(pub_nums): 24 | return bytes(bytearray.fromhex('%064X%064X' % (pub_nums.x, pub_nums.y))) 25 | 26 | 27 | @pytest.fixture 28 | def test_jwt_init(): 29 | """ 30 | Run tests against the library mock 31 | """ 32 | load_cryptoauthlib(atcab_mock()) 33 | 34 | 35 | @pytest.fixture 36 | def test_jwt_init_live(test_init_with_device): 37 | """ 38 | Use real hardware for these tests - otherwise skip 39 | """ 40 | load_cryptoauthlib() 41 | 42 | if Status.ATCA_SUCCESS != atcab_init(__config): 43 | raise Exception('Unable to connect to a device') 44 | 45 | # Check device type 46 | info = bytearray(4) 47 | assert Status.ATCA_SUCCESS == atcab_info(info) 48 | dev_type = get_device_type_id(get_device_name(info)) 49 | 50 | if dev_type != __config.devtype: 51 | __config.devtype = dev_type 52 | assert Status.ATCA_SUCCESS == atcab_release() 53 | time.sleep(1) 54 | assert Status.ATCA_SUCCESS == atcab_init(__config) 55 | 56 | 57 | @pytest.mark.parametrize("slot, config", [ 58 | pytest.param(0, None, id='Normal'), 59 | pytest.param(0, __config, id='Init/Reinit'), 60 | ]) 61 | def test_jwt_round_trip_ec_qa(test_jwt_init_live, slot, config): 62 | """ 63 | Test JWT with an asymetric key (Elliptic Curve: SECP256r1) 64 | """ 65 | # Load device public key 66 | public_key = bytearray(64) 67 | assert Status.ATCA_SUCCESS == atcab_get_pubkey(0, public_key) 68 | 69 | # Convert to the key to PEM format 70 | public_key_pem = bytearray.fromhex('3059301306072A8648CE3D020106082A8648CE3D03010703420004') + public_key 71 | public_key_pem = '-----BEGIN PUBLIC KEY-----\n' + base64.b64encode(public_key_pem).decode('ascii') + '\n-----END PUBLIC KEY-----' 72 | 73 | claims = { 74 | # The time that the token was issued at 75 | 'iat': datetime.datetime.utcnow(), 76 | # The time the token expires. 77 | 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60), 78 | # A Dummy/Test Audience to verify against 79 | 'aud': 'test_audience' 80 | } 81 | 82 | token = PyJWT(slot, config) 83 | encoded = token.encode(claims, public_key_pem, algorithm='ES256') 84 | 85 | # If the audience does not match or the signature fails to verify the following will raise an exception 86 | decoded = token.decode(encoded, public_key_pem, audience=claims['aud'], algorithms=['ES256']) 87 | 88 | assert claims == decoded 89 | 90 | 91 | @pytest.mark.parametrize("slot, config", [ 92 | pytest.param(1, None, id='Normal'), 93 | pytest.param(1, __config, id='Init/Reinit'), 94 | ]) 95 | def test_jwt_round_trip_hmac_qa(test_jwt_init_live, slot, config): 96 | """ 97 | Check JWT with a symmetric key (SHA256 based HMAC) 98 | """ 99 | # Set write key 100 | write_key = bytearray([0x37, 0x80, 0xe6, 0x3d, 0x49, 0x68, 0xad, 0xe5, 101 | 0xd8, 0x22, 0xc0, 0x13, 0xfc, 0xc3, 0x23, 0x84, 102 | 0x5d, 0x1b, 0x56, 0x9f, 0xe7, 0x05, 0xb6, 0x00, 103 | 0x06, 0xfe, 0xec, 0x14, 0x5a, 0x0d, 0xb1, 0xe3]) 104 | assert Status.ATCA_SUCCESS == atcab_write_zone(2, 4, 0, 0, write_key, 32); 105 | 106 | # Write HMAC key 107 | hmac_key = bytearray([0x73, 0x16, 0xe9, 0x64, 0x2b, 0x38, 0xfb, 0xad, 108 | 0x5d, 0xb7, 0x0a, 0x1b, 0x33, 0xf0, 0xdc, 0xb9, 109 | 0x4c, 0x35, 0x5e, 0x78, 0xd7, 0xf0, 0x00, 0xa9, 110 | 0xb3, 0x19, 0x41, 0xa0, 0x36, 0x0d, 0x09, 0x61]) 111 | assert Status.ATCA_SUCCESS == atcab_write_enc(slot, 0, hmac_key, write_key, 4); 112 | 113 | claims = { 114 | # The time that the token was issued at 115 | 'iat': datetime.datetime.utcnow(), 116 | # The time the token expires. 117 | 'exp': datetime.datetime.utcnow() + datetime.timedelta(minutes=60), 118 | # A Dummy/Test Audience to verify against 119 | 'aud': 'test_audience' 120 | } 121 | 122 | token = PyJWT(slot, config) 123 | encoded = token.encode(claims, b'', algorithm='HS256') 124 | 125 | # If the audience does not match or the signature fails to verify the following will raise an exception 126 | decoded = token.decode(encoded, bytes(hmac_key), audience=claims['aud'], algorithms=['HS256']) 127 | 128 | assert claims == decoded 129 | 130 | 131 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/test_library.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from cryptoauthlib.library import load_cryptoauthlib, get_cryptoauthlib, AtcaReference 3 | from cryptoauthlib_mock import atcab_mock 4 | 5 | 6 | def test_library(): 7 | inst = atcab_mock() 8 | load_cryptoauthlib(inst) 9 | assert get_cryptoauthlib() == inst 10 | 11 | 12 | def test_library_load_dll(test_init_with_lib): 13 | load_cryptoauthlib() 14 | 15 | 16 | def test_library_AtcaReference_integer(): 17 | a = AtcaReference(4) 18 | assert 3 != a 19 | assert 3 < a 20 | assert 3 <= a 21 | assert 4 <= a 22 | assert 4 == a 23 | assert 4 >= a 24 | assert 5 > a 25 | assert 5 >= a 26 | assert 4 == int(a) 27 | assert '4' == str(a) 28 | 29 | 30 | def test_library_AtcaReference_modify_integer(): 31 | f = lambda x: setattr(x, 'value', x.value + 1) 32 | 33 | a = AtcaReference(4) 34 | f(a) 35 | assert 5 == a 36 | f(a) 37 | assert 6 == a 38 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/test_status.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | from cryptoauthlib.status import * 3 | 4 | @pytest.fixture(scope="module") 5 | def test_status_init(): 6 | pass 7 | 8 | def test_atcaenum_to_string(test_status_init): 9 | assert isinstance(str(Status.ATCA_SUCCESS), str) 10 | 11 | 12 | def test_atcaenum_from_string(test_status_init): 13 | assert Status.ATCA_BAD_PARAM == Status['ATCA_BAD_PARAM'] 14 | 15 | 16 | def test_atcaenum_to_int(test_status_init): 17 | assert isinstance(int(Status.ATCA_BAD_PARAM), int) 18 | 19 | 20 | def test_atcaenum_from_int(test_status_init): 21 | assert Status.ATCA_BAD_PARAM == Status(0xE2) 22 | 23 | 24 | def test_atcaenum_eq(test_status_init): 25 | assert Status.ATCA_SUCCESS == Status.ATCA_SUCCESS 26 | 27 | 28 | def test_atcaenum_ne(test_status_init): 29 | assert Status.ATCA_BAD_PARAM != Status.ATCA_SUCCESS 30 | 31 | 32 | def test_atcaenum_int_eq(test_status_init): 33 | assert Status.ATCA_SUCCESS == 0 34 | 35 | 36 | def test_atcaenum_int_ne(test_status_init): 37 | assert Status.ATCA_SUCCESS != 226 38 | 39 | 40 | def test_atcaenum_string_eq(test_status_init): 41 | assert Status.ATCA_SUCCESS == 'ATCA_SUCCESS' 42 | 43 | 44 | def test_atcaenum_string_ne(test_status_init): 45 | assert Status.ATCA_SUCCESS != 'ATCA_BAD_PARAM' 46 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tests/test_tng.py: -------------------------------------------------------------------------------- 1 | import pytest 2 | 3 | from cryptoauthlib import * 4 | from cryptoauthlib.library import load_cryptoauthlib, get_size_by_name 5 | from cryptoauthlib_mock import atcab_mock 6 | 7 | 8 | @pytest.fixture 9 | def test_tng_init(): 10 | """ 11 | Run tests against the library mock 12 | """ 13 | load_cryptoauthlib(atcab_mock()) 14 | 15 | 16 | @pytest.fixture 17 | def test_tng_init_lib(test_init_with_lib): 18 | """ 19 | Run tests against a built library on the platform 20 | """ 21 | load_cryptoauthlib() 22 | 23 | 24 | @pytest.fixture 25 | def test_tng_init_live(test_init_with_device): 26 | """ 27 | Use real hardware for these tests - otherwise skip 28 | """ 29 | load_cryptoauthlib() 30 | if Status.ATCA_SUCCESS != atcab_init(cfg_ateccx08a_kithid_default()): 31 | raise Exception('Unable to connect to a device') 32 | 33 | 34 | def test_tng_get_type(test_tng_init): 35 | tng_type = AtcaReference(tng_type_t.TNGTYPE_UNKNOWN) 36 | assert tng_get_type(tng_type) == Status.ATCA_SUCCESS 37 | assert tng_type.value == atcab_mock.r_tng_type.value 38 | 39 | 40 | def test_tng_get_device_pubkey(test_tng_init): 41 | public_key = bytearray(64) 42 | assert tng_get_device_pubkey(public_key) == Status.ATCA_SUCCESS 43 | assert public_key == bytearray(atcab_mock.r_genkey_pubkey) 44 | 45 | 46 | def test_tng_atcacert_max_device_cert_size(test_tng_init): 47 | max_cert_size = AtcaReference(0) 48 | assert tng_atcacert_max_device_cert_size(max_cert_size) == CertStatus.ATCACERT_E_SUCCESS 49 | assert max_cert_size.value == atcab_mock.r_max_cert_size.value 50 | 51 | 52 | def test_tng_atcacert_read_device_cert_no_signer(test_tng_init): 53 | cert = bytearray(1024) 54 | cert_size = AtcaReference(len(cert)) 55 | assert tng_atcacert_read_device_cert(cert, cert_size) == CertStatus.ATCACERT_E_SUCCESS 56 | assert cert_size.value == atcab_mock.r_cert_size.value 57 | assert cert == bytearray(atcab_mock.r_cert) 58 | 59 | 60 | def test_tng_atcacert_read_device_cert_signer(test_tng_init): 61 | cert = bytearray(1024) 62 | cert_size = AtcaReference(len(cert)) 63 | signer_cert = bytes(512) 64 | assert tng_atcacert_read_device_cert(cert, cert_size, signer_cert) == CertStatus.ATCACERT_E_SUCCESS 65 | assert cert_size.value == atcab_mock.r_cert_size.value 66 | assert cert == bytearray(atcab_mock.r_cert) 67 | 68 | 69 | def test_tng_atcacert_device_public_key_no_cert(test_tng_init): 70 | public_key = bytearray(64) 71 | assert tng_atcacert_device_public_key(public_key) == CertStatus.ATCACERT_E_SUCCESS 72 | assert public_key == bytearray(atcab_mock.r_genkey_pubkey) 73 | 74 | 75 | def test_tng_atcacert_device_public_key_cert(test_tng_init): 76 | public_key = bytearray(64) 77 | cert = bytes(512) 78 | assert tng_atcacert_device_public_key(public_key, cert) == CertStatus.ATCACERT_E_SUCCESS 79 | assert public_key == bytearray(atcab_mock.r_genkey_pubkey) 80 | 81 | 82 | def test_tng_atcacert_max_signer_cert_size(test_tng_init): 83 | max_cert_size = AtcaReference(0) 84 | assert tng_atcacert_max_signer_cert_size(max_cert_size) == CertStatus.ATCACERT_E_SUCCESS 85 | assert max_cert_size.value == atcab_mock.r_max_cert_size.value 86 | 87 | 88 | def test_tng_atcacert_read_device_cert(test_tng_init): 89 | cert = bytearray(1024) 90 | cert_size = AtcaReference(len(cert)) 91 | assert tng_atcacert_read_signer_cert(cert, cert_size) == CertStatus.ATCACERT_E_SUCCESS 92 | assert cert_size.value == atcab_mock.r_cert_size.value 93 | assert cert == bytearray(atcab_mock.r_cert) 94 | 95 | 96 | def test_tng_atcacert_signer_public_key_no_cert(test_tng_init): 97 | public_key = bytearray(64) 98 | assert tng_atcacert_signer_public_key(public_key) == CertStatus.ATCACERT_E_SUCCESS 99 | assert public_key == bytearray(atcab_mock.r_genkey_pubkey) 100 | 101 | 102 | def test_tng_atcacert_signer_public_key_cert(test_tng_init): 103 | public_key = bytearray(64) 104 | cert = bytes(512) 105 | assert tng_atcacert_signer_public_key(public_key, cert) == CertStatus.ATCACERT_E_SUCCESS 106 | assert public_key == bytearray(atcab_mock.r_genkey_pubkey) 107 | 108 | 109 | def test_tng_atcacert_root_cert_size(test_tng_init): 110 | cert_size = AtcaReference(0) 111 | assert tng_atcacert_root_cert_size(cert_size) == CertStatus.ATCACERT_E_SUCCESS 112 | assert cert_size.value == atcab_mock.r_cert_size.value 113 | 114 | 115 | def test_tng_atcacert_root_cert(test_tng_init): 116 | cert = bytearray(1024) 117 | cert_size = AtcaReference(len(cert)) 118 | assert tng_atcacert_root_cert(cert, cert_size) == CertStatus.ATCACERT_E_SUCCESS 119 | assert cert_size.value == atcab_mock.r_cert_size.value 120 | assert cert == bytearray(atcab_mock.r_cert) 121 | 122 | 123 | def test_tng_atcacert_root_public_key(test_tng_init): 124 | public_key = bytearray(64) 125 | assert tng_atcacert_root_public_key(public_key) == CertStatus.ATCACERT_E_SUCCESS 126 | assert public_key == bytearray(atcab_mock.r_genkey_pubkey) 127 | -------------------------------------------------------------------------------- /src/ATECCX08A_Arduino/python/tox.ini: -------------------------------------------------------------------------------- 1 | [tox] 2 | envlist = pylint,py27,py35,py36,py37 3 | skipsdist = True 4 | 5 | [testenv:pylint] 6 | deps = pylint 7 | commands = pylint cryptoauthlib 8 | 9 | [testenv] 10 | passenv = CRYPTOAUTHLIB_NOUSB 11 | deps = pytest 12 | pytest-cov 13 | cryptography 14 | py27: enum34 15 | pytz 16 | pyjwt 17 | commands = python setup.py bdist_wheel 18 | pip uninstall cryptoauthlib -y 19 | pip install --no-index --find-links=dist cryptoauthlib 20 | pytest -vv --cov {envsitepackagesdir}/cryptoauthlib --with-lib --with-device 21 | coverage html -d .coverage_{envname} 22 | -------------------------------------------------------------------------------- /src/Examples/AES CBC/aes_cbc.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ATECCX08A_Arduino/cryptoauthlib.h" 3 | #include "aes_cbc.h" 4 | 5 | /** \brief Encrypt data using AES CBC algorithme 6 | * \param[in] cfg Logical interface configuration. Some predefined 7 | * configurations can be found in atca_cfgs.h 8 | * \param[in] data Words to encypt (must be divided by 16, max length 240) 9 | * \param[in] len length of Words to encypt (must be divided by 16, max length 240) 10 | * \param[out] iv Initial Vector used in the AES CBC (return the vector in this var) 11 | * \param[out] ciphertext return here the Cypher text 12 | * \param[in] key Slot number of the key 13 | * \return ATCA_SUCCESS on success, otherwise an error code. 14 | */ 15 | ATCA_STATUS aes_cbc_encrypt(ATCAIfaceCfg *cfg, uint8_t *data, int len, uint8_t *iv, uint8_t *ciphertext, uint8_t key) 16 | { 17 | 18 | atca_aes_cbc_ctx_t ctx; 19 | 20 | if (len > LIMIT_DATA_SIZE_CBC && len % 16 != 0) 21 | { 22 | Serial.print(F("ERROR : ATCA_BAD_PARAM")); 23 | return ATCA_BAD_PARAM; 24 | } 25 | uint8_t tmp_iv[IV_LENGTH_CBC]; 26 | uint8_t tmp_data[len]; 27 | 28 | ATCA_STATUS status = atcab_init(cfg); 29 | if (status == ATCA_SUCCESS) 30 | { 31 | status = atcab_aes_cbc_init(&ctx, key, 0, tmp_iv); 32 | 33 | if (status != ATCA_SUCCESS) 34 | { 35 | Serial.print(F("ERROR Encrypt : atcab_aes_cbc_init, Code Error 0x")); 36 | Serial.println(status, HEX); 37 | return; 38 | } 39 | memcpy(iv, tmp_iv, IV_LENGTH_CBC); 40 | memcpy(tmp_data, data, len); 41 | 42 | int max = len / 16; 43 | 44 | for (int j = 0; j < max; j++) 45 | { 46 | status = atcab_aes_cbc_encrypt_block(&ctx, &tmp_data[j * 16], &ciphertext[j * 16]); 47 | } 48 | if (status != ATCA_SUCCESS) 49 | { 50 | Serial.print(F("ERROR Encrypt : atcab_aes_cbc_encrypt_block, Code Error 0x")); 51 | Serial.println(status, HEX); 52 | } 53 | return status; 54 | } 55 | return status; 56 | } 57 | 58 | /** \brief Decrypt data using AES CBC algorithme 59 | * \param[in] cfg Logical interface configuration. Some predefined 60 | * configurations can be found in atca_cfgs.h 61 | * \param[in] ciphertext Words to decypt (must be divided by 16, max length 240) 62 | * \param[in] len length of Words to decypt (must be divided by 16, max length 240) 63 | * \param[in] iv Initial Vector to use in the AES CBC 64 | * \param[out] plaintext return here the decrypted text 65 | * \param[in] key Slot number of the key 66 | * \return ATCA_SUCCESS on success, otherwise an error code. 67 | */ 68 | ATCA_STATUS aes_cbc_decrypt(ATCAIfaceCfg *cfg, uint8_t *ciphertext, int len, uint8_t *iv, uint8_t *plaintext, uint8_t key) 69 | { 70 | 71 | atca_aes_cbc_ctx_t ctx; 72 | 73 | if (len > LIMIT_DATA_SIZE_CBC || len % 16 != 0) 74 | { 75 | Serial.print(F("ERROR Decrypt : ATCA_BAD_PARAM")); 76 | return ATCA_BAD_PARAM; 77 | } 78 | 79 | ATCA_STATUS status = atcab_init(cfg); 80 | if (status == ATCA_SUCCESS) 81 | { 82 | status = atcab_aes_cbc_init(&ctx, key, 0, iv); 83 | 84 | if (status != ATCA_SUCCESS) 85 | { 86 | Serial.print(F("ERROR Decrypt: atcab_aes_cbc_init, Code Error 0x")); 87 | Serial.println(status, HEX); 88 | return; 89 | } 90 | int max = len / 16; 91 | 92 | for (int j = 0; j < max; j++) 93 | { 94 | status = atcab_aes_cbc_decrypt_block(&ctx, &ciphertext[j * 16], &plaintext[j * 16]); 95 | } 96 | if (status != ATCA_SUCCESS) 97 | { 98 | Serial.print(F("ERROR Decrypt : atcab_aes_cbc_encrypt_block, Code Error 0x")); 99 | Serial.println(status, HEX); 100 | } 101 | 102 | return status; 103 | } 104 | return status; 105 | } 106 | -------------------------------------------------------------------------------- /src/Examples/AES CBC/aes_cbc.h: -------------------------------------------------------------------------------- 1 | #ifndef AES_CBC_H 2 | #define AES_CBC_H 3 | 4 | /* DO NOT MODIFIED */ 5 | #define IV_LENGTH_CBC 16 6 | #define LIMIT_DATA_SIZE_CBC 240 7 | 8 | ATCA_STATUS aes_cbc_decrypt(ATCAIfaceCfg *cfg, uint8_t *ciphertext, int len, uint8_t *iv, uint8_t *plaintext, uint8_t key); 9 | ATCA_STATUS aes_cbc_encrypt(ATCAIfaceCfg *cfg, uint8_t *data, int len, uint8_t *iv, uint8_t *ciphertext, uint8_t key); 10 | 11 | #endif /* aes_cbc.h */ -------------------------------------------------------------------------------- /src/Examples/AES_crypto_example.ino: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ATECCX08A_Arduino/cryptoauthlib.h" 3 | #include "Examples/AES CBC/aes_cbc.h" 4 | 5 | // Key Slot number 6 | uint8_t KEY_SLOT = (uint8_t)9; 7 | 8 | ATCAIfaceCfg cfg; 9 | ATCA_STATUS status; 10 | 11 | void setup() 12 | { 13 | Serial.begin(9600); 14 | 15 | // Init the constuctor for the library 16 | cfg.iface_type = ATCA_I2C_IFACE; // Type of communication -> I2C mode 17 | cfg.devtype = ATECC608A; // Type of chip 18 | cfg.atcai2c.slave_address = 0XC0; // I2C addresse (default value) 19 | cfg.atcai2c.bus = 1; 20 | cfg.atcai2c.baud = 100000; 21 | cfg.wake_delay = 1500; // Delay of wake up (1500 ms) 22 | cfg.rx_retries = 20; 23 | } 24 | 25 | void loop() 26 | { 27 | ATCA_STATUS status = atcab_init(&cfg); 28 | if (status != ATCA_SUCCESS) 29 | { 30 | Serial.println(F("atcab_init() failed : Code -> 0x")); 31 | Serial.println(status, HEX); 32 | } 33 | 34 | uint8_t plaintext[16] = "AAAAAAAAAAAAAAA"; 35 | 36 | uint8_t iv[IV_LENGTH_CBC]; 37 | uint8_t cypherdata[sizeof(plaintext)]; 38 | uint8_t decryptdata[sizeof(plaintext)]; 39 | 40 | Serial.println("Beginning of the encryption !"); 41 | status = aes_cbc_encrypt(&cfg, plaintext, sizeof(plaintext), iv, cypherdata, KEY_SLOT); 42 | if (status == ATCA_SUCCESS) 43 | { 44 | status = aes_cbc_decrypt(&cfg, cypherdata, sizeof(cypherdata), iv, decryptdata, KEY_SLOT); 45 | if (status == ATCA_SUCCESS) 46 | { 47 | Serial.print("Decrypted text is : "); 48 | for (size_t i = 0; i < sizeof(decryptdata); i++) 49 | { 50 | Serial.print((char)decryptdata[i]); 51 | } 52 | Serial.println(""); 53 | } 54 | else 55 | { 56 | // See file atca_status.h for the code Error 57 | Serial.print(F("Impossible do the decryption | Code Error 0x")); 58 | Serial.println(status, HEX); 59 | return; 60 | } 61 | } 62 | else 63 | { 64 | // See file atca_status.h for the code Error 65 | Serial.print(F("Impossible do the encryption | Code Error 0x")); 66 | Serial.println(status, HEX); 67 | return; 68 | } 69 | } -------------------------------------------------------------------------------- /src/Examples/Configuration/Configuration.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | #include "ATECCX08A_Arduino/cryptoauthlib.h" 3 | #include "Configuration.h" 4 | 5 | /** \brief Lock the CONFIG_ZONE or the DATA_ZONE 6 | * \param[in] cfg Logical interface configuration. Some predefined 7 | * configurations can be found in atca_cfgs.h 8 | * \param[in] zone LOCK_ZONE_DATA or LOCK_ZONE_CONFIG 9 | * \return ATCA_SUCCESS on success, otherwise an error code. 10 | */ 11 | ATCA_STATUS lock_zone(ATCAIfaceCfg *cfg, uint8_t zone) 12 | { 13 | if (zone != (uint8_t)LOCK_ZONE_CONFIG && zone != (uint8_t)LOCK_ZONE_DATA) 14 | return ATCA_BAD_PARAM; 15 | 16 | ATCA_STATUS status = atcab_init(cfg); 17 | 18 | if (status == ATCA_SUCCESS) 19 | { 20 | if (zone == (uint8_t)LOCK_ZONE_DATA) 21 | { 22 | return atcab_lock_data_zone(); 23 | } 24 | else if (zone == (uint8_t)LOCK_ZONE_CONFIG) 25 | { 26 | return atcab_lock_config_zone(); 27 | } 28 | else 29 | { 30 | return ATCA_BAD_PARAM; 31 | } 32 | } 33 | return ATCA_BAD_PARAM; 34 | } 35 | 36 | /** \brief Check if a the DATA_ZONE or CONFIG_ZONE is locked 37 | * \param[in] cfg Logical interface configuration. Some predefined 38 | * configurations can be found in atca_cfgs.h 39 | * \param[in] zone LOCK_ZONE_DATA or LOCK_ZONE_CONFIG 40 | * \return ATCA_SUCCESS on success, otherwise an error code. 41 | */ 42 | ATCA_STATUS check_lock_zone(ATCAIfaceCfg *cfg, uint8_t zone) 43 | { 44 | ATCA_STATUS status; 45 | bool lock = false; 46 | 47 | if (zone != (uint8_t)LOCK_ZONE_CONFIG && zone != (uint8_t)LOCK_ZONE_DATA) 48 | return ATCA_BAD_PARAM; 49 | 50 | status = atcab_init(cfg); 51 | if (status == ATCA_SUCCESS) 52 | { 53 | if (ATCA_SUCCESS != (status = atcab_is_locked(zone, &lock))) 54 | { 55 | return ATCA_FUNC_FAIL; 56 | } 57 | if (!lock) 58 | { 59 | return ATCA_NOT_LOCKED; 60 | } 61 | return ATCA_SUCCESS; 62 | } 63 | return ATCA_BAD_PARAM; 64 | } 65 | 66 | /** \brief Write a new configuration to the chip. 67 | * \param[in] cfg Logical interface configuration. Some predefined 68 | * configurations can be found in atca_cfgs.h 69 | * \param[in] config Array uint8_t of configuration (length 112) 70 | * \param[in] len Size of the configuration array 71 | * \return ATCA_SUCCESS on success, otherwise an error code. 72 | */ 73 | ATCA_STATUS write_configuration(ATCAIfaceCfg *cfg, uint8_t *config, size_t len) 74 | { 75 | if (len != 112) 76 | return ATCA_BAD_PARAM; 77 | 78 | ATCA_STATUS status; 79 | 80 | status = atcab_init(cfg); 81 | if (status == ATCA_SUCCESS) 82 | { 83 | 84 | // Write the configuration Array to the chip 85 | // Padding of 16 byte (16 first bytes cannot be writed) 86 | status = atcab_write_bytes_zone(ATCA_ZONE_CONFIG, 0, 16, (uint8_t *)config, len); 87 | return status; 88 | } 89 | return status; 90 | } 91 | 92 | /** \brief Write AES key in a given slot. 93 | * \param[in] cfg Logical interface configuration. Some predefined 94 | * configurations can be found in atca_cfgs.h 95 | * \param[in] key key slot number 96 | * \param[in] datakey key array uint8_t 97 | * \param[in] len Size of the key array 98 | * \return ATCA_SUCCESS on success, otherwise an error code. 99 | */ 100 | ATCA_STATUS write_key_slot(ATCAIfaceCfg *cfg, uint8_t key, uint8_t *datakey, size_t len) 101 | { 102 | if (key < 1 && key > 16) 103 | return ATCA_BAD_PARAM; 104 | 105 | if (len != 32) 106 | return ATCA_BAD_PARAM; 107 | 108 | ATCA_STATUS status = atcab_init(cfg); 109 | 110 | if (status == ATCA_SUCCESS) 111 | { 112 | status = atcab_write_zone(ATCA_ZONE_DATA, (uint16_t)key, 0, 0, datakey, 32); 113 | if (status != ATCA_SUCCESS) 114 | return status; 115 | } 116 | return status; 117 | } -------------------------------------------------------------------------------- /src/Examples/Configuration/Configuration.h: -------------------------------------------------------------------------------- 1 | #ifndef CONFIGURATION_H 2 | #define CONFIGURATION_H 3 | 4 | 5 | ATCA_STATUS lock_zone(ATCAIfaceCfg *cfg, uint8_t zone); 6 | ATCA_STATUS check_lock_zone(ATCAIfaceCfg *cfg, uint8_t zone); 7 | ATCA_STATUS write_configuration(ATCAIfaceCfg *cfg, uint8_t *config, size_t len); 8 | ATCA_STATUS write_key_slot(ATCAIfaceCfg *cfg, uint8_t key, uint8_t *datakey, size_t len); 9 | 10 | #endif /* Configuration.h */ -------------------------------------------------------------------------------- /src/main.cpp: -------------------------------------------------------------------------------- 1 | #include 2 | 3 | void setup() { 4 | // put your setup code here, to run once: 5 | } 6 | 7 | void loop() { 8 | // put your main code here, to run repeatedly: 9 | } -------------------------------------------------------------------------------- /test/README: -------------------------------------------------------------------------------- 1 | 2 | This directory is intended for PIO Unit Testing and project tests. 3 | 4 | Unit Testing is a software testing method by which individual units of 5 | source code, sets of one or more MCU program modules together with associated 6 | control data, usage procedures, and operating procedures, are tested to 7 | determine whether they are fit for use. Unit testing finds problems early 8 | in the development cycle. 9 | 10 | More information about PIO Unit Testing: 11 | - https://docs.platformio.org/page/plus/unit-testing.html 12 | --------------------------------------------------------------------------------