From 2ecc0b89f3cfdeaee7416c1df58c585c7788170a Mon Sep 17 00:00:00 2001 From: Gilles Peskine Date: Tue, 9 Feb 2021 18:44:02 +0100 Subject: [PATCH] Document mutex invariant for CTR_DRBG Signed-off-by: Gilles Peskine --- include/mbedtls/ctr_drbg.h | 7 +++++++ library/ctr_drbg.c | 2 ++ 2 files changed, 9 insertions(+) diff --git a/include/mbedtls/ctr_drbg.h b/include/mbedtls/ctr_drbg.h index 278fbbbb7..d8ef5d095 100644 --- a/include/mbedtls/ctr_drbg.h +++ b/include/mbedtls/ctr_drbg.h @@ -214,6 +214,13 @@ typedef struct mbedtls_ctr_drbg_context void *p_entropy; /*!< The context for the entropy function. */ #if defined(MBEDTLS_THREADING_C) + /* Invariant: the mutex is initialized if and only if f_entropy != NULL. + * This means that the mutex is initialized during the initial seeding + * in mbedtls_ctr_drbg_seed() and freed in mbedtls_ctr_drbg_free(). + * + * Note that this invariant may change without notice. Do not rely on it + * and do not access the mutex directly in application code. + */ mbedtls_threading_mutex_t mutex; #endif } diff --git a/library/ctr_drbg.c b/library/ctr_drbg.c index 5461b7121..90264e844 100644 --- a/library/ctr_drbg.c +++ b/library/ctr_drbg.c @@ -95,6 +95,7 @@ void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx ) return; #if defined(MBEDTLS_THREADING_C) + /* The mutex is initialized iff f_entropy is set. */ if( ctx->f_entropy != NULL ) mbedtls_mutex_free( &ctx->mutex ); #endif @@ -416,6 +417,7 @@ int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx, memset( key, 0, MBEDTLS_CTR_DRBG_KEYSIZE ); + /* The mutex is initialized iff f_entropy is set. */ #if defined(MBEDTLS_THREADING_C) mbedtls_mutex_init( &ctx->mutex ); #endif