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 #include "aes.h"
00032
00033 #if defined(MBEDTLS_THREADING_C)
00034 #include "mbedtls/threading.h"
00035 #endif
00036
00037 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
00038 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
00039 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
00040 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
00042 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
00043 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
00044 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
00045 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
00056 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
00057 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00058 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
00059
00063 #else
00064 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
00065
00069 #endif
00070 #endif
00071
00072 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
00073 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
00074
00075 #endif
00076
00077 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
00078 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
00079
00080 #endif
00081
00082 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
00083 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
00084
00085 #endif
00086
00087 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
00088 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
00089
00090 #endif
00091
00092
00093
00094 #define MBEDTLS_CTR_DRBG_PR_OFF 0
00095
00096 #define MBEDTLS_CTR_DRBG_PR_ON 1
00097
00099 #ifdef __cplusplus
00100 extern "C" {
00101 #endif
00102
00106 typedef struct
00107 {
00108 unsigned char counter[16];
00109 int reseed_counter;
00110 int prediction_resistance;
00114 size_t entropy_len;
00116 int reseed_interval;
00118 mbedtls_aes_context aes_ctx;
00120
00121
00122
00123 int (*f_entropy)(void *, unsigned char *, size_t);
00126 void *p_entropy;
00128 #if defined(MBEDTLS_THREADING_C)
00129 mbedtls_threading_mutex_t mutex;
00130 #endif
00131 }
00132 mbedtls_ctr_drbg_context;
00133
00141 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
00142
00162 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
00163 int (*f_entropy)(void *, unsigned char *, size_t),
00164 void *p_entropy,
00165 const unsigned char *custom,
00166 size_t len );
00167
00173 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
00174
00187 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
00188 int resistance );
00189
00198 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
00199 size_t len );
00200
00208 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
00209 int interval );
00210
00222 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
00223 const unsigned char *additional, size_t len );
00224
00236 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
00237 const unsigned char *additional, size_t add_len );
00238
00256 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
00257 unsigned char *output, size_t output_len,
00258 const unsigned char *additional, size_t add_len );
00259
00274 int mbedtls_ctr_drbg_random( void *p_rng,
00275 unsigned char *output, size_t output_len );
00276
00277 #if defined(MBEDTLS_FS_IO)
00278
00289 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00290
00303 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00304 #endif
00305
00311 int mbedtls_ctr_drbg_self_test( int verbose );
00312
00313
00314 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
00315 int (*)(void *, unsigned char *, size_t), void *,
00316 const unsigned char *, size_t, size_t );
00317
00318 #ifdef __cplusplus
00319 }
00320 #endif
00321
00322 #endif