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
00079
00080 #ifndef MBEDTLS_CTR_DRBG_H
00081 #define MBEDTLS_CTR_DRBG_H
00082
00083 #if !defined(MBEDTLS_CONFIG_FILE)
00084 #include "config.h"
00085 #else
00086 #include MBEDTLS_CONFIG_FILE
00087 #endif
00088
00089 #include "aes.h"
00090
00091 #if defined(MBEDTLS_THREADING_C)
00092 #include "threading.h"
00093 #endif
00094
00095 #define MBEDTLS_ERR_CTR_DRBG_ENTROPY_SOURCE_FAILED -0x0034
00096 #define MBEDTLS_ERR_CTR_DRBG_REQUEST_TOO_BIG -0x0036
00097 #define MBEDTLS_ERR_CTR_DRBG_INPUT_TOO_BIG -0x0038
00098 #define MBEDTLS_ERR_CTR_DRBG_FILE_IO_ERROR -0x003A
00100 #define MBEDTLS_CTR_DRBG_BLOCKSIZE 16
00101 #define MBEDTLS_CTR_DRBG_KEYSIZE 32
00102 #define MBEDTLS_CTR_DRBG_KEYBITS ( MBEDTLS_CTR_DRBG_KEYSIZE * 8 )
00103 #define MBEDTLS_CTR_DRBG_SEEDLEN ( MBEDTLS_CTR_DRBG_KEYSIZE + MBEDTLS_CTR_DRBG_BLOCKSIZE )
00118 #if !defined(MBEDTLS_CTR_DRBG_ENTROPY_LEN)
00119 #if defined(MBEDTLS_SHA512_C) && !defined(MBEDTLS_ENTROPY_FORCE_SHA256)
00120
00123 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 48
00124
00125 #else
00126
00134 #define MBEDTLS_CTR_DRBG_ENTROPY_LEN 32
00135 #endif
00136 #endif
00137
00138 #if !defined(MBEDTLS_CTR_DRBG_RESEED_INTERVAL)
00139 #define MBEDTLS_CTR_DRBG_RESEED_INTERVAL 10000
00140
00141 #endif
00142
00143 #if !defined(MBEDTLS_CTR_DRBG_MAX_INPUT)
00144 #define MBEDTLS_CTR_DRBG_MAX_INPUT 256
00145
00146 #endif
00147
00148 #if !defined(MBEDTLS_CTR_DRBG_MAX_REQUEST)
00149 #define MBEDTLS_CTR_DRBG_MAX_REQUEST 1024
00150
00151 #endif
00152
00153 #if !defined(MBEDTLS_CTR_DRBG_MAX_SEED_INPUT)
00154 #define MBEDTLS_CTR_DRBG_MAX_SEED_INPUT 384
00155
00156 #endif
00157
00158
00159
00160 #define MBEDTLS_CTR_DRBG_PR_OFF 0
00161
00162 #define MBEDTLS_CTR_DRBG_PR_ON 1
00163
00165 #ifdef __cplusplus
00166 extern "C" {
00167 #endif
00168
00172 typedef struct
00173 {
00174 unsigned char counter[16];
00175 int reseed_counter;
00176 int prediction_resistance;
00180 size_t entropy_len;
00182 int reseed_interval;
00184 mbedtls_aes_context aes_ctx;
00186
00187
00188
00189 int (*f_entropy)(void *, unsigned char *, size_t);
00192 void *p_entropy;
00194 #if defined(MBEDTLS_THREADING_C)
00195 mbedtls_threading_mutex_t mutex;
00196 #endif
00197 }
00198 mbedtls_ctr_drbg_context;
00199
00207 void mbedtls_ctr_drbg_init( mbedtls_ctr_drbg_context *ctx );
00208
00243 #if MBEDTLS_CTR_DRBG_ENTROPY_LEN < MBEDTLS_CTR_DRBG_KEYSIZE * 3 / 2
00244
00251 #endif
00252
00278 int mbedtls_ctr_drbg_seed( mbedtls_ctr_drbg_context *ctx,
00279 int (*f_entropy)(void *, unsigned char *, size_t),
00280 void *p_entropy,
00281 const unsigned char *custom,
00282 size_t len );
00283
00289 void mbedtls_ctr_drbg_free( mbedtls_ctr_drbg_context *ctx );
00290
00304 void mbedtls_ctr_drbg_set_prediction_resistance( mbedtls_ctr_drbg_context *ctx,
00305 int resistance );
00306
00321 void mbedtls_ctr_drbg_set_entropy_len( mbedtls_ctr_drbg_context *ctx,
00322 size_t len );
00323
00336 void mbedtls_ctr_drbg_set_reseed_interval( mbedtls_ctr_drbg_context *ctx,
00337 int interval );
00338
00354 int mbedtls_ctr_drbg_reseed( mbedtls_ctr_drbg_context *ctx,
00355 const unsigned char *additional, size_t len );
00356
00372 int mbedtls_ctr_drbg_update_ret( mbedtls_ctr_drbg_context *ctx,
00373 const unsigned char *additional,
00374 size_t add_len );
00375
00393 void mbedtls_ctr_drbg_update( mbedtls_ctr_drbg_context *ctx,
00394 const unsigned char *additional,
00395 size_t add_len );
00396
00423 int mbedtls_ctr_drbg_random_with_add( void *p_rng,
00424 unsigned char *output, size_t output_len,
00425 const unsigned char *additional, size_t add_len );
00426
00443 int mbedtls_ctr_drbg_random( void *p_rng,
00444 unsigned char *output, size_t output_len );
00445
00446 #if defined(MBEDTLS_FS_IO)
00447
00458 int mbedtls_ctr_drbg_write_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00459
00474 int mbedtls_ctr_drbg_update_seed_file( mbedtls_ctr_drbg_context *ctx, const char *path );
00475 #endif
00476
00482 int mbedtls_ctr_drbg_self_test( int verbose );
00483
00484
00485 int mbedtls_ctr_drbg_seed_entropy_len( mbedtls_ctr_drbg_context *,
00486 int (*)(void *, unsigned char *, size_t), void *,
00487 const unsigned char *, size_t, size_t );
00488
00489 #ifdef __cplusplus
00490 }
00491 #endif
00492
00493 #endif