2011-11-27 14:46:59 +00:00
|
|
|
/**
|
|
|
|
* \file ctr_drbg.h
|
|
|
|
*
|
|
|
|
* \brief CTR_DRBG based on AES-256 (NIST SP 800-90)
|
|
|
|
*
|
2015-01-23 09:45:19 +00:00
|
|
|
* Copyright (C) 2006-2014, ARM Limited, All Rights Reserved
|
2011-11-27 14:46:59 +00:00
|
|
|
*
|
2015-01-28 17:12:07 +00:00
|
|
|
* This file is part of mbed TLS (https://polarssl.org)
|
2011-11-27 14:46:59 +00: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_CTR_DRBG_H
|
|
|
|
#define POLARSSL_CTR_DRBG_H
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
|
|
|
|
#include "aes.h"
|
|
|
|
|
|
|
|
#define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */
|
|
|
|
#define POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */
|
|
|
|
#define POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */
|
2011-12-10 21:55:01 +00:00
|
|
|
#define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */
|
2011-11-27 14:46:59 +00:00
|
|
|
|
2011-11-29 10:50:51 +00:00
|
|
|
#define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */
|
|
|
|
#define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */
|
|
|
|
#define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 )
|
|
|
|
#define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE )
|
|
|
|
/**< The seed length (counter + AES key) */
|
2013-06-24 17:31:17 +00:00
|
|
|
|
2014-04-25 09:11:10 +00: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(CTR_DRBG_ENTROPY_LEN)
|
2014-02-06 14:55:25 +00:00
|
|
|
#if defined(POLARSSL_SHA512_C) && !defined(POLARSSL_ENTROPY_FORCE_SHA256)
|
2013-08-27 13:06:26 +00:00
|
|
|
#define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
|
|
|
|
#else
|
|
|
|
#define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default (48 with SHA-512, 32 with SHA-256) */
|
|
|
|
#endif
|
2014-04-25 09:11:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CTR_DRBG_RESEED_INTERVAL)
|
2011-11-27 14:46:59 +00:00
|
|
|
#define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */
|
2014-04-25 09:11:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CTR_DRBG_MAX_INPUT)
|
2013-06-24 17:31:17 +00:00
|
|
|
#define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */
|
2014-04-25 09:11:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CTR_DRBG_MAX_REQUEST)
|
2013-06-24 17:31:17 +00:00
|
|
|
#define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */
|
2014-04-25 09:11:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if !defined(CTR_DRBG_MAX_SEED_INPUT)
|
2013-06-24 17:31:17 +00:00
|
|
|
#define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */
|
2014-04-25 09:11:10 +00:00
|
|
|
#endif
|
|
|
|
|
|
|
|
/* \} name SECTION: Module settings */
|
2011-11-27 14:46:59 +00:00
|
|
|
|
|
|
|
#define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */
|
|
|
|
#define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */
|
|
|
|
|
|
|
|
#ifdef __cplusplus
|
|
|
|
extern "C" {
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief CTR_DRBG context structure
|
|
|
|
*/
|
|
|
|
typedef struct
|
|
|
|
{
|
|
|
|
unsigned char counter[16]; /*!< counter (V) */
|
|
|
|
int reseed_counter; /*!< reseed counter */
|
|
|
|
int prediction_resistance; /*!< enable prediction resistance (Automatic
|
2014-05-01 12:18:25 +00:00
|
|
|
reseed before every random generation) */
|
|
|
|
size_t entropy_len; /*!< amount of entropy grabbed on each
|
|
|
|
(re)seed */
|
2011-11-27 14:46:59 +00:00
|
|
|
int reseed_interval; /*!< reseed interval */
|
|
|
|
|
|
|
|
aes_context aes_ctx; /*!< AES context */
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Callbacks (Entropy)
|
|
|
|
*/
|
|
|
|
int (*f_entropy)(void *, unsigned char *, size_t);
|
|
|
|
|
|
|
|
void *p_entropy; /*!< context for the entropy function */
|
|
|
|
}
|
|
|
|
ctr_drbg_context;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief CTR_DRBG initialization
|
2014-05-01 11:03:14 +00:00
|
|
|
*
|
2011-11-27 14:46:59 +00:00
|
|
|
* Note: Personalization data can be provided in addition to the more generic
|
|
|
|
* entropy source to make this instantiation as unique as possible.
|
|
|
|
*
|
|
|
|
* \param ctx CTR_DRBG context to be initialized
|
|
|
|
* \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer
|
|
|
|
* length)
|
|
|
|
* \param p_entropy Entropy context
|
|
|
|
* \param custom Personalization data (Device specific identifiers)
|
2011-11-29 10:50:51 +00:00
|
|
|
* (Can be NULL)
|
2011-11-27 14:46:59 +00:00
|
|
|
* \param len Length of personalization data
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
|
|
|
|
*/
|
|
|
|
int ctr_drbg_init( ctr_drbg_context *ctx,
|
|
|
|
int (*f_entropy)(void *, unsigned char *, size_t),
|
|
|
|
void *p_entropy,
|
2011-12-03 11:29:32 +00:00
|
|
|
const unsigned char *custom,
|
2011-11-27 14:46:59 +00:00
|
|
|
size_t len );
|
|
|
|
|
2014-06-18 14:21:25 +00:00
|
|
|
/**
|
|
|
|
* \brief Clear CTR_CRBG context data
|
|
|
|
*
|
|
|
|
* \param ctx CTR_DRBG context to clear
|
|
|
|
*/
|
|
|
|
void ctr_drbg_free( ctr_drbg_context *ctx );
|
|
|
|
|
2011-11-27 14:46:59 +00:00
|
|
|
/**
|
|
|
|
* \brief Enable / disable prediction resistance (Default: Off)
|
|
|
|
*
|
|
|
|
* Note: If enabled, entropy is used for ctx->entropy_len before each call!
|
|
|
|
* Only use this if you have ample supply of good entropy!
|
|
|
|
*
|
|
|
|
* \param ctx CTR_DRBG context
|
|
|
|
* \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF
|
|
|
|
*/
|
|
|
|
void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx,
|
|
|
|
int resistance );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set the amount of entropy grabbed on each (re)seed
|
|
|
|
* (Default: CTR_DRBG_ENTROPY_LEN)
|
|
|
|
*
|
|
|
|
* \param ctx CTR_DRBG context
|
|
|
|
* \param len Amount of entropy to grab
|
|
|
|
*/
|
|
|
|
void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx,
|
|
|
|
size_t len );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Set the reseed interval
|
|
|
|
* (Default: CTR_DRBG_RESEED_INTERVAL)
|
|
|
|
*
|
|
|
|
* \param ctx CTR_DRBG context
|
|
|
|
* \param interval Reseed interval
|
|
|
|
*/
|
|
|
|
void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx,
|
|
|
|
int interval );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief CTR_DRBG reseeding (extracts data from entropy source)
|
2014-05-01 11:03:14 +00:00
|
|
|
*
|
2011-11-27 14:46:59 +00:00
|
|
|
* \param ctx CTR_DRBG context
|
2011-11-29 10:50:51 +00:00
|
|
|
* \param additional Additional data to add to state (Can be NULL)
|
2011-11-27 14:46:59 +00:00
|
|
|
* \param len Length of additional data
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
|
|
|
|
*/
|
|
|
|
int ctr_drbg_reseed( ctr_drbg_context *ctx,
|
2011-12-03 11:29:32 +00:00
|
|
|
const unsigned char *additional, size_t len );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief CTR_DRBG update state
|
|
|
|
*
|
|
|
|
* \param ctx CTR_DRBG context
|
2011-12-03 11:29:32 +00:00
|
|
|
* \param additional Additional data to update state with
|
|
|
|
* \param add_len Length of additional data
|
2014-11-25 16:41:50 +00:00
|
|
|
*
|
|
|
|
* \note If add_len is greater than CTR_DRBG_MAX_SEED_INPUT,
|
|
|
|
* only the first CTR_DRBG_MAX_SEED_INPUT bytes are used,
|
|
|
|
* the remaining ones are silently discarded.
|
2011-11-27 14:46:59 +00:00
|
|
|
*/
|
2011-12-03 11:29:32 +00:00
|
|
|
void ctr_drbg_update( ctr_drbg_context *ctx,
|
2011-12-05 13:23:51 +00:00
|
|
|
const unsigned char *additional, size_t add_len );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief CTR_DRBG generate random with additional update input
|
|
|
|
*
|
|
|
|
* Note: Automatically reseeds if reseed_counter is reached.
|
|
|
|
*
|
|
|
|
* \param p_rng CTR_DRBG context
|
|
|
|
* \param output Buffer to fill
|
|
|
|
* \param output_len Length of the buffer
|
2011-11-29 10:50:51 +00:00
|
|
|
* \param additional Additional data to update with (Can be NULL)
|
2011-11-27 14:46:59 +00:00
|
|
|
* \param add_len Length of additional data
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
|
|
|
|
*/
|
|
|
|
int ctr_drbg_random_with_add( void *p_rng,
|
|
|
|
unsigned char *output, size_t output_len,
|
2011-12-03 11:29:32 +00:00
|
|
|
const unsigned char *additional, size_t add_len );
|
2011-11-27 14:46:59 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief CTR_DRBG generate random
|
|
|
|
*
|
|
|
|
* Note: Automatically reseeds if reseed_counter is reached.
|
|
|
|
*
|
|
|
|
* \param p_rng CTR_DRBG context
|
|
|
|
* \param output Buffer to fill
|
|
|
|
* \param output_len Length of the buffer
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG
|
|
|
|
*/
|
|
|
|
int ctr_drbg_random( void *p_rng,
|
|
|
|
unsigned char *output, size_t output_len );
|
|
|
|
|
2011-12-05 13:23:51 +00:00
|
|
|
#if defined(POLARSSL_FS_IO)
|
|
|
|
/**
|
|
|
|
* \brief Write a seed file
|
|
|
|
*
|
2013-09-10 14:16:50 +00:00
|
|
|
* \param ctx CTR_DRBG context
|
2011-12-05 13:23:51 +00:00
|
|
|
* \param path Name of the file
|
|
|
|
*
|
2014-03-26 10:51:25 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error, or
|
2011-12-05 13:23:51 +00:00
|
|
|
* POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED
|
|
|
|
*/
|
|
|
|
int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* \brief Read and update a seed file. Seed is added to this
|
|
|
|
* instance
|
|
|
|
*
|
2013-09-10 14:16:50 +00:00
|
|
|
* \param ctx CTR_DRBG context
|
2011-12-05 13:23:51 +00:00
|
|
|
* \param path Name of the file
|
|
|
|
*
|
2014-03-26 10:51:25 +00:00
|
|
|
* \return 0 if successful,
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR on file error,
|
2011-12-05 13:23:51 +00:00
|
|
|
* POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or
|
|
|
|
* POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG
|
|
|
|
*/
|
|
|
|
int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path );
|
2014-05-01 11:03:14 +00:00
|
|
|
#endif /* POLARSSL_FS_IO */
|
2011-12-05 13:23:51 +00:00
|
|
|
|
2011-11-27 14:46:59 +00:00
|
|
|
/**
|
|
|
|
* \brief Checkup routine
|
|
|
|
*
|
|
|
|
* \return 0 if successful, or 1 if the test failed
|
|
|
|
*/
|
|
|
|
int ctr_drbg_self_test( int verbose );
|
|
|
|
|
2013-06-25 14:47:55 +00:00
|
|
|
/* Internal functions (do not call directly) */
|
2014-05-01 12:18:25 +00:00
|
|
|
int ctr_drbg_init_entropy_len( ctr_drbg_context *,
|
|
|
|
int (*)(void *, unsigned char *, size_t), void *,
|
|
|
|
const unsigned char *, size_t, size_t );
|
2013-06-25 14:47:55 +00:00
|
|
|
|
2011-11-27 14:46:59 +00:00
|
|
|
#ifdef __cplusplus
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#endif /* ctr_drbg.h */
|