Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | RSS feed

  1. /**
  2.  * \file entropy.h
  3.  *
  4.  * \brief Entropy accumulator implementation
  5.  *
  6.  *  Copyright (C) 2006-2011, Brainspark B.V.
  7.  *
  8.  *  This file is part of PolarSSL (http://www.polarssl.org)
  9.  *  Lead Maintainer: Paul Bakker <polarssl_maintainer at polarssl.org>
  10.  *
  11.  *  All rights reserved.
  12.  *
  13.  *  This program is free software; you can redistribute it and/or modify
  14.  *  it under the terms of the GNU General Public License as published by
  15.  *  the Free Software Foundation; either version 2 of the License, or
  16.  *  (at your option) any later version.
  17.  *
  18.  *  This program is distributed in the hope that it will be useful,
  19.  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
  20.  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  21.  *  GNU General Public License for more details.
  22.  *
  23.  *  You should have received a copy of the GNU General Public License along
  24.  *  with this program; if not, write to the Free Software Foundation, Inc.,
  25.  *  51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
  26.  */
  27. #ifndef POLARSSL_ENTROPY_H
  28. #define POLARSSL_ENTROPY_H
  29.  
  30. #include <string.h>
  31.  
  32. #include "config.h"
  33.  
  34. #include "sha4.h"
  35. #if defined(POLARSSL_HAVEGE_C)
  36. #include "havege.h"
  37. #endif
  38.  
  39. #define POLARSSL_ERR_ENTROPY_SOURCE_FAILED                 -0x003C  /**< Critical entropy source failure. */
  40. #define POLARSSL_ERR_ENTROPY_MAX_SOURCES                   -0x003E  /**< No more sources can be added. */
  41. #define POLARSSL_ERR_ENTROPY_NO_SOURCES_DEFINED            -0x0040  /**< No sources have been added to poll. */
  42.  
  43. #define ENTROPY_MAX_SOURCES     20      /**< Maximum number of sources supported */
  44. #define ENTROPY_MAX_GATHER      128     /**< Maximum amount requested from entropy sources */
  45. #define ENTROPY_BLOCK_SIZE      64      /**< Block size of entropy accumulator (SHA-512) */
  46.  
  47. #define ENTROPY_SOURCE_MANUAL   ENTROPY_MAX_SOURCES
  48.  
  49. #ifdef __cplusplus
  50. extern "C" {
  51. #endif
  52.  
  53. /**
  54.  * \brief           Entropy poll callback pointer
  55.  *
  56.  * \param data      Callback-specific data pointer
  57.  * \param output    Data to fill
  58.  * \param len       Maximum size to provide
  59.  * \param olen      The actual amount of bytes put into the buffer (Can be 0)
  60.  *
  61.  * \return          0 if no critical failures occurred,
  62.  *                  POLARSSL_ERR_ENTROPY_SOURCE_FAILED otherwise
  63.  */
  64. typedef int (*f_source_ptr)(void *, unsigned char *, size_t, size_t *);
  65.  
  66. /**
  67.  * \brief           Entropy source state
  68.  */
  69. typedef struct
  70. {
  71.     f_source_ptr    f_source;   /**< The entropy source callback */
  72.     void *          p_source;   /**< The callback data pointer */
  73.     size_t          size;       /**< Amount received */
  74.     size_t          threshold;  /**< Minimum level required before release */
  75. }
  76. source_state;
  77.  
  78. /**
  79.  * \brief           Entropy context structure
  80.  */
  81. typedef struct
  82. {
  83.     sha4_context    accumulator;
  84.     int             source_count;
  85.     source_state    source[ENTROPY_MAX_SOURCES];
  86. #if defined(POLARSSL_HAVEGE_C)
  87.     havege_state    havege_data;
  88. #endif
  89. }
  90. entropy_context;
  91.  
  92. /**
  93.  * \brief           Initialize the context
  94.  *
  95.  * \param ctx       Entropy context to initialize
  96.  */
  97. void entropy_init( entropy_context *ctx );
  98.  
  99. /**
  100.  * \brief           Adds an entropy source to poll
  101.  *
  102.  * \param ctx       Entropy context
  103.  * \param f_source  Entropy function
  104.  * \param p_source  Function data
  105.  * \param threshold Minimum required from source before entropy is released
  106.  *                  ( with entropy_func() )
  107.  *
  108.  * \return          0 if successful or POLARSSL_ERR_ENTROPY_MAX_SOURCES
  109.  */
  110. int entropy_add_source( entropy_context *ctx,
  111.                         f_source_ptr f_source, void *p_source,
  112.                         size_t threshold );
  113.  
  114. /**
  115.  * \brief           Trigger an extra gather poll for the accumulator
  116.  *
  117.  * \param ctx       Entropy context
  118.  *
  119.  * \return          0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
  120.  */
  121. int entropy_gather( entropy_context *ctx );
  122.  
  123. /**
  124.  * \brief           Retrieve entropy from the accumulator (Max ENTROPY_BLOCK_SIZE)
  125.  *
  126.  * \param data      Entropy context
  127.  * \param output    Buffer to fill
  128.  * \param len       Length of buffer
  129.  *
  130.  * \return          0 if successful, or POLARSSL_ERR_ENTROPY_SOURCE_FAILED
  131.  */
  132. int entropy_func( void *data, unsigned char *output, size_t len );
  133.  
  134. /**
  135.  * \brief           Add data to the accumulator manually
  136.  *
  137.  * \param ctx       Entropy context
  138.  * \param data      Data to add
  139.  * \param len       Length of data
  140.  *
  141.  * \return          0 if successful
  142.  */
  143. int entropy_update_manual( entropy_context *ctx,
  144.                            const unsigned char *data, size_t len );
  145.  
  146. #ifdef __cplusplus
  147. }
  148. #endif
  149.  
  150. #endif /* entropy.h */
  151.