From 3f77dfbd520b1360dd05b06e23004dd23846b0ea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Manuel=20P=C3=A9gouri=C3=A9-Gonnard?= Date: Fri, 19 Jun 2015 10:06:21 +0200 Subject: [PATCH] Add MBEDTLS_ENTROPY_HARDWARE_ALT Makes it easier for an external module to plug its hardware entropy collector. --- include/mbedtls/config.h | 13 +++++++++++++ include/mbedtls/entropy_poll.h | 14 ++++++++++++++ library/entropy.c | 4 ++++ 3 files changed, 31 insertions(+) diff --git a/include/mbedtls/config.h b/include/mbedtls/config.h index 3c0fb136b..f1cfe75b7 100644 --- a/include/mbedtls/config.h +++ b/include/mbedtls/config.h @@ -277,6 +277,19 @@ //#define MBEDTLS_AES_ENCRYPT_ALT //#define MBEDTLS_AES_DECRYPT_ALT +/** + * \def MBEDTLS_EMTROPY_HARDWARE_ALT + * + * Uncomment this macro to let mbed TLS use your own implementation of a + * hardware entropy collector. + * + * Your function must be called \c mbedtls_hardware_poll(), have the same + * prototype as declared in entropy_poll.h, and accept NULL as first argument. + * + * Uncomment to use your own hardware entropy collector. + */ +//#define MBEDTLS_ENTROPY_HARDWARE_ALT + /** * \def MBEDTLS_AES_ROM_TABLES * diff --git a/include/mbedtls/entropy_poll.h b/include/mbedtls/entropy_poll.h index 231042eee..8ee1e1a25 100644 --- a/include/mbedtls/entropy_poll.h +++ b/include/mbedtls/entropy_poll.h @@ -42,6 +42,7 @@ extern "C" { #define MBEDTLS_ENTROPY_MIN_PLATFORM 32 /**< Minimum for platform source */ #define MBEDTLS_ENTROPY_MIN_HAVEGE 32 /**< Minimum for HAVEGE */ #define MBEDTLS_ENTROPY_MIN_HARDCLOCK 4 /**< Minimum for mbedtls_timing_hardclock() */ +#define MBEDTLS_ENTROPY_MIN_HARDWARE 32 /**< Minimum for the hardware source */ #if !defined(MBEDTLS_NO_PLATFORM_ENTROPY) /** @@ -69,6 +70,19 @@ int mbedtls_hardclock_poll( void *data, unsigned char *output, size_t len, size_t *olen ); #endif +#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) +/** + * \brief Entropy poll callback for a hardware source + * + * \warning This is not provided by mbed TLS! + * See \c MBEDTLS_ENTROPY_HARDWARE_ALT in config.h. + * + * \note This must accept NULL as its first argument. + */ +int mbedtls_hardware_poll( void *data, + unsigned char *output, size_t len, size_t *olen ); +#endif + #ifdef __cplusplus } #endif diff --git a/library/entropy.c b/library/entropy.c index fa3dcde0a..4dddb7507 100644 --- a/library/entropy.c +++ b/library/entropy.c @@ -86,6 +86,10 @@ void mbedtls_entropy_init( mbedtls_entropy_context *ctx ) mbedtls_entropy_add_source( ctx, mbedtls_havege_poll, &ctx->havege_data, MBEDTLS_ENTROPY_MIN_HAVEGE ); #endif +#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT) + mbedtls_entropy_add_source( ctx, mbedtls_hardware_poll, NULL + MBEDTLS_ENTROPY_MIN_HARDWARE ); +#endif #endif /* MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES */ }