Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 5cf0790

Browse files
committed
Fix errors in fortuna PRNG reseeding logic that could cause a predictable
session key to be selected by pgp_sym_encrypt() in some cases. This only affects non-OpenSSL-using builds. Marko Kreen
1 parent 515112f commit 5cf0790

File tree

1 file changed

+14
-5
lines changed

1 file changed

+14
-5
lines changed

contrib/pgcrypto/fortuna.c

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@
2626
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
2727
* SUCH DAMAGE.
2828
*
29-
* $PostgreSQL: pgsql/contrib/pgcrypto/fortuna.c,v 1.5 2005/10/15 02:49:06 momjian Exp $
29+
* $PostgreSQL: pgsql/contrib/pgcrypto/fortuna.c,v 1.6 2006/05/21 20:22:16 tgl Exp $
3030
*/
3131

3232
#include "postgres.h"
@@ -219,21 +219,30 @@ encrypt_counter(FState * st, uint8 *dst)
219219
* microseconds.
220220
*/
221221
static int
222-
too_often(FState * st)
222+
enough_time_passed(FState * st)
223223
{
224224
int ok;
225225
struct timeval tv;
226226
struct timeval *last = &st->last_reseed_time;
227227

228228
gettimeofday(&tv, NULL);
229229

230+
/* check how much time has passed */
230231
ok = 0;
231-
if (tv.tv_sec != last->tv_sec)
232+
if (tv.tv_sec > last->tv_sec + 1)
232233
ok = 1;
234+
else if (tv.tv_sec == last->tv_sec + 1)
235+
{
236+
if (1000000 + tv.tv_usec - last->tv_usec >= RESEED_INTERVAL)
237+
ok = 1;
238+
}
233239
else if (tv.tv_usec - last->tv_usec >= RESEED_INTERVAL)
234240
ok = 1;
235241

236-
memcpy(last, &tv, sizeof(tv));
242+
/* reseed will happen, update last_reseed_time */
243+
if (ok)
244+
memcpy(last, &tv, sizeof(tv));
245+
237246
memset(&tv, 0, sizeof(tv));
238247

239248
return ok;
@@ -372,7 +381,7 @@ extract_data(FState * st, unsigned count, uint8 *dst)
372381
unsigned block_nr = 0;
373382

374383
/* Can we reseed? */
375-
if (st->pool0_bytes >= POOL0_FILL && !too_often(st))
384+
if (st->pool0_bytes >= POOL0_FILL && enough_time_passed(st))
376385
reseed(st);
377386

378387
/* Is counter initialized? */

0 commit comments

Comments
 (0)