47#if !defined(MBEDTLS_CONFIG_FILE)
50#include MBEDTLS_CONFIG_FILE
53#if defined(MBEDTLS_ENTROPY_C)
55#if defined(MBEDTLS_TEST_NULL_ENTROPY)
56#warning "**** WARNING! MBEDTLS_TEST_NULL_ENTROPY defined! "
57#warning "**** THIS BUILD HAS NO DEFINED ENTROPY SOURCES "
58#warning "**** THIS BUILD IS *NOT* SUITABLE FOR PRODUCTION USE "
67#if defined(MBEDTLS_FS_IO)
71#if defined(MBEDTLS_ENTROPY_NV_SEED)
75#if defined(MBEDTLS_SELF_TEST)
76#if defined(MBEDTLS_PLATFORM_C)
80#define mbedtls_printf printf
84#if defined(MBEDTLS_HAVEGE_C)
88#define ENTROPY_MAX_LOOP 256
92 ctx->source_count = 0;
95#if defined(MBEDTLS_THREADING_C)
96 mbedtls_mutex_init( &
ctx->mutex );
99 ctx->accumulator_started = 0;
100#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
105#if defined(MBEDTLS_HAVEGE_C)
112#if defined(MBEDTLS_TEST_NULL_ENTROPY)
117#if !defined(MBEDTLS_NO_DEFAULT_ENTROPY_SOURCES)
118#if !defined(MBEDTLS_NO_PLATFORM_ENTROPY)
123#if defined(MBEDTLS_TIMING_C)
128#if defined(MBEDTLS_HAVEGE_C)
133#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
138#if defined(MBEDTLS_ENTROPY_NV_SEED)
142 ctx->initial_entropy_run = 0;
151 if(
ctx->accumulator_started == -1 )
154#if defined(MBEDTLS_HAVEGE_C)
157#if defined(MBEDTLS_THREADING_C)
158 mbedtls_mutex_free( &
ctx->mutex );
160#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
165#if defined(MBEDTLS_ENTROPY_NV_SEED)
166 ctx->initial_entropy_run = 0;
168 ctx->source_count = 0;
170 ctx->accumulator_started = -1;
175 size_t threshold,
int strong )
179#if defined(MBEDTLS_THREADING_C)
180 if( (
ret = mbedtls_mutex_lock( &
ctx->mutex ) ) != 0 )
191 ctx->source[
idx].f_source = f_source;
192 ctx->source[
idx].p_source = p_source;
193 ctx->source[
idx].threshold = threshold;
194 ctx->source[
idx].strong = strong;
199#if defined(MBEDTLS_THREADING_C)
200 if( mbedtls_mutex_unlock( &
ctx->mutex ) != 0 )
211 const unsigned char *
data,
size_t len )
215 size_t use_len =
len;
216 const unsigned char *
p =
data;
221#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
233 header[1] = use_len & 0xFF;
240#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
241 if(
ctx->accumulator_started == 0 &&
245 ctx->accumulator_started = 1;
250 if(
ctx->accumulator_started == 0 &&
254 ctx->accumulator_started = 1;
267 const unsigned char *
data,
size_t len )
271#if defined(MBEDTLS_THREADING_C)
272 if( (
ret = mbedtls_mutex_lock( &
ctx->mutex ) ) != 0 )
278#if defined(MBEDTLS_THREADING_C)
279 if( mbedtls_mutex_unlock( &
ctx->mutex ) != 0 )
291 int ret,
i, have_one_strong = 0;
295 if(
ctx->source_count == 0 )
301 for(
i = 0;
i <
ctx->source_count;
i++ )
307 if( (
ret =
ctx->source[
i].f_source(
ctx->source[
i].p_source,
318 if( (
ret = entropy_update(
ctx, (
unsigned char)
i,
321 ctx->source[
i].size += olen;
325 if( have_one_strong == 0 )
341#if defined(MBEDTLS_THREADING_C)
342 if( (
ret = mbedtls_mutex_lock( &
ctx->mutex ) ) != 0 )
346 ret = entropy_gather_internal(
ctx );
348#if defined(MBEDTLS_THREADING_C)
349 if( mbedtls_mutex_unlock( &
ctx->mutex ) != 0 )
365#if defined(MBEDTLS_ENTROPY_NV_SEED)
369 if(
ctx->initial_entropy_run == 0 )
371 ctx->initial_entropy_run = 1;
372 if( (
ret = mbedtls_entropy_update_nv_seed(
ctx ) ) != 0 )
377#if defined(MBEDTLS_THREADING_C)
378 if( (
ret = mbedtls_mutex_lock( &
ctx->mutex ) ) != 0 )
387 if(
count++ > ENTROPY_MAX_LOOP )
393 if( (
ret = entropy_gather_internal(
ctx ) ) != 0 )
397 for(
i = 0;
i <
ctx->source_count;
i++ )
398 if(
ctx->source[
i].size <
ctx->source[
i].threshold )
405#if defined(MBEDTLS_ENTROPY_SHA512_ACCUMULATOR)
454 for(
i = 0;
i <
ctx->source_count;
i++ )
455 ctx->source[
i].size = 0;
464#if defined(MBEDTLS_THREADING_C)
465 if( mbedtls_mutex_unlock( &
ctx->mutex ) != 0 )
472#if defined(MBEDTLS_ENTROPY_NV_SEED)
493#if defined(MBEDTLS_FS_IO)
558 return( mbedtls_entropy_write_seed_file(
ctx,
path ) );
562#if defined(MBEDTLS_SELF_TEST)
563#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
567static int entropy_dummy_source(
void *
data,
unsigned char *output,
568 size_t len,
size_t *olen )
579#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
581static int mbedtls_entropy_source_self_test_gather(
unsigned char *
buf,
size_t buf_len )
584 size_t entropy_len = 0;
586 size_t attempts = buf_len;
588 while( attempts > 0 && entropy_len < buf_len )
590 if( (
ret = mbedtls_hardware_poll(
NULL,
buf + entropy_len,
591 buf_len - entropy_len, &olen ) ) != 0 )
598 if( entropy_len < buf_len )
607static int mbedtls_entropy_source_self_test_check_bits(
const unsigned char *
buf,
610 unsigned char set= 0xFF;
611 unsigned char unset = 0x00;
614 for(
i = 0;
i < buf_len;
i++ )
620 return(
set == 0xFF || unset == 0x00 );
634int mbedtls_entropy_source_self_test(
int verbose )
637 unsigned char buf0[2 *
sizeof(
unsigned long long int )];
638 unsigned char buf1[2 *
sizeof(
unsigned long long int )];
643 memset( buf0, 0x00,
sizeof( buf0 ) );
644 memset( buf1, 0x00,
sizeof( buf1 ) );
646 if( (
ret = mbedtls_entropy_source_self_test_gather( buf0,
sizeof( buf0 ) ) ) != 0 )
648 if( (
ret = mbedtls_entropy_source_self_test_gather( buf1,
sizeof( buf1 ) ) ) != 0 )
652 if( (
ret = mbedtls_entropy_source_self_test_check_bits( buf0,
sizeof( buf0 ) ) ) != 0 )
654 if( (
ret = mbedtls_entropy_source_self_test_check_bits( buf1,
sizeof( buf1 ) ) ) != 0 )
659 ret =
memcmp( buf0, buf1,
sizeof( buf0 ) ) == 0;
682int mbedtls_entropy_self_test(
int verbose )
685#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
695#if !defined(MBEDTLS_TEST_NULL_ENTROPY)
718 for(
i = 0;
i < 8;
i++ )
723 for(
j = 0;
j <
sizeof(
buf );
j++ )
727 for(
j = 0;
j <
sizeof(
buf );
j++ )
736#if defined(MBEDTLS_ENTROPY_HARDWARE_ALT)
737 if( (
ret = mbedtls_entropy_source_self_test( 0 ) ) != 0 )
int memcmp(void *Buffer1, void *Buffer2, ACPI_SIZE Count)
static void cleanup(void)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Entropy accumulator implementation.
#define MBEDTLS_ERR_ENTROPY_MAX_SOURCES
#define MBEDTLS_ENTROPY_MAX_GATHER
int(* mbedtls_entropy_f_source_ptr)(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback pointer.
#define MBEDTLS_ERR_ENTROPY_FILE_IO_ERROR
#define MBEDTLS_ENTROPY_SOURCE_WEAK
#define MBEDTLS_ERR_ENTROPY_NO_STRONG_SOURCE
#define MBEDTLS_ENTROPY_MAX_SOURCES
#define MBEDTLS_ENTROPY_SOURCE_MANUAL
int mbedtls_entropy_update_manual(mbedtls_entropy_context *ctx, const unsigned char *data, size_t len)
Add data to the accumulator manually (Thread-safe if MBEDTLS_THREADING_C is enabled)
#define MBEDTLS_ERR_ENTROPY_NO_SOURCES_DEFINED
int mbedtls_entropy_gather(mbedtls_entropy_context *ctx)
Trigger an extra gather poll for the accumulator (Thread-safe if MBEDTLS_THREADING_C is enabled)
#define MBEDTLS_ENTROPY_BLOCK_SIZE
#define MBEDTLS_ENTROPY_SOURCE_STRONG
int mbedtls_entropy_add_source(mbedtls_entropy_context *ctx, mbedtls_entropy_f_source_ptr f_source, void *p_source, size_t threshold, int strong)
Adds an entropy source to poll (Thread-safe if MBEDTLS_THREADING_C is enabled)
#define MBEDTLS_ERR_ENTROPY_SOURCE_FAILED
#define MBEDTLS_ENTROPY_MAX_SEED_SIZE
Platform-specific and custom entropy polling functions.
#define MBEDTLS_ENTROPY_MIN_HARDCLOCK
#define MBEDTLS_ENTROPY_MIN_PLATFORM
#define MBEDTLS_ENTROPY_MIN_HAVEGE
int mbedtls_platform_entropy_poll(void *data, unsigned char *output, size_t len, size_t *olen)
Entropy poll callback that provides 0 entropy.
int mbedtls_hardclock_poll(void *data, unsigned char *output, size_t len, size_t *olen)
mbedtls_timing_hardclock-based entropy poll callback
#define MBEDTLS_ENTROPY_MIN_HARDWARE
GLuint GLuint GLsizei count
GLint GLenum GLsizei GLsizei GLsizei GLint GLsizei const GLvoid * data
GLenum GLuint GLenum GLsizei const GLchar * buf
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint i
GLsizei GLenum const GLvoid GLsizei GLenum GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLint GLint GLint GLshort GLshort GLshort GLubyte GLubyte GLubyte GLuint GLuint GLuint GLushort GLushort GLushort GLbyte GLbyte GLbyte GLbyte GLdouble GLdouble GLdouble GLdouble GLfloat GLfloat GLfloat GLfloat GLint GLint GLint GLint GLshort GLshort GLshort GLshort GLubyte GLubyte GLubyte GLubyte GLuint GLuint GLuint GLuint GLushort GLushort GLushort GLushort GLboolean const GLdouble const GLfloat const GLint const GLshort const GLbyte const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLdouble const GLfloat const GLfloat const GLint const GLint const GLshort const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort const GLdouble const GLfloat const GLint const GLshort GLenum GLenum GLenum GLfloat GLenum GLint GLenum GLenum GLenum GLfloat GLenum GLenum GLint GLenum GLfloat GLenum GLint GLint GLushort GLenum GLenum GLfloat GLenum GLenum GLint GLfloat const GLubyte GLenum GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLint GLint GLsizei GLsizei GLint GLenum GLenum const GLvoid GLenum GLenum const GLfloat GLenum GLenum const GLint GLenum GLenum const GLdouble GLenum GLenum const GLfloat GLenum GLenum const GLint GLsizei GLuint GLfloat GLuint GLbitfield GLfloat GLint GLuint GLboolean GLenum GLfloat GLenum GLbitfield GLenum GLfloat GLfloat GLint GLint const GLfloat GLenum GLfloat GLfloat GLint GLint GLfloat GLfloat GLint GLint const GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat GLint GLfloat GLfloat const GLdouble const GLfloat const GLdouble const GLfloat GLint GLint GLint j
HAVEGE: HArdware Volatile Entropy Gathering and Expansion.
void mbedtls_havege_free(mbedtls_havege_state *hs)
Clear HAVEGE state.
void mbedtls_havege_init(mbedtls_havege_state *hs)
HAVEGE initialization.
_Check_return_opt_ _CRTIMP size_t __cdecl fread(_Out_writes_bytes_(_ElementSize *_Count) void *_DstBuf, _In_ size_t _ElementSize, _In_ size_t _Count, _Inout_ FILE *_File)
_Check_return_ _CRTIMP FILE *__cdecl fopen(_In_z_ const char *_Filename, _In_z_ const char *_Mode)
_Check_return_opt_ _CRTIMP int __cdecl fseek(_Inout_ FILE *_File, _In_ long _Offset, _In_ int _Origin)
_Check_return_opt_ _CRTIMP int __cdecl fclose(_Inout_ FILE *_File)
_Check_return_ _CRTIMP long __cdecl ftell(_Inout_ FILE *_File)
_Check_return_opt_ _CRTIMP size_t __cdecl fwrite(_In_reads_bytes_(_Size *_Count) const void *_Str, _In_ size_t _Size, _In_ size_t _Count, _Inout_ FILE *_File)
#define memcpy(s1, s2, n)
#define mbedtls_entropy_free
#define mbedtls_entropy_init
#define mbedtls_entropy_func
Configuration options (set of defines)
void mbedtls_sha256_free(mbedtls_sha256_context *ctx)
This function clears a SHA-256 context.
void mbedtls_sha256_init(mbedtls_sha256_context *ctx)
This function initializes a SHA-256 context.
int mbedtls_sha256_ret(const unsigned char *input, size_t ilen, unsigned char output[32], int is224)
This function calculates the SHA-224 or SHA-256 checksum of a buffer.
int mbedtls_sha256_finish_ret(mbedtls_sha256_context *ctx, unsigned char output[32])
This function finishes the SHA-256 operation, and writes the result to the output buffer.
int mbedtls_sha256_starts_ret(mbedtls_sha256_context *ctx, int is224)
This function starts a SHA-224 or SHA-256 checksum calculation.
int mbedtls_sha256_update_ret(mbedtls_sha256_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-256 checksum calculation.
int mbedtls_sha512_ret(const unsigned char *input, size_t ilen, unsigned char output[64], int is384)
This function calculates the SHA-512 or SHA-384 checksum of a buffer.
void mbedtls_sha512_free(mbedtls_sha512_context *ctx)
This function clears a SHA-512 context.
int mbedtls_sha512_starts_ret(mbedtls_sha512_context *ctx, int is384)
This function starts a SHA-384 or SHA-512 checksum calculation.
int mbedtls_sha512_update_ret(mbedtls_sha512_context *ctx, const unsigned char *input, size_t ilen)
This function feeds an input buffer into an ongoing SHA-512 checksum calculation.
int mbedtls_sha512_finish_ret(mbedtls_sha512_context *ctx, unsigned char output[64])
This function finishes the SHA-512 operation, and writes the result to the output buffer.
void mbedtls_sha512_init(mbedtls_sha512_context *ctx)
This function initializes a SHA-512 context.
Entropy context structure.
#define MBEDTLS_ERR_THREADING_MUTEX_ERROR