本次的任务是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 上的这些算法的特性如下:
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 算法是把任意长度的输入通过散列算法变换成固定长度的输出,是一种将任意长度的消息压缩到某一固定长度的消息摘要的函数。散列值的空间通常远小于输入的空间,不同的输入可能会散列成相同的输出,所以不可能从散列值来确定唯一的输入值。
目前哈希算法驱动只支持SHA1 SHA224,SHA256,SHA384,SHA512.
SHA 是Secure Hash Algorithm 安全散列算法缩写。
SHA-1可以生成一个被称为消息摘要的160位(20字节)散列值。允许的最大消息长度是2的64次方位。
使用步骤:
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 。
为什么需要分组模式?
明文的长度不固定,而分组密码只能处理特定长度的一块数据,这就需要对分组密码的算法进行迭代,以便将一段很长的明文全部加密,而迭代的方法就是分组的模式 。
简单介绍一下几种模式的区别
详细连接:
测试AES CTR 模式 速度能达到80Mbps , 而有个客户说他们安全芯片速度能到200Mbps, 用于视频加密,看来不同的场合。对性能由不同的要求。
这个性能主要影响就是处理的等待时间,对于高速的场合,延时太大,还是影响挺大的。
具体例子可参考示例代码:perform.c
视频地址:m2354_哔哩哔哩_bilibili)
代码giteenu-maker
1.使用scons --dist 制作单独的工程后修改最层Kconfig 中 NU_PKGS_DIR 目录, 将原来工程的libraries 放到工程当前目录。否则报找不到对应Kconfig 错误。
config NU_PKGS_DIR
string
option env="NU_PKGS_ROOT"
default "libraries/nu_packages"