test_ce.c 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676
  1. /*
  2. * Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
  3. *
  4. * Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
  5. * the the people's Republic of China and other countries.
  6. * All Allwinner Technology Co.,Ltd. trademarks are used with permission.
  7. *
  8. * DISCLAIMER
  9. * THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
  10. * IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
  11. * IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
  12. * ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
  13. * ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
  14. * COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
  15. * YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
  16. *
  17. *
  18. * THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
  19. * PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
  20. * WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
  21. * THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
  22. * OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
  23. * IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  24. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
  25. * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  26. * LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
  27. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
  28. * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  29. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
  30. * OF THE POSSIBILITY OF SUCH DAMAGE.
  31. */
  32. #include <stdio.h>
  33. #include <string.h>
  34. #include <unistd.h>
  35. #include <hal_cmd.h>
  36. #include <hal_mem.h>
  37. #include <sunxi_hal_ce.h>
  38. #include "test_ce.h"
  39. #define AES_MODE_ECB (0)
  40. #define AES_MODE_CBC (1)
  41. #define AES_MODE_CTR (2)
  42. #define AES_MODE_CTS (3)
  43. #define AES_MODE_OFB (4)
  44. #define AES_MODE_CFB (5)
  45. #define AES_DIR_ENCRYPT (0)
  46. #define AES_DIR_DECRYPT (1)
  47. #define HASH_METHOD_MD5 (16)
  48. #define HASH_METHOD_SHA1 (17)
  49. #define HASH_METHOD_SHA224 (18)
  50. #define HASH_METHOD_SHA256 (19)
  51. #define HASH_METHOD_SHA384 (20)
  52. #define HASH_METHOD_SHA512 (21)
  53. void ce_dump(char *str,unsigned char *data, int len, int align)
  54. {
  55. int i = 0;
  56. if(str)
  57. printf("\n%s: ",str);
  58. for(i = 0; i<len; i++)
  59. {
  60. if((i%align) == 0)
  61. {
  62. printf("\n");
  63. printf("0x08%x: ", data + i * align);
  64. }
  65. printf("%02x ",*(data++));
  66. }
  67. printf("\n");
  68. }
  69. int aes_test(void)
  70. {
  71. int ret = -1;
  72. int i = 0;
  73. int j = 0;
  74. int m = 0;
  75. uint8_t *enc_buffer = 0;
  76. uint32_t enc_len = 0;
  77. uint32_t blk_num = 0;
  78. uint8_t iv_next[AES_BLOCK_SIZE] = {0};
  79. uint8_t *(*aes_enc[])[5] = {aes_ecb, aes_cbc, aes_ctr, aes_cbc, aes_ofb, aes_cfb8};
  80. crypto_aes_req_ctx_t *aes_ctx = NULL;
  81. aes_ctx = (crypto_aes_req_ctx_t *)hal_malloc_align(sizeof(crypto_aes_req_ctx_t), CE_ALIGN_SIZE);
  82. if (aes_ctx == NULL) {
  83. printf (" malloc data buffer fail\n");
  84. return -1;
  85. }
  86. memset(aes_ctx, 0x0, sizeof(crypto_aes_req_ctx_t));
  87. aes_ctx->dst_buffer = (u8 *)hal_malloc_align(512, CE_ALIGN_SIZE);
  88. if (aes_ctx->dst_buffer == NULL) {
  89. printf (" malloc dest buffer fail\n");
  90. ret = -1;
  91. goto out;
  92. }
  93. for (m = AES_MODE_ECB; m < AES_MODE_CFB + 1; m++) {
  94. for (i = 0; i < sizeof(aes_key_len)/sizeof(aes_key_len[0]); i++) {
  95. for (j = 0; j < sizeof(aes_src)/sizeof(aes_src[0]); j++) {
  96. /* aes encrypt */
  97. aes_ctx->src_buffer = aes_src[j];
  98. aes_ctx->src_length = aes_src_len[j];
  99. aes_ctx->key = aes_key[i];
  100. aes_ctx->key_length = aes_key_len[i];
  101. if (m == AES_MODE_ECB)
  102. aes_ctx->iv = NULL;
  103. else
  104. aes_ctx->iv = aes_iv;
  105. if (m == AES_MODE_CTR) {
  106. memset(iv_next, 0, AES_BLOCK_SIZE);
  107. aes_ctx->iv_next = iv_next;
  108. } else
  109. aes_ctx->iv_next = NULL;
  110. if (m == AES_MODE_CFB)
  111. aes_ctx->bitwidth = 8;
  112. else
  113. aes_ctx->bitwidth = 0;
  114. aes_ctx->mode = m;
  115. aes_ctx->dir = AES_DIR_ENCRYPT;
  116. aes_ctx->dst_length = CE_ROUND_UP(aes_ctx->src_length, AES_BLOCK_SIZE);
  117. printf("###############AES, mode: %d, ksize %d, src len %d, begin###############\n", m, aes_key_len[i], aes_src_len[j]);
  118. ret = do_aes_crypto(aes_ctx);
  119. if (ret < 0) {
  120. printf ("aes encrypt fail %d\n", ret);
  121. goto out;
  122. }
  123. // for ecb/cbc/cts, enc data len should be 16 bytes aligned
  124. if (m == AES_MODE_ECB || m == AES_MODE_CBC || m == AES_MODE_CTS)
  125. enc_len = aes_ctx->dst_length;
  126. else
  127. enc_len = aes_src_len[j];
  128. // openssl enc do not support cts, so create enc data manually.
  129. if (m == AES_MODE_CTS){
  130. enc_buffer = (uint8_t *)hal_malloc(enc_len);
  131. if (enc_buffer == NULL) {
  132. printf ("malloc ctr buffer fail\n");
  133. ret = -1;
  134. goto out;
  135. }
  136. blk_num = enc_len / AES_BLOCK_SIZE;
  137. if (blk_num > 1) {
  138. if (blk_num > 2)
  139. memcpy(enc_buffer, aes_enc[m - AES_MODE_ECB][i][j], (blk_num - 2) * AES_BLOCK_SIZE);
  140. memcpy(enc_buffer + (blk_num - 2) * AES_BLOCK_SIZE,
  141. aes_enc[m - AES_MODE_ECB][i][j] + (blk_num - 1) * AES_BLOCK_SIZE,
  142. AES_BLOCK_SIZE);
  143. memcpy(enc_buffer + (blk_num - 1) * AES_BLOCK_SIZE,
  144. aes_enc[m - AES_MODE_ECB][i][j] + (blk_num - 2) * AES_BLOCK_SIZE,
  145. AES_BLOCK_SIZE);
  146. } else {
  147. memcpy(enc_buffer, aes_enc[m - AES_MODE_ECB][i][j], enc_len);
  148. }
  149. } else
  150. enc_buffer = aes_enc[m - AES_MODE_ECB][i][j];
  151. if (memcmp(aes_ctx->dst_buffer, enc_buffer, enc_len) != 0) {
  152. ce_dump("want data: ", enc_buffer, enc_len, 16);
  153. ce_dump("calc data: ", aes_ctx->dst_buffer, enc_len, 16);
  154. printf("###############AES ENC, mode: %d, ksize %d, src len %d, fail###############\n", m, aes_key_len[i], aes_src_len[j]);
  155. ret = -1;
  156. goto out;
  157. }
  158. /* aes decrypt */
  159. memset(aes_ctx->dst_buffer, 0x0, aes_ctx->dst_length);
  160. aes_ctx->dir = AES_DIR_DECRYPT;
  161. aes_ctx->src_buffer = enc_buffer;
  162. aes_ctx->src_length = enc_len;
  163. ret = do_aes_crypto(aes_ctx);
  164. if (ret < 0) {
  165. printf("aes decrypt fail %d\n", ret);
  166. goto out;
  167. }
  168. if (memcmp(aes_ctx->dst_buffer, aes_src[j], aes_src_len[j]) != 0) {
  169. ce_dump("want data: ", aes_src[j], aes_src_len[j], 16);
  170. ce_dump("calc data: ", aes_ctx->dst_buffer, aes_src_len[j], 16);
  171. printf("###############AES DEC, mode: %d, ksize %d, src len %d, fail###############\n", m, aes_key_len[i], aes_src_len[j]);
  172. ret = -1;
  173. goto out;
  174. }
  175. if (m == AES_MODE_CTS) {
  176. if (enc_buffer)
  177. hal_free(enc_buffer);
  178. }
  179. printf("###############AES, mode: %d, ksize %d, src len %d, pass###############\n\n\n", m, aes_key_len[i], aes_src_len[j]);
  180. }
  181. }
  182. }
  183. out:
  184. if (aes_ctx->dst_buffer != NULL) {
  185. hal_free_align(aes_ctx->dst_buffer);
  186. }
  187. if (m == AES_MODE_CTS) {
  188. if (enc_buffer)
  189. hal_free(enc_buffer);
  190. }
  191. hal_free_align(aes_ctx);
  192. return ret;
  193. }
  194. int hash_test(void)
  195. {
  196. int i = 0;
  197. int j = 0;
  198. uint8_t *dst_data = NULL;
  199. //uint32_t data_size = 512; SHA_MAX_DIGEST_SIZE
  200. uint32_t data_size = SHA_MAX_DIGEST_SIZE;
  201. uint32_t hash_length = 0;
  202. int ret = -1;
  203. uint8_t *(*hash_digest[]) = {hash_md5, hash_sha1, hash_sha224, hash_sha256, hash_sha384, hash_sha512};
  204. crypto_hash_req_ctx_t *hash_ctx = NULL;
  205. hash_ctx = (crypto_hash_req_ctx_t *)hal_malloc_align(sizeof(crypto_hash_req_ctx_t), CE_ALIGN_SIZE);
  206. if (hash_ctx == NULL) {
  207. printf (" malloc hash_ctx fail\n");
  208. ret = -1;
  209. goto out;
  210. }
  211. /*malloc dst buf*/
  212. dst_data = (u8 *)hal_malloc_align(data_size, CE_ALIGN_SIZE);
  213. if (dst_data == NULL) {
  214. printf (" malloc dst buffer fail\n");
  215. ret = -1;
  216. goto out;
  217. }
  218. for (i = HASH_METHOD_MD5; i < HASH_METHOD_SHA512 + 1; i++) {
  219. for (j = 0; j < sizeof(hash_src_len)/sizeof(hash_src_len[0]); j++) {
  220. hash_ctx->src_buffer = hash_src[j];
  221. hash_ctx->src_length = hash_src_len[j];
  222. memset(dst_data, 0x0, data_size);
  223. hash_ctx->dst_buffer = dst_data;
  224. hash_ctx->type = i;
  225. hash_ctx->md_size = 0;
  226. switch (i) {
  227. case HASH_METHOD_MD5:
  228. hash_ctx->dst_length = MD5_DIGEST_SIZE;
  229. hash_length = MD5_DIGEST_SIZE;
  230. break;
  231. case HASH_METHOD_SHA1:
  232. hash_ctx->dst_length = SHA1_DIGEST_SIZE;
  233. hash_length = SHA1_DIGEST_SIZE;
  234. break;
  235. case HASH_METHOD_SHA224:
  236. hash_ctx->dst_length = SHA256_DIGEST_SIZE;
  237. hash_length = SHA224_DIGEST_SIZE;
  238. break;
  239. case HASH_METHOD_SHA256:
  240. hash_ctx->dst_length = SHA256_DIGEST_SIZE;
  241. hash_length = SHA256_DIGEST_SIZE;
  242. break;
  243. case HASH_METHOD_SHA384:
  244. hash_ctx->dst_length = SHA512_DIGEST_SIZE;
  245. hash_length = SHA384_DIGEST_SIZE;
  246. break;
  247. case HASH_METHOD_SHA512:
  248. hash_ctx->dst_length = SHA512_DIGEST_SIZE;
  249. hash_length = SHA512_DIGEST_SIZE;
  250. break;
  251. default:
  252. break;
  253. }
  254. printf("############hash type: %d, src len: %d, begin#############\n", i, hash_src_len[j]);
  255. ret = do_hash_crypto(hash_ctx);
  256. if (ret < 0) {
  257. printf ("do_hash_crypto fail\n");
  258. goto out;
  259. }
  260. if (memcmp(hash_ctx->dst_buffer, hash_digest[i - HASH_METHOD_MD5][j], hash_length) == 0) {
  261. printf("############hash type: %d, src len: %d, pass#############\n\n\n", i, hash_src_len[j]);
  262. } else {
  263. ce_dump("want digest: ", hash_digest[i - HASH_METHOD_MD5][j], hash_length, 16);
  264. ce_dump("calc digest: ", hash_ctx->dst_buffer, hash_length, 16);
  265. printf("############hash type: %d, src len: %d, fail#############\n\n\n", i, hash_src_len[j]);
  266. ret = -1;
  267. goto out;
  268. }
  269. }
  270. }
  271. out:
  272. if (hash_ctx != NULL) {
  273. hal_free_align(hash_ctx);
  274. }
  275. if (dst_data != NULL) {
  276. hal_free_align(dst_data);
  277. }
  278. return ret;
  279. }
  280. int rng_test(void)
  281. {
  282. int ret = 0;
  283. int i = 0;
  284. uint8_t *rng_buf = NULL;
  285. uint32_t rng_size[] = {16, 31, 32, 8100};
  286. uint8_t key[24] = {
  287. 0xa1, 0xb7, 0x78, 0xf7, 0xbf, 0x2c, 0xfa, 0xad, 0x6a, 0x46, 0x79, 0xc2, 0xd2, 0x9c, 0x45, 0x1f,
  288. 0x3f, 0xcb, 0xef, 0xa5, 0x4e, 0x0e, 0xc3, 0x51
  289. };
  290. uint32_t key_len = 24;
  291. crypto_rng_req_ctx_t *rng_ctx = NULL;
  292. rng_ctx = (crypto_rng_req_ctx_t *)hal_malloc(sizeof(crypto_rng_req_ctx_t));
  293. if (rng_ctx == NULL) {
  294. printf (" malloc rng ctx fail\n");
  295. ret = -1;
  296. goto out;
  297. }
  298. /*malloc trng buf*/
  299. rng_buf = (u8 *)hal_malloc(8192);
  300. if (rng_buf == NULL) {
  301. printf ("malloc rng buffer fail\n");
  302. ret = -1;
  303. goto out;
  304. }
  305. /*TRNG test*/
  306. for (i = 0; i < sizeof(rng_size)/sizeof(uint32_t); i++) {
  307. printf("############TRNG, len: %d, begin#############\n", rng_size[i]);
  308. memset(rng_buf, 0, 8192);
  309. rng_ctx->rng_buf = rng_buf;
  310. rng_ctx->rng_len = rng_size[i];
  311. rng_ctx->mode = 0x30; /*CE_METHOD_TRNG*/
  312. rng_ctx->key = NULL;
  313. rng_ctx->key_len = 0;
  314. ret = do_rng_gen(rng_ctx);
  315. if (ret < 0) {
  316. printf("############TRNG, len: %d, fail#############\n\n\n", rng_size[i]);
  317. goto out;
  318. }
  319. #if 0
  320. if (rng_size[i] < 100)
  321. ce_dump("trng:", rng_buf, rng_size[i], 16);
  322. #endif
  323. printf("############TRNG, len: %d, pass#############\n\n\n", rng_size[i]);
  324. }
  325. /*PRNG test*/
  326. for (i = 0; i < sizeof(rng_size)/sizeof(uint32_t); i++) {
  327. printf("############PRNG, len: %d, begin#############\n", rng_size[i]);
  328. memset(rng_buf, 0, 8192);
  329. rng_ctx->rng_buf = rng_buf;
  330. rng_ctx->rng_len = rng_size[i];
  331. rng_ctx->mode = 0x31; /*CE_METHOD_PRNG*/
  332. rng_ctx->key = key;
  333. rng_ctx->key_len = key_len;
  334. ret = do_rng_gen(rng_ctx);
  335. if (ret < 0) {
  336. printf("############PRNG, len: %d, fail#############\n\n\n", rng_size[i]);
  337. goto out;
  338. }
  339. #if 0
  340. if (rng_size[i] < 100)
  341. ce_dump("prng:", rng_buf, rng_size[i], 16);
  342. #endif
  343. printf("############PRNG, len: %d, pass#############\n\n\n", rng_size[i]);
  344. }
  345. out:
  346. if (rng_ctx)
  347. hal_free(rng_ctx);
  348. if (rng_buf)
  349. hal_free(rng_buf);
  350. return ret;
  351. }
  352. int rsa_test(void)
  353. {
  354. int ret = 0;
  355. int i = 0;
  356. uint8_t dst_buffer[256] = {0};
  357. crypto_rsa_req_ctx_t *rsa_ctx = NULL;
  358. rsa_ctx = (crypto_rsa_req_ctx_t *)hal_malloc_align(sizeof(crypto_rsa_req_ctx_t), CE_ALIGN_SIZE);
  359. if (rsa_ctx == NULL) {
  360. printf (" malloc rsa ctx fail\n");
  361. return -1;
  362. }
  363. /*rsa enc and dec*/
  364. for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
  365. /* enc with public key*/
  366. printf("############RSA ENC/DEC, len: %d, begin#############\n", rsa_bitwidth[i]);
  367. memset(dst_buffer, 0, 256);
  368. memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
  369. rsa_ctx->key_n = rsa_keyn[i];
  370. rsa_ctx->n_len = rsa_bitwidth[i] / 8;
  371. rsa_ctx->key_e = rsa_keye[i];
  372. rsa_ctx->e_len = rsa_bitwidth[i] / 8;
  373. rsa_ctx->key_d = 0;
  374. rsa_ctx->d_len = 0;
  375. rsa_ctx->src_buffer = rsa_src[i];
  376. rsa_ctx->src_length = rsa_bitwidth[i] / 8;
  377. rsa_ctx->dst_buffer = dst_buffer;
  378. rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
  379. rsa_ctx->dir = 0;
  380. rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
  381. rsa_ctx->bitwidth = rsa_bitwidth[i];
  382. ret = do_rsa_crypto(rsa_ctx);
  383. if (ret < 0) {
  384. printf ("do rsa crypto failed: %d\n", ret);
  385. goto out;
  386. }
  387. ret = memcmp(rsa_ctx->dst_buffer, rsa_enc[i], rsa_bitwidth[i] / 8);
  388. if (ret) {
  389. printf("rsa encrypt failed\n");
  390. ce_dump("want data: ", rsa_enc[i], rsa_bitwidth[i] / 8, 16);
  391. ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
  392. printf("############RSA ENC, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
  393. goto out;
  394. }
  395. /* dec with private key */
  396. memset(dst_buffer, 0, 256);
  397. memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
  398. rsa_ctx->key_n = rsa_keyn[i];
  399. rsa_ctx->n_len = rsa_bitwidth[i] / 8;
  400. rsa_ctx->key_e = 0;
  401. rsa_ctx->e_len = 0;
  402. rsa_ctx->key_d = rsa_keyd[i];
  403. rsa_ctx->d_len = rsa_bitwidth[i] / 8;
  404. rsa_ctx->src_buffer = rsa_enc[i];
  405. rsa_ctx->src_length = rsa_bitwidth[i] / 8;
  406. rsa_ctx->dst_buffer = dst_buffer;
  407. rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
  408. rsa_ctx->dir = 0;
  409. rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
  410. rsa_ctx->bitwidth = rsa_bitwidth[i];
  411. ret = do_rsa_crypto(rsa_ctx);
  412. if (ret < 0) {
  413. printf ("do rsa crypto failed: %d\n", ret);
  414. goto out;
  415. }
  416. ret = memcmp(rsa_ctx->dst_buffer, rsa_src[i], rsa_bitwidth[i] / 8);
  417. if (ret) {
  418. printf("rsa decrypt failed\n");
  419. ce_dump("want data: ", rsa_src[i], rsa_bitwidth[i] / 8, 16);
  420. ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
  421. printf("############RSA DEC, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
  422. goto out;
  423. }
  424. printf("############RSA ENC/DEC, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
  425. }
  426. /* rsa sign/verify sha256 value */
  427. for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
  428. /* sign with private key */
  429. printf("############RSA SIGN/VERIFY SHA256, len: %d, begin#############\n", rsa_bitwidth[i]);
  430. memset(dst_buffer, 0, 256);
  431. memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
  432. rsa_ctx->key_n = rsa_keyn[i];
  433. rsa_ctx->n_len = rsa_bitwidth[i] / 8;
  434. rsa_ctx->key_e = 0;
  435. rsa_ctx->e_len = 0;
  436. rsa_ctx->key_d = rsa_keyd[i];
  437. rsa_ctx->d_len = rsa_bitwidth[i] / 8;
  438. rsa_ctx->src_buffer = rsa_sha256_raw[i];
  439. rsa_ctx->src_length = rsa_bitwidth[i] / 8;
  440. rsa_ctx->dst_buffer = dst_buffer;
  441. rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
  442. rsa_ctx->dir = 0;
  443. rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
  444. rsa_ctx->bitwidth = rsa_bitwidth[i];
  445. ret = do_rsa_crypto(rsa_ctx);
  446. if (ret < 0) {
  447. printf ("do rsa crypto failed: %d\n", ret);
  448. goto out;
  449. }
  450. ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256_sign[i], rsa_bitwidth[i] / 8);
  451. if (ret) {
  452. printf("rsa encrypt failed\n");
  453. ce_dump("want data: ", rsa_sha256_sign[i], rsa_bitwidth[i] / 8, 16);
  454. ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
  455. printf("############RSA SIGN SHA256, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
  456. //goto out;
  457. }
  458. /* verify with public key */
  459. memset(dst_buffer, 0, 256);
  460. memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
  461. rsa_ctx->key_n = rsa_keyn[i];
  462. rsa_ctx->n_len = rsa_bitwidth[i] / 8;
  463. rsa_ctx->key_e = rsa_keye[i];
  464. rsa_ctx->e_len = rsa_bitwidth[i] / 8;
  465. rsa_ctx->key_d = 0;
  466. rsa_ctx->d_len = 0;
  467. rsa_ctx->src_buffer = rsa_sha256_sign[i];
  468. rsa_ctx->src_length = rsa_bitwidth[i] / 8;
  469. rsa_ctx->dst_buffer = dst_buffer;
  470. rsa_ctx->dst_length = 256 / 8;
  471. rsa_ctx->dir = 0;
  472. rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
  473. rsa_ctx->bitwidth = rsa_bitwidth[i];
  474. ret = do_rsa_crypto(rsa_ctx);
  475. if (ret < 0) {
  476. printf ("do rsa crypto failed: %d\n", ret);
  477. goto out;
  478. }
  479. ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256[i], 256 / 8);
  480. if (ret) {
  481. printf("rsa decrypt failed\n");
  482. ce_dump("want data: ", rsa_sha256[i], 256 / 8, 16);
  483. ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
  484. printf("############RSA VERIFY SHA256, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
  485. goto out;
  486. }
  487. printf("############RSA SIGN/VERIFY SHA256, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
  488. }
  489. /* rsa sign/verify */
  490. for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
  491. /* sign with private key */
  492. printf("############RSA SIGN/VERIFY, len: %d, begin#############\n", rsa_bitwidth[i]);
  493. memset(dst_buffer, 0, 256);
  494. memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
  495. rsa_ctx->key_n = rsa_keyn[i];
  496. rsa_ctx->n_len = rsa_bitwidth[i] / 8;
  497. rsa_ctx->key_e = 0;
  498. rsa_ctx->e_len = 0;
  499. rsa_ctx->key_d = rsa_keyd[i];
  500. rsa_ctx->d_len = rsa_bitwidth[i] / 8;
  501. rsa_ctx->src_buffer = rsa_sign_raw[i];
  502. rsa_ctx->src_length = rsa_bitwidth[i] / 8;
  503. rsa_ctx->dst_buffer = dst_buffer;
  504. rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
  505. rsa_ctx->dir = 0;
  506. rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
  507. rsa_ctx->bitwidth = rsa_bitwidth[i];
  508. ret = do_rsa_crypto(rsa_ctx);
  509. if (ret < 0) {
  510. printf ("do rsa crypto failed: %d\n", ret);
  511. goto out;
  512. }
  513. ret = memcmp(rsa_ctx->dst_buffer, rsa_signature[i], rsa_bitwidth[i] / 8);
  514. if (ret) {
  515. printf("rsa encrypt failed\n");
  516. ce_dump("want data: ", rsa_signature[i], rsa_bitwidth[i] / 8, 16);
  517. ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
  518. printf("############RSA SIGN, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
  519. //goto out;
  520. }
  521. /* verify with public key */
  522. memset(dst_buffer, 0, 256);
  523. memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
  524. rsa_ctx->key_n = rsa_keyn[i];
  525. rsa_ctx->n_len = rsa_bitwidth[i] / 8;
  526. rsa_ctx->key_e = rsa_keye[i];
  527. rsa_ctx->e_len = rsa_bitwidth[i] / 8;
  528. rsa_ctx->key_d = 0;
  529. rsa_ctx->d_len = 0;
  530. rsa_ctx->src_buffer = rsa_signature[i];
  531. rsa_ctx->src_length = rsa_bitwidth[i] / 8;
  532. rsa_ctx->dst_buffer = dst_buffer;
  533. rsa_ctx->dst_length = 256 / 8;
  534. rsa_ctx->dir = 0;
  535. rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
  536. rsa_ctx->bitwidth = rsa_bitwidth[i];
  537. ret = do_rsa_crypto(rsa_ctx);
  538. if (ret < 0) {
  539. printf ("do rsa crypto failed: %d\n", ret);
  540. goto out;
  541. }
  542. ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256[i], 256 / 8);
  543. if (ret) {
  544. printf("rsa decrypt failed\n");
  545. ce_dump("want data: ", rsa_sha256[i], 256 / 8, 16);
  546. ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
  547. printf("############RSA VERIFY, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
  548. goto out;
  549. }
  550. printf("############RSA SIGN/VERIFY, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
  551. }
  552. out:
  553. if (rsa_ctx)
  554. hal_free_align(rsa_ctx);
  555. return ret;
  556. }
  557. int cmd_test_ce(int argc, const char *argv[])
  558. {
  559. int ret = 0;
  560. if (argc != 2) {
  561. printf("Parameter number Error!\n");
  562. printf("Usage: hal_ce <aes|hash|rsa|rng>\n");
  563. return -1;
  564. }
  565. sunxi_ce_init();
  566. if (strcmp(argv[1], "aes") == 0)
  567. ret = aes_test();
  568. else if (strcmp(argv[1], "hash") == 0)
  569. ret = hash_test();
  570. else if (strcmp(argv[1], "rsa") == 0)
  571. ret = rsa_test();
  572. else if (strcmp(argv[1], "rng") == 0)
  573. ret = rng_test();
  574. else {
  575. printf("Parameter Error!\n");
  576. printf("Usage: hal_ce <aes|hash|rsa|rng>\n");
  577. ret = -1;
  578. }
  579. sunxi_ce_uninit();
  580. return ret;
  581. }
  582. FINSH_FUNCTION_EXPORT_CMD(cmd_test_ce, hal_ce, tina rtos ce test demo)