Lines Matching refs:ctx
69 void mbedtls_entropy_init( mbedtls_entropy_context *ctx ) in mbedtls_entropy_init() argument
71 memset( ctx, 0, sizeof(mbedtls_entropy_context) ); in mbedtls_entropy_init()
74 mbedtls_mutex_init( &ctx->mutex ); in mbedtls_entropy_init()
78 mbedtls_sha512_starts( &ctx->accumulator, 0 ); in mbedtls_entropy_init()
80 mbedtls_sha256_starts( &ctx->accumulator, 0 ); in mbedtls_entropy_init()
83 mbedtls_havege_init( &ctx->havege_data ); in mbedtls_entropy_init()
87 mbedtls_entropy_add_source( ctx, mbedtls_null_entropy_poll, NULL, in mbedtls_entropy_init()
93 mbedtls_entropy_add_source( ctx, mbedtls_platform_entropy_poll, NULL, in mbedtls_entropy_init()
98 mbedtls_entropy_add_source( ctx, mbedtls_hardclock_poll, NULL, in mbedtls_entropy_init()
103 mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data, in mbedtls_entropy_init()
108 mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL, in mbedtls_entropy_init()
113 mbedtls_entropy_add_source( ctx, mbedtls_nv_seed_poll, NULL, in mbedtls_entropy_init()
120 void mbedtls_entropy_free( mbedtls_entropy_context *ctx ) in mbedtls_entropy_free() argument
123 mbedtls_havege_free( &ctx->havege_data ); in mbedtls_entropy_free()
126 mbedtls_mutex_free( &ctx->mutex ); in mbedtls_entropy_free()
128 mbedtls_zeroize( ctx, sizeof( mbedtls_entropy_context ) ); in mbedtls_entropy_free()
131 int mbedtls_entropy_add_source( mbedtls_entropy_context *ctx, in mbedtls_entropy_add_source() argument
138 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) in mbedtls_entropy_add_source()
142 index = ctx->source_count; in mbedtls_entropy_add_source()
149 ctx->source[index].f_source = f_source; in mbedtls_entropy_add_source()
150 ctx->source[index].p_source = p_source; in mbedtls_entropy_add_source()
151 ctx->source[index].threshold = threshold; in mbedtls_entropy_add_source()
152 ctx->source[index].strong = strong; in mbedtls_entropy_add_source()
154 ctx->source_count++; in mbedtls_entropy_add_source()
158 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) in mbedtls_entropy_add_source()
168 static int entropy_update( mbedtls_entropy_context *ctx, unsigned char source_id, in entropy_update() argument
191 mbedtls_sha512_update( &ctx->accumulator, header, 2 ); in entropy_update()
192 mbedtls_sha512_update( &ctx->accumulator, p, use_len ); in entropy_update()
194 mbedtls_sha256_update( &ctx->accumulator, header, 2 ); in entropy_update()
195 mbedtls_sha256_update( &ctx->accumulator, p, use_len ); in entropy_update()
201 int mbedtls_entropy_update_manual( mbedtls_entropy_context *ctx, in mbedtls_entropy_update_manual() argument
207 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) in mbedtls_entropy_update_manual()
211 ret = entropy_update( ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len ); in mbedtls_entropy_update_manual()
214 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) in mbedtls_entropy_update_manual()
224 static int entropy_gather_internal( mbedtls_entropy_context *ctx ) in entropy_gather_internal() argument
230 if( ctx->source_count == 0 ) in entropy_gather_internal()
236 for( i = 0; i < ctx->source_count; i++ ) in entropy_gather_internal()
238 if( ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG ) in entropy_gather_internal()
242 if( ( ret = ctx->source[i].f_source( ctx->source[i].p_source, in entropy_gather_internal()
253 entropy_update( ctx, (unsigned char) i, buf, olen ); in entropy_gather_internal()
254 ctx->source[i].size += olen; in entropy_gather_internal()
267 int mbedtls_entropy_gather( mbedtls_entropy_context *ctx ) in mbedtls_entropy_gather() argument
272 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) in mbedtls_entropy_gather()
276 ret = entropy_gather_internal( ctx ); in mbedtls_entropy_gather()
279 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) in mbedtls_entropy_gather()
289 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data; in mbedtls_entropy_func() local
299 if( ctx->initial_entropy_run == 0 ) in mbedtls_entropy_func()
301 ctx->initial_entropy_run = 1; in mbedtls_entropy_func()
302 if( ( ret = mbedtls_entropy_update_nv_seed( ctx ) ) != 0 ) in mbedtls_entropy_func()
308 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) in mbedtls_entropy_func()
323 if( ( ret = entropy_gather_internal( ctx ) ) != 0 ) in mbedtls_entropy_func()
327 for( i = 0; i < ctx->source_count; i++ ) in mbedtls_entropy_func()
328 if( ctx->source[i].size < ctx->source[i].threshold ) in mbedtls_entropy_func()
336 mbedtls_sha512_finish( &ctx->accumulator, buf ); in mbedtls_entropy_func()
341 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha512_context ) ); in mbedtls_entropy_func()
342 mbedtls_sha512_starts( &ctx->accumulator, 0 ); in mbedtls_entropy_func()
343 mbedtls_sha512_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); in mbedtls_entropy_func()
350 mbedtls_sha256_finish( &ctx->accumulator, buf ); in mbedtls_entropy_func()
355 memset( &ctx->accumulator, 0, sizeof( mbedtls_sha256_context ) ); in mbedtls_entropy_func()
356 mbedtls_sha256_starts( &ctx->accumulator, 0 ); in mbedtls_entropy_func()
357 mbedtls_sha256_update( &ctx->accumulator, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); in mbedtls_entropy_func()
365 for( i = 0; i < ctx->source_count; i++ ) in mbedtls_entropy_func()
366 ctx->source[i].size = 0; in mbedtls_entropy_func()
374 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) in mbedtls_entropy_func()
382 int mbedtls_entropy_update_nv_seed( mbedtls_entropy_context *ctx ) in mbedtls_entropy_update_nv_seed() argument
388 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 ) in mbedtls_entropy_update_nv_seed()
396 mbedtls_entropy_update_manual( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ); in mbedtls_entropy_update_nv_seed()
403 int mbedtls_entropy_write_seed_file( mbedtls_entropy_context *ctx, const char *path ) in mbedtls_entropy_write_seed_file() argument
412 if( ( ret = mbedtls_entropy_func( ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE ) ) != 0 ) in mbedtls_entropy_write_seed_file()
428 int mbedtls_entropy_update_seed_file( mbedtls_entropy_context *ctx, const char *path ) in mbedtls_entropy_update_seed_file() argument
452 mbedtls_entropy_update_manual( ctx, buf, n ); in mbedtls_entropy_update_seed_file()
454 return( mbedtls_entropy_write_seed_file( ctx, path ) ); in mbedtls_entropy_update_seed_file()
582 mbedtls_entropy_context ctx; in mbedtls_entropy_self_test() local
592 mbedtls_entropy_init( &ctx ); in mbedtls_entropy_self_test()
595 if( ( ret = mbedtls_entropy_gather( &ctx ) ) != 0 ) in mbedtls_entropy_self_test()
598 ret = mbedtls_entropy_add_source( &ctx, entropy_dummy_source, NULL, 16, in mbedtls_entropy_self_test()
603 if( ( ret = mbedtls_entropy_update_manual( &ctx, buf, sizeof buf ) ) != 0 ) in mbedtls_entropy_self_test()
616 if( ( ret = mbedtls_entropy_func( &ctx, buf, sizeof( buf ) ) ) != 0 ) in mbedtls_entropy_self_test()
638 mbedtls_entropy_free( &ctx ); in mbedtls_entropy_self_test()