crypto_sample.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365
  1. /*
  2. * Copyright (c) 2006-2021, RT-Thread Development Team
  3. *
  4. * SPDX-License-Identifier: Apache-2.0
  5. *
  6. * Change Logs:
  7. * Date Author Notes
  8. * 2020-06-27 thread-liu first version
  9. */
  10. #include <board.h>
  11. #include "drv_crypto.h"
  12. #include <hwcrypto.h>
  13. #include <string.h>
  14. #include <stdlib.h>
  15. #if defined(BSP_USING_RNG)
  16. static rt_err_t hw_rng_sample(int random_num)
  17. {
  18. rt_err_t result = RT_EOK;
  19. int i = 0, num0 = 0, num1 = 0;
  20. if (random_num == 0)
  21. {
  22. return RT_ERROR;
  23. }
  24. for (i = 0; i< random_num; i++)
  25. {
  26. result = rt_hwcrypto_rng_update();
  27. rt_kprintf("%d ", result);
  28. result%2 ? num1++ : num0++;
  29. }
  30. rt_kprintf("\neven numbers : %d, odd numbers: %d\n",num1, num0);
  31. return RT_EOK;
  32. }
  33. #endif
  34. #if defined(BSP_USING_CRC)
  35. static void hw_crc_sample(uint8_t *temp, int size)
  36. {
  37. struct rt_hwcrypto_ctx *ctx;
  38. rt_uint32_t result = 0;
  39. struct hwcrypto_crc_cfg cfg =
  40. {
  41. .last_val = 0xFFFFFFFF,
  42. .poly = 0x04C11DB7,
  43. .width = 32,
  44. .xorout = 0x00000000,
  45. .flags = 0,
  46. };
  47. ctx = rt_hwcrypto_crc_create(rt_hwcrypto_dev_default(), HWCRYPTO_CRC_CRC32);
  48. rt_hwcrypto_crc_cfg(ctx, &cfg);
  49. result = rt_hwcrypto_crc_update(ctx, temp, size);
  50. rt_kprintf("crc result: %x \n", result);
  51. rt_hwcrypto_crc_destroy(ctx);
  52. }
  53. #endif
  54. #if defined(BSP_USING_HASH)
  55. static void hw_hash_sample()
  56. {
  57. int i = 0;
  58. struct rt_hwcrypto_ctx *ctx = RT_NULL;
  59. const uint8_t hash_input[] = "RT-Thread was born in 2006, it is an open source, neutral, and community-based real-time operating system (RTOS).";
  60. static uint8_t sha1_output[20];
  61. static uint8_t sha1_except[20] = {0xff, 0x3c, 0x95, 0x54, 0x95, 0xf0, 0xad,
  62. 0x02, 0x1b, 0xa8, 0xbc, 0xa2, 0x2e, 0xa5,
  63. 0xb0, 0x62, 0x1b, 0xdf, 0x7f, 0xec};
  64. static uint8_t md5_output[16];
  65. static uint8_t md5_except[16] = {0x40, 0x86, 0x03, 0x80, 0x0d, 0x8c, 0xb9,
  66. 0x4c, 0xd6, 0x7d, 0x28, 0xfc, 0xf6, 0xc3,
  67. 0xac, 0x8b};
  68. static uint8_t sha224_output[28];
  69. static uint8_t sha224_except[28] = {0x6f, 0x62, 0x52, 0x7d, 0x80, 0xe6,
  70. 0x9f, 0x82, 0x78, 0x7a, 0x46, 0x91,
  71. 0xb0, 0xe9, 0x64, 0x89, 0xe6, 0xc3,
  72. 0x6b, 0x7e, 0xcf, 0xca, 0x11, 0x42,
  73. 0xc8, 0x77, 0x13, 0x79};
  74. static uint8_t sha256_output[32];
  75. static uint8_t sha256_except[32] = {0x74, 0x19, 0xb9, 0x0e, 0xd1, 0x46,
  76. 0x37, 0x0a, 0x55, 0x18, 0x26, 0x6c,
  77. 0x50, 0xd8, 0x71, 0x34, 0xfa, 0x1f,
  78. 0x5f, 0x5f, 0xe4, 0x9a, 0xe9, 0x40,
  79. 0x0a, 0x7d, 0xa0, 0x26, 0x1b, 0x86,
  80. 0x67, 0x45};
  81. rt_kprintf("Hash Test start: \n");
  82. rt_kprintf("Hash Test string: \n");
  83. for (i = 0; i < sizeof(hash_input); i++)
  84. {
  85. rt_kprintf("%c", hash_input[i]);
  86. }
  87. rt_kprintf("\n");
  88. /* sh1 test*/
  89. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA1);
  90. if (ctx == RT_NULL)
  91. {
  92. rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_SHA1);
  93. return ;
  94. }
  95. rt_kprintf("Create sha1 type success!\n");
  96. rt_kprintf("Except sha1 result: \n");
  97. for (i = 0; i < sizeof(sha1_except); i++)
  98. {
  99. rt_kprintf("%x ", sha1_except[i]);
  100. }
  101. rt_kprintf("\n");
  102. /* start sha1 */
  103. rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
  104. /* get sha1 result */
  105. rt_hwcrypto_hash_finish(ctx, sha1_output, rt_strlen((char const *)sha1_output));
  106. rt_kprintf("Actual sha1 result: \n");
  107. for (i = 0; i < sizeof(sha1_output); i++)
  108. {
  109. rt_kprintf("%x ", sha1_output[i]);
  110. }
  111. rt_kprintf("\n");
  112. if(rt_memcmp(sha1_output, sha1_except, sizeof(sha1_except)/sizeof(sha1_except[0])) != 0)
  113. {
  114. rt_kprintf("Hash type sha1 Test error, The actual result is not equal to the except result\n");
  115. }
  116. else
  117. {
  118. rt_kprintf("Hash type sha1 Test success, The actual result is equal to the except result\n");
  119. }
  120. /* deinit hash*/
  121. rt_hwcrypto_hash_destroy(ctx);
  122. /* md5 test*/
  123. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_MD5);
  124. if (ctx == RT_NULL)
  125. {
  126. rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_MD5);
  127. return ;
  128. }
  129. rt_kprintf("Create md5 type success!\n");
  130. rt_kprintf("Except md5 result: \n");
  131. for (i = 0; i < sizeof(md5_except); i++)
  132. {
  133. rt_kprintf("%x ", md5_except[i]);
  134. }
  135. rt_kprintf("\n");
  136. /* start md5 */
  137. rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
  138. /* get md5 result */
  139. rt_hwcrypto_hash_finish(ctx, md5_output, rt_strlen((char const *)md5_output));
  140. rt_kprintf("Actual md5 result: \n");
  141. for (i = 0; i < sizeof(md5_output); i++)
  142. {
  143. rt_kprintf("%x ", md5_output[i]);
  144. }
  145. rt_kprintf("\n");
  146. if(rt_memcmp(md5_output, md5_except, sizeof(md5_except)/sizeof(md5_except[0])) != 0)
  147. {
  148. rt_kprintf("Hash type md5 Test error, The actual result is not equal to the except result\n");
  149. }
  150. else
  151. {
  152. rt_kprintf("Hash type md5 Test success, The actual result is equal to the except result\n");
  153. }
  154. /* deinit hash*/
  155. rt_hwcrypto_hash_destroy(ctx);
  156. /* sha224 test */
  157. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA224);
  158. if (ctx == RT_NULL)
  159. {
  160. rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_SHA224);
  161. return ;
  162. }
  163. rt_kprintf("Create sha224 type success!\n");
  164. rt_kprintf("Except sha224 result: \n");
  165. for (i = 0; i < sizeof(sha224_except); i++)
  166. {
  167. rt_kprintf("%x ", sha224_except[i]);
  168. }
  169. rt_kprintf("\n");
  170. /* start sha224 */
  171. rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
  172. /* get sha224 result */
  173. rt_hwcrypto_hash_finish(ctx, sha224_output, rt_strlen((char const *)sha224_output));
  174. rt_kprintf("Actual sha224 result: \n");
  175. for (i = 0; i < sizeof(sha224_output); i++)
  176. {
  177. rt_kprintf("%x ", sha224_output[i]);
  178. }
  179. rt_kprintf("\n");
  180. if(rt_memcmp(sha224_output, sha224_except, sizeof(sha224_except)/sizeof(sha224_except[0])) != 0)
  181. {
  182. rt_kprintf("Hash type sha224 Test error, The actual result is not equal to the except result\n");
  183. }
  184. else
  185. {
  186. rt_kprintf("Hash type sha224 Test success, The actual result is equal to the except result\n");
  187. }
  188. rt_hwcrypto_hash_destroy(ctx);
  189. /* sha256 test*/
  190. ctx = rt_hwcrypto_hash_create(rt_hwcrypto_dev_default(), HWCRYPTO_TYPE_SHA256);
  191. if (ctx == RT_NULL)
  192. {
  193. rt_kprintf("create hash[%08x] context err!\n", HWCRYPTO_TYPE_SHA256);
  194. return ;
  195. }
  196. rt_kprintf("Create sha256 type success!\n");
  197. rt_kprintf("Except sha256 result: \n");
  198. for (i = 0; i < sizeof(sha256_except); i++)
  199. {
  200. rt_kprintf("%x ", sha256_except[i]);
  201. }
  202. rt_kprintf("\n");
  203. /* start sha256 */
  204. rt_hwcrypto_hash_update(ctx, hash_input, rt_strlen((char const *)hash_input));
  205. /* get sha256 result */
  206. rt_hwcrypto_hash_finish(ctx, sha256_output, rt_strlen((char const *)sha256_output));
  207. rt_kprintf("Actual sha256 result: \n");
  208. for (i = 0; i < sizeof(sha256_output); i++)
  209. {
  210. rt_kprintf("%x ", sha256_output[i]);
  211. }
  212. rt_kprintf("\n");
  213. if(rt_memcmp(sha256_output, sha256_except, sizeof(sha256_except)/sizeof(sha256_except[0])) != 0)
  214. {
  215. rt_kprintf("Hash type sha256 Test error, The actual result is not equal to the except result\n");
  216. }
  217. else
  218. {
  219. rt_kprintf("Hash type sha256 Test success, The actual result is equal to the except result\n");
  220. }
  221. rt_hwcrypto_hash_destroy(ctx);
  222. rt_kprintf("Hash Test over!\n");
  223. }
  224. #endif
  225. static int crypto(int argc, char **argv)
  226. {
  227. int result = RT_EOK;
  228. static rt_device_t device = RT_NULL;
  229. char *result_str;
  230. if (argc > 1)
  231. {
  232. if (!strcmp(argv[1], "probe"))
  233. {
  234. if (argc == 3)
  235. {
  236. char *dev_name = argv[2];
  237. device = rt_device_find(dev_name);
  238. result_str = (device == RT_NULL) ? "failure" : "success";
  239. rt_kprintf("probe %s %s \n", argv[2], result_str);
  240. }
  241. else
  242. {
  243. rt_kprintf("crypto probe <crypto_name> - probe crypto by name\n");
  244. }
  245. }
  246. else
  247. {
  248. if (device == RT_NULL)
  249. {
  250. rt_kprintf("Please using 'crypto probe <crypto_name>' first\n");
  251. return -RT_ERROR;
  252. }
  253. if (!strcmp(argv[1], "rng"))
  254. {
  255. #if defined (BSP_USING_RNG)
  256. if (argc == 3)
  257. {
  258. result = hw_rng_sample(atoi(argv[2]));
  259. if(result != RT_EOK)
  260. {
  261. rt_kprintf("please input a legal number, not <%d>\n", atoi(argv[2]));
  262. }
  263. }
  264. else
  265. {
  266. rt_kprintf("rng <number> - generate <number> digital\n");
  267. }
  268. #else
  269. rt_kprintf("please enable RNG first!\n");
  270. #endif
  271. }
  272. else if (!strcmp(argv[1], "crc"))
  273. {
  274. #if defined (BSP_USING_CRC)
  275. int size = 0, i = 0;
  276. if (argc > 3)
  277. {
  278. size = argc - 2;
  279. uint8_t *data = rt_malloc(size);
  280. if (data)
  281. {
  282. for (i = 0; i < size; i++)
  283. {
  284. data[i] = strtol(argv[2 + i], NULL, 0);
  285. }
  286. hw_crc_sample(data, size);
  287. rt_free(data);
  288. }
  289. else
  290. {
  291. rt_kprintf("Low memory!\n");
  292. }
  293. }
  294. else
  295. {
  296. rt_kprintf("crypto crc data1 ... dataN - calculate data1 ... dataN crc\n");
  297. }
  298. #else
  299. rt_kprintf("please enable CRC first!\n");
  300. #endif
  301. }
  302. else if (!strcmp(argv[1], "hash"))
  303. {
  304. #if defined (BSP_USING_HASH)
  305. if (argc == 3)
  306. {
  307. hw_hash_sample();
  308. }
  309. else
  310. {
  311. rt_kprintf("crypto hash sample - hash use sample\n");
  312. }
  313. #else
  314. rt_kprintf("please enable CRC first!\n");
  315. #endif
  316. }
  317. else
  318. {
  319. rt_kprintf("Unknown command. Please enter 'crypto' for help\n");
  320. }
  321. }
  322. }
  323. else
  324. {
  325. rt_kprintf("Usage: \n");
  326. rt_kprintf("crypto probe <crypto_name> - probe crypto by name\n");
  327. rt_kprintf("crypto rng number - generate numbers digital\n");
  328. rt_kprintf("crypto crc data1 ... dataN - calculate data1 ... dataN crc\n");
  329. rt_kprintf("crypto hash sample - hash use sample\n");
  330. result = -RT_ERROR;
  331. }
  332. return result;
  333. }
  334. MSH_CMD_EXPORT(crypto, crypto function);