Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 1 | /** |
| 2 | * \file ctr_drbg.h |
| 3 | * |
| 4 | * \brief CTR_DRBG based on AES-256 (NIST SP 800-90) |
| 5 | * |
| 6 | * Copyright (C) 2006-2010, 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_CTR_DRBG_H |
| 28 | #define POLARSSL_CTR_DRBG_H |
| 29 | |
| 30 | #include <string.h> |
| 31 | |
| 32 | #include "aes.h" |
| 33 | |
| 34 | #define POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034 /**< The entropy source failed. */ |
| 35 | #define POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036 /**< Too many random requested in single call. */ |
| 36 | #define POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038 /**< Input too large (Entropy + additional). */ |
| 37 | |
| 38 | #define CTR_DRBG_SEEDLEN 48 /**< The seed length (counter + AES key) */ |
| 39 | #define CTR_DRBG_ENTROPY_LEN 32 /**< Amount of entropy used per seed by default */ |
| 40 | #define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ |
| 41 | #define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ |
| 42 | #define CTR_DRBG_MAX_REQUEST 500 /**< Maximum number of requested bytes per call */ |
| 43 | #define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ |
| 44 | |
| 45 | #define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */ |
| 46 | #define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */ |
| 47 | |
| 48 | #ifdef __cplusplus |
| 49 | extern "C" { |
| 50 | #endif |
| 51 | |
| 52 | /** |
| 53 | * \brief CTR_DRBG context structure |
| 54 | */ |
| 55 | typedef struct |
| 56 | { |
| 57 | unsigned char counter[16]; /*!< counter (V) */ |
| 58 | int reseed_counter; /*!< reseed counter */ |
| 59 | int prediction_resistance; /*!< enable prediction resistance (Automatic |
| 60 | reseed before every random generation) */ |
| 61 | size_t entropy_len; /*!< amount of entropy grabbed on each (re)seed */ |
| 62 | int reseed_interval; /*!< reseed interval */ |
| 63 | |
| 64 | aes_context aes_ctx; /*!< AES context */ |
| 65 | |
| 66 | /* |
| 67 | * Callbacks (Entropy) |
| 68 | */ |
| 69 | int (*f_entropy)(void *, unsigned char *, size_t); |
| 70 | |
| 71 | void *p_entropy; /*!< context for the entropy function */ |
| 72 | } |
| 73 | ctr_drbg_context; |
| 74 | |
| 75 | /** |
| 76 | * \brief CTR_DRBG initialization |
| 77 | * |
| 78 | * Note: Personalization data can be provided in addition to the more generic |
| 79 | * entropy source to make this instantiation as unique as possible. |
| 80 | * |
| 81 | * \param ctx CTR_DRBG context to be initialized |
| 82 | * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer |
| 83 | * length) |
| 84 | * \param p_entropy Entropy context |
| 85 | * \param custom Personalization data (Device specific identifiers) |
| 86 | * \param len Length of personalization data |
| 87 | * |
| 88 | * \return 0 if successful, or |
| 89 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
| 90 | */ |
| 91 | int ctr_drbg_init( ctr_drbg_context *ctx, |
| 92 | int (*f_entropy)(void *, unsigned char *, size_t), |
| 93 | void *p_entropy, |
| 94 | unsigned char *custom, |
| 95 | size_t len ); |
| 96 | |
| 97 | /** |
| 98 | * \brief Enable / disable prediction resistance (Default: Off) |
| 99 | * |
| 100 | * Note: If enabled, entropy is used for ctx->entropy_len before each call! |
| 101 | * Only use this if you have ample supply of good entropy! |
| 102 | * |
| 103 | * \param ctx CTR_DRBG context |
| 104 | * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF |
| 105 | */ |
| 106 | void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx, |
| 107 | int resistance ); |
| 108 | |
| 109 | /** |
| 110 | * \brief Set the amount of entropy grabbed on each (re)seed |
| 111 | * (Default: CTR_DRBG_ENTROPY_LEN) |
| 112 | * |
| 113 | * \param ctx CTR_DRBG context |
| 114 | * \param len Amount of entropy to grab |
| 115 | */ |
| 116 | void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx, |
| 117 | size_t len ); |
| 118 | |
| 119 | /** |
| 120 | * \brief Set the reseed interval |
| 121 | * (Default: CTR_DRBG_RESEED_INTERVAL) |
| 122 | * |
| 123 | * \param ctx CTR_DRBG context |
| 124 | * \param interval Reseed interval |
| 125 | */ |
| 126 | void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx, |
| 127 | int interval ); |
| 128 | |
| 129 | /** |
| 130 | * \brief CTR_DRBG reseeding (extracts data from entropy source) |
| 131 | * |
| 132 | * \param ctx CTR_DRBG context |
| 133 | * \param additional Additional data to add to state |
| 134 | * \param len Length of additional data |
| 135 | * |
| 136 | * \return 0 if successful, or |
| 137 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
| 138 | */ |
| 139 | int ctr_drbg_reseed( ctr_drbg_context *ctx, |
| 140 | unsigned char *additional, size_t len ); |
| 141 | |
| 142 | /** |
| 143 | * \brief CTR_DRBG update state |
| 144 | * |
| 145 | * \param ctx CTR_DRBG context |
| 146 | * \param data Data to update with |
| 147 | * |
| 148 | * \return 0 if successful |
| 149 | */ |
| 150 | int ctr_drbg_update( ctr_drbg_context *ctx, |
| 151 | unsigned char data[CTR_DRBG_SEEDLEN] ); |
| 152 | |
| 153 | /** |
| 154 | * \brief CTR_DRBG generate random with additional update input |
| 155 | * |
| 156 | * Note: Automatically reseeds if reseed_counter is reached. |
| 157 | * |
| 158 | * \param p_rng CTR_DRBG context |
| 159 | * \param output Buffer to fill |
| 160 | * \param output_len Length of the buffer |
| 161 | * \param additional Additional data to update with |
| 162 | * \param add_len Length of additional data |
| 163 | * |
| 164 | * \return 0 if successful, or |
| 165 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or |
| 166 | * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG |
| 167 | */ |
| 168 | int ctr_drbg_random_with_add( void *p_rng, |
| 169 | unsigned char *output, size_t output_len, |
| 170 | unsigned char *additional, size_t add_len ); |
| 171 | |
| 172 | /** |
| 173 | * \brief CTR_DRBG generate random |
| 174 | * |
| 175 | * Note: Automatically reseeds if reseed_counter is reached. |
| 176 | * |
| 177 | * \param p_rng CTR_DRBG context |
| 178 | * \param output Buffer to fill |
| 179 | * \param output_len Length of the buffer |
| 180 | * |
| 181 | * \return 0 if successful, or |
| 182 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or |
| 183 | * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG |
| 184 | */ |
| 185 | int ctr_drbg_random( void *p_rng, |
| 186 | unsigned char *output, size_t output_len ); |
| 187 | |
| 188 | /** |
| 189 | * \brief Checkup routine |
| 190 | * |
| 191 | * \return 0 if successful, or 1 if the test failed |
| 192 | */ |
| 193 | int ctr_drbg_self_test( int verbose ); |
| 194 | |
| 195 | #ifdef __cplusplus |
| 196 | } |
| 197 | #endif |
| 198 | |
| 199 | #endif /* ctr_drbg.h */ |