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

基于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);
}

http://www.xdnf.cn/news/2844.html

相关文章:

  • 深度学习大模型: AI 阅卷替代人工阅卷
  • Field访问对象int字段,对象访问int字段,通过openjdk17 C++源码看对象字段访问原理
  • J-Link RTT打印输出调试信息
  • 深入蜂窝物联网:第二章 深度解读 NB-IoT:协议栈、部署与典型应用
  • 两地三中心
  • MySQL数据库(14)—— 使用C操作MySQL
  • 【ACL系列论文写作指北03-相关工作怎么写】-展示视野与定位创新
  • leetcode283-移动零
  • 第二章 信息技术发展(2.2 新一代信息技术及应用)
  • Linux428 chmod 0xxx 1xxx 2xxx 4xxx;umask;chown 属主属组 软件包rpm
  • ECharts散点图-散点图20,附视频讲解与代码下载
  • php数据库连接
  • Docker安装的mysql限制ip访问
  • [三分钟]web自动化测试(三):selenium自动化测试常用函数(下)
  • 基于蓝牙Beacon人员导航方案
  • 【Linux】第十二章 安装和更新软件包
  • 第七章:Server/Client Communication
  • 增量抽取的场景下,周期快照表最新分区的数据是如何生成?
  • 安卓开发学习随记
  • OpenCV 图形API(69)图像与通道拼接函数------将一个 GMat 类型的对象转换为另一个具有不同深度GMat对象函数convertTo()
  • vue3使其另一台服务器上的x.html,实现x.html调用中的函数,并向其传递数据。
  • kylin v10 + argo + ascend 310p多机多卡 pytorch distributed 训练
  • JavaWeb学习打卡-Day4-会话技术、JWT、Filter、Interceptor
  • WPF之Label控件详解
  • GoLand包的爆红问题解决
  • Coupang火箭计划深度攻略:eBay卖家突破韩国市场的三维数据作战模型
  • 面试算法高频08-动态规划-03
  • InitializingBean接口和@PostConstruct-笔记
  • Spring系列四:AOP切面编程 第二部分
  • EasyGBS国标GB28181设备管理软件打造园区安防高效解决方案