181 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			181 lines
		
	
	
		
			4.3 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include "os_types.h"
							 | 
						||
| 
								 | 
							
								#include <string.h>
							 | 
						||
| 
								 | 
							
								#include "iot_io.h"
							 | 
						||
| 
								 | 
							
								#include "mbedtls/sha256.h"
							 | 
						||
| 
								 | 
							
								#include "sec_sys.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static int sw_sha256 = 0;
							 | 
						||
| 
								 | 
							
								//static unsigned char buf[8196];
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * 32-bit integer manipulation macros (big endian)
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								#ifndef GET_UINT32_BE
							 | 
						||
| 
								 | 
							
								#define GET_UINT32_BE(n,b,i)                            \
							 | 
						||
| 
								 | 
							
								do {                                                    \
							 | 
						||
| 
								 | 
							
								    (n) = ( (uint32_t) (b)[(i)    ] << 24 )             \
							 | 
						||
| 
								 | 
							
								        | ( (uint32_t) (b)[(i) + 1] << 16 )             \
							 | 
						||
| 
								 | 
							
								        | ( (uint32_t) (b)[(i) + 2] <<  8 )             \
							 | 
						||
| 
								 | 
							
								        | ( (uint32_t) (b)[(i) + 3]       );            \
							 | 
						||
| 
								 | 
							
								} while( 0 )
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#ifndef PUT_UINT32_BE
							 | 
						||
| 
								 | 
							
								#define PUT_UINT32_BE(n,b,i)                            \
							 | 
						||
| 
								 | 
							
								do {                                                    \
							 | 
						||
| 
								 | 
							
								    (b)[(i)    ] = (unsigned char) ( (n) >> 24 );       \
							 | 
						||
| 
								 | 
							
								    (b)[(i) + 1] = (unsigned char) ( (n) >> 16 );       \
							 | 
						||
| 
								 | 
							
								    (b)[(i) + 2] = (unsigned char) ( (n) >>  8 );       \
							 | 
						||
| 
								 | 
							
								    (b)[(i) + 3] = (unsigned char) ( (n)       );       \
							 | 
						||
| 
								 | 
							
								} while( 0 )
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * FIPS-180-2 test vectors
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static const unsigned char sha256_test_buf[3][57] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    { "abc" },
							 | 
						||
| 
								 | 
							
								    { "abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq" },
							 | 
						||
| 
								 | 
							
								    { "" }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const int sha256_test_buflen[3] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    3, 56, 1000
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const unsigned char sha256_test_sum[3][32] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /*
							 | 
						||
| 
								 | 
							
								     * SHA-256 test vectors
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    { 0xBA, 0x78, 0x16, 0xBF, 0x8F, 0x01, 0xCF, 0xEA,
							 | 
						||
| 
								 | 
							
								      0x41, 0x41, 0x40, 0xDE, 0x5D, 0xAE, 0x22, 0x23,
							 | 
						||
| 
								 | 
							
								      0xB0, 0x03, 0x61, 0xA3, 0x96, 0x17, 0x7A, 0x9C,
							 | 
						||
| 
								 | 
							
								      0xB4, 0x10, 0xFF, 0x61, 0xF2, 0x00, 0x15, 0xAD },
							 | 
						||
| 
								 | 
							
								    { 0x24, 0x8D, 0x6A, 0x61, 0xD2, 0x06, 0x38, 0xB8,
							 | 
						||
| 
								 | 
							
								      0xE5, 0xC0, 0x26, 0x93, 0x0C, 0x3E, 0x60, 0x39,
							 | 
						||
| 
								 | 
							
								      0xA3, 0x3C, 0xE4, 0x59, 0x64, 0xFF, 0x21, 0x67,
							 | 
						||
| 
								 | 
							
								      0xF6, 0xEC, 0xED, 0xD4, 0x19, 0xDB, 0x06, 0xC1 },
							 | 
						||
| 
								 | 
							
								    { 0xCD, 0xC7, 0x6E, 0x5C, 0x99, 0x14, 0xFB, 0x92,
							 | 
						||
| 
								 | 
							
								      0x81, 0xA1, 0xC7, 0xE2, 0x84, 0xD7, 0x3E, 0x67,
							 | 
						||
| 
								 | 
							
								      0xF1, 0x80, 0x9A, 0x48, 0xA4, 0x97, 0x20, 0x0E,
							 | 
						||
| 
								 | 
							
								      0x04, 0x6D, 0x39, 0xCC, 0xC7, 0x11, 0x2C, 0xD0 }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#if 0
							 | 
						||
| 
								 | 
							
								static const unsigned char sha256_padding[64] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								 0x80, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
							 | 
						||
| 
								 | 
							
								    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
							 | 
						||
| 
								 | 
							
								    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
							 | 
						||
| 
								 | 
							
								    0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * Checkup routine
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								int mbedtls_sha256_self_test( int verbose )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    int i, ret = 0;
							 | 
						||
| 
								 | 
							
								    unsigned char sha256sum[32];
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_context ctx;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_init( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for( i = 0; i < 2; i++ )
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								            iot_printf( "  SHA-%d test #%d: ", 256, i + 1 );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if(sw_sha256){
							 | 
						||
| 
								 | 
							
											mbedtls_sha256_starts( &ctx, 0 );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											mbedtls_sha256_update( &ctx, sha256_test_buf[i],
							 | 
						||
| 
								 | 
							
																	 sha256_test_buflen[i] );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											mbedtls_sha256_finish( &ctx, sha256sum );
							 | 
						||
| 
								 | 
							
								        }else{
							 | 
						||
| 
								 | 
							
											sec_sys_sha256(sha256_test_buf[i], sha256_test_buflen[i], sha256sum);
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if( memcmp( sha256sum, sha256_test_sum[i], 32 ) != 0 )
							 | 
						||
| 
								 | 
							
								        {
							 | 
						||
| 
								 | 
							
								            if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								                iot_printf( "failed\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								            ret = 1;
							 | 
						||
| 
								 | 
							
								            goto exit;
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								            iot_printf( "passed\n" );
							 | 
						||
| 
								 | 
							
								    }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								        iot_printf( "\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exit:
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_free( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return( ret );
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static unsigned char test_buf_256[] = "This adapter uses AR7010+AR9280 (and presumably also the UB94 reference design for that chipset), which is found on a few other devices. It is, however, not likely that any alternative adapters will be usable with Sony products.";
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int sha256_test( int verbose )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    int i = 0;
							 | 
						||
| 
								 | 
							
									int ret = 0;
							 | 
						||
| 
								 | 
							
								    unsigned char sha256sum[2][32];
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_context ctx;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_init( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if( verbose != 0 )
							 | 
						||
| 
								 | 
							
										iot_printf( "  SHA-%d test #%d: ", 256, i + 1 );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									mbedtls_sha256_starts( &ctx, 0 );
							 | 
						||
| 
								 | 
							
									mbedtls_sha256_update( &ctx, test_buf_256, sizeof(test_buf_256) );
							 | 
						||
| 
								 | 
							
									mbedtls_sha256_finish( &ctx, sha256sum[0] );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									sec_sys_sha256(test_buf_256, sizeof(test_buf_256), sha256sum[1]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if( memcmp( sha256sum[0], sha256sum[1], 32 ) != 0 )
							 | 
						||
| 
								 | 
							
									{
							 | 
						||
| 
								 | 
							
										if( verbose != 0 )
							 | 
						||
| 
								 | 
							
											iot_printf( "failed\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
										ret = 1;
							 | 
						||
| 
								 | 
							
										goto exit;
							 | 
						||
| 
								 | 
							
									}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if( verbose != 0 )
							 | 
						||
| 
								 | 
							
										iot_printf( "passed\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								        iot_printf( "\n" );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								exit:
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_free( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return( ret );
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int sha256_main( )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    mbedtls_sha256_self_test(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    sha256_test(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									return 0;
							 | 
						||
| 
								 | 
							
								}
							 |