-  Added a generic entropy accumulator that provides support for adding custom entropy sources and added some generic and platform dependent entropy sources


diff --git a/include/polarssl/config.h b/include/polarssl/config.h
index 27f520b..1327c43 100644
--- a/include/polarssl/config.h
+++ b/include/polarssl/config.h
@@ -155,6 +155,17 @@
 #define POLARSSL_FS_IO
 
 /**
+ * \def POLARSSL_NO_PLATFORM_ENTROPY
+ *
+ * Do not use built-in platform entropy functions.
+ * This is useful if your platform does not support
+ * standards like the /dev/urandom or Windows CryptoAPI.
+ *
+ * Uncomment this macro to disable the built-in platform entropy functions.
+#define POLARSSL_NO_PLATFORM_ENTROPY
+ */
+
+/**
  * \def POLARSSL_PKCS1_V21
  *
  * Requires: POLARSSL_MD_C, POLARSSL_RSA_C
@@ -208,6 +219,7 @@
  * Module:  library/aes.c
  * Caller:  library/ssl_tls.c
  *          library/pem.c
+ *          library/ctr_drbg.c
  *
  * This module enables the following ciphersuites:
  *      SSL_RSA_AES_128_SHA
@@ -314,6 +326,8 @@
  * Module:  library/ctr_drbg.c
  * Caller:
  *
+ * Requires: POLARSSL_AES_C
+ *
  * This module provides the CTR_DRBG AES-256 random number generator.
  */
 #define POLARSSL_CTR_DRBG_C
@@ -363,6 +377,20 @@
 #define POLARSSL_DHM_C
 
 /**
+ * \def POLARSSL_ENTROPY_C
+ *
+ * Enable the platform-specific entropy code.
+ *
+ * Module:  library/entropy.c
+ * Caller:
+ *
+ * Requires: POLARSSL_SHA4_C
+ *
+ * This module provides a generic entropy pool
+ */
+#define POLARSSL_ENTROPY_C
+
+/**
  * \def POLARSSL_ERROR_C
  *
  * Enable error code to error string conversion.
diff --git a/include/polarssl/entropy.h b/include/polarssl/entropy.h
new file mode 100644
index 0000000..46df929
--- /dev/null
+++ b/include/polarssl/entropy.h
@@ -0,0 +1,129 @@
+/**
+ * \file entropy.h
+ *
+ * \brief Entropy accumulator implementation
+ *
+ *  Copyright (C) 2006-2011, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef POLARSSL_ENTROPY_H
+#define POLARSSL_ENTROPY_H
+
+#include <string.h>
+
+#include "sha4.h"
+
+#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED                 -0x003A  /**< Critical entropy source failure. */
+#define POLARSSL_ERR_ENTROPY_MAX_SOURCES                   -0x003C  /**< No more sources can be added. */
+
+#define ENTROPY_MIN_POOL        128     /**< Minimum amount of pool entropy needed for release */
+#define ENTROPY_MAX_SOURCES     20      /**< Maximum number of sources supported */
+#define ENTROPY_MAX_GATHER      128     /**< Maximum amount requested from entropy sources */
+#define ENTROPY_BLOCK_SIZE      64      /**< Block size of entropy accumulator (SHA-512) */
+
+#define ENTROPY_SOURCE_MANUAL   ENTROPY_MAX_SOURCES
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+/**
+ * \brief           Entropy poll callback pointer
+ *
+ * \param data      Callback-specific data pointer
+ * \param output    Data to fill
+ * \param len       Maximum size to provide
+ * \param olen      The actual amount of bytes put into the buffer (Can be 0)
+ *
+ * \return          0 if no critical failures occurred,
+ *                  POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
+ */
+typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
+
+/**
+ * \brief           Entropy context structure
+ */
+typedef struct 
+{
+    sha4_context    accumulator;
+    size_t          size;
+    int             source_count;
+    f_source_ptr    f_source[ENTROPY_MAX_SOURCES];
+    void *          p_source[ENTROPY_MAX_SOURCES];
+}
+entropy_context;
+
+/**
+ * \brief           Initialize the context
+ *
+ * \param ctx       Entropy context to initialize
+ */
+void entropy_init( entropy_context *ctx );
+
+/**
+ * \brief           Adds an entropy source to poll
+ *
+ * \param ctx       Entropy context
+ * \param f_source  Entropy function
+ * \param p_source  Function data
+ *
+ * \return          0 is successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
+ */
+int entropy_add_source( entropy_context *ctx,
+                        f_source_ptr f_source, void *p_source );
+
+/**
+ * \brief           Trigger an extra gather poll for the accumulator
+ *
+ * \param ctx       Entropy context
+ *
+ * \return          0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
+ */
+int entropy_gather( entropy_context *ctx );
+
+/**
+ * \brief           Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
+ *
+ * \param data      Entropy context
+ * \param output    Buffer to fill
+ * \param len       Length of buffer
+ *
+ * \return          0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
+ */
+int entropy_func( void *data, unsigned char *output, size_t len );
+
+/**
+ * \brief           Add data to the accumulator manually
+ * 
+ * \param ctx       Entropy context
+ * \param data      Data to add
+ * \param len       Length of data
+ *
+ * \return          0 if successful
+ */
+int entropy_update_manual( entropy_context *ctx,
+                           const unsigned char *data, size_t len );
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* entropy.h */
diff --git a/include/polarssl/entropy_poll.h b/include/polarssl/entropy_poll.h
new file mode 100644
index 0000000..d9acbe5
--- /dev/null
+++ b/include/polarssl/entropy_poll.h
@@ -0,0 +1,68 @@
+/**
+ * \file entropy_poll.h
+ *
+ * \brief Platform-specific and custom entropy polling functions
+ *
+ *  Copyright (C) 2006-2011, Brainspark B.V.
+ *
+ *  This file is part of PolarSSL (http://www.polarssl.org)
+ *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
+ *
+ *  All rights reserved.
+ *
+ *  This program is free software; you can redistribute it and/or modify
+ *  it under the terms of the GNU General Public License as published by
+ *  the Free Software Foundation; either version 2 of the License, or
+ *  (at your option) any later version.
+ *
+ *  This program is distributed in the hope that it will be useful,
+ *  but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *  GNU General Public License for more details.
+ *
+ *  You should have received a copy of the GNU General Public License along
+ *  with this program; if not, write to the Free Software Foundation, Inc.,
+ *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
+ */
+#ifndef POLARSSL_ENTROPY_POLL_H
+#define POLARSSL_ENTROPY_POLL_H
+
+#include <string.h>
+
+#include "config.h"
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#if !defined(POLARSSL_NO_PLATFORM_ENTROPY)
+/**
+ * \brief           Platform-specific entropy poll callback
+ */
+int platform_entropy_poll( void *data,
+                           unsigned char *output, size_t len, size_t *olen );
+#endif
+
+#if defined(POLARSSL_HAVEGE_C)
+/**
+ * \brief           HAVEGE based entropy poll callback
+ *
+ * Requires an HAVEGE state as its data pointer.
+ */
+int havege_poll( void *data,
+                 unsigned char *output, size_t len, size_t *olen );
+#endif
+
+#if defined(POLARSSL_TIMING_C)
+/**
+ * \brief           hardclock-based entropy poll callback
+ */
+int hardclock_poll( void *data,
+                    unsigned char *output, size_t len, size_t *olen );
+#endif
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* entropy_poll.h */
diff --git a/include/polarssl/error.h b/include/polarssl/error.h
index 7ef8a74..69e7b04 100644
--- a/include/polarssl/error.h
+++ b/include/polarssl/error.h
@@ -58,6 +58,7 @@
  * DES       1  0x0032-0x0032
  * NET      11  0x0040-0x0054
  * CTR_DBRG  3  0x0034-0x0038
+ * ENTROPY   2  0x003A-0x003C
  *
  * High-level module nr (3 bits - 0x1...-0x8...)
  * Name     ID  Nr of Errors