Toggle navigation
首页
问答
文章
积分商城
专家
专区
更多专区...
文档中心
返回主站
搜索
提问
会员
中心
登录
注册
M2354
【NuMaker-M2354试用】M2354测评_CRYPTO
发布于 2021-12-17 20:04:37 浏览:1077
订阅该版
[tocm] 本次的任务是CRYPTO, 就是硬件密码加速器(Cryptographic Accelerator) 。 对应RT-Thread 中的 hwcrypto 硬件加解密设备驱动框架,该框架主要由硬件加解密驱动抽象层以及各种加解密 API 接口两部分构成。对于上层应用,可对接安全套件或直接使用,使用方式十分灵活。对于驱动,需要对接的接口少,功能单一,驱动开发简单快捷。目前没有实现对非对称密码算法的支持。  # 功能模块硬件介绍 M2354支持的算法包括伪随机数生成器(PRNG),AES,SHA/HMAC,RSA, 和ECC 算法。 一般的MCU 是没有这些密码算法模块的,而M2354 是一款主打安全的MCU, 所以这些基础的密码算法的支持是必须要上的。而能提供这些算法的芯片其实是原来的安全芯片厂家的领域,主要市场是金融IC 卡 U盾等相关的。随着物联网发展,和对安全需求的增加,传统MCU 厂家和 原来的安全芯片厂家 开始相互跨界融合,充分发挥各自的优势,来满足物联网上的安全需求。 下面是官网对这款芯片的介绍,增加了很多物理级的安全防护措施,以达到能让微控制器应用系统轻易实现**数据存储安全**、**软件运行安全**与**信息通信安全**。  | **安全特性** | | | **应用领域** | | ------------ | ------------------------------------------------------------ | ----- | ------------------------------------------------------------ | | **•** | Arm® Cortex®-M23 TrustZone® 技术 | **•** | 物联网安全连接装置 | | **•** | 8 个安全区/非安全区内存保护单元 | **•** | 二次开发协作模式 | | **•** | 8 个Security Attribution Units (SAU) | **•** | 指纹卡、指纹锁 | | **•** | Implementation Defined Attribution Unit (IDAU) | **•** | 智慧家电 | | **•** | 3 KB OTP ROM 带额外 1 KB 的 lock bits | **•** | 智慧城市设施 | | **•** | 硬件密码加速器 (含AES, RSA, ECC并具备侧信道攻击与电压与时钟管脚故障攻击 | **•** | 无线传感器节点设备 | | | 防护) | **•** | 自动抄表 | | **•** | CRC 计算单元 | **•** | 可携式无线数据收集器 | | **•** | 最高 6 个破坏侦测引脚 | **•** | 数字货币认证装置 | | **•** | 128 位 Unique ID (UID), 128 位 Unique Customer ID (UCID) | **•** | 具可信应用程序 (Trusted Applications , TAs)之可信执行环境 (Trusted Execution | | **•** | 支援 Arm® Platform Security Architecture (PSA) 与 Trusted Base System Architecture-M (TBSA-M) | | Environment, TEE) | [M2354 系列 - Nuvoton](https://www.nuvoton.com.cn/products/microcontrollers/arm-cortex-m23-mcus/m2354-series/) 后续可以尝试其他的功能,本次主要是算法模块的使用。 新唐M2354 上的这些算法的特性如下: PRNG – Supports 128, 163, 192, 224, 233, 255, 256, 283, 384, 409, 512, 521 and 571 bits random number generation (283~571 bits only generated for Key Store) – Able to take the true random number seed from TRNG AES – Supports FIPS NIST 197 – Supports SP800-38A and addendum – Supports 128, 192, and 256 bits key – Supports both encryption and decryption – Supports ECB, CBC, CFB, OFB, CTR, CBC-CS1, CBC-CS2 and CBC-CS modes – Supports CCM mode, GCM mode and GHASH function – Supports SM4 block cipher algorithm – Supports key expander – Supports one technique to improve side-channel attack protection ability SHA – Supports FIPS NIST 180, 180-2, 180-4 – Supports SHA-160, SHA-224, SHA-256, SHA-384 and SHA-512 – Supports SM3 Cryptographic Hash Algorithm ECC – Supports both prime field GF(p) and binary filed GF(2m) – Supports NIST P-192, P-224, P-256, P-384, and P-521 – Supports NIST B-163, B-233, B-283, B-409, and B-571 – Supports NIST K-163, K-233, K-283, K-409, and K-571 – Supports Curve25519 – Supports Public Key Cryptographic Algorithm SM2 Based on Elliptic Curves – Supports point multiplication, addition and doubling operations in GF(p) and GF(2m) – Supports modulus division, multiplication, addition and subtraction operations in GF(p) – Supports three techniques to improve side-channel attack protection ability RSA – Supports both encryption and decryption with 1024, 2048, 3072 and 4096 bits – Supports CRT decryption with 2048, 3072 and 4096 bits – Supports three techniques to improve side-channel attack protection ability # 功能模块使用说明 下面介绍新唐 M2354 上各个模块的使用情况。 这次只是简单测试一下算法,后续会增加相关的实际应用。 bsp 已经将hwcrypto 驱动添加成功,使用list_device 进行查看,驱动添加成功以后,使用加解密api 就会非常方便。 ``` msh />list_device device type ref count -------- -------------------- ---------- i2c1 I2C Bus 0 hwcryto Miscellaneous Device 0 spi1 SPI Bus 0 spi0 SPI Bus 0 slcd Character Device 1 hidd Character Device 1 usbd USB Slave Device 0 usbh▒ USB Host Bus 0 rtc RTC 0 gpio Miscellaneous Device 0 pm PM Pseudo Device 0 sdh0 Block Device 0 eadc0 Miscellaneous Device 0 wdt Security Device 0 uart4 Character Device 1 uart1 Character Device 0 uart0 Character Device 2 msh /> ``` ## HASH hash 算法是把任意长度的输入通过散列算法变换成固定长度的输出,是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来确定唯一的输入值。 目前哈希算法驱动只支持SHA1 SHA224,SHA256,SHA384,SHA512. SHA 是Secure Hash Algorithm 安全散列算法缩写。 SHA-1可以生成一个被称为消息摘要的160[位](https://baike.baidu.com/item/位)(20[字节](https://baike.baidu.com/item/字节))散列值。允许的最大消息长度是2的64次方位。 * SHA-224 生成28字节散列值; * SHA-256 生成32字节散列值; * SHA-384 生成48字节散列值; * SHA-512生成64字节散列值; 使用步骤: ```c int hash_test(int argc, void **argv) { rt_uint8_t buf_in[32]; rt_uint8_t buf_out[32]; int i; struct rt_hwcrypto_ctx *ctx; hwcrypto_type type; if (0 == rt_strcmp("sha1", argv[1])) { type = HWCRYPTO_TYPE_SHA1; } else if (0 == rt_strcmp("224", argv[1])) { type = HWCRYPTO_TYPE_SHA224; } else if (0 == rt_strcmp("256", argv[1])) { type = HWCRYPTO_TYPE_SHA256; } else if (0 == rt_strcmp("384", argv[1])) { type = HWCRYPTO_TYPE_SHA384; } else if (0 == rt_strcmp("512",argv[1])) { type = HWCRYPTO_TYPE_SHA512; } /* 填充测试数据 */ for (i = 0; i < sizeof(buf_in); i++) { buf_in[i] = (rt_uint8_t)i; } /* 创建一个 SHA1/SHA2类型的上下文 */ ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), type); //重置上下文 这个要有,否则会卡死 rt_hwcrypto_hash_reset(ctx); /* 将输入数据进行 hash 运算 */ rt_hwcrypto_hash_update(ctx, buf_in, 32); /* 获得运算结果 */ rt_hwcrypto_hash_finish(ctx, buf_out, 32); for (i = 0; i < 32; i++) { rt_kprintf("0x%02x ", buf_out[i]); } rt_kprintf("\n"); /* 删除上下文,释放资源 */ rt_hwcrypto_hash_destroy(ctx); } MSH_CMD_EXPORT(hash_test, hash test ); ``` 运行测试命令: ``` msh />hash_test sha1 0xae 0x5b 0xd8 0xef 0xea 0x53 0x22 0xc4 0xd9 0x98 0x6d 0x06 0x68 0x0a 0x78 0x13 0x92 0xf9 0xa6 0x42 0x00 0x00 0x00 0x00 0x00 0x00 0x07 0x40 0x0a 0x00 0x00 0x00 msh />hash_test 224 0x71 0x44 0x6e 0xa9 0x33 0x81 0xba 0x09 0x1f 0x94 0xaf 0xcd 0xc5 0xb9 0x38 0x32 0x32 0x90 0xa1 0xa0 0x27 0xc2 0x2a 0x75 0xe8 0x8a 0x04 0xd0 0x0a 0x00 0x00 0x00 msh />hash_test 256 0x63 0x0d 0xcd 0x29 0x66 0xc4 0x33 0x66 0x91 0x12 0x54 0x48 0xbb 0xb2 0x5b 0x4f 0xf4 0x12 0xa4 0x9c 0x73 0x2d 0xb2 0xc8 0xab 0xc1 0xb8 0x58 0x1b 0xd7 0x10 0xdd msh />hash_test 384 0xe7 0x11 0x24 0x91 0xfa 0xee 0xfd 0x57 0x78 0x6d 0xa7 0x3f 0x36 0x7b 0x25 0xa6 0xf5 0x76 0x9f 0x5c 0x98 0xfa 0x7b 0x70 0x4d 0x8d 0x37 0x74 0x77 0x24 0xa6 0x47 msh />hash_test 512 0x3d 0x94 0xee 0xa4 0x9c 0x58 0x0a 0xef 0x81 0x69 0x35 0x76 0x2b 0xe0 0x49 0x55 0x9d 0x6d 0x14 0x40 0xde 0xde 0x12 0xe6 0xa1 0x25 0xf1 0x84 0x1f 0xff 0x8e 0x6f msh /> ``` ## 对称加解密算法 目前驱动只支持对称加密算法常用的5种分组模式 ECB,CBC,CFB,OFB,CTR 。 为什么需要分组模式? 明文的长度不固定,而分组密码只能处理特定长度的一块数据,这就需要对分组密码的算法进行迭代,以便将一段很长的明文全部加密,而迭代的方法就是分组的模式 。 简单介绍一下几种模式的区别  详细连接: [分组加密模式](https://blog.csdn.net/u013073067/article/details/87086562) [五种分组模式](https://blog.csdn.net/ayang1986/article/details/105690743/) # 外设性能指标测试 测试AES CTR 模式 速度能达到80Mbps , 而有个客户说他们安全芯片速度能到200Mbps, 用于视频加密,看来不同的场合。对性能由不同的要求。 这个性能主要影响就是处理的等待时间,对于高速的场合,延时太大,还是影响挺大的。  具体例子可参考示例代码:perform.c # 功能模块演示  视频地址:[m2354_哔哩哔哩_bilibili](https://www.bilibili.com/video/BV1Pb4y1v7UV?pop_share=1)) # 代码 代码gitee[nu-maker](https://gitee.com/Gerryfan/nu-maker-m2354) ## 注意事项 1.使用scons --dist 制作单独的工程后修改最层Kconfig 中 NU_PKGS_DIR 目录, 将原来工程的libraries 放到工程当前目录。否则报找不到对应Kconfig 错误。 ``` config NU_PKGS_DIR string option env="NU_PKGS_ROOT" default "libraries/nu_packages" ``` # 心得体会 1. 这个M2354的bsp 支持的很完善,不管是调试,烧录程序,都和ST 调试一模一样,很流畅,很方便,一句话即使 用起来还是挺爽的,还是要大大的赞一个; 2. 密码算法这块,驱动框架支持的算法本身不是很多,尤其是芯片支持的非对称算法,后续可以支持的话,可能会更好的步署测试一下加密认证相关的应用; 3. 新唐这个很多安全的机制还没有试用,觉得非常好的一个芯片,期待后面还可以继续深入的玩一玩。
0
条评论
默认排序
按发布时间排序
登录
注册新账号
关于作者
小住住
这家伙很懒,什么也没写!
文章
4
回答
126
被采纳
2
关注TA
发私信
相关文章
推荐文章
1
RT-Thread应用项目汇总
2
玩转RT-Thread系列教程
3
国产MCU移植系列教程汇总,欢迎查看!
4
机器人操作系统 (ROS2) 和 RT-Thread 通信
5
五分钟玩转RT-Thread新社区
6
【技术三千问】之《玩转ART-Pi》,看这篇就够了!干货汇总
7
关于STM32H7开发板上使用SDIO接口驱动SD卡挂载文件系统的问题总结
8
STM32的“GPU”——DMA2D实例详解
9
RT-Thread隐藏的宝藏之completion
10
【ART-PI】RT-Thread 开启RTC 与 Alarm组件
热门标签
RT-Thread Studio
串口
Env
LWIP
SPI
AT
Bootloader
Hardfault
CAN总线
FinSH
ART-Pi
DMA
USB
文件系统
RT-Thread
SCons
RT-Thread Nano
线程
MQTT
STM32
RTC
rt-smart
FAL
I2C_IIC
UART
ESP8266
cubemx
WIZnet_W5500
ota在线升级
PWM
BSP
flash
freemodbus
packages_软件包
潘多拉开发板_Pandora
定时器
ADC
GD32
flashDB
socket
编译报错
中断
Debug
rt_mq_消息队列_msg_queue
keil_MDK
ulog
SFUD
msh
C++_cpp
MicroPython
本月问答贡献
RTT_逍遥
10
个答案
3
次被采纳
xiaorui
3
个答案
2
次被采纳
winfeng
2
个答案
2
次被采纳
三世执戟
8
个答案
1
次被采纳
KunYi
8
个答案
1
次被采纳
本月文章贡献
catcatbing
3
篇文章
5
次点赞
lizimu
2
篇文章
9
次点赞
swet123
1
篇文章
4
次点赞
Days
1
篇文章
4
次点赞
YZRD
1
篇文章
2
次点赞
回到
顶部
发布
问题
投诉
建议
回到
底部