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 
33 #include <stdio.h>
34 #include <string.h>
35 #include <unistd.h>
36 
37 #include <hal_cmd.h>
38 #include <hal_mem.h>
39 #include <sunxi_hal_ce.h>
40 
41 #include "test_ce.h"
42 
43 #define AES_MODE_ECB        (0)
44 #define AES_MODE_CBC        (1)
45 #define AES_MODE_CTR        (2)
46 #define AES_MODE_CTS        (3)
47 #define AES_MODE_OFB        (4)
48 #define AES_MODE_CFB        (5)
49 
50 #define AES_DIR_ENCRYPT     (0)
51 #define AES_DIR_DECRYPT     (1)
52 
53 #define HASH_METHOD_MD5     (16)
54 #define HASH_METHOD_SHA1    (17)
55 #define HASH_METHOD_SHA224  (18)
56 #define HASH_METHOD_SHA256  (19)
57 #define HASH_METHOD_SHA384  (20)
58 #define HASH_METHOD_SHA512  (21)
59 
ce_dump(char * str,unsigned char * data,int len,int align)60 void ce_dump(char *str,unsigned char *data, int len, int align)
61 {
62     int i = 0;
63     if(str)
64         printf("\n%s: ",str);
65     for(i = 0; i<len; i++)
66     {
67         if((i%align) == 0)
68         {
69             printf("\n");
70             printf("0x08%x: ", data + i * align);
71         }
72         printf("%02x ",*(data++));
73     }
74     printf("\n");
75 }
76 
aes_test(void)77 int aes_test(void)
78 {
79     int ret = -1;
80     int i = 0;
81     int j = 0;
82     int m = 0;
83     uint8_t *enc_buffer = 0;
84     uint32_t enc_len = 0;
85     uint32_t blk_num = 0;
86     uint8_t iv_next[AES_BLOCK_SIZE] = {0};
87     uint8_t *(*aes_enc[])[5] = {aes_ecb, aes_cbc, aes_ctr, aes_cbc, aes_ofb, aes_cfb8};
88     crypto_aes_req_ctx_t *aes_ctx = NULL;
89 
90     aes_ctx = (crypto_aes_req_ctx_t *)hal_malloc_align(sizeof(crypto_aes_req_ctx_t), CE_ALIGN_SIZE);
91     if (aes_ctx == NULL) {
92         printf (" malloc data buffer fail\n");
93         return -1;
94     }
95     memset(aes_ctx, 0x0, sizeof(crypto_aes_req_ctx_t));
96 
97     aes_ctx->dst_buffer = (u8 *)hal_malloc_align(512, CE_ALIGN_SIZE);
98     if (aes_ctx->dst_buffer == NULL) {
99         printf (" malloc dest buffer fail\n");
100         ret = -1;
101         goto out;
102     }
103 
104     for (m = AES_MODE_ECB; m < AES_MODE_CFB + 1; m++) {
105         for (i = 0; i < sizeof(aes_key_len)/sizeof(aes_key_len[0]); i++) {
106             for (j = 0; j < sizeof(aes_src)/sizeof(aes_src[0]); j++) {
107                 /* aes encrypt */
108                 aes_ctx->src_buffer = aes_src[j];
109                 aes_ctx->src_length = aes_src_len[j];
110                 aes_ctx->key = aes_key[i];
111                 aes_ctx->key_length = aes_key_len[i];
112                 if (m == AES_MODE_ECB)
113                     aes_ctx->iv = NULL;
114                 else
115                     aes_ctx->iv = aes_iv;
116                 if (m == AES_MODE_CTR) {
117                     memset(iv_next, 0, AES_BLOCK_SIZE);
118                     aes_ctx->iv_next = iv_next;
119                 } else
120                     aes_ctx->iv_next = NULL;
121                 if (m == AES_MODE_CFB)
122                     aes_ctx->bitwidth = 8;
123                 else
124                     aes_ctx->bitwidth = 0;
125                 aes_ctx->mode = m;
126                 aes_ctx->dir = AES_DIR_ENCRYPT;
127                 aes_ctx->dst_length = CE_ROUND_UP(aes_ctx->src_length, AES_BLOCK_SIZE);
128 
129                 printf("###############AES, mode: %d, ksize %d, src len %d, begin###############\n", m, aes_key_len[i], aes_src_len[j]);
130 
131                 ret = do_aes_crypto(aes_ctx);
132                 if (ret < 0) {
133                     printf ("aes encrypt fail %d\n", ret);
134                     goto out;
135                 }
136 
137                 // for ecb/cbc/cts, enc data len should be 16 bytes aligned
138                 if (m == AES_MODE_ECB || m == AES_MODE_CBC || m == AES_MODE_CTS)
139                     enc_len = aes_ctx->dst_length;
140                 else
141                     enc_len = aes_src_len[j];
142 
143                 // openssl enc do not support cts, so create enc data manually.
144                 if (m == AES_MODE_CTS){
145                     enc_buffer = (uint8_t *)hal_malloc(enc_len);
146                     if (enc_buffer == NULL) {
147                         printf ("malloc ctr buffer fail\n");
148                         ret = -1;
149                         goto out;
150                     }
151 
152                     blk_num = enc_len / AES_BLOCK_SIZE;
153                     if (blk_num > 1) {
154                         if (blk_num > 2)
155                             memcpy(enc_buffer, aes_enc[m - AES_MODE_ECB][i][j], (blk_num - 2) * AES_BLOCK_SIZE);
156                         memcpy(enc_buffer + (blk_num - 2) * AES_BLOCK_SIZE,
157                                 aes_enc[m - AES_MODE_ECB][i][j] + (blk_num - 1) * AES_BLOCK_SIZE,
158                                 AES_BLOCK_SIZE);
159                         memcpy(enc_buffer + (blk_num - 1) * AES_BLOCK_SIZE,
160                                 aes_enc[m - AES_MODE_ECB][i][j] + (blk_num - 2) * AES_BLOCK_SIZE,
161                                 AES_BLOCK_SIZE);
162                     } else {
163                         memcpy(enc_buffer, aes_enc[m - AES_MODE_ECB][i][j], enc_len);
164                     }
165                 } else
166                     enc_buffer = aes_enc[m - AES_MODE_ECB][i][j];
167 
168                 if (memcmp(aes_ctx->dst_buffer, enc_buffer, enc_len) != 0) {
169                     ce_dump("want data: ", enc_buffer, enc_len, 16);
170                     ce_dump("calc data: ", aes_ctx->dst_buffer, enc_len, 16);
171                     printf("###############AES ENC, 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 
176                 /* aes decrypt */
177                 memset(aes_ctx->dst_buffer, 0x0, aes_ctx->dst_length);
178                 aes_ctx->dir = AES_DIR_DECRYPT;
179                 aes_ctx->src_buffer = enc_buffer;
180                 aes_ctx->src_length = enc_len;
181 
182                 ret = do_aes_crypto(aes_ctx);
183                 if (ret < 0) {
184                     printf("aes decrypt fail %d\n", ret);
185                     goto out;
186                 }
187 
188                 if (memcmp(aes_ctx->dst_buffer, aes_src[j], aes_src_len[j]) != 0) {
189                     ce_dump("want data: ", aes_src[j], aes_src_len[j], 16);
190                     ce_dump("calc data: ", aes_ctx->dst_buffer, aes_src_len[j], 16);
191                     printf("###############AES DEC, mode: %d, ksize %d, src len %d, fail###############\n", m, aes_key_len[i], aes_src_len[j]);
192                     ret = -1;
193                     goto out;
194                 }
195 
196                 if (m == AES_MODE_CTS) {
197                     if (enc_buffer)
198                         hal_free(enc_buffer);
199                 }
200 
201                 printf("###############AES, mode: %d, ksize %d, src len %d, pass###############\n\n\n", m, aes_key_len[i], aes_src_len[j]);
202             }
203         }
204     }
205 
206 out:
207     if (aes_ctx->dst_buffer != NULL) {
208         hal_free_align(aes_ctx->dst_buffer);
209     }
210     if (m == AES_MODE_CTS) {
211         if (enc_buffer)
212             hal_free(enc_buffer);
213     }
214 
215     hal_free_align(aes_ctx);
216 
217     return ret;
218 }
219 
hash_test(void)220 int hash_test(void)
221 {
222     int i = 0;
223     int j = 0;
224     uint8_t *dst_data = NULL;
225     //uint32_t data_size = 512; SHA_MAX_DIGEST_SIZE
226     uint32_t data_size = SHA_MAX_DIGEST_SIZE;
227     uint32_t hash_length = 0;
228     int ret = -1;
229     uint8_t *(*hash_digest[]) = {hash_md5, hash_sha1, hash_sha224, hash_sha256, hash_sha384, hash_sha512};
230     crypto_hash_req_ctx_t *hash_ctx = NULL;
231 
232     hash_ctx = (crypto_hash_req_ctx_t *)hal_malloc_align(sizeof(crypto_hash_req_ctx_t), CE_ALIGN_SIZE);
233     if (hash_ctx == NULL) {
234         printf (" malloc hash_ctx fail\n");
235         ret = -1;
236         goto out;
237     }
238 
239     /*malloc dst buf*/
240     dst_data = (u8 *)hal_malloc_align(data_size, CE_ALIGN_SIZE);
241     if (dst_data == NULL) {
242         printf (" malloc dst buffer fail\n");
243         ret = -1;
244         goto out;
245     }
246 
247     for (i = HASH_METHOD_MD5; i < HASH_METHOD_SHA512 + 1; i++) {
248         for (j = 0; j < sizeof(hash_src_len)/sizeof(hash_src_len[0]); j++) {
249             hash_ctx->src_buffer = hash_src[j];
250             hash_ctx->src_length = hash_src_len[j];
251             memset(dst_data, 0x0, data_size);
252             hash_ctx->dst_buffer = dst_data;
253             hash_ctx->type = i;
254             hash_ctx->md_size = 0;
255             switch (i) {
256                 case HASH_METHOD_MD5:
257                     hash_ctx->dst_length = MD5_DIGEST_SIZE;
258                     hash_length = MD5_DIGEST_SIZE;
259                     break;
260                 case HASH_METHOD_SHA1:
261                     hash_ctx->dst_length = SHA1_DIGEST_SIZE;
262                     hash_length = SHA1_DIGEST_SIZE;
263                     break;
264                 case HASH_METHOD_SHA224:
265                     hash_ctx->dst_length = SHA256_DIGEST_SIZE;
266                     hash_length = SHA224_DIGEST_SIZE;
267                     break;
268                 case HASH_METHOD_SHA256:
269                     hash_ctx->dst_length = SHA256_DIGEST_SIZE;
270                     hash_length = SHA256_DIGEST_SIZE;
271                     break;
272                 case HASH_METHOD_SHA384:
273                     hash_ctx->dst_length = SHA512_DIGEST_SIZE;
274                     hash_length = SHA384_DIGEST_SIZE;
275                     break;
276                 case HASH_METHOD_SHA512:
277                     hash_ctx->dst_length = SHA512_DIGEST_SIZE;
278                     hash_length = SHA512_DIGEST_SIZE;
279                     break;
280                 default:
281                     break;
282             }
283 
284             printf("############hash type: %d, src len: %d, begin#############\n", i, hash_src_len[j]);
285 
286             ret = do_hash_crypto(hash_ctx);
287             if (ret < 0) {
288                 printf ("do_hash_crypto fail\n");
289                 goto out;
290             }
291 
292             if (memcmp(hash_ctx->dst_buffer, hash_digest[i - HASH_METHOD_MD5][j], hash_length) == 0) {
293                 printf("############hash type: %d, src len: %d, pass#############\n\n\n", i, hash_src_len[j]);
294             } else {
295                 ce_dump("want digest: ", hash_digest[i - HASH_METHOD_MD5][j], hash_length, 16);
296                 ce_dump("calc digest: ", hash_ctx->dst_buffer, hash_length, 16);
297                 printf("############hash type: %d, src len: %d, fail#############\n\n\n", i, hash_src_len[j]);
298                 ret = -1;
299                 goto out;
300             }
301         }
302     }
303 
304 out:
305     if (hash_ctx != NULL) {
306         hal_free_align(hash_ctx);
307     }
308 
309     if (dst_data != NULL) {
310         hal_free_align(dst_data);
311     }
312 
313     return ret;
314 }
315 
316 
rng_test(void)317 int rng_test(void)
318 {
319     int ret = 0;
320     int i = 0;
321     uint8_t *rng_buf = NULL;
322     uint32_t rng_size[] = {16, 31, 32, 8100};
323     uint8_t key[24] = {
324         0xa1, 0xb7, 0x78, 0xf7, 0xbf, 0x2c, 0xfa, 0xad, 0x6a, 0x46, 0x79, 0xc2, 0xd2, 0x9c, 0x45, 0x1f,
325         0x3f, 0xcb, 0xef, 0xa5, 0x4e, 0x0e, 0xc3, 0x51
326     };
327     uint32_t key_len = 24;
328     crypto_rng_req_ctx_t *rng_ctx = NULL;
329 
330     rng_ctx = (crypto_rng_req_ctx_t *)hal_malloc(sizeof(crypto_rng_req_ctx_t));
331     if (rng_ctx == NULL) {
332         printf (" malloc rng ctx fail\n");
333         ret = -1;
334         goto out;
335     }
336 
337     /*malloc trng buf*/
338     rng_buf = (u8 *)hal_malloc(8192);
339     if (rng_buf == NULL) {
340         printf ("malloc rng buffer fail\n");
341         ret = -1;
342         goto out;
343     }
344 
345     /*TRNG test*/
346     for (i = 0; i < sizeof(rng_size)/sizeof(uint32_t); i++) {
347         printf("############TRNG, len: %d, begin#############\n", rng_size[i]);
348         memset(rng_buf, 0, 8192);
349         rng_ctx->rng_buf = rng_buf;
350         rng_ctx->rng_len = rng_size[i];
351         rng_ctx->mode = 0x30; /*CE_METHOD_TRNG*/
352         rng_ctx->key = NULL;
353         rng_ctx->key_len = 0;
354         ret = do_rng_gen(rng_ctx);
355         if (ret < 0) {
356             printf("############TRNG, len: %d, fail#############\n\n\n", rng_size[i]);
357             goto out;
358         }
359 #if 0
360         if (rng_size[i] < 100)
361             ce_dump("trng:", rng_buf, rng_size[i], 16);
362 #endif
363         printf("############TRNG, len: %d, pass#############\n\n\n", rng_size[i]);
364     }
365 
366     /*PRNG test*/
367     for (i = 0; i < sizeof(rng_size)/sizeof(uint32_t); i++) {
368         printf("############PRNG, len: %d, begin#############\n", rng_size[i]);
369         memset(rng_buf, 0, 8192);
370         rng_ctx->rng_buf = rng_buf;
371         rng_ctx->rng_len = rng_size[i];
372         rng_ctx->mode = 0x31; /*CE_METHOD_PRNG*/
373         rng_ctx->key = key;
374         rng_ctx->key_len = key_len;
375         ret = do_rng_gen(rng_ctx);
376         if (ret < 0) {
377             printf("############PRNG, len: %d, fail#############\n\n\n", rng_size[i]);
378             goto out;
379         }
380 #if 0
381         if (rng_size[i] < 100)
382             ce_dump("prng:", rng_buf, rng_size[i], 16);
383 #endif
384         printf("############PRNG, len: %d, pass#############\n\n\n", rng_size[i]);
385     }
386 
387 out:
388     if (rng_ctx)
389         hal_free(rng_ctx);
390 
391     if (rng_buf)
392         hal_free(rng_buf);
393 
394     return ret;
395 }
396 
rsa_test(void)397 int rsa_test(void)
398 {
399     int ret = 0;
400     int i = 0;
401     uint8_t dst_buffer[256] = {0};
402     crypto_rsa_req_ctx_t *rsa_ctx = NULL;
403 
404     rsa_ctx = (crypto_rsa_req_ctx_t *)hal_malloc_align(sizeof(crypto_rsa_req_ctx_t), CE_ALIGN_SIZE);
405     if (rsa_ctx == NULL) {
406         printf (" malloc rsa ctx fail\n");
407         return -1;
408     }
409 
410     /*rsa enc and dec*/
411     for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
412         /* enc with public key*/
413         printf("############RSA ENC/DEC, len: %d, begin#############\n", rsa_bitwidth[i]);
414         memset(dst_buffer, 0, 256);
415         memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
416 
417         rsa_ctx->key_n = rsa_keyn[i];
418         rsa_ctx->n_len = rsa_bitwidth[i] / 8;
419         rsa_ctx->key_e = rsa_keye[i];
420         rsa_ctx->e_len = rsa_bitwidth[i] / 8;
421         rsa_ctx->key_d = 0;
422         rsa_ctx->d_len = 0;
423 
424         rsa_ctx->src_buffer = rsa_src[i];
425         rsa_ctx->src_length = rsa_bitwidth[i] / 8;
426         rsa_ctx->dst_buffer = dst_buffer;
427         rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
428 
429         rsa_ctx->dir = 0;
430         rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
431         rsa_ctx->bitwidth = rsa_bitwidth[i];
432 
433         ret = do_rsa_crypto(rsa_ctx);
434         if (ret < 0) {
435             printf ("do rsa crypto failed: %d\n", ret);
436             goto out;
437         }
438 
439         ret = memcmp(rsa_ctx->dst_buffer, rsa_enc[i], rsa_bitwidth[i] / 8);
440         if (ret) {
441             printf("rsa encrypt failed\n");
442             ce_dump("want data: ", rsa_enc[i], rsa_bitwidth[i] / 8, 16);
443             ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
444             printf("############RSA ENC, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
445             goto out;
446         }
447 
448         /* dec with private key */
449         memset(dst_buffer, 0, 256);
450         memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
451 
452         rsa_ctx->key_n = rsa_keyn[i];
453         rsa_ctx->n_len = rsa_bitwidth[i] / 8;
454         rsa_ctx->key_e = 0;
455         rsa_ctx->e_len = 0;
456         rsa_ctx->key_d = rsa_keyd[i];
457         rsa_ctx->d_len = rsa_bitwidth[i] / 8;
458 
459         rsa_ctx->src_buffer = rsa_enc[i];
460         rsa_ctx->src_length = rsa_bitwidth[i] / 8;
461         rsa_ctx->dst_buffer = dst_buffer;
462         rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
463 
464         rsa_ctx->dir = 0;
465         rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
466         rsa_ctx->bitwidth = rsa_bitwidth[i];
467 
468         ret = do_rsa_crypto(rsa_ctx);
469         if (ret < 0) {
470             printf ("do rsa crypto failed: %d\n", ret);
471             goto out;
472         }
473 
474         ret = memcmp(rsa_ctx->dst_buffer, rsa_src[i], rsa_bitwidth[i] / 8);
475         if (ret) {
476             printf("rsa decrypt failed\n");
477             ce_dump("want data: ", rsa_src[i], rsa_bitwidth[i] / 8, 16);
478             ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
479             printf("############RSA DEC, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
480             goto out;
481         }
482 
483         printf("############RSA ENC/DEC, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
484     }
485 
486     /* rsa sign/verify sha256 value */
487     for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
488         /* sign with private key */
489         printf("############RSA SIGN/VERIFY SHA256, len: %d, begin#############\n", rsa_bitwidth[i]);
490         memset(dst_buffer, 0, 256);
491         memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
492 
493         rsa_ctx->key_n = rsa_keyn[i];
494         rsa_ctx->n_len = rsa_bitwidth[i] / 8;
495         rsa_ctx->key_e = 0;
496         rsa_ctx->e_len = 0;
497         rsa_ctx->key_d = rsa_keyd[i];
498         rsa_ctx->d_len = rsa_bitwidth[i] / 8;
499 
500         rsa_ctx->src_buffer = rsa_sha256_raw[i];
501         rsa_ctx->src_length = rsa_bitwidth[i] / 8;
502         rsa_ctx->dst_buffer = dst_buffer;
503         rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
504 
505         rsa_ctx->dir = 0;
506         rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
507         rsa_ctx->bitwidth = rsa_bitwidth[i];
508 
509         ret = do_rsa_crypto(rsa_ctx);
510         if (ret < 0) {
511             printf ("do rsa crypto failed: %d\n", ret);
512             goto out;
513         }
514 
515         ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256_sign[i], rsa_bitwidth[i] / 8);
516         if (ret) {
517             printf("rsa encrypt failed\n");
518             ce_dump("want data: ", rsa_sha256_sign[i], rsa_bitwidth[i] / 8, 16);
519             ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
520             printf("############RSA SIGN SHA256, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
521             //goto out;
522         }
523 
524         /* verify with public key */
525         memset(dst_buffer, 0, 256);
526         memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
527 
528         rsa_ctx->key_n = rsa_keyn[i];
529         rsa_ctx->n_len = rsa_bitwidth[i] / 8;
530         rsa_ctx->key_e = rsa_keye[i];
531         rsa_ctx->e_len = rsa_bitwidth[i] / 8;
532         rsa_ctx->key_d = 0;
533         rsa_ctx->d_len = 0;
534 
535         rsa_ctx->src_buffer = rsa_sha256_sign[i];
536         rsa_ctx->src_length = rsa_bitwidth[i] / 8;
537         rsa_ctx->dst_buffer = dst_buffer;
538         rsa_ctx->dst_length = 256 / 8;
539 
540         rsa_ctx->dir = 0;
541         rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
542         rsa_ctx->bitwidth = rsa_bitwidth[i];
543 
544         ret = do_rsa_crypto(rsa_ctx);
545         if (ret < 0) {
546             printf ("do rsa crypto failed: %d\n", ret);
547             goto out;
548         }
549 
550         ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256[i], 256 / 8);
551         if (ret) {
552             printf("rsa decrypt failed\n");
553             ce_dump("want data: ", rsa_sha256[i], 256 / 8, 16);
554             ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
555             printf("############RSA VERIFY SHA256, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
556             goto out;
557         }
558 
559         printf("############RSA SIGN/VERIFY SHA256, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
560     }
561 
562     /* rsa sign/verify */
563     for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
564         /* sign with private key */
565         printf("############RSA SIGN/VERIFY, len: %d, begin#############\n", rsa_bitwidth[i]);
566         memset(dst_buffer, 0, 256);
567         memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
568 
569         rsa_ctx->key_n = rsa_keyn[i];
570         rsa_ctx->n_len = rsa_bitwidth[i] / 8;
571         rsa_ctx->key_e = 0;
572         rsa_ctx->e_len = 0;
573         rsa_ctx->key_d = rsa_keyd[i];
574         rsa_ctx->d_len = rsa_bitwidth[i] / 8;
575 
576         rsa_ctx->src_buffer = rsa_sign_raw[i];
577         rsa_ctx->src_length = rsa_bitwidth[i] / 8;
578         rsa_ctx->dst_buffer = dst_buffer;
579         rsa_ctx->dst_length = rsa_bitwidth[i] / 8;
580 
581         rsa_ctx->dir = 0;
582         rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
583         rsa_ctx->bitwidth = rsa_bitwidth[i];
584 
585         ret = do_rsa_crypto(rsa_ctx);
586         if (ret < 0) {
587             printf ("do rsa crypto failed: %d\n", ret);
588             goto out;
589         }
590 
591         ret = memcmp(rsa_ctx->dst_buffer, rsa_signature[i], rsa_bitwidth[i] / 8);
592         if (ret) {
593             printf("rsa encrypt failed\n");
594             ce_dump("want data: ", rsa_signature[i], rsa_bitwidth[i] / 8, 16);
595             ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
596             printf("############RSA SIGN, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
597             //goto out;
598         }
599 
600         /* verify with public key */
601         memset(dst_buffer, 0, 256);
602         memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));
603 
604         rsa_ctx->key_n = rsa_keyn[i];
605         rsa_ctx->n_len = rsa_bitwidth[i] / 8;
606         rsa_ctx->key_e = rsa_keye[i];
607         rsa_ctx->e_len = rsa_bitwidth[i] / 8;
608         rsa_ctx->key_d = 0;
609         rsa_ctx->d_len = 0;
610 
611         rsa_ctx->src_buffer = rsa_signature[i];
612         rsa_ctx->src_length = rsa_bitwidth[i] / 8;
613         rsa_ctx->dst_buffer = dst_buffer;
614         rsa_ctx->dst_length = 256 / 8;
615 
616         rsa_ctx->dir = 0;
617         rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
618         rsa_ctx->bitwidth = rsa_bitwidth[i];
619 
620         ret = do_rsa_crypto(rsa_ctx);
621         if (ret < 0) {
622             printf ("do rsa crypto failed: %d\n", ret);
623             goto out;
624         }
625 
626         ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256[i], 256 / 8);
627         if (ret) {
628             printf("rsa decrypt failed\n");
629             ce_dump("want data: ", rsa_sha256[i], 256 / 8, 16);
630             ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
631             printf("############RSA VERIFY, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
632             goto out;
633         }
634 
635         printf("############RSA SIGN/VERIFY, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
636     }
637 
638 out:
639     if (rsa_ctx)
640         hal_free_align(rsa_ctx);
641 
642     return ret;
643 }
644 
cmd_test_ce(int argc,const char * argv[])645 int cmd_test_ce(int argc, const char *argv[])
646 {
647     int ret = 0;
648 
649     if (argc != 2) {
650         printf("Parameter number Error!\n");
651         printf("Usage: hal_ce <aes|hash|rsa|rng>\n");
652         return -1;
653     }
654 
655     sunxi_ce_init();
656 
657     if (strcmp(argv[1], "aes") == 0)
658         ret = aes_test();
659     else if (strcmp(argv[1], "hash") == 0)
660         ret = hash_test();
661     else if (strcmp(argv[1], "rsa") == 0)
662         ret = rsa_test();
663     else if (strcmp(argv[1], "rng") == 0)
664         ret = rng_test();
665     else {
666         printf("Parameter Error!\n");
667         printf("Usage: hal_ce <aes|hash|rsa|rng>\n");
668         ret = -1;
669     }
670 
671     sunxi_ce_uninit();
672 
673     return ret;
674 }
675 
676 FINSH_FUNCTION_EXPORT_CMD(cmd_test_ce, hal_ce, tina rtos ce test demo)
677