summaryrefslogtreecommitdiff
path: root/bsp/allwinner/libraries/sunxi-hal/hal/test/ce/test_ce.c
blob: e114bd7ff919b11c3807ef14f17d6a1340105484 (plain)
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
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
/*
* Copyright (c) 2019-2025 Allwinner Technology Co., Ltd. ALL rights reserved.
*
* Allwinner is a trademark of Allwinner Technology Co.,Ltd., registered in
* the the people's Republic of China and other countries.
* All Allwinner Technology Co.,Ltd. trademarks are used with permission.
*
* DISCLAIMER
* THIRD PARTY LICENCES MAY BE REQUIRED TO IMPLEMENT THE SOLUTION/PRODUCT.
* IF YOU NEED TO INTEGRATE THIRD PARTY’S TECHNOLOGY (SONY, DTS, DOLBY, AVS OR MPEGLA, ETC.)
* IN ALLWINNERS’SDK OR PRODUCTS, YOU SHALL BE SOLELY RESPONSIBLE TO OBTAIN
* ALL APPROPRIATELY REQUIRED THIRD PARTY LICENCES.
* ALLWINNER SHALL HAVE NO WARRANTY, INDEMNITY OR OTHER OBLIGATIONS WITH RESPECT TO MATTERS
* COVERED UNDER ANY REQUIRED THIRD PARTY LICENSE.
* YOU ARE SOLELY RESPONSIBLE FOR YOUR USAGE OF THIRD PARTY’S TECHNOLOGY.
*
*
* THIS SOFTWARE IS PROVIDED BY ALLWINNER"AS IS" AND TO THE MAXIMUM EXTENT
* PERMITTED BY LAW, ALLWINNER EXPRESSLY DISCLAIMS ALL WARRANTIES OF ANY KIND,
* WHETHER EXPRESS, IMPLIED OR STATUTORY, INCLUDING WITHOUT LIMITATION REGARDING
* THE TITLE, NON-INFRINGEMENT, ACCURACY, CONDITION, COMPLETENESS, PERFORMANCE
* OR MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
* IN NO EVENT SHALL ALLWINNER BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS, OR BUSINESS INTERRUPTION)
* HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
* STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
* OF THE POSSIBILITY OF SUCH DAMAGE.
*/

#include <stdio.h>
#include <string.h>
#include <unistd.h>

#include <hal_cmd.h>
#include <hal_mem.h>
#include <sunxi_hal_ce.h>

#include "test_ce.h"

#define AES_MODE_ECB        (0)
#define AES_MODE_CBC        (1)
#define AES_MODE_CTR        (2)
#define AES_MODE_CTS        (3)
#define AES_MODE_OFB        (4)
#define AES_MODE_CFB        (5)

#define AES_DIR_ENCRYPT     (0)
#define AES_DIR_DECRYPT     (1)

#define HASH_METHOD_MD5     (16)
#define HASH_METHOD_SHA1    (17)
#define HASH_METHOD_SHA224  (18)
#define HASH_METHOD_SHA256  (19)
#define HASH_METHOD_SHA384  (20)
#define HASH_METHOD_SHA512  (21)

void ce_dump(char *str,unsigned char *data, int len, int align)
{
    int i = 0;
    if(str)
        printf("\n%s: ",str);
    for(i = 0; i<len; i++)
    {
        if((i%align) == 0)
        {
            printf("\n");
            printf("0x08%x: ", data + i * align);
        }
        printf("%02x ",*(data++));
    }
    printf("\n");
}

int aes_test(void)
{
    int ret = -1;
    int i = 0;
    int j = 0;
    int m = 0;
    uint8_t *enc_buffer = 0;
    uint32_t enc_len = 0;
    uint32_t blk_num = 0;
    uint8_t iv_next[AES_BLOCK_SIZE] = {0};
    uint8_t *(*aes_enc[])[5] = {aes_ecb, aes_cbc, aes_ctr, aes_cbc, aes_ofb, aes_cfb8};
    crypto_aes_req_ctx_t *aes_ctx = NULL;

    aes_ctx = (crypto_aes_req_ctx_t *)hal_malloc_align(sizeof(crypto_aes_req_ctx_t), CE_ALIGN_SIZE);
    if (aes_ctx == NULL) {
        printf (" malloc data buffer fail\n");
        return -1;
    }
    memset(aes_ctx, 0x0, sizeof(crypto_aes_req_ctx_t));

    aes_ctx->dst_buffer = (u8 *)hal_malloc_align(512, CE_ALIGN_SIZE);
    if (aes_ctx->dst_buffer == NULL) {
        printf (" malloc dest buffer fail\n");
        ret = -1;
        goto out;
    }

    for (m = AES_MODE_ECB; m < AES_MODE_CFB + 1; m++) {
        for (i = 0; i < sizeof(aes_key_len)/sizeof(aes_key_len[0]); i++) {
            for (j = 0; j < sizeof(aes_src)/sizeof(aes_src[0]); j++) {
                /* aes encrypt */
                aes_ctx->src_buffer = aes_src[j];
                aes_ctx->src_length = aes_src_len[j];
                aes_ctx->key = aes_key[i];
                aes_ctx->key_length = aes_key_len[i];
                if (m == AES_MODE_ECB)
                    aes_ctx->iv = NULL;
                else
                    aes_ctx->iv = aes_iv;
                if (m == AES_MODE_CTR) {
                    memset(iv_next, 0, AES_BLOCK_SIZE);
                    aes_ctx->iv_next = iv_next;
                } else
                    aes_ctx->iv_next = NULL;
                if (m == AES_MODE_CFB)
                    aes_ctx->bitwidth = 8;
                else
                    aes_ctx->bitwidth = 0;
                aes_ctx->mode = m;
                aes_ctx->dir = AES_DIR_ENCRYPT;
                aes_ctx->dst_length = CE_ROUND_UP(aes_ctx->src_length, AES_BLOCK_SIZE);

                printf("###############AES, mode: %d, ksize %d, src len %d, begin###############\n", m, aes_key_len[i], aes_src_len[j]);

                ret = do_aes_crypto(aes_ctx);
                if (ret < 0) {
                    printf ("aes encrypt fail %d\n", ret);
                    goto out;
                }

                // for ecb/cbc/cts, enc data len should be 16 bytes aligned
                if (m == AES_MODE_ECB || m == AES_MODE_CBC || m == AES_MODE_CTS)
                    enc_len = aes_ctx->dst_length;
                else
                    enc_len = aes_src_len[j];

                // openssl enc do not support cts, so create enc data manually.
                if (m == AES_MODE_CTS){
                    enc_buffer = (uint8_t *)hal_malloc(enc_len);
                    if (enc_buffer == NULL) {
                        printf ("malloc ctr buffer fail\n");
                        ret = -1;
                        goto out;
                    }

                    blk_num = enc_len / AES_BLOCK_SIZE;
                    if (blk_num > 1) {
                        if (blk_num > 2)
                            memcpy(enc_buffer, aes_enc[m - AES_MODE_ECB][i][j], (blk_num - 2) * AES_BLOCK_SIZE);
                        memcpy(enc_buffer + (blk_num - 2) * AES_BLOCK_SIZE,
                                aes_enc[m - AES_MODE_ECB][i][j] + (blk_num - 1) * AES_BLOCK_SIZE,
                                AES_BLOCK_SIZE);
                        memcpy(enc_buffer + (blk_num - 1) * AES_BLOCK_SIZE,
                                aes_enc[m - AES_MODE_ECB][i][j] + (blk_num - 2) * AES_BLOCK_SIZE,
                                AES_BLOCK_SIZE);
                    } else {
                        memcpy(enc_buffer, aes_enc[m - AES_MODE_ECB][i][j], enc_len);
                    }
                } else
                    enc_buffer = aes_enc[m - AES_MODE_ECB][i][j];

                if (memcmp(aes_ctx->dst_buffer, enc_buffer, enc_len) != 0) {
                    ce_dump("want data: ", enc_buffer, enc_len, 16);
                    ce_dump("calc data: ", aes_ctx->dst_buffer, enc_len, 16);
                    printf("###############AES ENC, mode: %d, ksize %d, src len %d, fail###############\n", m, aes_key_len[i], aes_src_len[j]);
                    ret = -1;
                    goto out;
                }

                /* aes decrypt */
                memset(aes_ctx->dst_buffer, 0x0, aes_ctx->dst_length);
                aes_ctx->dir = AES_DIR_DECRYPT;
                aes_ctx->src_buffer = enc_buffer;
                aes_ctx->src_length = enc_len;

                ret = do_aes_crypto(aes_ctx);
                if (ret < 0) {
                    printf("aes decrypt fail %d\n", ret);
                    goto out;
                }

                if (memcmp(aes_ctx->dst_buffer, aes_src[j], aes_src_len[j]) != 0) {
                    ce_dump("want data: ", aes_src[j], aes_src_len[j], 16);
                    ce_dump("calc data: ", aes_ctx->dst_buffer, aes_src_len[j], 16);
                    printf("###############AES DEC, mode: %d, ksize %d, src len %d, fail###############\n", m, aes_key_len[i], aes_src_len[j]);
                    ret = -1;
                    goto out;
                }

                if (m == AES_MODE_CTS) {
                    if (enc_buffer)
                        hal_free(enc_buffer);
                }

                printf("###############AES, mode: %d, ksize %d, src len %d, pass###############\n\n\n", m, aes_key_len[i], aes_src_len[j]);
            }
        }
    }

out:
    if (aes_ctx->dst_buffer != NULL) {
        hal_free_align(aes_ctx->dst_buffer);
    }
    if (m == AES_MODE_CTS) {
        if (enc_buffer)
            hal_free(enc_buffer);
    }

    hal_free_align(aes_ctx);

    return ret;
}

int hash_test(void)
{
    int i = 0;
    int j = 0;
    uint8_t *dst_data = NULL;
    //uint32_t data_size = 512; SHA_MAX_DIGEST_SIZE
    uint32_t data_size = SHA_MAX_DIGEST_SIZE;
    uint32_t hash_length = 0;
    int ret = -1;
    uint8_t *(*hash_digest[]) = {hash_md5, hash_sha1, hash_sha224, hash_sha256, hash_sha384, hash_sha512};
    crypto_hash_req_ctx_t *hash_ctx = NULL;

    hash_ctx = (crypto_hash_req_ctx_t *)hal_malloc_align(sizeof(crypto_hash_req_ctx_t), CE_ALIGN_SIZE);
    if (hash_ctx == NULL) {
        printf (" malloc hash_ctx fail\n");
        ret = -1;
        goto out;
    }

    /*malloc dst buf*/
    dst_data = (u8 *)hal_malloc_align(data_size, CE_ALIGN_SIZE);
    if (dst_data == NULL) {
        printf (" malloc dst buffer fail\n");
        ret = -1;
        goto out;
    }

    for (i = HASH_METHOD_MD5; i < HASH_METHOD_SHA512 + 1; i++) {
        for (j = 0; j < sizeof(hash_src_len)/sizeof(hash_src_len[0]); j++) {
            hash_ctx->src_buffer = hash_src[j];
            hash_ctx->src_length = hash_src_len[j];
            memset(dst_data, 0x0, data_size);
            hash_ctx->dst_buffer = dst_data;
            hash_ctx->type = i;
            hash_ctx->md_size = 0;
            switch (i) {
                case HASH_METHOD_MD5:
                    hash_ctx->dst_length = MD5_DIGEST_SIZE;
                    hash_length = MD5_DIGEST_SIZE;
                    break;
                case HASH_METHOD_SHA1:
                    hash_ctx->dst_length = SHA1_DIGEST_SIZE;
                    hash_length = SHA1_DIGEST_SIZE;
                    break;
                case HASH_METHOD_SHA224:
                    hash_ctx->dst_length = SHA256_DIGEST_SIZE;
                    hash_length = SHA224_DIGEST_SIZE;
                    break;
                case HASH_METHOD_SHA256:
                    hash_ctx->dst_length = SHA256_DIGEST_SIZE;
                    hash_length = SHA256_DIGEST_SIZE;
                    break;
                case HASH_METHOD_SHA384:
                    hash_ctx->dst_length = SHA512_DIGEST_SIZE;
                    hash_length = SHA384_DIGEST_SIZE;
                    break;
                case HASH_METHOD_SHA512:
                    hash_ctx->dst_length = SHA512_DIGEST_SIZE;
                    hash_length = SHA512_DIGEST_SIZE;
                    break;
                default:
                    break;
            }

            printf("############hash type: %d, src len: %d, begin#############\n", i, hash_src_len[j]);

            ret = do_hash_crypto(hash_ctx);
            if (ret < 0) {
                printf ("do_hash_crypto fail\n");
                goto out;
            }

            if (memcmp(hash_ctx->dst_buffer, hash_digest[i - HASH_METHOD_MD5][j], hash_length) == 0) {
                printf("############hash type: %d, src len: %d, pass#############\n\n\n", i, hash_src_len[j]);
            } else {
                ce_dump("want digest: ", hash_digest[i - HASH_METHOD_MD5][j], hash_length, 16);
                ce_dump("calc digest: ", hash_ctx->dst_buffer, hash_length, 16);
                printf("############hash type: %d, src len: %d, fail#############\n\n\n", i, hash_src_len[j]);
                ret = -1;
                goto out;
            }
        }
    }

out:
    if (hash_ctx != NULL) {
        hal_free_align(hash_ctx);
    }

    if (dst_data != NULL) {
        hal_free_align(dst_data);
    }

    return ret;
}


int rng_test(void)
{
    int ret = 0;
    int i = 0;
    uint8_t *rng_buf = NULL;
    uint32_t rng_size[] = {16, 31, 32, 8100};
    uint8_t key[24] = {
        0xa1, 0xb7, 0x78, 0xf7, 0xbf, 0x2c, 0xfa, 0xad, 0x6a, 0x46, 0x79, 0xc2, 0xd2, 0x9c, 0x45, 0x1f,
        0x3f, 0xcb, 0xef, 0xa5, 0x4e, 0x0e, 0xc3, 0x51
    };
    uint32_t key_len = 24;
    crypto_rng_req_ctx_t *rng_ctx = NULL;

    rng_ctx = (crypto_rng_req_ctx_t *)hal_malloc(sizeof(crypto_rng_req_ctx_t));
    if (rng_ctx == NULL) {
        printf (" malloc rng ctx fail\n");
        ret = -1;
        goto out;
    }

    /*malloc trng buf*/
    rng_buf = (u8 *)hal_malloc(8192);
    if (rng_buf == NULL) {
        printf ("malloc rng buffer fail\n");
        ret = -1;
        goto out;
    }

    /*TRNG test*/
    for (i = 0; i < sizeof(rng_size)/sizeof(uint32_t); i++) {
        printf("############TRNG, len: %d, begin#############\n", rng_size[i]);
        memset(rng_buf, 0, 8192);
        rng_ctx->rng_buf = rng_buf;
        rng_ctx->rng_len = rng_size[i];
        rng_ctx->mode = 0x30; /*CE_METHOD_TRNG*/
        rng_ctx->key = NULL;
        rng_ctx->key_len = 0;
        ret = do_rng_gen(rng_ctx);
        if (ret < 0) {
            printf("############TRNG, len: %d, fail#############\n\n\n", rng_size[i]);
            goto out;
        }
#if 0
        if (rng_size[i] < 100)
            ce_dump("trng:", rng_buf, rng_size[i], 16);
#endif
        printf("############TRNG, len: %d, pass#############\n\n\n", rng_size[i]);
    }

    /*PRNG test*/
    for (i = 0; i < sizeof(rng_size)/sizeof(uint32_t); i++) {
        printf("############PRNG, len: %d, begin#############\n", rng_size[i]);
        memset(rng_buf, 0, 8192);
        rng_ctx->rng_buf = rng_buf;
        rng_ctx->rng_len = rng_size[i];
        rng_ctx->mode = 0x31; /*CE_METHOD_PRNG*/
        rng_ctx->key = key;
        rng_ctx->key_len = key_len;
        ret = do_rng_gen(rng_ctx);
        if (ret < 0) {
            printf("############PRNG, len: %d, fail#############\n\n\n", rng_size[i]);
            goto out;
        }
#if 0
        if (rng_size[i] < 100)
            ce_dump("prng:", rng_buf, rng_size[i], 16);
#endif
        printf("############PRNG, len: %d, pass#############\n\n\n", rng_size[i]);
    }

out:
    if (rng_ctx)
        hal_free(rng_ctx);

    if (rng_buf)
        hal_free(rng_buf);

    return ret;
}

int rsa_test(void)
{
    int ret = 0;
    int i = 0;
    uint8_t dst_buffer[256] = {0};
    crypto_rsa_req_ctx_t *rsa_ctx = NULL;

    rsa_ctx = (crypto_rsa_req_ctx_t *)hal_malloc_align(sizeof(crypto_rsa_req_ctx_t), CE_ALIGN_SIZE);
    if (rsa_ctx == NULL) {
        printf (" malloc rsa ctx fail\n");
        return -1;
    }

    /*rsa enc and dec*/
    for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
        /* enc with public key*/
        printf("############RSA ENC/DEC, len: %d, begin#############\n", rsa_bitwidth[i]);
        memset(dst_buffer, 0, 256);
        memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));

        rsa_ctx->key_n = rsa_keyn[i];
        rsa_ctx->n_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_e = rsa_keye[i];
        rsa_ctx->e_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_d = 0;
        rsa_ctx->d_len = 0;

        rsa_ctx->src_buffer = rsa_src[i];
        rsa_ctx->src_length = rsa_bitwidth[i] / 8;
        rsa_ctx->dst_buffer = dst_buffer;
        rsa_ctx->dst_length = rsa_bitwidth[i] / 8;

        rsa_ctx->dir = 0;
        rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
        rsa_ctx->bitwidth = rsa_bitwidth[i];

        ret = do_rsa_crypto(rsa_ctx);
        if (ret < 0) {
            printf ("do rsa crypto failed: %d\n", ret);
            goto out;
        }

        ret = memcmp(rsa_ctx->dst_buffer, rsa_enc[i], rsa_bitwidth[i] / 8);
        if (ret) {
            printf("rsa encrypt failed\n");
            ce_dump("want data: ", rsa_enc[i], rsa_bitwidth[i] / 8, 16);
            ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
            printf("############RSA ENC, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
            goto out;
        }

        /* dec with private key */
        memset(dst_buffer, 0, 256);
        memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));

        rsa_ctx->key_n = rsa_keyn[i];
        rsa_ctx->n_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_e = 0;
        rsa_ctx->e_len = 0;
        rsa_ctx->key_d = rsa_keyd[i];
        rsa_ctx->d_len = rsa_bitwidth[i] / 8;

        rsa_ctx->src_buffer = rsa_enc[i];
        rsa_ctx->src_length = rsa_bitwidth[i] / 8;
        rsa_ctx->dst_buffer = dst_buffer;
        rsa_ctx->dst_length = rsa_bitwidth[i] / 8;

        rsa_ctx->dir = 0;
        rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
        rsa_ctx->bitwidth = rsa_bitwidth[i];

        ret = do_rsa_crypto(rsa_ctx);
        if (ret < 0) {
            printf ("do rsa crypto failed: %d\n", ret);
            goto out;
        }

        ret = memcmp(rsa_ctx->dst_buffer, rsa_src[i], rsa_bitwidth[i] / 8);
        if (ret) {
            printf("rsa decrypt failed\n");
            ce_dump("want data: ", rsa_src[i], rsa_bitwidth[i] / 8, 16);
            ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
            printf("############RSA DEC, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
            goto out;
        }

        printf("############RSA ENC/DEC, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
    }

    /* rsa sign/verify sha256 value */
    for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
        /* sign with private key */
        printf("############RSA SIGN/VERIFY SHA256, len: %d, begin#############\n", rsa_bitwidth[i]);
        memset(dst_buffer, 0, 256);
        memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));

        rsa_ctx->key_n = rsa_keyn[i];
        rsa_ctx->n_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_e = 0;
        rsa_ctx->e_len = 0;
        rsa_ctx->key_d = rsa_keyd[i];
        rsa_ctx->d_len = rsa_bitwidth[i] / 8;

        rsa_ctx->src_buffer = rsa_sha256_raw[i];
        rsa_ctx->src_length = rsa_bitwidth[i] / 8;
        rsa_ctx->dst_buffer = dst_buffer;
        rsa_ctx->dst_length = rsa_bitwidth[i] / 8;

        rsa_ctx->dir = 0;
        rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
        rsa_ctx->bitwidth = rsa_bitwidth[i];

        ret = do_rsa_crypto(rsa_ctx);
        if (ret < 0) {
            printf ("do rsa crypto failed: %d\n", ret);
            goto out;
        }

        ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256_sign[i], rsa_bitwidth[i] / 8);
        if (ret) {
            printf("rsa encrypt failed\n");
            ce_dump("want data: ", rsa_sha256_sign[i], rsa_bitwidth[i] / 8, 16);
            ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
            printf("############RSA SIGN SHA256, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
            //goto out;
        }

        /* verify with public key */
        memset(dst_buffer, 0, 256);
        memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));

        rsa_ctx->key_n = rsa_keyn[i];
        rsa_ctx->n_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_e = rsa_keye[i];
        rsa_ctx->e_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_d = 0;
        rsa_ctx->d_len = 0;

        rsa_ctx->src_buffer = rsa_sha256_sign[i];
        rsa_ctx->src_length = rsa_bitwidth[i] / 8;
        rsa_ctx->dst_buffer = dst_buffer;
        rsa_ctx->dst_length = 256 / 8;

        rsa_ctx->dir = 0;
        rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
        rsa_ctx->bitwidth = rsa_bitwidth[i];

        ret = do_rsa_crypto(rsa_ctx);
        if (ret < 0) {
            printf ("do rsa crypto failed: %d\n", ret);
            goto out;
        }

        ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256[i], 256 / 8);
        if (ret) {
            printf("rsa decrypt failed\n");
            ce_dump("want data: ", rsa_sha256[i], 256 / 8, 16);
            ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
            printf("############RSA VERIFY SHA256, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
            goto out;
        }

        printf("############RSA SIGN/VERIFY SHA256, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
    }

    /* rsa sign/verify */
    for (i = 0; i < sizeof(rsa_bitwidth)/sizeof(rsa_bitwidth[0]); i ++) {
        /* sign with private key */
        printf("############RSA SIGN/VERIFY, len: %d, begin#############\n", rsa_bitwidth[i]);
        memset(dst_buffer, 0, 256);
        memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));

        rsa_ctx->key_n = rsa_keyn[i];
        rsa_ctx->n_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_e = 0;
        rsa_ctx->e_len = 0;
        rsa_ctx->key_d = rsa_keyd[i];
        rsa_ctx->d_len = rsa_bitwidth[i] / 8;

        rsa_ctx->src_buffer = rsa_sign_raw[i];
        rsa_ctx->src_length = rsa_bitwidth[i] / 8;
        rsa_ctx->dst_buffer = dst_buffer;
        rsa_ctx->dst_length = rsa_bitwidth[i] / 8;

        rsa_ctx->dir = 0;
        rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
        rsa_ctx->bitwidth = rsa_bitwidth[i];

        ret = do_rsa_crypto(rsa_ctx);
        if (ret < 0) {
            printf ("do rsa crypto failed: %d\n", ret);
            goto out;
        }

        ret = memcmp(rsa_ctx->dst_buffer, rsa_signature[i], rsa_bitwidth[i] / 8);
        if (ret) {
            printf("rsa encrypt failed\n");
            ce_dump("want data: ", rsa_signature[i], rsa_bitwidth[i] / 8, 16);
            ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
            printf("############RSA SIGN, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
            //goto out;
        }

        /* verify with public key */
        memset(dst_buffer, 0, 256);
        memset(rsa_ctx, 0, sizeof(crypto_rsa_req_ctx_t));

        rsa_ctx->key_n = rsa_keyn[i];
        rsa_ctx->n_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_e = rsa_keye[i];
        rsa_ctx->e_len = rsa_bitwidth[i] / 8;
        rsa_ctx->key_d = 0;
        rsa_ctx->d_len = 0;

        rsa_ctx->src_buffer = rsa_signature[i];
        rsa_ctx->src_length = rsa_bitwidth[i] / 8;
        rsa_ctx->dst_buffer = dst_buffer;
        rsa_ctx->dst_length = 256 / 8;

        rsa_ctx->dir = 0;
        rsa_ctx->type = 0x20; /*CE_METHOD_RSA*/
        rsa_ctx->bitwidth = rsa_bitwidth[i];

        ret = do_rsa_crypto(rsa_ctx);
        if (ret < 0) {
            printf ("do rsa crypto failed: %d\n", ret);
            goto out;
        }

        ret = memcmp(rsa_ctx->dst_buffer, rsa_sha256[i], 256 / 8);
        if (ret) {
            printf("rsa decrypt failed\n");
            ce_dump("want data: ", rsa_sha256[i], 256 / 8, 16);
            ce_dump("calc data: ", rsa_ctx->dst_buffer, rsa_ctx->dst_length, 16);
            printf("############RSA VERIFY, len: %d, fail#############\n\n\n", rsa_bitwidth[i]);
            goto out;
        }

        printf("############RSA SIGN/VERIFY, len: %d, pass#############\n\n\n", rsa_bitwidth[i]);
    }

out:
    if (rsa_ctx)
        hal_free_align(rsa_ctx);

    return ret;
}

int cmd_test_ce(int argc, const char *argv[])
{
    int ret = 0;

    if (argc != 2) {
        printf("Parameter number Error!\n");
        printf("Usage: hal_ce <aes|hash|rsa|rng>\n");
        return -1;
    }

    sunxi_ce_init();

    if (strcmp(argv[1], "aes") == 0)
        ret = aes_test();
    else if (strcmp(argv[1], "hash") == 0)
        ret = hash_test();
    else if (strcmp(argv[1], "rsa") == 0)
        ret = rsa_test();
    else if (strcmp(argv[1], "rng") == 0)
        ret = rng_test();
    else {
        printf("Parameter Error!\n");
        printf("Usage: hal_ce <aes|hash|rsa|rng>\n");
        ret = -1;
    }

    sunxi_ce_uninit();

    return ret;
}

FINSH_FUNCTION_EXPORT_CMD(cmd_test_ce, hal_ce, tina rtos ce test demo)