mirror of
https://git.suyu.dev/suyu/mbedtls.git
synced 2025-12-24 00:06:32 +01:00
Merge remote-tracking branch 'public/pr/1902' into development
This commit is contained in:
commit
a07d86e8af
8 changed files with 774 additions and 223 deletions
File diff suppressed because it is too large
Load diff
|
|
@ -1,15 +1,97 @@
|
|||
/* BEGIN_HEADER */
|
||||
#include "mbedtls/entropy.h"
|
||||
#include "mbedtls/ctr_drbg.h"
|
||||
#include "string.h"
|
||||
|
||||
static int test_offset_idx;
|
||||
/* Modes for ctr_drbg_validate */
|
||||
enum reseed_mode
|
||||
{
|
||||
RESEED_NEVER, /* never reseed */
|
||||
RESEED_FIRST, /* instantiate, reseed, generate, generate */
|
||||
RESEED_SECOND, /* instantiate, generate, reseed, generate */
|
||||
RESEED_ALWAYS /* prediction resistance, no explicit reseed */
|
||||
};
|
||||
|
||||
static size_t test_offset_idx = 0;
|
||||
static size_t test_max_idx = 0;
|
||||
static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len )
|
||||
{
|
||||
const unsigned char *p = (unsigned char *) data;
|
||||
if( test_offset_idx + len > test_max_idx )
|
||||
return( MBEDTLS_ERR_ENTROPY_SOURCE_FAILED );
|
||||
memcpy( buf, p + test_offset_idx, len );
|
||||
test_offset_idx += len;
|
||||
return( 0 );
|
||||
}
|
||||
|
||||
static void ctr_drbg_validate_internal( int reseed_mode, data_t * nonce,
|
||||
int entropy_len_arg, data_t * entropy,
|
||||
data_t * reseed,
|
||||
data_t * add1, data_t * add2,
|
||||
data_t * result )
|
||||
{
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char buf[64];
|
||||
|
||||
size_t entropy_chunk_len = (size_t) entropy_len_arg;
|
||||
|
||||
TEST_ASSERT( entropy_chunk_len <= sizeof( buf ) );
|
||||
|
||||
test_offset_idx = 0;
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
|
||||
test_max_idx = entropy->len;
|
||||
|
||||
/* CTR_DRBG_Instantiate(entropy[:entropy->len], nonce, perso, <ignored>)
|
||||
* where nonce||perso = nonce[nonce->len] */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len(
|
||||
&ctx,
|
||||
mbedtls_test_entropy_func, entropy->x,
|
||||
nonce->x, nonce->len,
|
||||
entropy_chunk_len ) == 0 );
|
||||
if( reseed_mode == RESEED_ALWAYS )
|
||||
mbedtls_ctr_drbg_set_prediction_resistance(
|
||||
&ctx,
|
||||
MBEDTLS_CTR_DRBG_PR_ON );
|
||||
|
||||
if( reseed_mode == RESEED_FIRST )
|
||||
{
|
||||
/* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
|
||||
* reseed[:reseed->len]) */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_reseed(
|
||||
&ctx,
|
||||
reseed->x, reseed->len ) == 0 );
|
||||
}
|
||||
|
||||
/* CTR_DRBG_Generate(result->len * 8 bits, add1[:add1->len]) -> buf */
|
||||
/* Then reseed if prediction resistance is enabled. */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add(
|
||||
&ctx,
|
||||
buf, result->len,
|
||||
add1->x, add1->len ) == 0 );
|
||||
|
||||
|
||||
if( reseed_mode == RESEED_SECOND )
|
||||
{
|
||||
/* CTR_DRBG_Reseed(entropy[idx:idx+entropy->len],
|
||||
* reseed[:reseed->len]) */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_reseed(
|
||||
&ctx,
|
||||
reseed->x, reseed->len ) == 0 );
|
||||
}
|
||||
|
||||
/* CTR_DRBG_Generate(result->len * 8 bits, add2->x[:add2->len]) -> buf */
|
||||
/* Then reseed if prediction resistance is enabled. */
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add(
|
||||
&ctx,
|
||||
buf, result->len,
|
||||
add2->x, add2->len ) == 0 );
|
||||
TEST_ASSERT( memcmp( buf, result->x, result->len ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctx );
|
||||
}
|
||||
|
||||
/* END_HEADER */
|
||||
|
||||
/* BEGIN_DEPENDENCIES
|
||||
|
|
@ -18,7 +100,7 @@ static int mbedtls_test_entropy_func( void *data, unsigned char *buf, size_t len
|
|||
*/
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_special_behaviours( )
|
||||
void ctr_drbg_special_behaviours( )
|
||||
{
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char output[512];
|
||||
|
|
@ -50,54 +132,63 @@ exit:
|
|||
}
|
||||
/* END_CASE */
|
||||
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_validate_no_reseed( data_t * add_init, data_t * entropy,
|
||||
data_t * add1, data_t * add2,
|
||||
data_t * result_string )
|
||||
{
|
||||
data_t empty = { 0, 0 };
|
||||
ctr_drbg_validate_internal( RESEED_NEVER, add_init,
|
||||
entropy->len, entropy,
|
||||
&empty, add1, add2,
|
||||
result_string );
|
||||
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_validate_pr( data_t * add_init, data_t * entropy,
|
||||
data_t * add1, data_t * add2,
|
||||
data_t * result_str )
|
||||
data_t * result_string )
|
||||
{
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char buf[512];
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
|
||||
|
||||
test_offset_idx = 0;
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy->x, add_init->x, add_init->len, 32 ) == 0 );
|
||||
mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
|
||||
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1->x, add1->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2->x, add2->len ) == 0 );
|
||||
TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctx );
|
||||
data_t empty = { 0, 0 };
|
||||
ctr_drbg_validate_internal( RESEED_ALWAYS, add_init,
|
||||
entropy->len / 3, entropy,
|
||||
&empty, add1, add2,
|
||||
result_string );
|
||||
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_validate_nopr( data_t * add_init, data_t * entropy,
|
||||
void ctr_drbg_validate_reseed_between( data_t * add_init, data_t * entropy,
|
||||
data_t * add1, data_t * add_reseed,
|
||||
data_t * add2, data_t * result_str )
|
||||
data_t * add2, data_t * result_string )
|
||||
{
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
unsigned char buf[512];
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
|
||||
|
||||
test_offset_idx = 0;
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_test_entropy_func, entropy->x, add_init->x, add_init->len, 32 ) == 0 );
|
||||
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1->x, add1->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed->x, add_reseed->len ) == 0 );
|
||||
TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add2->x, add2->len ) == 0 );
|
||||
TEST_ASSERT( hexcmp( buf, result_str->x, 16, result_str->len ) == 0 );
|
||||
|
||||
exit:
|
||||
mbedtls_ctr_drbg_free( &ctx );
|
||||
ctr_drbg_validate_internal( RESEED_SECOND, add_init,
|
||||
entropy->len / 2, entropy,
|
||||
add_reseed, add1, add2,
|
||||
result_string );
|
||||
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_validate_reseed_first( data_t * add_init, data_t * entropy,
|
||||
data_t * add1, data_t * add_reseed,
|
||||
data_t * add2, data_t * result_string )
|
||||
{
|
||||
ctr_drbg_validate_internal( RESEED_FIRST, add_init,
|
||||
entropy->len / 2, entropy,
|
||||
add_reseed, add1, add2,
|
||||
result_string );
|
||||
goto exit; // goto is needed to avoid warning ( no test assertions in func)
|
||||
}
|
||||
/* END_CASE */
|
||||
|
||||
|
||||
|
||||
/* BEGIN_CASE */
|
||||
void ctr_drbg_entropy_usage( )
|
||||
{
|
||||
|
|
@ -106,10 +197,11 @@ void ctr_drbg_entropy_usage( )
|
|||
unsigned char entropy[1024];
|
||||
mbedtls_ctr_drbg_context ctx;
|
||||
size_t i, reps = 10;
|
||||
int last_idx;
|
||||
size_t last_idx;
|
||||
|
||||
mbedtls_ctr_drbg_init( &ctx );
|
||||
test_offset_idx = 0;
|
||||
test_max_idx = sizeof( entropy );
|
||||
memset( entropy, 0, sizeof( entropy ) );
|
||||
memset( out, 0, sizeof( out ) );
|
||||
memset( add, 0, sizeof( add ) );
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue