Lines Matching refs:ctx
27 void mbedtls_entropy_init(mbedtls_entropy_context *ctx) in mbedtls_entropy_init() argument
29 ctx->source_count = 0; in mbedtls_entropy_init()
30 memset(ctx->source, 0, sizeof(ctx->source)); in mbedtls_entropy_init()
33 mbedtls_mutex_init(&ctx->mutex); in mbedtls_entropy_init()
36 ctx->accumulator_started = 0; in mbedtls_entropy_init()
37 mbedtls_md_init(&ctx->accumulator); in mbedtls_entropy_init()
44 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL, in mbedtls_entropy_init()
49 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL, in mbedtls_entropy_init()
54 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL, in mbedtls_entropy_init()
57 ctx->initial_entropy_run = 0; in mbedtls_entropy_init()
62 void mbedtls_entropy_free(mbedtls_entropy_context *ctx) in mbedtls_entropy_free() argument
64 if (ctx == NULL) { in mbedtls_entropy_free()
70 if (ctx->accumulator_started == -1) { in mbedtls_entropy_free()
75 mbedtls_mutex_free(&ctx->mutex); in mbedtls_entropy_free()
77 mbedtls_md_free(&ctx->accumulator); in mbedtls_entropy_free()
79 ctx->initial_entropy_run = 0; in mbedtls_entropy_free()
81 ctx->source_count = 0; in mbedtls_entropy_free()
82 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source)); in mbedtls_entropy_free()
83 ctx->accumulator_started = -1; in mbedtls_entropy_free()
86 int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx, in mbedtls_entropy_add_source() argument
93 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { in mbedtls_entropy_add_source()
98 idx = ctx->source_count; in mbedtls_entropy_add_source()
104 ctx->source[idx].f_source = f_source; in mbedtls_entropy_add_source()
105 ctx->source[idx].p_source = p_source; in mbedtls_entropy_add_source()
106 ctx->source[idx].threshold = threshold; in mbedtls_entropy_add_source()
107 ctx->source[idx].strong = strong; in mbedtls_entropy_add_source()
109 ctx->source_count++; in mbedtls_entropy_add_source()
113 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { in mbedtls_entropy_add_source()
124 static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id, in entropy_update() argument
150 if (ctx->accumulator_started == 0) { in entropy_update()
151 ret = mbedtls_md_setup(&ctx->accumulator, in entropy_update()
156 ret = mbedtls_md_starts(&ctx->accumulator); in entropy_update()
160 ctx->accumulator_started = 1; in entropy_update()
162 if ((ret = mbedtls_md_update(&ctx->accumulator, header, 2)) != 0) { in entropy_update()
165 ret = mbedtls_md_update(&ctx->accumulator, p, use_len); in entropy_update()
173 int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx, in mbedtls_entropy_update_manual() argument
179 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { in mbedtls_entropy_update_manual()
184 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len); in mbedtls_entropy_update_manual()
187 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { in mbedtls_entropy_update_manual()
198 static int entropy_gather_internal(mbedtls_entropy_context *ctx) in entropy_gather_internal() argument
206 if (ctx->source_count == 0) { in entropy_gather_internal()
213 for (i = 0; i < ctx->source_count; i++) { in entropy_gather_internal()
214 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { in entropy_gather_internal()
219 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source, in entropy_gather_internal()
228 if ((ret = entropy_update(ctx, (unsigned char) i, in entropy_gather_internal()
232 ctx->source[i].size += olen; in entropy_gather_internal()
249 int mbedtls_entropy_gather(mbedtls_entropy_context *ctx) in mbedtls_entropy_gather() argument
254 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { in mbedtls_entropy_gather()
259 ret = entropy_gather_internal(ctx); in mbedtls_entropy_gather()
262 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { in mbedtls_entropy_gather()
274 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data; in mbedtls_entropy_func() local
285 if (ctx->initial_entropy_run == 0) { in mbedtls_entropy_func()
286 ctx->initial_entropy_run = 1; in mbedtls_entropy_func()
287 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) { in mbedtls_entropy_func()
294 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) { in mbedtls_entropy_func()
308 if ((ret = entropy_gather_internal(ctx)) != 0) { in mbedtls_entropy_func()
314 for (i = 0; i < ctx->source_count; i++) { in mbedtls_entropy_func()
315 if (ctx->source[i].size < ctx->source[i].threshold) { in mbedtls_entropy_func()
318 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) { in mbedtls_entropy_func()
319 strong_size += ctx->source[i].size; in mbedtls_entropy_func()
331 if ((ret = mbedtls_md_finish(&ctx->accumulator, buf)) != 0) { in mbedtls_entropy_func()
338 mbedtls_md_free(&ctx->accumulator); in mbedtls_entropy_func()
339 mbedtls_md_init(&ctx->accumulator); in mbedtls_entropy_func()
340 ret = mbedtls_md_setup(&ctx->accumulator, in mbedtls_entropy_func()
345 ret = mbedtls_md_starts(&ctx->accumulator); in mbedtls_entropy_func()
349 if ((ret = mbedtls_md_update(&ctx->accumulator, buf, in mbedtls_entropy_func()
362 for (i = 0; i < ctx->source_count; i++) { in mbedtls_entropy_func()
363 ctx->source[i].size = 0; in mbedtls_entropy_func()
374 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) { in mbedtls_entropy_func()
383 int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx) in mbedtls_entropy_update_nv_seed() argument
389 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) { in mbedtls_entropy_update_nv_seed()
399 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE); in mbedtls_entropy_update_nv_seed()
406 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()
442 int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path) in mbedtls_entropy_update_seed_file() argument
467 ret = mbedtls_entropy_update_manual(ctx, buf, n); in mbedtls_entropy_update_seed_file()
478 return mbedtls_entropy_write_seed_file(ctx, path); in mbedtls_entropy_update_seed_file()
606 mbedtls_entropy_context ctx; in mbedtls_entropy_self_test() local
615 mbedtls_entropy_init(&ctx); in mbedtls_entropy_self_test()
618 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) { in mbedtls_entropy_self_test()
622 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16, in mbedtls_entropy_self_test()
628 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) { in mbedtls_entropy_self_test()
641 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) { in mbedtls_entropy_self_test()
664 mbedtls_entropy_free(&ctx); in mbedtls_entropy_self_test()