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 | * |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame^] | 6 | * Copyright (C) 2006-2013, Brainspark B.V. |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 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). */ |
Paul Bakker | 69e095c | 2011-12-10 21:55:01 +0000 | [diff] [blame] | 37 | #define POLARSSL_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A /**< Read/write error in file. */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 38 | |
Paul Bakker | 2bc7cf1 | 2011-11-29 10:50:51 +0000 | [diff] [blame] | 39 | #define CTR_DRBG_BLOCKSIZE 16 /**< Block size used by the cipher */ |
| 40 | #define CTR_DRBG_KEYSIZE 32 /**< Key size used by the cipher */ |
| 41 | #define CTR_DRBG_KEYBITS ( CTR_DRBG_KEYSIZE * 8 ) |
| 42 | #define CTR_DRBG_SEEDLEN ( CTR_DRBG_KEYSIZE + CTR_DRBG_BLOCKSIZE ) |
| 43 | /**< The seed length (counter + AES key) */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame^] | 44 | |
| 45 | #if !defined(POLARSSL_CONFIG_OPTIONS) |
| 46 | #define CTR_DRBG_ENTROPY_LEN 48 /**< Amount of entropy used per seed by default */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 47 | #define CTR_DRBG_RESEED_INTERVAL 10000 /**< Interval before reseed is performed by default */ |
Paul Bakker | 9bcf16c | 2013-06-24 19:31:17 +0200 | [diff] [blame^] | 48 | #define CTR_DRBG_MAX_INPUT 256 /**< Maximum number of additional input bytes */ |
| 49 | #define CTR_DRBG_MAX_REQUEST 1024 /**< Maximum number of requested bytes per call */ |
| 50 | #define CTR_DRBG_MAX_SEED_INPUT 384 /**< Maximum size of (re)seed buffer */ |
| 51 | #endif /* !POLARSSL_CONFIG_OPTIONS */ |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 52 | |
| 53 | #define CTR_DRBG_PR_OFF 0 /**< No prediction resistance */ |
| 54 | #define CTR_DRBG_PR_ON 1 /**< Prediction resistance enabled */ |
| 55 | |
| 56 | #ifdef __cplusplus |
| 57 | extern "C" { |
| 58 | #endif |
| 59 | |
| 60 | /** |
| 61 | * \brief CTR_DRBG context structure |
| 62 | */ |
| 63 | typedef struct |
| 64 | { |
| 65 | unsigned char counter[16]; /*!< counter (V) */ |
| 66 | int reseed_counter; /*!< reseed counter */ |
| 67 | int prediction_resistance; /*!< enable prediction resistance (Automatic |
| 68 | reseed before every random generation) */ |
| 69 | size_t entropy_len; /*!< amount of entropy grabbed on each (re)seed */ |
| 70 | int reseed_interval; /*!< reseed interval */ |
| 71 | |
| 72 | aes_context aes_ctx; /*!< AES context */ |
| 73 | |
| 74 | /* |
| 75 | * Callbacks (Entropy) |
| 76 | */ |
| 77 | int (*f_entropy)(void *, unsigned char *, size_t); |
| 78 | |
| 79 | void *p_entropy; /*!< context for the entropy function */ |
| 80 | } |
| 81 | ctr_drbg_context; |
| 82 | |
| 83 | /** |
| 84 | * \brief CTR_DRBG initialization |
| 85 | * |
| 86 | * Note: Personalization data can be provided in addition to the more generic |
| 87 | * entropy source to make this instantiation as unique as possible. |
| 88 | * |
| 89 | * \param ctx CTR_DRBG context to be initialized |
| 90 | * \param f_entropy Entropy callback (p_entropy, buffer to fill, buffer |
| 91 | * length) |
| 92 | * \param p_entropy Entropy context |
| 93 | * \param custom Personalization data (Device specific identifiers) |
Paul Bakker | 2bc7cf1 | 2011-11-29 10:50:51 +0000 | [diff] [blame] | 94 | * (Can be NULL) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 95 | * \param len Length of personalization data |
| 96 | * |
| 97 | * \return 0 if successful, or |
| 98 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
| 99 | */ |
| 100 | int ctr_drbg_init( ctr_drbg_context *ctx, |
| 101 | int (*f_entropy)(void *, unsigned char *, size_t), |
| 102 | void *p_entropy, |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 103 | const unsigned char *custom, |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 104 | size_t len ); |
| 105 | |
| 106 | /** |
| 107 | * \brief Enable / disable prediction resistance (Default: Off) |
| 108 | * |
| 109 | * Note: If enabled, entropy is used for ctx->entropy_len before each call! |
| 110 | * Only use this if you have ample supply of good entropy! |
| 111 | * |
| 112 | * \param ctx CTR_DRBG context |
| 113 | * \param resistance CTR_DRBG_PR_ON or CTR_DRBG_PR_OFF |
| 114 | */ |
| 115 | void ctr_drbg_set_prediction_resistance( ctr_drbg_context *ctx, |
| 116 | int resistance ); |
| 117 | |
| 118 | /** |
| 119 | * \brief Set the amount of entropy grabbed on each (re)seed |
| 120 | * (Default: CTR_DRBG_ENTROPY_LEN) |
| 121 | * |
| 122 | * \param ctx CTR_DRBG context |
| 123 | * \param len Amount of entropy to grab |
| 124 | */ |
| 125 | void ctr_drbg_set_entropy_len( ctr_drbg_context *ctx, |
| 126 | size_t len ); |
| 127 | |
| 128 | /** |
| 129 | * \brief Set the reseed interval |
| 130 | * (Default: CTR_DRBG_RESEED_INTERVAL) |
| 131 | * |
| 132 | * \param ctx CTR_DRBG context |
| 133 | * \param interval Reseed interval |
| 134 | */ |
| 135 | void ctr_drbg_set_reseed_interval( ctr_drbg_context *ctx, |
| 136 | int interval ); |
| 137 | |
| 138 | /** |
| 139 | * \brief CTR_DRBG reseeding (extracts data from entropy source) |
| 140 | * |
| 141 | * \param ctx CTR_DRBG context |
Paul Bakker | 2bc7cf1 | 2011-11-29 10:50:51 +0000 | [diff] [blame] | 142 | * \param additional Additional data to add to state (Can be NULL) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 143 | * \param len Length of additional data |
| 144 | * |
| 145 | * \return 0 if successful, or |
| 146 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
| 147 | */ |
| 148 | int ctr_drbg_reseed( ctr_drbg_context *ctx, |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 149 | const unsigned char *additional, size_t len ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 150 | |
| 151 | /** |
| 152 | * \brief CTR_DRBG update state |
| 153 | * |
| 154 | * \param ctx CTR_DRBG context |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 155 | * \param additional Additional data to update state with |
| 156 | * \param add_len Length of additional data |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 157 | */ |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 158 | void ctr_drbg_update( ctr_drbg_context *ctx, |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 159 | const unsigned char *additional, size_t add_len ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 160 | |
| 161 | /** |
| 162 | * \brief CTR_DRBG generate random with additional update input |
| 163 | * |
| 164 | * Note: Automatically reseeds if reseed_counter is reached. |
| 165 | * |
| 166 | * \param p_rng CTR_DRBG context |
| 167 | * \param output Buffer to fill |
| 168 | * \param output_len Length of the buffer |
Paul Bakker | 2bc7cf1 | 2011-11-29 10:50:51 +0000 | [diff] [blame] | 169 | * \param additional Additional data to update with (Can be NULL) |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 170 | * \param add_len Length of additional data |
| 171 | * |
| 172 | * \return 0 if successful, or |
| 173 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or |
| 174 | * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG |
| 175 | */ |
| 176 | int ctr_drbg_random_with_add( void *p_rng, |
| 177 | unsigned char *output, size_t output_len, |
Paul Bakker | 1bc9efc | 2011-12-03 11:29:32 +0000 | [diff] [blame] | 178 | const unsigned char *additional, size_t add_len ); |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 179 | |
| 180 | /** |
| 181 | * \brief CTR_DRBG generate random |
| 182 | * |
| 183 | * Note: Automatically reseeds if reseed_counter is reached. |
| 184 | * |
| 185 | * \param p_rng CTR_DRBG context |
| 186 | * \param output Buffer to fill |
| 187 | * \param output_len Length of the buffer |
| 188 | * |
| 189 | * \return 0 if successful, or |
| 190 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED, or |
| 191 | * POLARSSL_ERR_CTR_DRBG_REQUEST_TOO_BIG |
| 192 | */ |
| 193 | int ctr_drbg_random( void *p_rng, |
| 194 | unsigned char *output, size_t output_len ); |
| 195 | |
Paul Bakker | fc754a9 | 2011-12-05 13:23:51 +0000 | [diff] [blame] | 196 | #if defined(POLARSSL_FS_IO) |
| 197 | /** |
| 198 | * \brief Write a seed file |
| 199 | * |
| 200 | * \param path Name of the file |
| 201 | * |
| 202 | * \return 0 if successful, 1 on file error, or |
| 203 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED |
| 204 | */ |
| 205 | int ctr_drbg_write_seed_file( ctr_drbg_context *ctx, const char *path ); |
| 206 | |
| 207 | /** |
| 208 | * \brief Read and update a seed file. Seed is added to this |
| 209 | * instance |
| 210 | * |
| 211 | * \param path Name of the file |
| 212 | * |
| 213 | * \return 0 if successful, 1 on file error, |
| 214 | * POLARSSL_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED or |
| 215 | * POLARSSL_ERR_CTR_DRBG_INPUT_TOO_BIG |
| 216 | */ |
| 217 | int ctr_drbg_update_seed_file( ctr_drbg_context *ctx, const char *path ); |
| 218 | #endif |
| 219 | |
Paul Bakker | 0e04d0e | 2011-11-27 14:46:59 +0000 | [diff] [blame] | 220 | /** |
| 221 | * \brief Checkup routine |
| 222 | * |
| 223 | * \return 0 if successful, or 1 if the test failed |
| 224 | */ |
| 225 | int ctr_drbg_self_test( int verbose ); |
| 226 | |
| 227 | #ifdef __cplusplus |
| 228 | } |
| 229 | #endif |
| 230 | |
| 231 | #endif /* ctr_drbg.h */ |