From: Nikos Mavrogiannopoulos Date: Sun, 27 Jan 2013 10:51:05 +0000 (+0100) Subject: Added gnutls_rnd_refresh(). X-Git-Tag: gnutls_3_1_7~52 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=6e28ce9dbb230b52836dbce77f7e6a3a21d8eb6e;p=thirdparty%2Fgnutls.git Added gnutls_rnd_refresh(). --- diff --git a/NEWS b/NEWS index 9f5c9c8f02..68c924e0cc 100644 --- a/NEWS +++ b/NEWS @@ -14,7 +14,8 @@ in a template from an RFC4514 string. ** libgnutls: Added functions to directly set the DN in a certificate or request from an RFC4514 string. -** libgnutls: Optimizations in the nonce level of the random generator. +** libgnutls: Optimizations in the random generator. The re-seeding of +it is now explicitly done on every session deinit. ** libgnutls: Simplified the DTLS sliding window implementation. @@ -53,6 +54,7 @@ gnutls_range_split: Added gnutls_record_send_range: Added gnutls_record_set_max_empty_records: Added gnutls_record_can_use_length_hiding: Added +gnutls_rnd_refresh: Added GNUTLS_SEC_PARAM_EXPORT: Added GNUTLS_SEC_PARAM_VERY_WEAK: Added diff --git a/lib/crypto-backend.h b/lib/crypto-backend.h index 58c7530c0e..f807ce5d20 100644 --- a/lib/crypto-backend.h +++ b/lib/crypto-backend.h @@ -80,6 +80,7 @@ { int (*init) (void **ctx); int (*rnd) (void *ctx, int level, void *data, size_t datasize); + void (*rnd_refresh) (void *ctx); void (*deinit) (void *ctx); } gnutls_crypto_rnd_st; diff --git a/lib/gnutls_state.c b/lib/gnutls_state.c index 06560328ce..652b46e30a 100644 --- a/lib/gnutls_state.c +++ b/lib/gnutls_state.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2002-2012 Free Software Foundation, Inc. + * Copyright (C) 2002-2013 Free Software Foundation, Inc. * * Author: Nikos Mavrogiannopoulos * @@ -45,6 +45,7 @@ #include #include #include +#include #include #include @@ -432,6 +433,8 @@ gnutls_deinit (gnutls_session_t session) if (session == NULL) return; + _gnutls_rnd_refresh(); + /* remove auth info firstly */ _gnutls_free_auth_info (session); diff --git a/lib/includes/gnutls/crypto.h b/lib/includes/gnutls/crypto.h index c8d5755e4e..93db48ce96 100644 --- a/lib/includes/gnutls/crypto.h +++ b/lib/includes/gnutls/crypto.h @@ -99,6 +99,8 @@ extern "C" int gnutls_rnd (gnutls_rnd_level_t level, void *data, size_t len); + void gnutls_rnd_refresh (void); + #ifdef __cplusplus } #endif diff --git a/lib/nettle/rnd.c b/lib/nettle/rnd.c index 0894473f1f..a1c6b4ec63 100644 --- a/lib/nettle/rnd.c +++ b/lib/nettle/rnd.c @@ -437,15 +437,16 @@ wrap_nettle_rnd (void *_ctx, int level, void *data, size_t datasize) int ret; RND_LOCK; - gettime(¤t_time); /* update state only when having a non-nonce or if nonce * and nsecs%4096 == 0, i.e., one out of 4096 times called . * * The reason we do that is to avoid any delays when generating nonces. */ - if (level != GNUTLS_RND_NONCE || ((current_time.tv_nsec & 0x0fff) == 0)) + if (level != GNUTLS_RND_NONCE) { + gettime(¤t_time); + ret = do_trivia_source (0); if (ret < 0) { @@ -468,10 +469,24 @@ wrap_nettle_rnd (void *_ctx, int level, void *data, size_t datasize) return 0; } +static void +wrap_nettle_rnd_refresh (void *_ctx) +{ + RND_LOCK; + gettime(¤t_time); + + do_trivia_source (0); + do_device_source (0); + + RND_UNLOCK; + return; +} + int crypto_rnd_prio = INT_MAX; gnutls_crypto_rnd_st _gnutls_rnd_ops = { .init = wrap_nettle_rnd_init, .deinit = wrap_nettle_rnd_deinit, .rnd = wrap_nettle_rnd, + .rnd_refresh = wrap_nettle_rnd_refresh, }; diff --git a/lib/random.c b/lib/random.c index 06b08dba60..3af03befeb 100644 --- a/lib/random.c +++ b/lib/random.c @@ -73,3 +73,20 @@ gnutls_rnd (gnutls_rnd_level_t level, void *data, size_t len) { return _gnutls_rnd(level, data, len); } + +/** + * gnutls_rnd_refresh: + * + * This function refreshes the random generator state. + * That is the current precise time, CPU usage, and + * other values are input into its state. + * + * On a slower rate input from /dev/urandom is mixed too. + * + * Since: 3.1.7 + **/ +void +gnutls_rnd_refresh () +{ + _gnutls_rnd_refresh(); +} diff --git a/lib/random.h b/lib/random.h index 3d24363f2c..3a514f13f4 100644 --- a/lib/random.h +++ b/lib/random.h @@ -40,6 +40,12 @@ _gnutls_rnd (gnutls_rnd_level_t level, void *data, size_t len) return 0; } +inline static void +_gnutls_rnd_refresh (void) +{ + _gnutls_rnd_ops.rnd_refresh (gnutls_rnd_ctx); +} + void _gnutls_rnd_deinit (void); int _gnutls_rnd_init (void);