2011-12-03 22:45:14 +01:00
|
|
|
/**
|
|
|
|
* \file entropy.h
|
|
|
|
*
|
|
|
|
* \brief Entropy accumulator implementation
|
|
|
|
*
|
2015-01-23 10:45:19 +01:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
2015-01-23 12:06:27 +01:00
|
|
|
* This file is part of mbed TLS (https://www.polarssl.org)
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
|
|
|
* 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>
|
|
|
|
|
2014-04-29 12:39:06 +02:00
|
|
|
#if !defined(POLARSSL_CONFIG_FILE)
|
2011-12-15 20:49:30 +01:00
|
|
|
#include "config.h"
|
2014-04-29 12:39:06 +02:00
|
|
|
#else
|
|
|
|
#include POLARSSL_CONFIG_FILE
|
|
|
|
#endif
|
2011-12-15 20:49:30 +01:00
|
|
|
|
2014-04-02 13:50:05 +02:00
|
|
|
#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
|
2013-06-30 14:49:12 +02:00
|
|
|
#include "sha512.h"
|
2013-08-27 15:06:26 +02:00
|
|
|
#define POLARSSL_ENTROPY_SHA512_ACCUMULATOR
|
|
|
|
#else
|
|
|
|
#if defined(POLARSSL_SHA256_C)
|
|
|
|
#define POLARSSL_ENTROPY_SHA256_ACCUMULATOR
|
|
|
|
#include "sha256.h"
|
|
|
|
#endif
|
|
|
|
#endif
|
|
|
|
|
2013-09-28 15:23:57 +02:00
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
#include "threading.h"
|
|
|
|
#endif
|
|
|
|
|
2011-12-15 20:49:30 +01:00
|
|
|
#if defined(POLARSSL_HAVEGE_C)
|
|
|
|
#include "havege.h"
|
|
|
|
#endif
|
2011-12-03 22:45:14 +01:00
|
|
|
|
2011-12-10 22:55:01 +01:00
|
|
|
#define POLARSSL_ERR_ENTROPY_SOURCE_FAILED -0x003C /**< Critical entropy source failure. */
|
|
|
|
#define POLARSSL_ERR_ENTROPY_MAX_SOURCES -0x003E /**< No more sources can be added. */
|
2011-12-15 21:11:16 +01:00
|
|
|
#define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED -0x0040 /**< No sources have been added to poll. */
|
2014-03-26 11:54:05 +01:00
|
|
|
#define POLARSSL_ERR_ENTROPY_FILE_IO_ERROR -0x0058 /**< Read/write error in file. */
|
2011-12-03 22:45:14 +01:00
|
|
|
|
2014-04-25 11:11:10 +02:00
|
|
|
/**
|
|
|
|
* \name SECTION: Module settings
|
|
|
|
*
|
|
|
|
* The configuration options you can set for this module are in this section.
|
|
|
|
* Either change them in config.h or define them on the compiler command line.
|
|
|
|
* \{
|
|
|
|
*/
|
|
|
|
|
|
|
|
#if !defined(ENTROPY_MAX_SOURCES)
|
2011-12-03 22:45:14 +01:00
|
|
|
#define ENTROPY_MAX_SOURCES 20 /**< Maximum number of sources supported */
|
2014-04-25 11:11:10 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(ENTROPY_MAX_GATHER)
|
2011-12-03 22:45:14 +01:00
|
|
|
#define ENTROPY_MAX_GATHER 128 /**< Maximum amount requested from entropy sources */
|
2014-04-25 11:11:10 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* \} name SECTION: Module settings */
|
2013-06-24 19:31:17 +02:00
|
|
|
|
2013-08-27 15:06:26 +02:00
|
|
|
#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
|
2011-12-03 22:45:14 +01:00
|
|
|
#define ENTROPY_BLOCK_SIZE 64 /**< Block size of entropy accumulator (SHA-512) */
|
2013-08-27 15:06:26 +02:00
|
|
|
#else
|
|
|
|
#define ENTROPY_BLOCK_SIZE 32 /**< Block size of entropy accumulator (SHA-256) */
|
|
|
|
#endif
|
2011-12-03 22:45:14 +01:00
|
|
|
|
2014-03-26 11:54:05 +01:00
|
|
|
#define ENTROPY_MAX_SEED_SIZE 1024 /**< Maximum size of seed we read from seed file */
|
2011-12-03 22:45:14 +01:00
|
|
|
#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
|
|
|
|
*/
|
2013-12-30 17:57:27 +01:00
|
|
|
typedef int (*f_source_ptr)(void *data, unsigned char *output, size_t len,
|
|
|
|
size_t *olen);
|
2011-12-03 22:45:14 +01:00
|
|
|
|
2011-12-10 18:02:19 +01:00
|
|
|
/**
|
|
|
|
* \brief Entropy source state
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
f_source_ptr f_source; /**< The entropy source callback */
|
|
|
|
void * p_source; /**< The callback data pointer */
|
|
|
|
size_t size; /**< Amount received */
|
|
|
|
size_t threshold; /**< Minimum level required before release */
|
|
|
|
}
|
|
|
|
source_state;
|
|
|
|
|
2011-12-03 22:45:14 +01:00
|
|
|
/**
|
|
|
|
* \brief Entropy context structure
|
|
|
|
*/
|
2013-06-30 14:34:05 +02:00
|
|
|
typedef struct
|
2011-12-03 22:45:14 +01:00
|
|
|
{
|
2013-08-27 15:06:26 +02:00
|
|
|
#if defined(POLARSSL_ENTROPY_SHA512_ACCUMULATOR)
|
2013-06-30 14:34:05 +02:00
|
|
|
sha512_context accumulator;
|
2013-08-27 15:06:26 +02:00
|
|
|
#else
|
|
|
|
sha256_context accumulator;
|
|
|
|
#endif
|
2011-12-03 22:45:14 +01:00
|
|
|
int source_count;
|
2011-12-10 18:02:19 +01:00
|
|
|
source_state source[ENTROPY_MAX_SOURCES];
|
2011-12-15 20:49:30 +01:00
|
|
|
#if defined(POLARSSL_HAVEGE_C)
|
|
|
|
havege_state havege_data;
|
|
|
|
#endif
|
2013-09-28 15:23:57 +02:00
|
|
|
#if defined(POLARSSL_THREADING_C)
|
|
|
|
threading_mutex_t mutex; /*!< mutex */
|
|
|
|
#endif
|
2011-12-03 22:45:14 +01:00
|
|
|
}
|
|
|
|
entropy_context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Initialize the context
|
|
|
|
*
|
|
|
|
* \param ctx Entropy context to initialize
|
|
|
|
*/
|
|
|
|
void entropy_init( entropy_context *ctx );
|
|
|
|
|
2013-09-28 15:23:03 +02:00
|
|
|
/**
|
|
|
|
* \brief Free the data in the context
|
|
|
|
*
|
|
|
|
* \param ctx Entropy context to free
|
|
|
|
*/
|
|
|
|
void entropy_free( entropy_context *ctx );
|
|
|
|
|
2011-12-03 22:45:14 +01:00
|
|
|
/**
|
|
|
|
* \brief Adds an entropy source to poll
|
2014-02-06 15:01:20 +01:00
|
|
|
* (Thread-safe if POLARSSL_THREADING_C is enabled)
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
|
|
|
* \param ctx Entropy context
|
|
|
|
* \param f_source Entropy function
|
|
|
|
* \param p_source Function data
|
2011-12-10 18:02:19 +01:00
|
|
|
* \param threshold Minimum required from source before entropy is released
|
|
|
|
* ( with entropy_func() )
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
2011-12-15 21:11:16 +01:00
|
|
|
* \return 0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
|
2011-12-03 22:45:14 +01:00
|
|
|
*/
|
|
|
|
int entropy_add_source( entropy_context *ctx,
|
2011-12-10 18:02:19 +01:00
|
|
|
f_source_ptr f_source, void *p_source,
|
|
|
|
size_t threshold );
|
2011-12-03 22:45:14 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Trigger an extra gather poll for the accumulator
|
2014-02-06 15:01:20 +01:00
|
|
|
* (Thread-safe if POLARSSL_THREADING_C is enabled)
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
|
|
|
* \param ctx Entropy context
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
|
|
|
|
*/
|
|
|
|
int entropy_gather( entropy_context *ctx );
|
|
|
|
|
|
|
|
/**
|
2014-05-01 14:18:25 +02:00
|
|
|
* \brief Retrieve entropy from the accumulator
|
|
|
|
* (Maximum length: ENTROPY_BLOCK_SIZE)
|
2013-09-28 15:23:57 +02:00
|
|
|
* (Thread-safe if POLARSSL_THREADING_C is enabled)
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
|
|
|
* \param data Entropy context
|
|
|
|
* \param output Buffer to fill
|
2014-05-30 11:42:01 +02:00
|
|
|
* \param len Number of bytes desired, must be at most ENTROPY_BLOCK_SIZE
|
2011-12-03 22:45:14 +01:00
|
|
|
*
|
|
|
|
* \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
|
2014-02-06 15:01:20 +01:00
|
|
|
* (Thread-safe if POLARSSL_THREADING_C is enabled)
|
2014-05-01 13:03:14 +02:00
|
|
|
*
|
2011-12-03 22:45:14 +01:00
|
|
|
* \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 );
|
|
|
|
|
2014-03-26 11:54:05 +01:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
/**
|
|
|
|
* \brief Write a seed file
|
|
|
|
*
|
|
|
|
* \param ctx Entropy context
|
|
|
|
* \param path Name of the file
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error, or
|
|
|
|
* POLARSSL_ERR_ENTROPY_SOURCE_FAILED
|
|
|
|
*/
|
|
|
|
int entropy_write_seed_file( entropy_context *ctx, const char *path );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Read and update a seed file. Seed is added to this
|
|
|
|
* instance. No more than ENTROPY_MAX_SEED_SIZE bytes are
|
|
|
|
* read from the seed file. The rest is ignored.
|
|
|
|
*
|
|
|
|
* \param ctx Entropy context
|
|
|
|
* \param path Name of the file
|
|
|
|
*
|
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_ENTROPY_FILE_IO_ERROR on file error,
|
|
|
|
* POLARSSL_ERR_ENTROPY_SOURCE_FAILED
|
|
|
|
*/
|
|
|
|
int entropy_update_seed_file( entropy_context *ctx, const char *path );
|
2014-05-01 13:03:14 +02:00
|
|
|
#endif /* POLARSSL_FS_IO */
|
2014-03-26 11:54:05 +01:00
|
|
|
|
2014-05-30 10:34:15 +02:00
|
|
|
#if defined(POLARSSL_SELF_TEST)
|
|
|
|
/**
|
|
|
|
* \brief Checkup routine
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or 1 if a test failed
|
|
|
|
*/
|
|
|
|
int entropy_self_test( int verbose );
|
|
|
|
#endif /* POLARSSL_SELF_TEST */
|
|
|
|
|
2011-12-03 22:45:14 +01:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* entropy.h */
|