Lines Matching refs:ctx
52 void mbedtls_hmac_drbg_init( mbedtls_hmac_drbg_context *ctx ) in mbedtls_hmac_drbg_init() argument
54 memset( ctx, 0, sizeof( mbedtls_hmac_drbg_context ) ); in mbedtls_hmac_drbg_init()
56 ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL; in mbedtls_hmac_drbg_init()
62 int mbedtls_hmac_drbg_update_ret( mbedtls_hmac_drbg_context *ctx, in mbedtls_hmac_drbg_update_ret() argument
66 size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info ); in mbedtls_hmac_drbg_update_ret()
75 if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 ) in mbedtls_hmac_drbg_update_ret()
77 if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, in mbedtls_hmac_drbg_update_ret()
78 ctx->V, md_len ) ) != 0 ) in mbedtls_hmac_drbg_update_ret()
80 if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, in mbedtls_hmac_drbg_update_ret()
85 if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, in mbedtls_hmac_drbg_update_ret()
89 if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, K ) ) != 0 ) in mbedtls_hmac_drbg_update_ret()
93 if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, K, md_len ) ) != 0 ) in mbedtls_hmac_drbg_update_ret()
95 if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, in mbedtls_hmac_drbg_update_ret()
96 ctx->V, md_len ) ) != 0 ) in mbedtls_hmac_drbg_update_ret()
98 if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 ) in mbedtls_hmac_drbg_update_ret()
108 void mbedtls_hmac_drbg_update( mbedtls_hmac_drbg_context *ctx, in mbedtls_hmac_drbg_update() argument
112 (void) mbedtls_hmac_drbg_update_ret( ctx, additional, add_len ); in mbedtls_hmac_drbg_update()
119 int mbedtls_hmac_drbg_seed_buf( mbedtls_hmac_drbg_context *ctx, in mbedtls_hmac_drbg_seed_buf() argument
125 if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 ) in mbedtls_hmac_drbg_seed_buf()
129 mbedtls_mutex_init( &ctx->mutex ); in mbedtls_hmac_drbg_seed_buf()
137 if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, in mbedtls_hmac_drbg_seed_buf()
140 memset( ctx->V, 0x01, mbedtls_md_get_size( md_info ) ); in mbedtls_hmac_drbg_seed_buf()
142 if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, data, data_len ) ) != 0 ) in mbedtls_hmac_drbg_seed_buf()
153 static int hmac_drbg_reseed_core( mbedtls_hmac_drbg_context *ctx, in hmac_drbg_reseed_core() argument
165 total_entropy_len = ctx->entropy_len; in hmac_drbg_reseed_core()
167 total_entropy_len = ctx->entropy_len * 3 / 2; in hmac_drbg_reseed_core()
180 if( ( ret = ctx->f_entropy( ctx->p_entropy, in hmac_drbg_reseed_core()
181 seed, ctx->entropy_len ) ) != 0 ) in hmac_drbg_reseed_core()
185 seedlen += ctx->entropy_len; in hmac_drbg_reseed_core()
198 if( ( ret = ctx->f_entropy( ctx->p_entropy, in hmac_drbg_reseed_core()
200 ctx->entropy_len / 2 ) ) != 0 ) in hmac_drbg_reseed_core()
205 seedlen += ctx->entropy_len / 2; in hmac_drbg_reseed_core()
217 if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, seed, seedlen ) ) != 0 ) in hmac_drbg_reseed_core()
221 ctx->reseed_counter = 1; in hmac_drbg_reseed_core()
232 int mbedtls_hmac_drbg_reseed( mbedtls_hmac_drbg_context *ctx, in mbedtls_hmac_drbg_reseed() argument
235 return( hmac_drbg_reseed_core( ctx, additional, len, 0 ) ); in mbedtls_hmac_drbg_reseed()
244 int mbedtls_hmac_drbg_seed( mbedtls_hmac_drbg_context *ctx, in mbedtls_hmac_drbg_seed() argument
254 if( ( ret = mbedtls_md_setup( &ctx->md_ctx, md_info, 1 ) ) != 0 ) in mbedtls_hmac_drbg_seed()
259 mbedtls_mutex_init( &ctx->mutex ); in mbedtls_hmac_drbg_seed()
269 if( ( ret = mbedtls_md_hmac_starts( &ctx->md_ctx, ctx->V, md_size ) ) != 0 ) in mbedtls_hmac_drbg_seed()
271 memset( ctx->V, 0x01, md_size ); in mbedtls_hmac_drbg_seed()
273 ctx->f_entropy = f_entropy; in mbedtls_hmac_drbg_seed()
274 ctx->p_entropy = p_entropy; in mbedtls_hmac_drbg_seed()
276 if( ctx->entropy_len == 0 ) in mbedtls_hmac_drbg_seed()
285 ctx->entropy_len = md_size <= 20 ? 16 : /* 160-bits hash -> 128 bits */ in mbedtls_hmac_drbg_seed()
290 if( ( ret = hmac_drbg_reseed_core( ctx, custom, len, in mbedtls_hmac_drbg_seed()
302 void mbedtls_hmac_drbg_set_prediction_resistance( mbedtls_hmac_drbg_context *ctx, in mbedtls_hmac_drbg_set_prediction_resistance() argument
305 ctx->prediction_resistance = resistance; in mbedtls_hmac_drbg_set_prediction_resistance()
311 void mbedtls_hmac_drbg_set_entropy_len( mbedtls_hmac_drbg_context *ctx, size_t len ) in mbedtls_hmac_drbg_set_entropy_len() argument
313 ctx->entropy_len = len; in mbedtls_hmac_drbg_set_entropy_len()
319 void mbedtls_hmac_drbg_set_reseed_interval( mbedtls_hmac_drbg_context *ctx, int interval ) in mbedtls_hmac_drbg_set_reseed_interval() argument
321 ctx->reseed_interval = interval; in mbedtls_hmac_drbg_set_reseed_interval()
333 mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng; in mbedtls_hmac_drbg_random_with_add() local
334 size_t md_len = mbedtls_md_get_size( ctx->md_ctx.md_info ); in mbedtls_hmac_drbg_random_with_add()
347 if( ctx->f_entropy != NULL && /* For no-reseeding instances */ in mbedtls_hmac_drbg_random_with_add()
348 ( ctx->prediction_resistance == MBEDTLS_HMAC_DRBG_PR_ON || in mbedtls_hmac_drbg_random_with_add()
349 ctx->reseed_counter > ctx->reseed_interval ) ) in mbedtls_hmac_drbg_random_with_add()
351 if( ( ret = mbedtls_hmac_drbg_reseed( ctx, additional, add_len ) ) != 0 ) in mbedtls_hmac_drbg_random_with_add()
360 if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, in mbedtls_hmac_drbg_random_with_add()
370 if( ( ret = mbedtls_md_hmac_reset( &ctx->md_ctx ) ) != 0 ) in mbedtls_hmac_drbg_random_with_add()
372 if( ( ret = mbedtls_md_hmac_update( &ctx->md_ctx, in mbedtls_hmac_drbg_random_with_add()
373 ctx->V, md_len ) ) != 0 ) in mbedtls_hmac_drbg_random_with_add()
375 if( ( ret = mbedtls_md_hmac_finish( &ctx->md_ctx, ctx->V ) ) != 0 ) in mbedtls_hmac_drbg_random_with_add()
378 memcpy( out, ctx->V, use_len ); in mbedtls_hmac_drbg_random_with_add()
384 if( ( ret = mbedtls_hmac_drbg_update_ret( ctx, in mbedtls_hmac_drbg_random_with_add()
389 ctx->reseed_counter++; in mbedtls_hmac_drbg_random_with_add()
402 mbedtls_hmac_drbg_context *ctx = (mbedtls_hmac_drbg_context *) p_rng; in mbedtls_hmac_drbg_random() local
405 if( ( ret = mbedtls_mutex_lock( &ctx->mutex ) ) != 0 ) in mbedtls_hmac_drbg_random()
409 ret = mbedtls_hmac_drbg_random_with_add( ctx, output, out_len, NULL, 0 ); in mbedtls_hmac_drbg_random()
412 if( mbedtls_mutex_unlock( &ctx->mutex ) != 0 ) in mbedtls_hmac_drbg_random()
423 void mbedtls_hmac_drbg_free( mbedtls_hmac_drbg_context *ctx ) in mbedtls_hmac_drbg_free() argument
425 if( ctx == NULL ) in mbedtls_hmac_drbg_free()
430 if( ctx->md_ctx.md_info != NULL ) in mbedtls_hmac_drbg_free()
431 mbedtls_mutex_free( &ctx->mutex ); in mbedtls_hmac_drbg_free()
433 mbedtls_md_free( &ctx->md_ctx ); in mbedtls_hmac_drbg_free()
434 mbedtls_platform_zeroize( ctx, sizeof( mbedtls_hmac_drbg_context ) ); in mbedtls_hmac_drbg_free()
435 ctx->reseed_interval = MBEDTLS_HMAC_DRBG_RESEED_INTERVAL; in mbedtls_hmac_drbg_free()
439 int mbedtls_hmac_drbg_write_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path ) in mbedtls_hmac_drbg_write_seed_file() argument
448 if( ( ret = mbedtls_hmac_drbg_random( ctx, buf, sizeof( buf ) ) ) != 0 ) in mbedtls_hmac_drbg_write_seed_file()
466 int mbedtls_hmac_drbg_update_seed_file( mbedtls_hmac_drbg_context *ctx, const char *path ) in mbedtls_hmac_drbg_update_seed_file() argument
491 ret = mbedtls_hmac_drbg_update_ret( ctx, buf, n ); in mbedtls_hmac_drbg_update_seed_file()
499 return( mbedtls_hmac_drbg_write_seed_file( ctx, path ) ); in mbedtls_hmac_drbg_update_seed_file()
571 mbedtls_hmac_drbg_context ctx; in mbedtls_hmac_drbg_self_test() local
575 mbedtls_hmac_drbg_init( &ctx ); in mbedtls_hmac_drbg_self_test()
584 CHK( mbedtls_hmac_drbg_seed( &ctx, md_info, in mbedtls_hmac_drbg_self_test()
587 mbedtls_hmac_drbg_set_prediction_resistance( &ctx, MBEDTLS_HMAC_DRBG_PR_ON ); in mbedtls_hmac_drbg_self_test()
588 CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); in mbedtls_hmac_drbg_self_test()
589 CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); in mbedtls_hmac_drbg_self_test()
591 mbedtls_hmac_drbg_free( &ctx ); in mbedtls_hmac_drbg_self_test()
593 mbedtls_hmac_drbg_free( &ctx ); in mbedtls_hmac_drbg_self_test()
604 mbedtls_hmac_drbg_init( &ctx ); in mbedtls_hmac_drbg_self_test()
607 CHK( mbedtls_hmac_drbg_seed( &ctx, md_info, in mbedtls_hmac_drbg_self_test()
610 CHK( mbedtls_hmac_drbg_reseed( &ctx, NULL, 0 ) ); in mbedtls_hmac_drbg_self_test()
611 CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); in mbedtls_hmac_drbg_self_test()
612 CHK( mbedtls_hmac_drbg_random( &ctx, buf, OUTPUT_LEN ) ); in mbedtls_hmac_drbg_self_test()
614 mbedtls_hmac_drbg_free( &ctx ); in mbedtls_hmac_drbg_self_test()
616 mbedtls_hmac_drbg_free( &ctx ); in mbedtls_hmac_drbg_self_test()