341 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
			
		
		
	
	
			341 lines
		
	
	
		
			11 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| /****************************************************************************
 | ||
| 
 | ||
| Copyright(c) 2019 by Aerospace C.Power (Chongqing) Microelectronics. ALL RIGHTS RESERVED.
 | ||
| 
 | ||
| This Information is proprietary to Aerospace C.Power (Chongqing) Microelectronics and MAY NOT
 | ||
| be copied by any method or incorporated into another program without
 | ||
| the express written consent of Aerospace C.Power. This Information or any portion
 | ||
| thereof remains the property of Aerospace C.Power. The Information contained herein
 | ||
| is believed to be accurate and Aerospace C.Power assumes no responsibility or
 | ||
| liability for its use in any way and conveys no license or title under
 | ||
| any patent or copyright and makes no representation or warranty that this
 | ||
| Information is free from patent or copyright infringement.
 | ||
| 
 | ||
| ****************************************************************************/
 | ||
| #include "chip_reg_base.h"
 | ||
| #include "hw_reg_api.h"
 | ||
| #include "iot_bitops.h"
 | ||
| #include "flash.h"
 | ||
| #include "os_lock.h"
 | ||
| #include "iot_config.h"
 | ||
| #include "ahb_rf.h"
 | ||
| #include "ahb.h"
 | ||
| #include "clk.h"
 | ||
| #include "sfc.h"
 | ||
| 
 | ||
| #include "sec_glb.h"
 | ||
| 
 | ||
| #include "platform.h"
 | ||
| #include "dbg_io.h"
 | ||
| #include "sha1.h"
 | ||
| #include "sha256.h"
 | ||
| #include "aes.h"
 | ||
| #include "md5.h"
 | ||
| #include "sha512.h"
 | ||
| #include "gp_timer.h"
 | ||
| #include "sec_sys.h"
 | ||
| #include "iot_string.h"
 | ||
| #include "pk.h"
 | ||
| #include "oid.h"
 | ||
| #include "init.h"
 | ||
| 
 | ||
| int32_t iot_printf(const char *fmt, ...);
 | ||
| 
 | ||
| void p_buf(unsigned char *b, uint32_t l)
 | ||
| {
 | ||
|     uint32_t i;
 | ||
|     for (i = 0; i < l; i++) {
 | ||
|         iot_printf("%02x", b[i]);
 | ||
|     };
 | ||
|     iot_printf("\n");
 | ||
| }
 | ||
| 
 | ||
| unsigned char test_data[] = "admin";
 | ||
| 
 | ||
| void rom_aes_test()
 | ||
| {
 | ||
|     mbedtls_aes_context aes_ctx;
 | ||
|     /* 密钥数值  */
 | ||
|     unsigned char key[16] = { 0x01,0x02,0x03,0x04,0x05,0x06,0x07,0x08,0x09,0x0A,0x0B,0x0C,0x0D,0x0E,0x0F,0x00 };
 | ||
|     /* 明文空间  */
 | ||
|     unsigned char plain[16] = "helloworld";
 | ||
|     /* 解密后明文的空间  */
 | ||
|     unsigned char dec_plain[16] = { 0 };
 | ||
|     /* 密文空间  */
 | ||
|     unsigned char cipher[16] = { 0 };
 | ||
|     iot_printf("\n>>>>mbedtls_aes<<<<\n");
 | ||
|     iot_printf("func:%s line:%d mbedtls_aes_init:%p\n", __func__, __LINE__, mbedtls_aes_init);
 | ||
|     mbedtls_aes_init(&aes_ctx);
 | ||
|     /* 设置加密密钥 */
 | ||
|     mbedtls_aes_setkey_enc(&aes_ctx, key, 128);
 | ||
|     iot_printf("raw data:%s\n", plain);
 | ||
|     mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_ENCRYPT, plain, cipher);
 | ||
|     iot_printf("crypt data:%s\n", cipher);
 | ||
|     /* 设置解密密钥  */
 | ||
|     mbedtls_aes_setkey_dec(&aes_ctx, key, 128);
 | ||
|     mbedtls_aes_crypt_ecb(&aes_ctx, MBEDTLS_AES_DECRYPT, cipher, dec_plain);
 | ||
|     iot_printf("decrypt data:%s\n", dec_plain);
 | ||
|     mbedtls_aes_free(&aes_ctx);
 | ||
| }
 | ||
| 
 | ||
| void rom_sha1_test(void)
 | ||
| {
 | ||
|     unsigned char sha1_result[20];
 | ||
|     mbedtls_sha1_context sha1_ctx;
 | ||
|     iot_printf("\n>>>>mbedtls_sha1<<<<\n");
 | ||
|     mbedtls_sha1_init(&sha1_ctx);
 | ||
|     mbedtls_sha1_starts(&sha1_ctx);
 | ||
|     mbedtls_sha1_update(&sha1_ctx, test_data, strlen((const char *)test_data));
 | ||
|     mbedtls_sha1_finish(&sha1_ctx, sha1_result);
 | ||
|     mbedtls_sha1_free(&sha1_ctx);
 | ||
|     iot_printf("mbedtls_sha1_finish addr:%p sha1: ", mbedtls_sha1_finish);
 | ||
|     p_buf(sha1_result, 20);
 | ||
|     memset(sha1_result, 0, 20);
 | ||
|     mbedtls_sha1(test_data, strlen((const char *)test_data), sha1_result);
 | ||
|     iot_printf("mbedtls_sha1 addr:%p sha1: ", mbedtls_sha1);
 | ||
|     p_buf(sha1_result, 20);
 | ||
| }
 | ||
| 
 | ||
| void rom_md5_test(void)
 | ||
| {
 | ||
|     unsigned char md5_result[16];
 | ||
|     mbedtls_md5_context md5_ctx;
 | ||
|     iot_printf("\n>>>>mbedtls_md5<<<<\n");
 | ||
|     mbedtls_md5_init(&md5_ctx);
 | ||
|     mbedtls_md5_starts(&md5_ctx);
 | ||
|     mbedtls_md5_update(&md5_ctx, test_data, strlen((char *)test_data));
 | ||
|     mbedtls_md5_finish(&md5_ctx, md5_result);
 | ||
|     mbedtls_md5_free(&md5_ctx);
 | ||
|     iot_printf("mbedtls_md5_finish addr: %p md5: ", mbedtls_md5_finish);
 | ||
| 
 | ||
|     memset(md5_result, 0, 16);
 | ||
|     mbedtls_md5(test_data, strlen((const char *)test_data), md5_result);
 | ||
|     iot_printf("mbedtls_md5 addr: %p md5: ", mbedtls_md5);
 | ||
|     p_buf(md5_result, 16);
 | ||
| }
 | ||
| 
 | ||
| void rom_sha256_test(void)
 | ||
| {
 | ||
|     mbedtls_sha256_context sha256_ctx;
 | ||
|     unsigned char sha256_result[32];
 | ||
|     iot_printf("\n>>>>mbedtls_sha256<<<<\n");
 | ||
|     mbedtls_sha256_init(&sha256_ctx);
 | ||
|     mbedtls_sha256_starts(&sha256_ctx, 0);
 | ||
|     mbedtls_sha256_update(&sha256_ctx, test_data, strlen((char *)test_data));
 | ||
|     mbedtls_sha256_finish(&sha256_ctx, sha256_result);
 | ||
|     mbedtls_sha256_free(&sha256_ctx);
 | ||
|     iot_printf("mbedtls_sha256_finish addr: %p sha256 1: ", mbedtls_sha256_finish);
 | ||
|     p_buf(sha256_result, 32);
 | ||
| 
 | ||
|     memset(sha256_result, 0, 32);
 | ||
|     memset(&sha256_ctx, 0, sizeof(sha256_ctx));
 | ||
| 
 | ||
|     mbedtls_sha256(test_data, strlen((char *)test_data), sha256_result, 0);
 | ||
|     iot_printf("mbedtls_sha256 addr: %p sha256 2: ", mbedtls_sha256);
 | ||
|     p_buf(sha256_result, 32);
 | ||
| }
 | ||
| 
 | ||
| void rom_sha512_test(void)
 | ||
| {
 | ||
|     mbedtls_sha512_context sha512_ctx;
 | ||
|     unsigned char sha512_result[64];
 | ||
|     iot_printf("\n>>>>mbedtls_sha512<<<<\n");
 | ||
|     mbedtls_sha512_init(&sha512_ctx);
 | ||
|     mbedtls_sha512_starts(&sha512_ctx, 0);
 | ||
|     mbedtls_sha512_update(&sha512_ctx, test_data, strlen((char *)test_data));
 | ||
|     mbedtls_sha512_finish(&sha512_ctx, sha512_result);
 | ||
|     mbedtls_sha512_free(&sha512_ctx);
 | ||
|     iot_printf("mbedtls_sha512_finish addr: %p sha512: ", mbedtls_sha512_finish);
 | ||
|     p_buf(sha512_result, 64);
 | ||
| }
 | ||
| 
 | ||
| /* md 系列是对摘要算法的一种通用调用方式 */
 | ||
| void rom_md_test(const char *md_type)
 | ||
| {
 | ||
|     unsigned char result[64];
 | ||
|     int ret;
 | ||
|     mbedtls_md_context_t mdctx;
 | ||
|     iot_printf("\n>>>>mbedtls_md<<<<\n");
 | ||
|     mbedtls_md_init(&mdctx);
 | ||
|     const mbedtls_md_info_t *md_info = mbedtls_md_info_from_string(md_type);
 | ||
|     if (NULL == md_info) iot_printf("xxxxxxx\n");
 | ||
|     ret = mbedtls_md_setup(&mdctx, md_info, 0);
 | ||
|     if (ret) {
 | ||
|         iot_printf("%d ret = %d\n", __LINE__, ret);
 | ||
|         goto n1;
 | ||
|     }
 | ||
|     ret = mbedtls_md_starts(&mdctx);
 | ||
|     if (ret) iot_printf("%d\n", __LINE__);
 | ||
|     ret = mbedtls_md_update(&mdctx, test_data, strlen((char *)test_data));
 | ||
|     if (ret) iot_printf("%d\n", __LINE__);
 | ||
|     ret = mbedtls_md_finish(&mdctx, result);
 | ||
|     if (ret) iot_printf("%d\n", __LINE__);
 | ||
|     mbedtls_md_free(&mdctx);
 | ||
| 
 | ||
|     iot_printf("mbedtls_md_finish addr: %p md5: ", mbedtls_md_finish);
 | ||
|     p_buf(result, 16);
 | ||
| n1:
 | ||
|     mbedtls_md(md_info, test_data, strlen((char *)test_data), result);
 | ||
|     iot_printf("mbedtls_md addr: %p md5: ", mbedtls_md);
 | ||
|     p_buf(result, 16);
 | ||
| }
 | ||
| 
 | ||
| 
 | ||
| void rom_oid_test(void)
 | ||
| {
 | ||
|     const char *oid = NULL;
 | ||
|     size_t olen = 0;
 | ||
|     iot_printf("\n>>>>mbedtls_oid<<<<\n");
 | ||
|     /* 1 */
 | ||
|     oid = NULL; olen = 0;
 | ||
|     mbedtls_oid_get_oid_by_cipher_alg(MBEDTLS_CIPHER_AES_256_CBC, &oid, &olen);
 | ||
|     iot_printf("mbedtls_oid_get_oid_by_cipher_alg addr: %p olen: %d MBEDTLS_CIPHER_AES_256_CBC oid: ",
 | ||
|     mbedtls_oid_get_oid_by_cipher_alg, olen);
 | ||
|     p_buf((unsigned char*)oid, olen);
 | ||
| 
 | ||
|     /* 2*/
 | ||
|     oid = NULL; olen = 0;
 | ||
|     mbedtls_oid_get_oid_by_ec_grp(MBEDTLS_ECP_DP_SECP192R1, &oid, &olen);
 | ||
|     iot_printf("mbedtls_oid_get_oid_by_ec_grp addr: %p olen: %d MBEDTLS_ECP_DP_SECP192R1 oid: ",
 | ||
|         mbedtls_oid_get_oid_by_ec_grp, olen);
 | ||
|     p_buf((unsigned char*)oid, olen);
 | ||
| 
 | ||
|     /* 3 */
 | ||
|     oid = NULL; olen = 0;
 | ||
|     mbedtls_oid_get_oid_by_kdf_alg(MBEDTLS_KDF_KDF1, &oid, &olen);
 | ||
|     iot_printf("mbedtls_oid_get_oid_by_kdf_alg addr: %p olen: %d MBEDTLS_KDF_KDF1 oid: ",
 | ||
|         mbedtls_oid_get_oid_by_kdf_alg, olen);
 | ||
|     p_buf((unsigned char*)oid, olen);
 | ||
| 
 | ||
|     /* 4 */
 | ||
|     oid = NULL; olen = 0;
 | ||
|     mbedtls_oid_get_oid_by_md(MBEDTLS_MD_MD5, &oid, &olen);
 | ||
|     iot_printf("mbedtls_oid_get_oid_by_md addr: %p olen: %d MBEDTLS_MD_MD5 oid: ",
 | ||
|     mbedtls_oid_get_oid_by_md, olen);
 | ||
|     p_buf((unsigned char*)oid, olen);
 | ||
| 
 | ||
|     /* 5 */
 | ||
|     oid = NULL; olen = 0;
 | ||
|     mbedtls_oid_get_oid_by_pk_alg(MBEDTLS_PK_RSA, &oid, &olen);
 | ||
|     iot_printf("mbedtls_oid_get_oid_by_pk_alg addr: %p olen: %d MBEDTLS_PK_RSA oid: ",
 | ||
|         mbedtls_oid_get_oid_by_pk_alg, olen);
 | ||
|     p_buf((unsigned char*)oid, olen);
 | ||
| 
 | ||
|     /* 6 */
 | ||
|     oid = NULL; olen = 0;
 | ||
|     mbedtls_oid_get_oid_by_sig_alg(MBEDTLS_PK_RSA, MBEDTLS_MD_MD5, &oid, &olen);
 | ||
|     iot_printf("mbedtls_oid_get_oid_by_sig_alg addr: %p olen: %d MBEDTLS_PK_RSA,MBEDTLS_MD_MD5  oid: ",
 | ||
|         mbedtls_oid_get_oid_by_sig_alg, olen);
 | ||
|     p_buf((unsigned char*)oid, olen);
 | ||
| 
 | ||
|     /* 7 */
 | ||
|     char r[32] = {0};
 | ||
|     unsigned char t1[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x01};
 | ||
|     int ret = 0;
 | ||
|     mbedtls_asn1_buf asn1_oid = {0};
 | ||
|     asn1_oid.len = 9;
 | ||
|     asn1_oid.p = t1;
 | ||
|     ret = mbedtls_oid_get_numeric_string(r, sizeof(r), &asn1_oid);
 | ||
|     iot_printf("mbedtls_oid_get_numeric_string addr: %p ret: %d buf:%s\n",
 | ||
|         mbedtls_oid_get_numeric_string, ret, r);
 | ||
| 
 | ||
|     /* 8 */
 | ||
|     unsigned char t2[] = {0x60, 0x86, 0x48, 0x01, 0x65, 0x03, 0x04, 0x01, 0x2a};
 | ||
|     mbedtls_cipher_type_t cipher_alg;
 | ||
|     asn1_oid.len = 9;
 | ||
|     asn1_oid.p = t2;
 | ||
|     mbedtls_oid_get_cipher_alg(&asn1_oid, &cipher_alg);
 | ||
|     iot_printf("mbedtls_cipher_type_t = %d\n", cipher_alg);
 | ||
| 
 | ||
|     /* 9 */
 | ||
|     unsigned char t3[] = {0x2a, 0x86, 0x48, 0xce, 0x3d, 0x03, 0x01, 0x01};
 | ||
|     mbedtls_ecp_group_id grp_id;
 | ||
|     asn1_oid.len = 8;
 | ||
|     asn1_oid.p = t3;
 | ||
|     mbedtls_oid_get_ec_grp(&asn1_oid, &grp_id);
 | ||
|     iot_printf("mbedtls_ecp_group_id = %d\n", grp_id);
 | ||
| 
 | ||
|     /* 10 */
 | ||
|     unsigned char t4[] = {0x28, 0x81, 0x8c, 0x71, 0x02, 0x05, 0x01};
 | ||
|     mbedtls_kdf_type_t kdf_alg;
 | ||
|     asn1_oid.len = 7;
 | ||
|     asn1_oid.p = t4;
 | ||
|     mbedtls_oid_get_kdf_alg(&asn1_oid, &kdf_alg);
 | ||
|     iot_printf("mbedtls_kdf_type_t = %d\n", kdf_alg);
 | ||
| 
 | ||
|     /* 11 */
 | ||
|     unsigned char t5[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x02, 0x05};
 | ||
|     mbedtls_md_type_t md_alg;
 | ||
|     asn1_oid.len = 8;
 | ||
|     asn1_oid.p = t5;
 | ||
|     mbedtls_oid_get_md_alg(&asn1_oid, &md_alg);
 | ||
|     iot_printf("mbedtls_md_type_t = %d\n", md_alg);
 | ||
| 
 | ||
|     /* 12 */
 | ||
|     mbedtls_pk_type_t pk_alg;
 | ||
|     asn1_oid.p = t1;
 | ||
|     asn1_oid.len = 9;
 | ||
|     mbedtls_oid_get_pk_alg(&asn1_oid, &pk_alg);
 | ||
|     iot_printf("mbedtls_pk_type_t = %d\n", pk_alg);
 | ||
| 
 | ||
|     /* 13 */
 | ||
|     unsigned char t6[] = {0x2a, 0x86, 0x48, 0x86, 0xf7, 0x0d, 0x01, 0x01, 0x04};
 | ||
|     asn1_oid.p = t6;
 | ||
|     asn1_oid.len = 9;
 | ||
|     mbedtls_oid_get_sig_alg(&asn1_oid, &md_alg, &pk_alg);
 | ||
|     iot_printf("mbedtls_pk_type_t md_alg = %d pk_alg = %d\n", md_alg, pk_alg);
 | ||
| //    mbedtls_oid_get_pkcs12_pbe_alg(const mbedtls_asn1_buf * oid, mbedtls_md_type_t * md_alg, mbedtls_cipher_type_t * cipher_alg)
 | ||
| //    mbedtls_oid_get_sig_alg_desc(const mbedtls_asn1_buf * oid, const char * * desc)这两个接口没有对应的id,没有验证
 | ||
| }
 | ||
| 
 | ||
| int crypto_app_start (void);
 | ||
| void rom_pk_test()
 | ||
| {
 | ||
|     iot_printf("\n>>>>mbedtls_pk<<<<\n");
 | ||
|     crypto_app_start();
 | ||
| }
 | ||
| void rom_mbedtls_init()
 | ||
| {
 | ||
|     mbedtls_platform_set_printf((void *)iot_printf);
 | ||
|     mbedtls_platform_set_snprintf((void*)iot_snprintf);
 | ||
| }
 | ||
| 
 | ||
| void mbed_main() {
 | ||
|     uint32_t loop = 0x10000;
 | ||
| 
 | ||
|     dbg_uart_init();
 | ||
|     gp_timer_init();
 | ||
|     gp_timer_set(0, 0xffffffff, 0);
 | ||
|     gp_timer_start(0);
 | ||
|     while (gp_timer_get_current_val(0) < 1000000);
 | ||
|     gp_timer_stop(0);
 | ||
|     /* 硬件使能 */
 | ||
|     sec_glb_enable(SEC_GLB_SEC);
 | ||
|     /* mbedtls 库初始化 */
 | ||
|     crypto_init();
 | ||
|     /* 设置打印的回调函数 */
 | ||
|     rom_mbedtls_init();
 | ||
| 
 | ||
|     rom_sha1_test();
 | ||
|     rom_md5_test();
 | ||
|     rom_md_test("MD5");
 | ||
|     rom_oid_test();
 | ||
|     rom_sha256_test();
 | ||
|     rom_sha512_test();
 | ||
|     rom_aes_test();
 | ||
|     rom_pk_test();
 | ||
| 
 | ||
|     loop = 0x10000;
 | ||
|     while(loop--);
 | ||
|     return;
 | ||
| }
 | ||
| 
 | ||
| #ifdef __GNUC__
 | ||
| 
 | ||
| int main(void) {
 | ||
|     mbed_main();
 | ||
|     return 0;
 | ||
| }
 | ||
| #endif // __GCC__
 | ||
| 
 |