00001
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071
00072
00073
00074
00075
00076
00077
00078 #ifndef MBEDTLS_CTR_DRBG_H
00079 #define MBEDTLS_CTR_DRBG_H
00080
00081 #if !defined(MBEDTLS_CONFIG_FILE)
00082 #include "config.h"
00083 #else
00084 #include MBEDTLS_CONFIG_FILE
00085 #endif
00086
00087 #include "aes.h"
00088
00089 #if defined(MBEDTLS_THREADING_C)
00090 #include "threading.h"
00091 #endif
00092
00093 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
00094 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
00095 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
00096 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
00098 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
00099 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
00100 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
00101 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
00116 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
00117 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00118
00121 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
00122
00123 #else
00124
00132 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
00133 #endif
00134 #endif
00135
00136 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
00137 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
00138
00139 #endif
00140
00141 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
00142 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
00143
00144 #endif
00145
00146 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
00147 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
00148
00149 #endif
00150
00151 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
00152 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
00153
00154 #endif
00155
00156
00157
00158 #define MBEDTLS_CTR_DRBG_PR_OFF 0
00159
00160 #define MBEDTLS_CTR_DRBG_PR_ON 1
00161
00163 #ifdef __cplusplus
00164 extern "C" {
00165 #endif
00166
00170 typedef struct
00171 {
00172 unsigned char counter[16];
00173 int reseed_counter;
00174 int prediction_resistance;
00178 size_t entropy_len;
00180 int reseed_interval;
00182 mbedtls_aes_context aes_ctx;
00184
00185
00186
00187 int (*f_entropy)(void *, unsigned char *, size_t);
00190 void *p_entropy;
00192 #if defined(MBEDTLS_THREADING_C)
00193 mbedtls_threading_mutex_t mutex;
00194 #endif
00195 }
00196 mbedtls_ctr_drbg_context;
00197
00205 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
00206
00241 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
00242
00249 #endif
00250
00276 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
00277 int (*f_entropy)(void *, unsigned char *, size_t),
00278 void *p_entropy,
00279 const unsigned char *custom,
00280 size_t len );
00281
00287 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
00288
00302 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
00303 int resistance );
00304
00319 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
00320 size_t len );
00321
00334 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
00335 int interval );
00336
00352 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
00353 const unsigned char *additional, size_t len );
00354
00370 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
00371 const unsigned char *additional,
00372 size_t add_len );
00373
00391 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
00392 const unsigned char *additional,
00393 size_t add_len );
00394
00421 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
00422 unsigned char *output, size_t output_len,
00423 const unsigned char *additional, size_t add_len );
00424
00441 int mbedtls_ctr_drbg_random( void *p_rng,
00442 unsigned char *output, size_t output_len );
00443
00444 #if defined(MBEDTLS_FS_IO)
00445
00456 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00457
00472 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00473 #endif
00474
00480 int mbedtls_ctr_drbg_self_test( int verbose );
00481
00482
00483 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
00484 int (*)(void *, unsigned char *, size_t), void *,
00485 const unsigned char *, size_t, size_t );
00486
00487 #ifdef __cplusplus
00488 }
00489 #endif
00490
00491 #endif