mbedtls学习--平台安全架构库函数接口

本文详细介绍了ARM的Platform Secure Architecture(PSA)及其在Mbedtls中的实现,涵盖密钥管理、消息摘要、MAC、非对称加密、对称加密和AEAD等安全功能。通过示例代码展示了如何使用这些功能进行签名认证、加密解密操作,为物联网设备的安全编程提供了指导。

平台安全架构

PSA,全称Platform Secure Architecture,平台安全架构,是ARM公司在物联网安全领域联合众多芯片厂商推出的一个重要的软件实现架构,主要内容包括:

  1. 威胁模型
  2. 安全分析
  3. 硬件和固件架构规范
  4. 开源固件参考实现
  5. 独立评估和认证计划–PSA Certified

TF-M是PSA的一种实现方式,主要基于ARM Cortex-M系列的芯片,除此之外还有TF-A等。Mbedtls也是ARM维护的基于ARM平台的开源嵌入式加密库,现已是trust-frimware的一部分,除了基本的各类加解密算法及安全工具箱的实现,为方便嵌入式开发人员快速编写符合PSA规范的代码,mbedtls提供了各类安全工具的调用接口。源代码见mbdetls/library/psa_crypto.c文件。

Mbedtls PSA库

Key management

PSA支持对称和非对称密钥,实现基础是Mbedtls提供的各类密码箱工具。在导入key的时候需要注意key的type(类型)、usage(用途)和alg(算法)等属性,决定了密文或者签名的长度和解密的数据是否正确。Key在使用时,程序会检查key的policy的合法性,因此key的管理非常重要。主要的key管理函数如下:

  • 设置和检查key的用途
static void psa_set_key_usage_flags(psa_key_attributes_t *attributes,
                                    psa_key_usage_t usage_flags);
static psa_key_usage_t psa_get_key_usage_flags(
    const psa_key_attributes_t *attributes);
  • 设置和检查key的算法
static void psa_set_key_algorithm(psa_key_attributes_t *attributes,
                                  psa_algorithm_t alg);
static psa_algorithm_t psa_get_key_algorithm(
    const psa_key_attributes_t *attributes);
  • 设置和检查key的类型
static void psa_set_key_type(psa_key_attributes_t *attributes,
                             psa_key_type_t type);
static psa_key_type_t psa_get_key_type(const psa_key_attributes_t *attributes);
  • 获取key的属性
psa_status_t psa_get_key_attributes(mbedtls_svc_key_id_t key,
                                    psa_key_attributes_t *attributes);
  • 删除key
psa_status_t psa_destroy_key(mbedtls_svc_key_id_t key);
  • 导入导出key,注意psa_export_public_key函数只能是非对称密钥类型。
psa_status_t psa_import_key(const psa_key_attributes_t *attributes,
                            const uint8_t *data,
                            size_t data_length,
                            mbedtls_svc_key_id_t *key);
psa_status_t psa_export_key(mbedtls_svc_key_id_t key,
                            uint8_t *data,
                            size_t data_size,
                            size_t *data_length);
psa_status_t psa_export_public_key(mbedtls_svc_key_id_t key,
                                   uint8_t *data,
                                   size_t data_size,
                                   size_t *data_length);

导入key后,常用来使用的参数是mbedtls_svc_key_id_t *key,即psa_key_id_t *key,在TF-M中又被命名为psa_key_handle_t *key,这是一个重要的参数,而psa_key_attributes_t *attributes则是指key的属性,内容如下:

typedef struct psa_client_key_attributes_s psa_key_attributes_t;

/* This is the client view of the `key_attributes` structure. Only
 * fields which need to be set by the PSA crypto client are present.
 * The PSA crypto service will maintain a different version of the
 * data structure internally. */
struct psa_client_key_attributes_s
{
   
   
    uint16_t type;
    uint16_t bits;
    uint32_t lifetime;
    psa_key_id_t id;
    uint32_t usage;
    uint32_t alg;
};

Message digests

消息摘要,主要计算消息的单向散列函数值,计算hash的函数和Mbedtls类似,psa_hash_operation_t *operation需要先设置使用的算法,算法必须保证PSA_ALG_IS_HASH(alg)为真。

psa_status_t psa_hash_setup(psa_hash_operation_t *operation,
                            psa_algorithm_t alg);
psa_status_t psa_hash_update(psa_hash_operation_t *operation,
                             const uint8_t *input,
                             size_t input_length);
psa_status_t psa_hash_finish(psa_hash_operation_t *operation,
                             uint8_t *hash,
                             size_t hash_size,
                             size_t *hash_length);

PSA提供了一个更简单的Hash计算接口,适合轻量化的计算,使用时需要注意确保hash的长度hash_size应当大于算法输出的长度(根据PSA_HASH_LENGTH(alg)计算)。

psa_status_t psa_hash_compute(psa_algorithm_t alg,
                              const uint8_t *input,
                              size_t input_length,
                              uint8_t *hash,
                              size_t hash_size,
                              size_t *hash_length);

MAC

消息认证码(Message authentication codes),用于认证消息,计算的输入为消息和消息发送者持有的密钥(对称密钥),计算和验证消息认证码,计算MAC的函数与Mbedtls类似,可以多次调用update函数计算长消息的MAC,psa_mac_operation_t *operation包含计算消息验证码用的算法和key的信息。
此处key的usage属性必须为PSA_KEY_USAGE_SIGN_MESSAGE | PSA_KEY_USAGE_VERIFY_MESSAGE,算法必须保证PSA_ALG_IS_MAC(alg)为真。

psa_status_t psa_mac_sign_setup(psa_mac_operation_t *operation,
                                mbedtls_svc_key_id_t key,
                                psa_algorithm_t alg);
psa_status_t psa_mac_verify_setup(psa_mac_operation_t *operation,
                                  mbedtls_svc_key_id_t key,
                                  psa_algorithm_t alg);
psa_status_t psa_mac_update(psa_mac_operation_t *operation,
/******************************************************************************** * @file sp_weak.c * @author * @brief Provide weak interface for modules * @since * @attention * @warning * @todo ****************************************************************************** * Copyright (c) 2024 SP Limited. All rights reserved. ******************************************************************************/ /*=========================================================================== * include files ===========================================================================*/ #include <stdio.h> #include "string.h" #include <stdarg.h> #include <stdlib.h> #include <stdbool.h> #include "app_log.h" #include "sp_fs_api.h" #include "drv_spi.h" #include "drv_lcd.h" #include "mbedtls/error.h" #include "mbedtls/ssl.h" #include "mbedtls/net.h" #include "mbedtls/x509_crt.h" #include "mbedtls/pk.h" #include "mbedtls/debug.h" #include "mbedtls/base64.h" #include "mbedtls/platform.h" #include "sp_audio.h" #include "sp_sdk_api.h" #include "sp_osi_api.h" #include "sp_osi_api.h" #include "drv_iomux.h" #include "drv_def.h" #include "drv_spi.h" #include "gpio_api.h" #include "drv_pwm.h" #include "drv_seglcd.h" #include "app_log.h" #include "drv_lcd.h" /*=========================================================================== * Macro Definition ===========================================================================*/ /*=========================================================================== * Private Variate ===========================================================================*/ static int sp_func_empty(void) { void *lr0 = __builtin_return_address(0); app_log("\r\n sp_func_empty lr0 0x%x \r\n", (uint32_t)lr0); return 0; } /* sflash */ bool fs_mountExt(SP_ExtFsConfig_t *cfg) __attribute__ ((weak, alias("sp_func_empty"))); uint32_t fs_extFreeSize(const char *path) __attribute__ ((weak, alias("sp_func_empty"))); int extflash_spi_init(uint8_t sspport, uint8_t spi_clock) __attribute__ ((weak, alias("sp_func_empty"))); uint32_t extflash_erase(uint8_t sspport, uint32_t flash_addr, uint32_t size) __attribute__ ((weak, alias("sp_func_empty"))); //void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, // const mbedtls_x509_crt_profile *profile ) __attribute__ ((weak, alias("sp_func_empty"))); //int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, // uint32_t flags ) __attribute__ ((weak, alias("sp_func_empty"))); /******************************************************************************** * @file sp_weak.c * @author * @brief Provide weak interface for modules * @since * @attention * @warning * @todo ****************************************************************************** * Copyright (c) 2024 SP Limited. All rights reserved. ******************************************************************************/ /*=========================================================================== * include files ===========================================================================*/ #include <stdio.h> #include "string.h" #include <stdarg.h> #include <stdlib.h> #include <stdbool.h> #include "app_log.h" #include "sp_fs_api.h" #include "drv_spi.h" #include "drv_lcd.h" #include "mbedtls/error.h" #include "mbedtls/ssl.h" #include "mbedtls/net.h" #include "mbedtls/x509_crt.h" #include "mbedtls/pk.h" #include "mbedtls/debug.h" #include "mbedtls/base64.h" #include "mbedtls/platform.h" #include "sp_audio.h" #include "sp_sdk_api.h" #include "sp_osi_api.h" #include "sp_osi_api.h" #include "drv_iomux.h" #include "drv_def.h" #include "drv_spi.h" #include "gpio_api.h" #include "drv_pwm.h" #include "drv_seglcd.h" #include "app_log.h" #include "drv_lcd.h" /*=========================================================================== * Macro Definition ===========================================================================*/ /*=========================================================================== * Private Variate ===========================================================================*/ static int sp_func_empty(void) { void *lr0 = __builtin_return_address(0); app_log("\r\n sp_func_empty lr0 0x%x \r\n", (uint32_t)lr0); return 0; } /* sflash */ bool fs_mountExt(SP_ExtFsConfig_t *cfg) __attribute__ ((weak, alias("sp_func_empty"))); uint32_t fs_extFreeSize(const char *path) __attribute__ ((weak, alias("sp_func_empty"))); int extflash_spi_init(uint8_t sspport, uint8_t spi_clock) __attribute__ ((weak, alias("sp_func_empty"))); uint32_t extflash_erase(uint8_t sspport, uint32_t flash_addr, uint32_t size) __attribute__ ((weak, alias("sp_func_empty"))); //void mbedtls_ssl_conf_cert_profile( mbedtls_ssl_config *conf, // const mbedtls_x509_crt_profile *profile ) __attribute__ ((weak, alias("sp_func_empty"))); //int mbedtls_x509_crt_verify_info( char *buf, size_t size, const char *prefix, // uint32_t flags ) __attribute__ ((weak, alias("sp_func_empty"))); 作用
07-12
评论 2
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Yanjing-233

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值