当前位置: 首页 > news >正文

基于STM32、HAL库的ATSHA204A安全验证及加密芯片驱动程序设计

一、简介:

ATSHA204A是Microchip公司生产的一款高性能加密认证芯片,主要特性包括:

  • 基于SHA-256哈希算法的安全认证

  • 4.5KB EEPROM存储空间(可配置为密钥存储、OTP区域等)

  • 唯一的72位序列号

  • 支持I2C和单线接口

  • 工作电压:2.0V至5.5V

  • 低功耗设计

典型应用场景:

  • 设备认证

  • 安全启动验证

  • 固件保护

  • 防克隆保护

二、硬件接口:

TSHA204A          STM32L4XX
----------------------------
VCC        →       3.3V
GND        →       GND
SDA        →       PB7 (I2C1_SDA)
SCL        →       PB6 (I2C1_SCL)

注意:ATSHA204A的地址引脚决定了I2C地址,通常为0xC0(如果地址引脚全部接地)。

三、头文件:

#ifndef ATSHA204A_H
#define ATSHA204A_H

#include "stm32l4xx_hal.h"

#define ATSHA204A_I2C_ADDR         0xC0
#define ATSHA204A_CMD_WAKE         0x00
#define ATSHA204A_CMD_SLEEP        0x01
#define ATSHA204A_CMD_READ         0x02
#define ATSHA204A_CMD_WRITE        0x12
#define ATSHA204A_CMD_MAC          0x08
#define ATSHA204A_CMD_NONCE        0x16
#define ATSHA204A_CMD_RANDOM       0x1B

#define ATSHA204A_ZONE_CONFIG      0x00
#define ATSHA204A_ZONE_OTP         0x01
#define ATSHA204A_ZONE_DATA        0x02

#define ATSHA204A_STATUS_SUCCESS   0x00
#define ATSHA204A_STATUS_AFTER_WAKE 0x11

#define ATSHA204A_PACKET_SIZE      32
#define ATSHA204A_RESPONSE_TIMEOUT 1000

typedef struct {
    I2C_HandleTypeDef *hi2c;
    uint8_t i2c_addr;
} ATSHA204A_HandleTypeDef;

HAL_StatusTypeDef ATSHA204A_Init(ATSHA204A_HandleTypeDef *hatsha, I2C_HandleTypeDef *hi2c, uint8_t i2c_addr);
HAL_StatusTypeDef ATSHA204A_Wakeup(ATSHA204A_HandleTypeDef *hatsha);
HAL_StatusTypeDef ATSHA204A_Sleep(ATSHA204A_HandleTypeDef *hatsha);
HAL_StatusTypeDef ATSHA204A_Read(ATSHA204A_HandleTypeDef *hatsha, uint8_t zone, uint8_t block, uint8_t offset, uint8_t *data, uint8_t len);
HAL_StatusTypeDef ATSHA204A_Write(ATSHA204A_HandleTypeDef *hatsha, uint8_t zone, uint8_t block, uint8_t offset, const uint8_t *data, uint8_t len);
HAL_StatusTypeDef ATSHA204A_GetSerialNumber(ATSHA204A_HandleTypeDef *hatsha, uint8_t *serial);
HAL_StatusTypeDef ATSHA204A_GenerateRandom(ATSHA204A_HandleTypeDef *hatsha, uint8_t *random);
HAL_StatusTypeDef ATSHA204A_CalculateMAC(ATSHA204A_HandleTypeDef *hatsha, uint8_t *challenge, uint8_t *mac);

#endif /* ATSHA204A_H */

四、源文件:

#include "atsha204a.h"
#include <string.h>

static HAL_StatusTypeDef ATSHA204A_SendCommand(ATSHA204A_HandleTypeDef *hatsha, uint8_t *command, uint8_t cmd_len);
static HAL_StatusTypeDef ATSHA204A_ReceiveResponse(ATSHA204A_HandleTypeDef *hatsha, uint8_t *response, uint8_t resp_len);

HAL_StatusTypeDef ATSHA204A_Init(ATSHA204A_HandleTypeDef *hatsha, I2C_HandleTypeDef *hi2c, uint8_t i2c_addr) {
    hatsha->hi2c = hi2c;
    hatsha->i2c_addr = i2c_addr;
    return ATSHA204A_Wakeup(hatsha);
}

HAL_StatusTypeDef ATSHA204A_Wakeup(ATSHA204A_HandleTypeDef *hatsha) {
    // Wakeup is a special case - requires a low pulse on SDA
    // For I2C, we send a start condition with address 0x00
    uint8_t dummy = 0;
    HAL_StatusTypeDef status = HAL_I2C_Master_Transmit(hatsha->hi2c, 0x00, &dummy, 1, ATSHA204A_RESPONSE_TIMEOUT);
    
    // Wait for device to wake up
    HAL_Delay(2);
    
    return status;
}

HAL_StatusTypeDef ATSHA204A_Sleep(ATSHA204A_HandleTypeDef *hatsha) {
    uint8_t command[4] = {0};
    command[0] = 1; // Opcode count
    command[1] = ATSHA204A_CMD_SLEEP;
    
    return ATSHA204A_SendCommand(hatsha, command, 4);
}

HAL_StatusTypeDef ATSHA204A_Read(ATSHA204A_HandleTypeDef *hatsha, uint8_t zone, uint8_t block, uint8_t offset, uint8_t *data, uint8_t len) {
    uint8_t command[8] = {0};
    uint8_t response[ATSHA204A_PACKET_SIZE] = {0};
    
    command[0] = 7; // Opcode count
    command[1] = ATSHA204A_CMD_READ;
    command[2] = zone;
    command[3] = block;
    command[4] = offset;
    
    HAL_StatusTypeDef status = ATSHA204A_SendCommand(hatsha, command, 8);
    if (status != HAL_OK) return status;
    
    HAL_Delay(5); // Wait for command to complete
    
    status = ATSHA204A_ReceiveResponse(hatsha, response, ATSHA204A_PACKET_SIZE);
    if (status != HAL_OK) return status;
    
    if (response[0] != 0) return HAL_ERROR; // Check status byte
    
    memcpy(data, &response[1], len);
    return HAL_OK;
}

HAL_StatusTypeDef ATSHA204A_Write(ATSHA204A_HandleTypeDef *hatsha, uint8_t zone, uint8_t block, uint8_t offset, const uint8_t *data, uint8_t len) {
    uint8_t command[8 + 32] = {0}; // Max write size
    
    command[0] = 7 + len; // Opcode count
    command[1] = ATSHA204A_CMD_WRITE;
    command[2] = zone;
    command[3] = block;
    command[4] = offset;
    
    memcpy(&command[5], data, len);
    
    HAL_StatusTypeDef status = ATSHA204A_SendCommand(hatsha, command, 8 + len);
    if (status != HAL_OK) return status;
    
    HAL_Delay(20); // Write operations take longer
    
    return HAL_OK;
}

HAL_StatusTypeDef ATSHA204A_GetSerialNumber(ATSHA204A_HandleTypeDef *hatsha, uint8_t *serial) {
    return ATSHA204A_Read(hatsha, ATSHA204A_ZONE_CONFIG, 0, 0, serial, 9); // First 9 bytes of config zone contain serial
}

HAL_StatusTypeDef ATSHA204A_GenerateRandom(ATSHA204A_HandleTypeDef *hatsha, uint8_t *random) {
    uint8_t command[4] = {0};
    uint8_t response[35] = {0};
    
    command[0] = 1; // Opcode count
    command[1] = ATSHA204A_CMD_RANDOM;
    command[2] = 0x00; // Mode
    
    HAL_StatusTypeDef status = ATSHA204A_SendCommand(hatsha, command, 4);
    if (status != HAL_OK) return status;
    
    HAL_Delay(20); // Random number generation takes time
    
    status = ATSHA204A_ReceiveResponse(hatsha, response, 35);
    if (status != HAL_OK) return status;
    
    if (response[0] != 0) return HAL_ERROR; // Check status byte
    
    memcpy(random, &response[1], 32); // Copy 32-byte random number
    return HAL_OK;
}

HAL_StatusTypeDef ATSHA204A_CalculateMAC(ATSHA204A_HandleTypeDef *hatsha, uint8_t *challenge, uint8_t *mac) {
    uint8_t command[88] = {0}; // Max command size
    uint8_t response[35] = {0};
    
    command[0] = 83; // Opcode count
    command[1] = ATSHA204A_CMD_MAC;
    command[2] = 0x01; // Mode
    command[3] = 0x00; // Slot
    
    memcpy(&command[4], challenge, 32); // Copy challenge
    
    HAL_StatusTypeDef status = ATSHA204A_SendCommand(hatsha, command, 88);
    if (status != HAL_OK) return status;
    
    HAL_Delay(50); // MAC calculation takes time
    
    status = ATSHA204A_ReceiveResponse(hatsha, response, 35);
    if (status != HAL_OK) return status;
    
    if (response[0] != 0) return HAL_ERROR; // Check status byte
    
    memcpy(mac, &response[1], 32); // Copy 32-byte MAC
    return HAL_OK;
}

static HAL_StatusTypeDef ATSHA204A_SendCommand(ATSHA204A_HandleTypeDef *hatsha, uint8_t *command, uint8_t cmd_len) {
    // Calculate CRC
    // Note: Actual CRC implementation needed here
    command[cmd_len-2] = 0; // CRC placeholder
    command[cmd_len-1] = 0; // CRC placeholder
    
    return HAL_I2C_Master_Transmit(hatsha->hi2c, hatsha->i2c_addr, command, cmd_len, ATSHA204A_RESPONSE_TIMEOUT);
}

static HAL_StatusTypeDef ATSHA204A_ReceiveResponse(ATSHA204A_HandleTypeDef *hatsha, uint8_t *response, uint8_t resp_len) {
    return HAL_I2C_Master_Receive(hatsha->hi2c, hatsha->i2c_addr, response, resp_len, ATSHA204A_RESPONSE_TIMEOUT);
}

五、应用:

#include "atsha204a.h"
#include "stdio.h"

ATSHA204A_HandleTypeDef hatsha;

void ATSHA204A_Example(void) {
    uint8_t serial[9] = {0};
    uint8_t random[32] = {0};
    uint8_t mac[32] = {0};
    uint8_t challenge[32] = {0};
    
    // Initialize with I2C1 and default address
    if (ATSHA204A_Init(&hatsha, &hi2c1, ATSHA204A_I2C_ADDR) != HAL_OK) {
        printf("ATSHA204A initialization failed\r\n");
        return;
    }
    
    // Get serial number
    if (ATSHA204A_GetSerialNumber(&hatsha, serial) == HAL_OK) {
        printf("Serial Number: ");
        for (int i = 0; i < 9; i++) {
            printf("%02X", serial[i]);
        }
        printf("\r\n");
    }
    
    // Generate random number
    if (ATSHA204A_GenerateRandom(&hatsha, random) == HAL_OK) {
        printf("Random Number: ");
        for (int i = 0; i < 32; i++) {
            printf("%02X", random[i]);
        }
        printf("\r\n");
    }
    
    // Generate challenge (in real application, this would be from host)
    for (int i = 0; i < 32; i++) {
        challenge[i] = i;
    }
    
    // Calculate MAC
    if (ATSHA204A_CalculateMAC(&hatsha, challenge, mac) == HAL_OK) {
        printf("MAC: ");
        for (int i = 0; i < 32; i++) {
            printf("%02X", mac[i]);
        }
        printf("\r\n");
    }
    
    // Put device to sleep
    ATSHA204A_Sleep(&hatsha);
}

相关文章:

  • RAG工程-基于LangChain 实现 Advanced RAG(预检索优化)
  • 代码随想录第30天:动态规划3
  • vue3 css模拟语音通话不同语音、正在加载等的效果
  • 《Operating System Concepts》阅读笔记:p821-p897
  • C++——入门基础
  • C++学习笔记(四十二)——STL之堆操作算法
  • 嵌入式程序发开思路
  • IntelliJ IDEA 内存优化
  • 数据中心平台建设方案技术方案(Word)
  • 制作一款打飞机游戏28:编辑器完善鲁棒性
  • 回归预测 | Matlab实现DBO-LightGBM蜣螂算法优化轻量级梯度提升机多输入单输出回归预测,作者:机器学习之心
  • 在使用Python的Selenium库打卡网页后,通过CDP命令获取所有cookies(包括Httponly和Secure的cookies)
  • 【大模型ChatGPT+R-Meta】AI赋能R-Meta分析核心技术:从热点挖掘到高级模型、助力高效科研与论文发表“
  • jclasslib 与 BinEd 结合的二进制分析技术指南
  • Vue中自定义指令钩子详解
  • 数据库系统概论|第三章:关系数据库标准语言SQL—课程笔记6
  • git 查看用户信息
  • git pull报错error: cannot lock ref ‘refs/remotes/origin/feature/xxx
  • InferType和_checked_type的区别?
  • Qt/C++面试【速通笔记四】—Qt中的MVC模式
  • 四川省社科联期刊:不建议在读硕士、博士将导师挂名为第一作者
  • “90后”樊鑫履新乌兰察布市察右中旗副旗长人选
  • 第二十届中国电影华表奖揭晓!完整获奖名单来了
  • 十四届全国人大常委会第十五次会议在京举行,审议民营经济促进法草案等
  • 中日友好医院通报“医师肖某被举报”:基本属实,开除党籍并解聘
  • 百岁太极拳大师、陈氏太极拳第十一代嫡宗传人陈全忠逝世