92 lines
		
	
	
		
			2.7 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			92 lines
		
	
	
		
			2.7 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 "os_types.h"
							 | 
						||
| 
								 | 
							
								#include "os_mem.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "iot_io.h"
							 | 
						||
| 
								 | 
							
								#include "mbedtls/chacha20.h"
							 | 
						||
| 
								 | 
							
								#include "sec_sys.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define mbedtls_printf iot_printf
							 | 
						||
| 
								 | 
							
								#if 0
							 | 
						||
| 
								 | 
							
								static const uint8_t key[32] = {
							 | 
						||
| 
								 | 
							
								    0x80, 0x81, 0x82, 0x83, 0x84, 0x85, 0x86, 0x87,
							 | 
						||
| 
								 | 
							
								    0x88, 0x89, 0x8a, 0x8b, 0x8c, 0x8d, 0x8e, 0x8f,
							 | 
						||
| 
								 | 
							
								    0x90, 0x91, 0x92, 0x93, 0x94, 0x95, 0x96, 0x97,
							 | 
						||
| 
								 | 
							
								    0x98, 0x99, 0x9a, 0x9b, 0x9c, 0x9d, 0x9e, 0x9f
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								static const uint8_t key[32] = {
							 | 
						||
| 
								 | 
							
								    0x00, 0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07,
							 | 
						||
| 
								 | 
							
								    0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f,
							 | 
						||
| 
								 | 
							
								    0x10, 0x11, 0x12, 0x13, 0x14, 0x15, 0x16, 0x17,
							 | 
						||
| 
								 | 
							
								    0x18, 0x19, 0x1a, 0x1b, 0x1c, 0x1d, 0x1e, 0x1f
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//t uint8_t m[10] = { 0x86, 0xd0, 0x99, 0x74, 0x84, 0x0b, 0xde, 0xd2, 0xa5, 0xca };
							 | 
						||
| 
								 | 
							
								static const uint8_t m[64] = { 0x0 };
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								//static const uint8_t nonce[12] = { 0x07, 0x00, 0x00, 0x00, 0x40, 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47 };
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const uint8_t nonce[12] = { 0x00, 0x00, 0x00, 0x09, 0x00, 0x00, 0x00, 0x4a, 0x00, 0x00, 0x00, 0x00};
							 | 
						||
| 
								 | 
							
								static uint8_t c[2][64];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int chacha20_test( int verbose )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    int ret = 0;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    uint32_t ic_l = 0x1;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								        mbedtls_printf( "  CHACHA20 test: ");
							 | 
						||
| 
								 | 
							
								#if 0
							 | 
						||
| 
								 | 
							
								        uint32_t ic_h = 0x2;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        uint64_t ic = ic_l |((uint64_t)ic_h << 32);
							 | 
						||
| 
								 | 
							
								    	stream_ref_xor_ic(c[0], m, 64, nonce, ic, key);
							 | 
						||
| 
								 | 
							
								    	sec_sys_chacha20(c[1], m, 64, nonce, ic, key);
							 | 
						||
| 
								 | 
							
								#else
							 | 
						||
| 
								 | 
							
								        stream_ietf_ref_xor_ic(c[0], m, 64, nonce, ic_l, key);
							 | 
						||
| 
								 | 
							
								        sec_sys_itef_chacha20(c[1], m, 64, nonce, ic_l, key);
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if( os_mem_cmp( c[0], c[1], 64 ) != 0 )
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								            mbedtls_printf( "failed\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        ret = 1;
							 | 
						||
| 
								 | 
							
								        goto exit;
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								        mbedtls_printf( "passed\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								        mbedtls_printf( "\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exit:
							 | 
						||
| 
								 | 
							
								    return( ret );
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int chacha20_main( )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    sec_sys_chacha20_init();
							 | 
						||
| 
								 | 
							
								    chacha20_test(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return 0;
							 | 
						||
| 
								 | 
							
								}
							 |