00001
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #ifndef MBEDTLS_CTR_DRBG_H
00029 #define MBEDTLS_CTR_DRBG_H
00030
00031 #if !defined(MBEDTLS_CONFIG_FILE)
00032 #include "config.h"
00033 #else
00034 #include MBEDTLS_CONFIG_FILE
00035 #endif
00036
00037 #include "aes.h"
00038
00039 #if defined(MBEDTLS_THREADING_C)
00040 #include "threading.h"
00041 #endif
00042
00043 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
00044 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
00045 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
00046 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
00048 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
00049 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
00050 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
00051 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
00062 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
00063 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00064 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
00065
00069 #else
00070 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
00071
00075 #endif
00076 #endif
00077
00078 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
00079 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
00080
00081 #endif
00082
00083 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
00084 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
00085
00086 #endif
00087
00088 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
00089 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
00090
00091 #endif
00092
00093 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
00094 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
00095
00096 #endif
00097
00098
00099
00100 #define MBEDTLS_CTR_DRBG_PR_OFF 0
00101
00102 #define MBEDTLS_CTR_DRBG_PR_ON 1
00103
00105 #ifdef __cplusplus
00106 extern "C" {
00107 #endif
00108
00112 typedef struct
00113 {
00114 unsigned char counter[16];
00115 int reseed_counter;
00116 int prediction_resistance;
00120 size_t entropy_len;
00122 int reseed_interval;
00124 mbedtls_aes_context aes_ctx;
00126
00127
00128
00129 int (*f_entropy)(void *, unsigned char *, size_t);
00132 void *p_entropy;
00134 #if defined(MBEDTLS_THREADING_C)
00135 mbedtls_threading_mutex_t mutex;
00136 #endif
00137 }
00138 mbedtls_ctr_drbg_context;
00139
00147 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
00148
00168 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
00169 int (*f_entropy)(void *, unsigned char *, size_t),
00170 void *p_entropy,
00171 const unsigned char *custom,
00172 size_t len );
00173
00179 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
00180
00193 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
00194 int resistance );
00195
00204 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
00205 size_t len );
00206
00214 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
00215 int interval );
00216
00228 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
00229 const unsigned char *additional, size_t len );
00230
00245 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
00246 const unsigned char *additional,
00247 size_t add_len );
00248
00264 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
00265 const unsigned char *additional,
00266 size_t add_len );
00267
00285 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
00286 unsigned char *output, size_t output_len,
00287 const unsigned char *additional, size_t add_len );
00288
00303 int mbedtls_ctr_drbg_random( void *p_rng,
00304 unsigned char *output, size_t output_len );
00305
00306 #if defined(MBEDTLS_FS_IO)
00307
00318 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00319
00332 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00333 #endif
00334
00340 int mbedtls_ctr_drbg_self_test( int verbose );
00341
00342
00343 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
00344 int (*)(void *, unsigned char *, size_t), void *,
00345 const unsigned char *, size_t, size_t );
00346
00347 #ifdef __cplusplus
00348 }
00349 #endif
00350
00351 #endif