# **Secure Boot for ATECC608A**
## ATECC608A Data Sheet
* The ATECC608A includes an EEPROM array which can be used for storage of up to 16 keys, certificates,miscellaneous read/write, read-only or secret data, consumption logging, and security configurations.
### New features of 608A:
1. Secure boot function, with IO encryption and authentication
2. AES command, including encrypt/decrypt
3. Updated NIST SP800-90 A/B/C Random Number Generator
4. Flexible SHA/HMAC command with context save/restore
5. SHA command execution time significantly reduced
6. Verify command output can be validated with a MAC
7. Added self test command, optional automatic power-on self test
8. Programmable I2C address after data (secret) zone lock
### 從508刪除的功能:
1. HMAC command removed, replaced via new more powerful SHA command
2. OTP consumption mode eliminated, now read only(After the device configuration zone islocked, no change to the OTP zone is allowed. Only reading of the OTP zone data is allowed.)
## **AN8984-CryptoAuthLib**
### **CryptoAuthLib Overview**
* Atmel *CryptoAuthLib*:
[Tab]A software library incorporated in firmware and drivers designed to work with Atmel CryptoAuthentication™ devices such as the Atmel ++ATECC508A++ and ATSHA204A CryptoAuth devices.
* *CryptoAuthLib* 主要有提供下列API:
-- Basic API – Best for ease of use.
-- Core API – Best for use of any device feature, power developer.
-- PKI X.509 Certificates – PKI applications which store and retrieve X.509 certificates.
->主要是讓ATECC CryptoAuthentication 裝置可以接收並儲存 X509 certificate
-- PKI TLS – Secure communication key agreement protocols.
-- **Crypto Utilities** – General software **hash implementations**.
->Hash/SHA之類的主要在這裏面
-- HAL – Hardware abstraction layer integrates with physical interfaces.
*各API細節可參考 Section3.1: *CrtyptoAuthLib* API Levels
**General Architecture**

*↑General Archetecture的部分Microchip只有放這張圖,document裡面沒有針對這張圖詳細解釋,如果不希望被老師問太細的話,建議不要放這張圖
### **CH3 CryptoAuthLib "Hello World"**
```C
#include <asf.h>
#include "cryptoauthlib.h"
int main(void)
{
uint8_t random_number[32];
system_init();
/*Configure system tick to generate periodic interrupts */
SysTick_Config(system_gclk_gen_get_hz(GCLK_GENERATOR_0));
config_led();
// initialize CryptoAuthLib for an ECC default I2C interface
atcab_init(&cfg_ateccx08a_i2c_default);
atcab_random(&random_number); // get a random number from the chip
// use random for challenge and authentication…
while (true) {
// your application code
}
}
```
上面程式碼簡易地示範了2個function call
步驟如下:
>Step 1. To start, include cryptoauthlib.h header file
Step 2. Call actab_init() with a pointer for a standard default configuraion for an 508A I2C interface
Step 3. Call actab_random() passing a pointer to some place to receive the 32 byte number
### **CH3.5 Placing CryptoAuthLib In Your Project**
* 只要compiler知道如何include它所需要的file,則CryptoAuthLib source tree 可以被放在project source tree 中的任何地方(詳細可參考Section 3.6 / 3.6.1)
* 但是在Atmel Studio中,較常見的做法是放在 ./src 資料夾底下(如下圖),而下一小節也是用這個路徑做示範

### **CH3.6 Compiling CryptoAuth Hello World with Atmel Studio**
#### **CH3.6.1 設定include CryptoAuthLib 的路徑**
目標:CryptoAuthLab資料夾的路徑(src/cryptoauthlib/lib)給include進來
步驟: Project Properties > Toolchain > 右方的list裡選 src/cryproauthlib/lib (如下圖)

#### **CH3.6.2 選擇合適的CryptoAuthLib HAL**
目標:讓IDE(以Atmel Studio為例)知道該include那些特定的HAL files(並排除其他HAL files)
步驟:(以I2C在SAM D21上使用ASF application為例)

因此,要包含的檔案有:
  *atca_hal.c*
  *atch_hal.h*
*這兩個檔案包含了所有類型的HAL implementation的定義,因此不論選哪種HAL,都需要這兩個檔案
另外這3個檔案也要選:
  *hal_samd21_i2c_asf.c*
  *hal_samd21_i2c_asf.h*
  *hal_samd21_timer_asf.c*
-> 這些檔案利用SAM D21 的 ASF I2C API來implement HAL API
#### **CH3.6.3 Defining Compiler Symbols**
* CryptoAuthLib support multiple types of hardware interfaces (such as I2C, SWI, UART etc.)
* In order for the CryptoAuthLib to know which interface type to build, one or more interface symbol must be defined.
步驟:如何將2個compiler symbol加入到 Atmel Studio project裡Project Proprties > Toolchain > 從Defined Symbols 清單選擇(-D)
  ATCA_HAL_I2C
  ATCA_PRINTF
*1 *ATCA_HAL_I2C allows the compiler to pull in a HAL implementation for I2C. Other choices include:
  -- *ATCA_HAL_SWI* Single-Wire Interface; typically bit-banged.
  -- *ATCA_HAL_UART* Used with either Kit Protocol or using the UART to encode/decode Atmel Single-Wire Interfaces.
*2 *ATCAPRINTF*: allows the compiler to build the CryptoAuthLib functions which use the sprintf family of C library functions. For space optimization reasons, there may be an application for which is not wanted to be pulled into the sprintf family. In that case, do not define ATCAPRINTF in the project; however, certain helper functions that use printf will not be usable.

## **reference**
**1. Getting Started with ATECC608A SecureBoot Use Case Example
2. AN8984-CryptoAuthLib
3. Differences Between the ATECC608A and ATECC508A CryptoAuthentication™ Devices
4. CryptoAuthentication™ Device Summary Datasheet**
# **Problems for 508A**
* 蒐集燒508時遇到的問題,可以一次丟上論壇發問
1.

這邊的Comsumption mode描述究竟是什麼意思?
(在ConfigZone Lock之前,OTP Zone是不能進行讀寫操作。)
2.

講義中提供的表格 9-14 格是存 Public Key類別,但講義示範是存data 類別(EEPROM status)
所以9-14格正常是只能照表格存public key還寧作為存data的slot?(for 508A)
3.
I2C連接將 EXT1 改成 EXT2, 應與 /lib/host/atca_cfgs.c 有關 (已解決)
4. [20200326]
讀取SD的buffer是否能採用動態配置記憶體(目前是採用靜態配置)?
# Reference
1. ATECC508A芯片开发笔记(一):初识加密芯片
https://blog.csdn.net/HowieXue/article/details/75193922?depth_1-utm_source=distribute.pc_relevant_right.none-task&utm_source=distribute.pc_relevant_right.none-task
2. ★ ATECC508A Device Organizaion
https://www.verical.com/datasheet/microchip-technology-authenticators-atecc508a-sshaw-t-3291718.pdf →連結損壞,無法提供datasheet
*替代連結:https://content.arduino.cc/assets/mkr-microchip_atecc508a_cryptoauthentication_device_summary_datasheet-20005927a.pdf
3. Microchip Github // atcab_read_enc() function詳細?
https://github.com/MicrochipTech/cryptoauth-openssl-engine/blob/master/cryptoauthlib/lib/basic/atca_basic.c
4. ATECC508A 加密晶片使用
https://www.twblogs.net/a/5b8e1dd12b7177188342f9a5
5. FatFS f_open()函數詳解
https://blog.csdn.net/tiankongtiankong01/article/details/51200977
https://blog.csdn.net/fanwenjieok/article/details/37693625