1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
|
/*
* Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
*
* SPDX-License-Identifier: Apache-2.0
*
* Change Logs:
* Date Author Notes
* 2024-12-30 CDT first version
* 2026-05-27 CDT Support HC32F4A2
* 2026-06-03 CDT Support HC32F467
*/
#include <rtthread.h>
#include <rtdevice.h>
#include <stdlib.h>
#include <string.h>
#if defined(BSP_USING_HWCRYPTO)
#define ARR_SIZE(arr) (sizeof(arr) / sizeof(*arr))
#define PRINT_DIGIT_ARR(arr) \
do \
{ \
rt_kprintf("%s: ", #arr); \
for (int i = 0; i < ARR_SIZE(arr); i++) \
rt_kprintf("%d ", arr[i]); \
rt_kprintf("\n"); \
} while (0)
#define WDT_DEVICE_NAME "crypto"
static void _crypto_cmd_print_usage(void)
{
#if defined(BSP_USING_RNG)
rt_kprintf("crypto_sample [option] \n");
rt_kprintf(" rang: get random number from rang module. \n");
rt_kprintf(" e.g. msh >crypto_sample rang get \n");
#endif
#if defined(BSP_USING_CRC)
rt_kprintf("crypto_sample [option] \n");
rt_kprintf(" crc: test crc module. \n");
rt_kprintf(" e.g. msh >crypto_sample crc 16/32 \n");
#endif
#if defined(BSP_USING_AES)
rt_kprintf("crypto_sample [option] \n");
rt_kprintf(" aes: test aes module. \n");
#if defined(HC32F460)
rt_kprintf(" e.g. msh >crypto_sample aes 128 \n");
#elif defined(HC32F4A0) || defined(HC32F4A2) || defined(HC32F448) || defined(HC32F472) || defined(HC32F4A8) || \
defined(HC32F467)
rt_kprintf(" e.g. msh >crypto_sample aes 128/192/256 \n");
#endif
#endif
#if defined(BSP_USING_HASH)
rt_kprintf("crypto_sample [option] \n");
rt_kprintf(" hash: test hash module. \n");
rt_kprintf(" e.g. msh >crypto_sample hash test \n");
#endif
}
#if defined(BSP_USING_CRC)
/* menuconfig:
Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto Drivers --->
[*]Using Hardeware CRC
* CRC16命令调用:crypto_sample crc 16
* CRC32命令调用:crypto_sample crc 32
* 程序功能:打印CRC输入数据和计数结果,使用第三方软件计算数据,再做比较
*/
#define CRC16_WIDTH 16U
#define CRC32_WIDTH 32U
static void crc_test(rt_uint32_t width)
{
rt_uint8_t temp_in[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
struct rt_hwcrypto_ctx *ctx;
rt_uint32_t result = 0;
/* CRC16_X25 */
struct hwcrypto_crc_cfg cfg = {
.last_val = 0xFFFFU,
.poly = 0x1021U,
.width = CRC16_WIDTH,
.xorout = 0xFFFFU,
.flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT,
};
/* CRC32 */
if (width == CRC32_WIDTH)
{
cfg.last_val = 0xFFFFFFFFUL;
cfg.poly = 0x04C11DB7UL;
cfg.width = CRC32_WIDTH;
cfg.xorout = 0xFFFFFFFFUL;
cfg.flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT;
ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
}
else if (width == CRC16_WIDTH)
{
ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC16);
}
else
{
rt_kprintf("crc%d not support! \n", width);
return;
}
rt_hwcrypto_crc_cfg(ctx, &cfg);
rt_kprintf("temp_in: ");
for (int i = 0; i < sizeof(temp_in) / 2U; i++)
{
rt_kprintf("%d ", temp_in[i]);
}
rt_kprintf("\n");
result = rt_hwcrypto_crc_update(ctx, temp_in, sizeof(temp_in) / 2U);
rt_kprintf("crc%d expect_result: 0x%X \n", width, (width == 16) ? 0x0A38 : 0x515Ad3CC);
rt_kprintf(" calc_result: 0x%X \n\n", result);
/* Accumulate test */
PRINT_DIGIT_ARR(temp_in);
result = rt_hwcrypto_crc_update(ctx, &temp_in[sizeof(temp_in) / 2U], sizeof(temp_in) / 2U);
rt_kprintf("crc%d expect_result: 0x%X \n", width, (width == 16) ? 0x2FE2 : 0x456CD746);
rt_kprintf(" calc_result: 0x%X \n\n", result);
rt_hwcrypto_crc_destroy(ctx);
}
#endif
#if defined(BSP_USING_AES)
/* menuconfig:
Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto Drivers --->
[*]Using Hardeware AES
* AES命令调用:crypto_sample aes 128/192/256
* 程序功能:打印明文、密文、密文解密后的数据
*/
#define AES_DATA_LEN 32U /* data of length must be a multiple of 16(128 Bit) */
static void aes_test(rt_uint16_t key_bitlen)
{
rt_uint32_t result = RT_EOK;
struct rt_hwcrypto_ctx *ctx;
rt_uint8_t enc_out[AES_DATA_LEN];
rt_uint8_t dec_out[AES_DATA_LEN];
const char *enc_in = "abcdefghijksdwpa123456789asdfghj";
const char *key128 = "1234567890abcdef";
const char *key192 = "1234567890abcdefghijklmn";
const char *key256 = "1234567890abcdefghijklmnopqrstuv";
const char *key;
rt_uint8_t type_max = 1U;
hwcrypto_type types[] = {
HWCRYPTO_TYPE_AES_ECB,
HWCRYPTO_TYPE_AES_CBC,
HWCRYPTO_TYPE_AES_CTR,
HWCRYPTO_TYPE_AES_CFB,
HWCRYPTO_TYPE_AES_OFB,
};
const char *type_str[] = {
"aes_ecb",
"aes_cbc",
"aes_ctr",
"aes_cfb",
"aes_ofb",
};
#if defined(HC32F4A8)
type_max = 5U;
#endif
for (int i = 0; i < type_max; i++)
{
ctx = rt_hwcrypto_symmetric_create(rt_hwcrypto_dev_default(), types[i]);
if (ctx == RT_NULL)
{
rt_kprintf("create AES context err!");
return;
}
switch (key_bitlen)
{
case 128:
key = key128;
break;
case 192:
key = key192;
break;
case 256:
key = key256;
break;
default:
key = key128;
break;
}
result = rt_hwcrypto_symmetric_setkey(ctx, (rt_uint8_t *)key, key_bitlen);
#if defined(HC32F4A8)
const char *iv = "1234567812345678";
result = rt_hwcrypto_symmetric_setiv(ctx, (const rt_uint8_t *)iv, strlen(iv));
#endif
if (result == RT_EOK)
{
result = rt_hwcrypto_symmetric_crypt(ctx, HWCRYPTO_MODE_ENCRYPT, AES_DATA_LEN, (rt_uint8_t *)enc_in, enc_out);
if (result != RT_EOK)
{
goto _exit;
}
rt_kprintf("%s src data:", type_str[i]);
for (int i = 0; i < AES_DATA_LEN; i++)
{
rt_kprintf("%c", enc_in[i]);
}
rt_kprintf("\n");
rt_kprintf("%s enc data:", type_str[i]);
for (int i = 0; i < AES_DATA_LEN; i++)
{
rt_kprintf("%02x ", enc_out[i]);
}
rt_kprintf("\n");
result = rt_hwcrypto_symmetric_crypt(ctx, HWCRYPTO_MODE_DECRYPT, AES_DATA_LEN, (rt_uint8_t *)enc_out, dec_out);
if (result != RT_EOK)
{
goto _exit;
}
rt_kprintf("%s dec data:", type_str[i]);
for (int i = 0; i < AES_DATA_LEN; i++)
{
rt_kprintf("%c", dec_out[i]);
}
rt_kprintf("\n\n");
_exit:
rt_hwcrypto_symmetric_destroy(ctx);
}
}
}
#endif
#if defined(BSP_USING_HASH)
/* menuconfig:
Hardware Drivers Config--->On-Chip Peripheral Driver--->Using Hardware Crypto Drivers --->
[*]Using Hardeware Hash
* Hash命令调用:crypto_sample hash test
* 程序功能:打印hash原始消息、期望的SHA256结果和计算的SHA256结果
*/
#define HASH_SHA256_MSG_DIGEST_SIZE (32U)
static void hash_sha256_test(void)
{
const char *in = "0123456789abcdefghijklmnopqrstuvwxyz";
uint8_t calc_out[HASH_SHA256_MSG_DIGEST_SIZE];
const uint8_t expect_out[HASH_SHA256_MSG_DIGEST_SIZE] = { 0x74, 0xE7, 0xE5, 0xBB, 0x9D, 0x22, 0xD6, 0xDB, 0x26, 0xBF,
0x76, 0x94, 0x6D, 0x40, 0xFF, 0xF3, 0xEA, 0x9F, 0x03, 0x46,
0xB8, 0x84, 0xFD, 0x06, 0x94, 0x92, 0x0F, 0xCC, 0xFA, 0xD1,
0x5E, 0x33 };
struct rt_hwcrypto_ctx *ctx;
ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA256);
if (ctx != RT_NULL)
{
rt_hwcrypto_hash_update(ctx, (rt_uint8_t *)in, rt_strlen(in));
rt_kprintf("hash in data:");
for (int i = 0; i < rt_strlen(in); i++)
{
rt_kprintf("%c", in[i]);
}
rt_kprintf("\n");
rt_hwcrypto_hash_finish(ctx, calc_out, HASH_SHA256_MSG_DIGEST_SIZE);
rt_kprintf("hash out expect_data:");
for (int i = 0; i < HASH_SHA256_MSG_DIGEST_SIZE; i++)
{
rt_kprintf("%x ", expect_out[i]);
}
rt_kprintf("\n");
rt_kprintf("hash out calc_data:");
for (int i = 0; i < HASH_SHA256_MSG_DIGEST_SIZE; i++)
{
rt_kprintf("%x ", calc_out[i]);
}
rt_kprintf("\n\n");
rt_hwcrypto_hash_destroy(ctx);
}
}
#endif
static int crypto_sample(int argc, char *argv[])
{
rt_err_t ret = RT_EOK;
if (argc != 3)
{
goto _exit;
}
#if defined(BSP_USING_RNG)
if (!rt_strcmp("rang", argv[1]))
{
if (!rt_strcmp("get", argv[2]))
{
rt_uint32_t result = rt_hwcrypto_rng_update();
rt_kprintf("random number = %x \n\n", result);
}
else
{
goto _exit;
}
}
#endif
#if defined(BSP_USING_CRC)
else if (!rt_strcmp("crc", argv[1]))
{
rt_uint32_t width = atoi(argv[2]);
if (width == CRC16_WIDTH || width == CRC32_WIDTH)
{
crc_test(width);
}
else
{
goto _exit;
}
}
#endif
#if defined(BSP_USING_AES)
else if (!rt_strcmp("aes", argv[1]))
{
rt_uint32_t key_bitlen = atoi(argv[2]);
if (key_bitlen == 128 || key_bitlen == 192 || key_bitlen == 256)
{
aes_test(key_bitlen);
}
else
{
goto _exit;
}
}
#endif
#if defined(BSP_USING_HASH)
else if (!rt_strcmp("hash", argv[1]))
{
if (!rt_strcmp("test", argv[2]))
{
hash_sha256_test();
}
else
{
goto _exit;
}
}
#endif
else
{
goto _exit;
}
return ret;
_exit:
_crypto_cmd_print_usage();
return -RT_ERROR;
}
MSH_CMD_EXPORT(crypto_sample, crypto[option]);
#endif
|