chore: import local project into Gitea
This commit is contained in:
@@ -0,0 +1,152 @@
|
||||
/*
|
||||
* Copyright (C) 2020, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#include <test/helpers.h>
|
||||
#include <test/macros.h>
|
||||
#include <string.h>
|
||||
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
static mbedtls_platform_context platform_ctx;
|
||||
#endif
|
||||
|
||||
int mbedtls_test_platform_setup( void )
|
||||
{
|
||||
int ret = 0;
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
ret = mbedtls_platform_setup( &platform_ctx );
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
return( ret );
|
||||
}
|
||||
|
||||
void mbedtls_test_platform_teardown( void )
|
||||
{
|
||||
#if defined(MBEDTLS_PLATFORM_C)
|
||||
mbedtls_platform_teardown( &platform_ctx );
|
||||
#endif /* MBEDTLS_PLATFORM_C */
|
||||
}
|
||||
|
||||
int mbedtls_test_unhexify( unsigned char *obuf, const char *ibuf )
|
||||
{
|
||||
unsigned char c, c2;
|
||||
int len = strlen( ibuf ) / 2;
|
||||
TEST_HELPER_ASSERT( strlen( ibuf ) % 2 == 0 ); /* must be even number of bytes */
|
||||
|
||||
while( *ibuf != 0 )
|
||||
{
|
||||
c = *ibuf++;
|
||||
if( c >= '0' && c <= '9' )
|
||||
c -= '0';
|
||||
else if( c >= 'a' && c <= 'f' )
|
||||
c -= 'a' - 10;
|
||||
else if( c >= 'A' && c <= 'F' )
|
||||
c -= 'A' - 10;
|
||||
else
|
||||
TEST_HELPER_ASSERT( 0 );
|
||||
|
||||
c2 = *ibuf++;
|
||||
if( c2 >= '0' && c2 <= '9' )
|
||||
c2 -= '0';
|
||||
else if( c2 >= 'a' && c2 <= 'f' )
|
||||
c2 -= 'a' - 10;
|
||||
else if( c2 >= 'A' && c2 <= 'F' )
|
||||
c2 -= 'A' - 10;
|
||||
else
|
||||
TEST_HELPER_ASSERT( 0 );
|
||||
|
||||
*obuf++ = ( c << 4 ) | c2;
|
||||
}
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
void mbedtls_test_hexify( unsigned char *obuf,
|
||||
const unsigned char *ibuf,
|
||||
int len )
|
||||
{
|
||||
unsigned char l, h;
|
||||
|
||||
while( len != 0 )
|
||||
{
|
||||
h = *ibuf / 16;
|
||||
l = *ibuf % 16;
|
||||
|
||||
if( h < 10 )
|
||||
*obuf++ = '0' + h;
|
||||
else
|
||||
*obuf++ = 'a' + h - 10;
|
||||
|
||||
if( l < 10 )
|
||||
*obuf++ = '0' + l;
|
||||
else
|
||||
*obuf++ = 'a' + l - 10;
|
||||
|
||||
++ibuf;
|
||||
len--;
|
||||
}
|
||||
}
|
||||
|
||||
unsigned char *mbedtls_test_zero_alloc( size_t len )
|
||||
{
|
||||
void *p;
|
||||
size_t actual_len = ( len != 0 ) ? len : 1;
|
||||
|
||||
p = mbedtls_calloc( 1, actual_len );
|
||||
TEST_HELPER_ASSERT( p != NULL );
|
||||
|
||||
memset( p, 0x00, actual_len );
|
||||
|
||||
return( p );
|
||||
}
|
||||
|
||||
unsigned char *mbedtls_test_unhexify_alloc( const char *ibuf, size_t *olen )
|
||||
{
|
||||
unsigned char *obuf;
|
||||
|
||||
*olen = strlen( ibuf ) / 2;
|
||||
|
||||
if( *olen == 0 )
|
||||
return( mbedtls_test_zero_alloc( *olen ) );
|
||||
|
||||
obuf = mbedtls_calloc( 1, *olen );
|
||||
TEST_HELPER_ASSERT( obuf != NULL );
|
||||
|
||||
(void) mbedtls_test_unhexify( obuf, ibuf );
|
||||
|
||||
return( obuf );
|
||||
}
|
||||
|
||||
int mbedtls_test_hexcmp( uint8_t * a, uint8_t * b,
|
||||
uint32_t a_len, uint32_t b_len )
|
||||
{
|
||||
int ret = 0;
|
||||
uint32_t i = 0;
|
||||
|
||||
if( a_len != b_len )
|
||||
return( -1 );
|
||||
|
||||
for( i = 0; i < a_len; i++ )
|
||||
{
|
||||
if( a[i] != b[i] )
|
||||
{
|
||||
ret = -1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
return ret;
|
||||
}
|
||||
@@ -0,0 +1,128 @@
|
||||
/**
|
||||
* \file random.c
|
||||
*
|
||||
* \brief This file contains the helper functions to generate random numbers
|
||||
* for the purpose of testing.
|
||||
*/
|
||||
|
||||
/*
|
||||
* Copyright (C) 2020, ARM Limited, All Rights Reserved
|
||||
* SPDX-License-Identifier: Apache-2.0
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License"); you may
|
||||
* not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
|
||||
* WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*
|
||||
* This file is part of mbed TLS (https://tls.mbed.org)
|
||||
*/
|
||||
|
||||
#include <test/macros.h>
|
||||
#include <test/random.h>
|
||||
#include <string.h>
|
||||
|
||||
int mbedtls_test_rnd_std_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len )
|
||||
{
|
||||
#if !defined(__OpenBSD__)
|
||||
size_t i;
|
||||
|
||||
if( rng_state != NULL )
|
||||
rng_state = NULL;
|
||||
|
||||
for( i = 0; i < len; ++i )
|
||||
output[i] = rand();
|
||||
#else
|
||||
if( rng_state != NULL )
|
||||
rng_state = NULL;
|
||||
|
||||
arc4random_buf( output, len );
|
||||
#endif /* !OpenBSD */
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_test_rnd_zero_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len )
|
||||
{
|
||||
if( rng_state != NULL )
|
||||
rng_state = NULL;
|
||||
|
||||
memset( output, 0, len );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_test_rnd_buffer_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len )
|
||||
{
|
||||
mbedtls_test_rnd_buf_info *info = (mbedtls_test_rnd_buf_info *) rng_state;
|
||||
size_t use_len;
|
||||
|
||||
if( rng_state == NULL )
|
||||
return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
|
||||
|
||||
use_len = len;
|
||||
if( len > info->length )
|
||||
use_len = info->length;
|
||||
|
||||
if( use_len )
|
||||
{
|
||||
memcpy( output, info->buf, use_len );
|
||||
info->buf += use_len;
|
||||
info->length -= use_len;
|
||||
}
|
||||
|
||||
if( len - use_len > 0 )
|
||||
return( mbedtls_test_rnd_std_rand( NULL, output + use_len,
|
||||
len - use_len ) );
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
int mbedtls_test_rnd_pseudo_rand( void *rng_state,
|
||||
unsigned char *output,
|
||||
size_t len )
|
||||
{
|
||||
mbedtls_test_rnd_pseudo_info *info =
|
||||
(mbedtls_test_rnd_pseudo_info *) rng_state;
|
||||
uint32_t i, *k, sum, delta=0x9E3779B9;
|
||||
unsigned char result[4], *out = output;
|
||||
|
||||
if( rng_state == NULL )
|
||||
return( mbedtls_test_rnd_std_rand( NULL, output, len ) );
|
||||
|
||||
k = info->key;
|
||||
|
||||
while( len > 0 )
|
||||
{
|
||||
size_t use_len = ( len > 4 ) ? 4 : len;
|
||||
sum = 0;
|
||||
|
||||
for( i = 0; i < 32; i++ )
|
||||
{
|
||||
info->v0 += ( ( ( info->v1 << 4 ) ^ ( info->v1 >> 5 ) )
|
||||
+ info->v1 ) ^ ( sum + k[sum & 3] );
|
||||
sum += delta;
|
||||
info->v1 += ( ( ( info->v0 << 4 ) ^ ( info->v0 >> 5 ) )
|
||||
+ info->v0 ) ^ ( sum + k[( sum>>11 ) & 3] );
|
||||
}
|
||||
|
||||
PUT_UINT32_BE( info->v0, result, 0 );
|
||||
memcpy( out, result, use_len );
|
||||
len -= use_len;
|
||||
out += 4;
|
||||
}
|
||||
|
||||
return( 0 );
|
||||
}
|
||||
Reference in New Issue
Block a user