00001
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053 #ifndef MBEDTLS_CTR_DRBG_H
00054 #define MBEDTLS_CTR_DRBG_H
00055
00056 #if !defined(MBEDTLS_CONFIG_FILE)
00057 #include "config.h"
00058 #else
00059 #include MBEDTLS_CONFIG_FILE
00060 #endif
00061
00062 #include "aes.h"
00063
00064 #if defined(MBEDTLS_THREADING_C)
00065 #include "threading.h"
00066 #endif
00067
00068 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
00069 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
00070 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
00071 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
00073 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
00074 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
00075 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
00076 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
00091 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
00092 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00093
00096 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
00097
00098 #else
00099
00107 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
00108 #endif
00109 #endif
00110
00111 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
00112 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
00113
00114 #endif
00115
00116 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
00117 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
00118
00119 #endif
00120
00121 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
00122 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
00123
00124 #endif
00125
00126 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
00127 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
00128
00129 #endif
00130
00131
00132
00133 #define MBEDTLS_CTR_DRBG_PR_OFF 0
00134
00135 #define MBEDTLS_CTR_DRBG_PR_ON 1
00136
00138 #ifdef __cplusplus
00139 extern "C" {
00140 #endif
00141
00145 typedef struct
00146 {
00147 unsigned char counter[16];
00148 int reseed_counter;
00149 int prediction_resistance;
00153 size_t entropy_len;
00155 int reseed_interval;
00157 mbedtls_aes_context aes_ctx;
00159
00160
00161
00162 int (*f_entropy)(void *, unsigned char *, size_t);
00165 void *p_entropy;
00167 #if defined(MBEDTLS_THREADING_C)
00168 mbedtls_threading_mutex_t mutex;
00169 #endif
00170 }
00171 mbedtls_ctr_drbg_context;
00172
00180 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
00181
00216 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
00217
00224 #endif
00225
00251 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
00252 int (*f_entropy)(void *, unsigned char *, size_t),
00253 void *p_entropy,
00254 const unsigned char *custom,
00255 size_t len );
00256
00262 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
00263
00277 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
00278 int resistance );
00279
00294 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
00295 size_t len );
00296
00309 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
00310 int interval );
00311
00327 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
00328 const unsigned char *additional, size_t len );
00329
00345 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
00346 const unsigned char *additional,
00347 size_t add_len );
00348
00366 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
00367 const unsigned char *additional,
00368 size_t add_len );
00369
00396 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
00397 unsigned char *output, size_t output_len,
00398 const unsigned char *additional, size_t add_len );
00399
00416 int mbedtls_ctr_drbg_random( void *p_rng,
00417 unsigned char *output, size_t output_len );
00418
00419 #if defined(MBEDTLS_FS_IO)
00420
00431 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00432
00447 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00448 #endif
00449
00455 int mbedtls_ctr_drbg_self_test( int verbose );
00456
00457
00458 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
00459 int (*)(void *, unsigned char *, size_t), void *,
00460 const unsigned char *, size_t, size_t );
00461
00462 #ifdef __cplusplus
00463 }
00464 #endif
00465
00466 #endif