Lines Matching refs:ctx
27 void mbedtls_entropy_init(mbedtls_entropy_context *ctx)
29 ctx->source_count = 0;
30 memset(ctx->source, 0, sizeof(ctx->source));
33 mbedtls_mutex_init(&ctx->mutex);
36 ctx->accumulator_started = 0;
37 mbedtls_md_init(&ctx->accumulator);
44 mbedtls_entropy_add_source(ctx, mbedtls_platform_entropy_poll, NULL,
49 mbedtls_entropy_add_source(ctx, mbedtls_hardware_poll, NULL,
54 mbedtls_entropy_add_source(ctx, mbedtls_nv_seed_poll, NULL,
57 ctx->initial_entropy_run = 0;
62 void mbedtls_entropy_free(mbedtls_entropy_context *ctx)
66 if (ctx->accumulator_started == -1) {
71 mbedtls_mutex_free(&ctx->mutex);
73 mbedtls_md_free(&ctx->accumulator);
75 ctx->initial_entropy_run = 0;
77 ctx->source_count = 0;
78 mbedtls_platform_zeroize(ctx->source, sizeof(ctx->source));
79 ctx->accumulator_started = -1;
82 int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx,
89 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
94 idx = ctx->source_count;
100 ctx->source[idx].f_source = f_source;
101 ctx->source[idx].p_source = p_source;
102 ctx->source[idx].threshold = threshold;
103 ctx->source[idx].strong = strong;
105 ctx->source_count++;
109 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
120 static int entropy_update(mbedtls_entropy_context *ctx, unsigned char source_id,
146 if (ctx->accumulator_started == 0) {
147 ret = mbedtls_md_setup(&ctx->accumulator,
152 ret = mbedtls_md_starts(&ctx->accumulator);
156 ctx->accumulator_started = 1;
158 if ((ret = mbedtls_md_update(&ctx->accumulator, header, 2)) != 0) {
161 ret = mbedtls_md_update(&ctx->accumulator, p, use_len);
169 int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx,
175 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
180 ret = entropy_update(ctx, MBEDTLS_ENTROPY_SOURCE_MANUAL, data, len);
183 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
194 static int entropy_gather_internal(mbedtls_entropy_context *ctx)
202 if (ctx->source_count == 0) {
209 for (i = 0; i < ctx->source_count; i++) {
210 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
215 if ((ret = ctx->source[i].f_source(ctx->source[i].p_source,
224 if ((ret = entropy_update(ctx, (unsigned char) i,
228 ctx->source[i].size += olen;
245 int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
250 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
255 ret = entropy_gather_internal(ctx);
258 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
270 mbedtls_entropy_context *ctx = (mbedtls_entropy_context *) data;
281 if (ctx->initial_entropy_run == 0) {
282 ctx->initial_entropy_run = 1;
283 if ((ret = mbedtls_entropy_update_nv_seed(ctx)) != 0) {
290 if ((ret = mbedtls_mutex_lock(&ctx->mutex)) != 0) {
304 if ((ret = entropy_gather_internal(ctx)) != 0) {
310 for (i = 0; i < ctx->source_count; i++) {
311 if (ctx->source[i].size < ctx->source[i].threshold) {
314 if (ctx->source[i].strong == MBEDTLS_ENTROPY_SOURCE_STRONG) {
315 strong_size += ctx->source[i].size;
327 if ((ret = mbedtls_md_finish(&ctx->accumulator, buf)) != 0) {
334 mbedtls_md_free(&ctx->accumulator);
335 mbedtls_md_init(&ctx->accumulator);
336 ret = mbedtls_md_setup(&ctx->accumulator,
341 ret = mbedtls_md_starts(&ctx->accumulator);
345 if ((ret = mbedtls_md_update(&ctx->accumulator, buf,
358 for (i = 0; i < ctx->source_count; i++) {
359 ctx->source[i].size = 0;
370 if (mbedtls_mutex_unlock(&ctx->mutex) != 0) {
379 int mbedtls_entropy_update_nv_seed(mbedtls_entropy_context *ctx)
385 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
395 ret = mbedtls_entropy_update_manual(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE);
402 int mbedtls_entropy_write_seed_file(mbedtls_entropy_context *ctx, const char *path)
408 if ((ret = mbedtls_entropy_func(ctx, buf, MBEDTLS_ENTROPY_BLOCK_SIZE)) != 0) {
438 int mbedtls_entropy_update_seed_file(mbedtls_entropy_context *ctx, const char *path)
463 ret = mbedtls_entropy_update_manual(ctx, buf, n);
474 return mbedtls_entropy_write_seed_file(ctx, path);
602 mbedtls_entropy_context ctx;
611 mbedtls_entropy_init(&ctx);
614 if ((ret = mbedtls_entropy_gather(&ctx)) != 0) {
618 ret = mbedtls_entropy_add_source(&ctx, entropy_dummy_source, NULL, 16,
624 if ((ret = mbedtls_entropy_update_manual(&ctx, buf, sizeof(buf))) != 0) {
637 if ((ret = mbedtls_entropy_func(&ctx, buf, sizeof(buf))) != 0) {
660 mbedtls_entropy_free(&ctx);