test_crypto.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. /*
  2. * Copyright (c) 2022-2024, Xiaohua Semiconductor Co., Ltd.
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2024-12-30 CDT first version
  9. */
  10. #include <rtthread.h>
  11. #include <rtdevice.h>
  12. #include <stdlib.h>
  13. #if defined(BSP_USING_HWCRYPTO)
  14. #define ARR_SIZE(arr) (sizeof(arr)/sizeof(*arr))
  15. #define PRINT_DIGIT_ARR(arr) do { \
  16. rt_kprintf("%s: ", #arr); \
  17. for(int i = 0; i < ARR_SIZE(arr); i++) \
  18. rt_kprintf("%d ", arr[i]); \
  19. rt_kprintf("\n"); \
  20. } while(0)
  21. #define WDT_DEVICE_NAME "crypto"
  22. static void _crypto_cmd_print_usage(void)
  23. {
  24. #if defined(BSP_USING_RNG)
  25. rt_kprintf("crypto_sample [option] \n");
  26. rt_kprintf(" rang: get random number from rang module. \n");
  27. rt_kprintf(" e.g. msh >crypto_sample rang get \n");
  28. #endif
  29. #if defined(BSP_USING_CRC)
  30. rt_kprintf("crypto_sample [option] \n");
  31. rt_kprintf(" crc: test crc module. \n");
  32. rt_kprintf(" e.g. msh >crypto_sample crc 16/32 \n");
  33. #endif
  34. #if defined(BSP_USING_AES)
  35. rt_kprintf("crypto_sample [option] \n");
  36. rt_kprintf(" aes: test aes module. \n");
  37. #if defined(HC32F460)
  38. rt_kprintf(" e.g. msh >crypto_sample aes 128 \n");
  39. #elif defined (HC32F4A0) || defined (HC32F448) || defined (HC32F472)
  40. rt_kprintf(" e.g. msh >crypto_sample aes 128/192/256 \n");
  41. #endif
  42. #endif
  43. #if defined(BSP_USING_HASH)
  44. rt_kprintf("crypto_sample [option] \n");
  45. rt_kprintf(" hash: test hash module. \n");
  46. rt_kprintf(" e.g. msh >crypto_sample hash test \n");
  47. #endif
  48. }
  49. #if defined(BSP_USING_CRC)
  50. #define CRC16_WIDTH 16U
  51. #define CRC32_WIDTH 32U
  52. static void crc_test(rt_uint32_t width)
  53. {
  54. rt_uint8_t temp_in[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  55. struct rt_hwcrypto_ctx *ctx;
  56. rt_uint32_t result = 0;
  57. /* CRC16_X25 */
  58. struct hwcrypto_crc_cfg cfg =
  59. {
  60. .last_val = 0xFFFFU,
  61. .poly = 0x1021U,
  62. .width = CRC16_WIDTH,
  63. .xorout = 0xFFFFU,
  64. .flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT,
  65. };
  66. /* CRC32 */
  67. if (width == CRC32_WIDTH)
  68. {
  69. cfg.last_val = 0xFFFFFFFFUL;
  70. cfg.poly = 0x04C11DB7UL;
  71. cfg.width = CRC32_WIDTH;
  72. cfg.xorout = 0xFFFFFFFFUL;
  73. cfg.flags = CRC_FLAG_REFIN | CRC_FLAG_REFOUT;
  74. ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
  75. }
  76. else if (width == CRC16_WIDTH)
  77. {
  78. ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC16);
  79. }
  80. else
  81. {
  82. rt_kprintf("crc%d not support! \n", width);
  83. return;
  84. }
  85. rt_hwcrypto_crc_cfg(ctx, &cfg);
  86. rt_kprintf("temp_in: ");
  87. for (int i = 0; i < sizeof(temp_in) / 2U; i++)
  88. {
  89. rt_kprintf("%d ", temp_in[i]);
  90. }
  91. rt_kprintf("\n");
  92. result = rt_hwcrypto_crc_update(ctx, temp_in, sizeof(temp_in) / 2U);
  93. rt_kprintf("crc%d result: 0x%x \n", width, result);
  94. /* Accumulate test */
  95. PRINT_DIGIT_ARR(temp_in);
  96. result = rt_hwcrypto_crc_update(ctx, &temp_in[sizeof(temp_in) / 2U], sizeof(temp_in) / 2U);
  97. rt_kprintf("crc%d result: 0x%x \n", width, result);
  98. rt_hwcrypto_crc_destroy(ctx);
  99. }
  100. #endif
  101. #if defined(BSP_USING_AES)
  102. #define AES_DATA_LEN 32U /* data of length must be a multiple of 16(128 Bit) */
  103. static void aes_test(rt_uint16_t key_bitlen)
  104. {
  105. rt_uint32_t result = RT_EOK;
  106. struct rt_hwcrypto_ctx *ctx;
  107. rt_uint8_t enc_out[AES_DATA_LEN];
  108. rt_uint8_t dec_out[AES_DATA_LEN];
  109. const char *enc_in = "abcdefghijksdwpa123456789asdfghj";
  110. const char *key128 = "1234567890abcdef";
  111. const char *key192 = "1234567890abcdefghijklmn";
  112. const char *key256 = "1234567890abcdefghijklmnopqrstuv";
  113. const char *key;
  114. ctx = rt_hwcrypto_symmetric_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_AES_ECB);
  115. if (ctx == RT_NULL)
  116. {
  117. rt_kprintf("create AES-CBC context err!");
  118. return;
  119. }
  120. switch (key_bitlen)
  121. {
  122. case 128:
  123. key = key128;
  124. break;
  125. case 192:
  126. key = key192;
  127. break;
  128. case 256:
  129. key = key256;
  130. break;
  131. default:
  132. key = key128;
  133. break;
  134. }
  135. result = rt_hwcrypto_symmetric_setkey(ctx, (rt_uint8_t *)key, key_bitlen);
  136. if (result == RT_EOK)
  137. {
  138. result = rt_hwcrypto_symmetric_crypt(ctx, HWCRYPTO_MODE_ENCRYPT, AES_DATA_LEN, (rt_uint8_t *)enc_in, enc_out);
  139. if (result != RT_EOK)
  140. {
  141. goto _exit;
  142. }
  143. rt_kprintf("aes src data:");
  144. for (int i = 0; i < AES_DATA_LEN; i++)
  145. {
  146. rt_kprintf("%c", enc_in[i]);
  147. }
  148. rt_kprintf("\n");
  149. rt_kprintf("aes enc data:");
  150. for (int i = 0; i < AES_DATA_LEN; i++)
  151. {
  152. rt_kprintf("%x ", enc_out[i]);
  153. }
  154. rt_kprintf("\n");
  155. result = rt_hwcrypto_symmetric_crypt(ctx, HWCRYPTO_MODE_DECRYPT, AES_DATA_LEN, (rt_uint8_t *)enc_out, dec_out);
  156. if (result != RT_EOK)
  157. {
  158. goto _exit;
  159. }
  160. rt_kprintf("aes dec data:");
  161. for (int i = 0; i < AES_DATA_LEN; i++)
  162. {
  163. rt_kprintf("%c", dec_out[i]);
  164. }
  165. rt_kprintf("\n");
  166. _exit:
  167. rt_hwcrypto_symmetric_destroy(ctx);
  168. }
  169. }
  170. #endif
  171. #if defined(BSP_USING_HASH)
  172. #define HASH_SHA256_MSG_DIGEST_SIZE (32U)
  173. static void hash_sha256_test(void)
  174. {
  175. const char *in = "0123456789abcdefghijklmnopqrstuvwxyz";
  176. uint8_t out[HASH_SHA256_MSG_DIGEST_SIZE];
  177. struct rt_hwcrypto_ctx *ctx;
  178. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA256);
  179. if (ctx != RT_NULL)
  180. {
  181. rt_hwcrypto_hash_update(ctx, (rt_uint8_t *)in, rt_strlen(in));
  182. rt_kprintf("hash in data:");
  183. for (int i = 0; i < rt_strlen(in); i++)
  184. {
  185. rt_kprintf("%c", in[i]);
  186. }
  187. rt_kprintf("\n");
  188. rt_hwcrypto_hash_finish(ctx, out, HASH_SHA256_MSG_DIGEST_SIZE);
  189. rt_kprintf("hash out data:");
  190. for (int i = 0; i < HASH_SHA256_MSG_DIGEST_SIZE; i++)
  191. {
  192. rt_kprintf("%x ", out[i]);
  193. }
  194. rt_kprintf("\n");
  195. rt_hwcrypto_hash_destroy(ctx);
  196. }
  197. }
  198. #endif
  199. static int crypto_sample(int argc, char *argv[])
  200. {
  201. rt_err_t ret = RT_EOK;
  202. if (argc != 3)
  203. {
  204. goto _exit;
  205. }
  206. #if defined(BSP_USING_RNG)
  207. if (!rt_strcmp("rang", argv[1]))
  208. {
  209. if (!rt_strcmp("get", argv[2]))
  210. {
  211. rt_uint32_t result = rt_hwcrypto_rng_update();
  212. rt_kprintf("random number = %x \n", result);
  213. }
  214. else
  215. {
  216. goto _exit;
  217. }
  218. }
  219. #endif
  220. #if defined(BSP_USING_CRC)
  221. else if (!rt_strcmp("crc", argv[1]))
  222. {
  223. rt_uint32_t width = atoi(argv[2]);
  224. if (width == CRC16_WIDTH || width == CRC32_WIDTH)
  225. {
  226. crc_test(width);
  227. }
  228. else
  229. {
  230. goto _exit;
  231. }
  232. }
  233. #endif
  234. #if defined(BSP_USING_AES)
  235. else if (!rt_strcmp("aes", argv[1]))
  236. {
  237. rt_uint32_t key_bitlen = atoi(argv[2]);
  238. if (key_bitlen == 128 || key_bitlen == 192 || key_bitlen == 256)
  239. {
  240. aes_test(key_bitlen);
  241. }
  242. else
  243. {
  244. goto _exit;
  245. }
  246. }
  247. #endif
  248. #if defined(BSP_USING_HASH)
  249. else if (!rt_strcmp("hash", argv[1]))
  250. {
  251. if (!rt_strcmp("test", argv[2]))
  252. {
  253. hash_sha256_test();
  254. }
  255. else
  256. {
  257. goto _exit;
  258. }
  259. }
  260. #endif
  261. else
  262. {
  263. goto _exit;
  264. }
  265. return ret;
  266. _exit:
  267. _crypto_cmd_print_usage();
  268. return -RT_ERROR;
  269. }
  270. MSH_CMD_EXPORT(crypto_sample, crypto [option]);
  271. #endif