mirror of
				https://github.com/cuberite/polarssl.git
				synced 2025-11-03 20:22:59 -05:00 
			
		
		
		
	Split mbedtls_ctr_drbg_init() -> seed()
This commit is contained in:
		
							parent
							
								
									f9e9481bc5
								
							
						
					
					
						commit
						8d128efd48
					
				@ -18,6 +18,7 @@ API Changes
 | 
			
		||||
     mbedtls_ccm_init() -> mbedtls_ccm_setkey()
 | 
			
		||||
     mbedtls_gcm_init() -> mbedtls_gcm_setkey()
 | 
			
		||||
     mbedtls_hmac_drbg_init() -> mbedtls_hmac_drbg_init(_buf)()
 | 
			
		||||
     mbedtls_ctr_drbg_init()  -> mbedtls_ctr_drbg_init(_buf)()
 | 
			
		||||
   * In the threading layer, mbedtls_mutex_init() and mbedtls_mutex_free() now
 | 
			
		||||
     return void.
 | 
			
		||||
   * ecdsa_write_signature() gained an addtional md_alg argument and
 | 
			
		||||
 | 
			
		||||
@ -103,12 +103,22 @@ typedef struct
 | 
			
		||||
mbedtls_ctr_drbg_context;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \brief               CTR_DRBG initialization
 | 
			
		||||
 * \brief               CTR_DRBG context initialization
 | 
			
		||||
 *                      Makes the context ready for mbetls_ctr_drbg_seed() or
 | 
			
		||||
 *                      mbedtls_ctr_drbg_free().
 | 
			
		||||
 *
 | 
			
		||||
 * \param ctx           CTR_DRBG context to be initialized
 | 
			
		||||
 */
 | 
			
		||||
void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \brief               CTR_DRBG initial seeding
 | 
			
		||||
 *                      Seed and setup entropy source for future reseeds.
 | 
			
		||||
 *
 | 
			
		||||
 * Note: Personalization data can be provided in addition to the more generic
 | 
			
		||||
 *       entropy source to make this instantiation as unique as possible.
 | 
			
		||||
 *
 | 
			
		||||
 * \param ctx           CTR_DRBG context to be initialized
 | 
			
		||||
 * \param ctx           CTR_DRBG context to be seeded
 | 
			
		||||
 * \param f_entropy     Entropy callback (p_entropy, buffer to fill, buffer
 | 
			
		||||
 *                      length)
 | 
			
		||||
 * \param p_entropy     Entropy context
 | 
			
		||||
@ -119,7 +129,7 @@ mbedtls_ctr_drbg_context;
 | 
			
		||||
 * \return              0 if successful, or
 | 
			
		||||
 *                      MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
 | 
			
		||||
 */
 | 
			
		||||
int mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx,
 | 
			
		||||
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
 | 
			
		||||
                   int (*f_entropy)(void *, unsigned char *, size_t),
 | 
			
		||||
                   void *p_entropy,
 | 
			
		||||
                   const unsigned char *custom,
 | 
			
		||||
@ -262,7 +272,7 @@ int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char
 | 
			
		||||
int mbedtls_ctr_drbg_self_test( int verbose );
 | 
			
		||||
 | 
			
		||||
/* Internal functions (do not call directly) */
 | 
			
		||||
int mbedtls_ctr_drbg_init_entropy_len( mbedtls_ctr_drbg_context *,
 | 
			
		||||
int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
 | 
			
		||||
                               int (*)(void *, unsigned char *, size_t), void *,
 | 
			
		||||
                               const unsigned char *, size_t, size_t );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -90,7 +90,7 @@ typedef struct
 | 
			
		||||
} mbedtls_hmac_drbg_context;
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \brief               HMAC_DRBG initialization (just make references valid)
 | 
			
		||||
 * \brief               HMAC_DRBG context initialization
 | 
			
		||||
 *                      Makes the context ready for mbetls_hmac_drbg_seed(),
 | 
			
		||||
 *                      mbedtls_hmac_drbg_seed_buf() or
 | 
			
		||||
 *                      mbedtls_hmac_drbg_free().
 | 
			
		||||
@ -101,7 +101,7 @@ void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx );
 | 
			
		||||
 | 
			
		||||
/**
 | 
			
		||||
 * \brief               HMAC_DRBG initial seeding
 | 
			
		||||
 *                      Seed and setup entropy pool for later re-seeding.
 | 
			
		||||
 *                      Seed and setup entropy source for future reseeds.
 | 
			
		||||
 *
 | 
			
		||||
 * \param ctx           HMAC_DRBG context to be seeded
 | 
			
		||||
 * \param md_info       MD algorithm to use for HMAC_DRBG
 | 
			
		||||
 | 
			
		||||
@ -55,11 +55,19 @@ static void mbedtls_zeroize( void *v, size_t n ) {
 | 
			
		||||
    volatile unsigned char *p = v; while( n-- ) *p++ = 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * CTR_DRBG context initialization
 | 
			
		||||
 */
 | 
			
		||||
void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx )
 | 
			
		||||
{
 | 
			
		||||
    memset( ctx, 0, sizeof( mbedtls_ctr_drbg_context ) );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
/*
 | 
			
		||||
 * Non-public function wrapped by ctr_crbg_init(). Necessary to allow NIST
 | 
			
		||||
 * tests to succeed (which require known length fixed entropy)
 | 
			
		||||
 */
 | 
			
		||||
int mbedtls_ctr_drbg_init_entropy_len(
 | 
			
		||||
int mbedtls_ctr_drbg_seed_entropy_len(
 | 
			
		||||
                   mbedtls_ctr_drbg_context *ctx,
 | 
			
		||||
                   int (*f_entropy)(void *, unsigned char *, size_t),
 | 
			
		||||
                   void *p_entropy,
 | 
			
		||||
@ -92,13 +100,13 @@ int mbedtls_ctr_drbg_init_entropy_len(
 | 
			
		||||
    return( 0 );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
int mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx,
 | 
			
		||||
int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
 | 
			
		||||
                   int (*f_entropy)(void *, unsigned char *, size_t),
 | 
			
		||||
                   void *p_entropy,
 | 
			
		||||
                   const unsigned char *custom,
 | 
			
		||||
                   size_t len )
 | 
			
		||||
{
 | 
			
		||||
    return( mbedtls_ctr_drbg_init_entropy_len( ctx, f_entropy, p_entropy, custom, len,
 | 
			
		||||
    return( mbedtls_ctr_drbg_seed_entropy_len( ctx, f_entropy, p_entropy, custom, len,
 | 
			
		||||
                                       MBEDTLS_CTR_DRBG_ENTROPY_LEN ) );
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
@ -513,6 +521,8 @@ int mbedtls_ctr_drbg_self_test( int verbose )
 | 
			
		||||
    mbedtls_ctr_drbg_context ctx;
 | 
			
		||||
    unsigned char buf[16];
 | 
			
		||||
 | 
			
		||||
    mbedtls_ctr_drbg_init( &ctx );
 | 
			
		||||
 | 
			
		||||
    /*
 | 
			
		||||
     * Based on a NIST CTR_DRBG test vector (PR = True)
 | 
			
		||||
     */
 | 
			
		||||
@ -520,7 +530,7 @@ int mbedtls_ctr_drbg_self_test( int verbose )
 | 
			
		||||
        mbedtls_printf( "  CTR_DRBG (PR = TRUE) : " );
 | 
			
		||||
 | 
			
		||||
    test_offset = 0;
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
 | 
			
		||||
                                (void *) entropy_source_pr, nonce_pers_pr, 16, 32 ) );
 | 
			
		||||
    mbedtls_ctr_drbg_set_prediction_resistance( &ctx, MBEDTLS_CTR_DRBG_PR_ON );
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_random( &ctx, buf, MBEDTLS_CTR_DRBG_BLOCKSIZE ) );
 | 
			
		||||
@ -537,7 +547,7 @@ int mbedtls_ctr_drbg_self_test( int verbose )
 | 
			
		||||
        mbedtls_printf( "  CTR_DRBG (PR = FALSE): " );
 | 
			
		||||
 | 
			
		||||
    test_offset = 0;
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_init_entropy_len( &ctx, ctr_drbg_self_test_entropy,
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_seed_entropy_len( &ctx, ctr_drbg_self_test_entropy,
 | 
			
		||||
                            (void *) entropy_source_nopr, nonce_pers_nopr, 16, 32 ) );
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_random( &ctx, buf, 16 ) );
 | 
			
		||||
    CHK( mbedtls_ctr_drbg_reseed( &ctx, NULL, 0 ) );
 | 
			
		||||
 | 
			
		||||
@ -516,13 +516,15 @@ int main( int argc, char *argv[] )
 | 
			
		||||
    {
 | 
			
		||||
        mbedtls_ctr_drbg_context ctr_drbg;
 | 
			
		||||
 | 
			
		||||
        if( mbedtls_ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
 | 
			
		||||
        mbedtls_ctr_drbg_init( &ctr_drbg );
 | 
			
		||||
 | 
			
		||||
        if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
 | 
			
		||||
            mbedtls_exit(1);
 | 
			
		||||
        TIME_AND_TSC( "CTR_DRBG (NOPR)",
 | 
			
		||||
                if( mbedtls_ctr_drbg_random( &ctr_drbg, buf, BUFSIZE ) != 0 )
 | 
			
		||||
                mbedtls_exit(1) );
 | 
			
		||||
 | 
			
		||||
        if( mbedtls_ctr_drbg_init( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
 | 
			
		||||
        if( mbedtls_ctr_drbg_seed( &ctr_drbg, myrand, NULL, NULL, 0 ) != 0 )
 | 
			
		||||
            mbedtls_exit(1);
 | 
			
		||||
        mbedtls_ctr_drbg_set_prediction_resistance( &ctr_drbg, MBEDTLS_CTR_DRBG_PR_ON );
 | 
			
		||||
        TIME_AND_TSC( "CTR_DRBG (PR)",
 | 
			
		||||
 | 
			
		||||
@ -30,6 +30,7 @@ void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
 | 
			
		||||
    unsigned char output_str[512];
 | 
			
		||||
    int add_init_len, add1_len, add2_len;
 | 
			
		||||
 | 
			
		||||
    mbedtls_ctr_drbg_init( &ctx );
 | 
			
		||||
    memset( output_str, 0, 512 );
 | 
			
		||||
 | 
			
		||||
    unhexify( entropy, entropy_string );
 | 
			
		||||
@ -38,7 +39,7 @@ void ctr_drbg_validate_pr( char *add_init_string, char *entropy_string,
 | 
			
		||||
    add2_len = unhexify( add2, add2_string );
 | 
			
		||||
 | 
			
		||||
    test_offset_idx = 0;
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_init_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, 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, add1_len ) == 0 );
 | 
			
		||||
@ -66,6 +67,7 @@ void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
 | 
			
		||||
    unsigned char output_str[512];
 | 
			
		||||
    int add_init_len, add1_len, add_reseed_len, add2_len;
 | 
			
		||||
 | 
			
		||||
    mbedtls_ctr_drbg_init( &ctx );
 | 
			
		||||
    memset( output_str, 0, 512 );
 | 
			
		||||
 | 
			
		||||
    unhexify( entropy, entropy_string );
 | 
			
		||||
@ -75,7 +77,7 @@ void ctr_drbg_validate_nopr( char *add_init_string, char *entropy_string,
 | 
			
		||||
    add2_len = unhexify( add2, add2_string );
 | 
			
		||||
 | 
			
		||||
    test_offset_idx = 0;
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_init_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_seed_entropy_len( &ctx, mbedtls_entropy_func, entropy, add_init, add_init_len, 32 ) == 0 );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_random_with_add( &ctx, buf, 16, add1, add1_len ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_reseed( &ctx, add_reseed, add_reseed_len ) == 0 );
 | 
			
		||||
@ -98,6 +100,7 @@ void ctr_drbg_entropy_usage( )
 | 
			
		||||
    size_t i, reps = 10;
 | 
			
		||||
    int last_idx;
 | 
			
		||||
 | 
			
		||||
    mbedtls_ctr_drbg_init( &ctx );
 | 
			
		||||
    test_offset_idx = 0;
 | 
			
		||||
    memset( entropy, 0, sizeof( entropy ) );
 | 
			
		||||
    memset( out, 0, sizeof( out ) );
 | 
			
		||||
@ -105,7 +108,7 @@ void ctr_drbg_entropy_usage( )
 | 
			
		||||
 | 
			
		||||
    /* Init must use entropy */
 | 
			
		||||
    last_idx = test_offset_idx;
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_init( &ctx, mbedtls_entropy_func, entropy, NULL, 0 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, mbedtls_entropy_func, entropy, NULL, 0 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( last_idx < test_offset_idx );
 | 
			
		||||
 | 
			
		||||
    /* By default, PR is off and reseed_interval is large,
 | 
			
		||||
@ -171,7 +174,9 @@ void ctr_drbg_seed_file( char *path, int ret )
 | 
			
		||||
{
 | 
			
		||||
    mbedtls_ctr_drbg_context ctx;
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_init( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
 | 
			
		||||
    mbedtls_ctr_drbg_init( &ctx );
 | 
			
		||||
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctx, rnd_std_rand, NULL, NULL, 0 ) == 0 );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_write_seed_file( &ctx, path ) == ret );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_update_seed_file( &ctx, path ) == ret );
 | 
			
		||||
 | 
			
		||||
 | 
			
		||||
@ -666,8 +666,10 @@ void mbedtls_rsa_gen_key( int nrbits, int exponent, int result)
 | 
			
		||||
    mbedtls_ctr_drbg_context ctr_drbg;
 | 
			
		||||
    const char *pers = "test_suite_rsa";
 | 
			
		||||
 | 
			
		||||
    mbedtls_ctr_drbg_init( &ctr_drbg );
 | 
			
		||||
 | 
			
		||||
    mbedtls_entropy_init( &entropy );
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_init( &ctr_drbg, mbedtls_entropy_func, &entropy,
 | 
			
		||||
    TEST_ASSERT( mbedtls_ctr_drbg_seed( &ctr_drbg, mbedtls_entropy_func, &entropy,
 | 
			
		||||
                                (const unsigned char *) pers, strlen( pers ) ) == 0 );
 | 
			
		||||
 | 
			
		||||
    mbedtls_rsa_init( &ctx, 0, 0 );
 | 
			
		||||
 | 
			
		||||
		Loading…
	
	
			
			x
			
			
		
	
		Reference in New Issue
	
	Block a user