193 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
		
		
			
		
	
	
			193 lines
		
	
	
		
			5.2 KiB
		
	
	
	
		
			C
		
	
	
	
	
	
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#include <stdio.h>
							 | 
						||
| 
								 | 
							
								#include <string.h>
							 | 
						||
| 
								 | 
							
								#include "os_types.h"
							 | 
						||
| 
								 | 
							
								#include "iot_io.h"
							 | 
						||
| 
								 | 
							
								#include "mbedtls/sha512.h"
							 | 
						||
| 
								 | 
							
								#include "sec_sys.h"
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								#define mbedtls_printf iot_printf
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static int sw_sha512 = 0;
							 | 
						||
| 
								 | 
							
								#if 0
							 | 
						||
| 
								 | 
							
								static unsigned char buf[1024];
							 | 
						||
| 
								 | 
							
								#endif
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * FIPS-180-2 test vectors
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								static const unsigned char sha512_test_buf[3][113] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    { "abc" },
							 | 
						||
| 
								 | 
							
								    { "abcdefghbcdefghicdefghijdefghijkefghijklfghijklmghijklmn"
							 | 
						||
| 
								 | 
							
								      "hijklmnoijklmnopjklmnopqklmnopqrlmnopqrsmnopqrstnopqrstu" },
							 | 
						||
| 
								 | 
							
								    { "" }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const int sha512_test_buflen[3] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    3, 112, 1000
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static const unsigned char sha512_test_sum[6][64] =
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    /*
							 | 
						||
| 
								 | 
							
								     * SHA-384 test vectors
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    { 0xCB, 0x00, 0x75, 0x3F, 0x45, 0xA3, 0x5E, 0x8B,
							 | 
						||
| 
								 | 
							
								      0xB5, 0xA0, 0x3D, 0x69, 0x9A, 0xC6, 0x50, 0x07,
							 | 
						||
| 
								 | 
							
								      0x27, 0x2C, 0x32, 0xAB, 0x0E, 0xDE, 0xD1, 0x63,
							 | 
						||
| 
								 | 
							
								      0x1A, 0x8B, 0x60, 0x5A, 0x43, 0xFF, 0x5B, 0xED,
							 | 
						||
| 
								 | 
							
								      0x80, 0x86, 0x07, 0x2B, 0xA1, 0xE7, 0xCC, 0x23,
							 | 
						||
| 
								 | 
							
								      0x58, 0xBA, 0xEC, 0xA1, 0x34, 0xC8, 0x25, 0xA7 },
							 | 
						||
| 
								 | 
							
								    { 0x09, 0x33, 0x0C, 0x33, 0xF7, 0x11, 0x47, 0xE8,
							 | 
						||
| 
								 | 
							
								      0x3D, 0x19, 0x2F, 0xC7, 0x82, 0xCD, 0x1B, 0x47,
							 | 
						||
| 
								 | 
							
								      0x53, 0x11, 0x1B, 0x17, 0x3B, 0x3B, 0x05, 0xD2,
							 | 
						||
| 
								 | 
							
								      0x2F, 0xA0, 0x80, 0x86, 0xE3, 0xB0, 0xF7, 0x12,
							 | 
						||
| 
								 | 
							
								      0xFC, 0xC7, 0xC7, 0x1A, 0x55, 0x7E, 0x2D, 0xB9,
							 | 
						||
| 
								 | 
							
								      0x66, 0xC3, 0xE9, 0xFA, 0x91, 0x74, 0x60, 0x39 },
							 | 
						||
| 
								 | 
							
								    { 0x9D, 0x0E, 0x18, 0x09, 0x71, 0x64, 0x74, 0xCB,
							 | 
						||
| 
								 | 
							
								      0x08, 0x6E, 0x83, 0x4E, 0x31, 0x0A, 0x4A, 0x1C,
							 | 
						||
| 
								 | 
							
								      0xED, 0x14, 0x9E, 0x9C, 0x00, 0xF2, 0x48, 0x52,
							 | 
						||
| 
								 | 
							
								      0x79, 0x72, 0xCE, 0xC5, 0x70, 0x4C, 0x2A, 0x5B,
							 | 
						||
| 
								 | 
							
								      0x07, 0xB8, 0xB3, 0xDC, 0x38, 0xEC, 0xC4, 0xEB,
							 | 
						||
| 
								 | 
							
								      0xAE, 0x97, 0xDD, 0xD8, 0x7F, 0x3D, 0x89, 0x85 },
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    /*
							 | 
						||
| 
								 | 
							
								     * SHA-512 test vectors
							 | 
						||
| 
								 | 
							
								     */
							 | 
						||
| 
								 | 
							
								    { 0xDD, 0xAF, 0x35, 0xA1, 0x93, 0x61, 0x7A, 0xBA,
							 | 
						||
| 
								 | 
							
								      0xCC, 0x41, 0x73, 0x49, 0xAE, 0x20, 0x41, 0x31,
							 | 
						||
| 
								 | 
							
								      0x12, 0xE6, 0xFA, 0x4E, 0x89, 0xA9, 0x7E, 0xA2,
							 | 
						||
| 
								 | 
							
								      0x0A, 0x9E, 0xEE, 0xE6, 0x4B, 0x55, 0xD3, 0x9A,
							 | 
						||
| 
								 | 
							
								      0x21, 0x92, 0x99, 0x2A, 0x27, 0x4F, 0xC1, 0xA8,
							 | 
						||
| 
								 | 
							
								      0x36, 0xBA, 0x3C, 0x23, 0xA3, 0xFE, 0xEB, 0xBD,
							 | 
						||
| 
								 | 
							
								      0x45, 0x4D, 0x44, 0x23, 0x64, 0x3C, 0xE8, 0x0E,
							 | 
						||
| 
								 | 
							
								      0x2A, 0x9A, 0xC9, 0x4F, 0xA5, 0x4C, 0xA4, 0x9F },
							 | 
						||
| 
								 | 
							
								    { 0x8E, 0x95, 0x9B, 0x75, 0xDA, 0xE3, 0x13, 0xDA,
							 | 
						||
| 
								 | 
							
								      0x8C, 0xF4, 0xF7, 0x28, 0x14, 0xFC, 0x14, 0x3F,
							 | 
						||
| 
								 | 
							
								      0x8F, 0x77, 0x79, 0xC6, 0xEB, 0x9F, 0x7F, 0xA1,
							 | 
						||
| 
								 | 
							
								      0x72, 0x99, 0xAE, 0xAD, 0xB6, 0x88, 0x90, 0x18,
							 | 
						||
| 
								 | 
							
								      0x50, 0x1D, 0x28, 0x9E, 0x49, 0x00, 0xF7, 0xE4,
							 | 
						||
| 
								 | 
							
								      0x33, 0x1B, 0x99, 0xDE, 0xC4, 0xB5, 0x43, 0x3A,
							 | 
						||
| 
								 | 
							
								      0xC7, 0xD3, 0x29, 0xEE, 0xB6, 0xDD, 0x26, 0x54,
							 | 
						||
| 
								 | 
							
								      0x5E, 0x96, 0xE5, 0x5B, 0x87, 0x4B, 0xE9, 0x09 },
							 | 
						||
| 
								 | 
							
								    { 0xE7, 0x18, 0x48, 0x3D, 0x0C, 0xE7, 0x69, 0x64,
							 | 
						||
| 
								 | 
							
								      0x4E, 0x2E, 0x42, 0xC7, 0xBC, 0x15, 0xB4, 0x63,
							 | 
						||
| 
								 | 
							
								      0x8E, 0x1F, 0x98, 0xB1, 0x3B, 0x20, 0x44, 0x28,
							 | 
						||
| 
								 | 
							
								      0x56, 0x32, 0xA8, 0x03, 0xAF, 0xA9, 0x73, 0xEB,
							 | 
						||
| 
								 | 
							
								      0xDE, 0x0F, 0xF2, 0x44, 0x87, 0x7E, 0xA6, 0x0A,
							 | 
						||
| 
								 | 
							
								      0x4C, 0xB0, 0x43, 0x2C, 0xE5, 0x77, 0xC3, 0x1B,
							 | 
						||
| 
								 | 
							
								      0xEB, 0x00, 0x9C, 0x5C, 0x2C, 0x49, 0xAA, 0x2E,
							 | 
						||
| 
								 | 
							
								      0x4E, 0xAD, 0xB2, 0x17, 0xAD, 0x8C, 0xC0, 0x9B }
							 | 
						||
| 
								 | 
							
								};
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								/*
							 | 
						||
| 
								 | 
							
								 * Checkup routine
							 | 
						||
| 
								 | 
							
								 */
							 | 
						||
| 
								 | 
							
								int mbedtls_sha512_self_test( int verbose )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    int i,j,k,ret = 0;
							 | 
						||
| 
								 | 
							
								    unsigned char sha512sum[64];
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_context ctx;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_init( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    for( i = 0; i < 6; i++ )
							 | 
						||
| 
								 | 
							
								    {
							 | 
						||
| 
								 | 
							
								        j = i % 3;
							 | 
						||
| 
								 | 
							
								        k = i < 3;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if(j == 2)
							 | 
						||
| 
								 | 
							
								        	continue;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if( verbose != 0 )
							 | 
						||
| 
								 | 
							
								            mbedtls_printf( "  SHA-%d test #%d: ", 512 - k * 128, j + 1 );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if(sw_sha512){
							 | 
						||
| 
								 | 
							
											mbedtls_sha512_starts( &ctx, k );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											mbedtls_sha512_update( &ctx, sha512_test_buf[j],
							 | 
						||
| 
								 | 
							
																 sha512_test_buflen[j] );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
											mbedtls_sha512_finish( &ctx, sha512sum );
							 | 
						||
| 
								 | 
							
								        }else{
							 | 
						||
| 
								 | 
							
								        	if(k){
							 | 
						||
| 
								 | 
							
								        	    sec_sys_sha384(sha512_test_buf[j], sha512_test_buflen[j], sha512sum);
							 | 
						||
| 
								 | 
							
								        	}else{
							 | 
						||
| 
								 | 
							
								        		sec_sys_sha512(sha512_test_buf[j], sha512_test_buflen[j], sha512sum);
							 | 
						||
| 
								 | 
							
								        	}
							 | 
						||
| 
								 | 
							
								        }
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								        if( memcmp( sha512sum, sha512_test_sum[i], 64 - k * 16 ) != 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:
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_free( &ctx );
							 | 
						||
| 
								 | 
							
								    return( ret );
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								static unsigned char test_buf_512[] = "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 sha512_test( int verbose )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    int  ret = 0;
							 | 
						||
| 
								 | 
							
								    unsigned char sha512sum[2][64];
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_context ctx;
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_init( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if( verbose != 0 )
							 | 
						||
| 
								 | 
							
										mbedtls_printf( "  SHA-%d test #%d: ", 512, 1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									mbedtls_sha512_starts( &ctx, 0 );
							 | 
						||
| 
								 | 
							
									mbedtls_sha512_update( &ctx, test_buf_512, sizeof(test_buf_512) );
							 | 
						||
| 
								 | 
							
									mbedtls_sha512_finish( &ctx, sha512sum[0] );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									sec_sys_sha512(test_buf_512, sizeof(test_buf_512), sha512sum[1]);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									if( memcmp( sha512sum[0], sha512sum[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:
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_free( &ctx );
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    return( ret );
							 | 
						||
| 
								 | 
							
								}
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								int sha512_main( )
							 | 
						||
| 
								 | 
							
								{
							 | 
						||
| 
								 | 
							
								    mbedtls_sha512_self_test(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
								    sha512_test(1);
							 | 
						||
| 
								 | 
							
								
							 | 
						||
| 
								 | 
							
									return 0;
							 | 
						||
| 
								 | 
							
								}
							 |