|
26 | 26 | * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
27 | 27 | * SUCH DAMAGE.
|
28 | 28 | *
|
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 $ |
30 | 30 | */
|
31 | 31 |
|
32 | 32 | #include "postgres.h"
|
@@ -219,21 +219,30 @@ encrypt_counter(FState * st, uint8 *dst)
|
219 | 219 | * microseconds.
|
220 | 220 | */
|
221 | 221 | static int
|
222 |
| -too_often(FState * st) |
| 222 | +enough_time_passed(FState * st) |
223 | 223 | {
|
224 | 224 | int ok;
|
225 | 225 | struct timeval tv;
|
226 | 226 | struct timeval *last = &st->last_reseed_time;
|
227 | 227 |
|
228 | 228 | gettimeofday(&tv, NULL);
|
229 | 229 |
|
| 230 | + /* check how much time has passed */ |
230 | 231 | ok = 0;
|
231 |
| - if (tv.tv_sec != last->tv_sec) |
| 232 | + if (tv.tv_sec > last->tv_sec + 1) |
232 | 233 | 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 | + } |
233 | 239 | else if (tv.tv_usec - last->tv_usec >= RESEED_INTERVAL)
|
234 | 240 | ok = 1;
|
235 | 241 |
|
236 |
| - memcpy(last, &tv, sizeof(tv)); |
| 242 | + /* reseed will happen, update last_reseed_time */ |
| 243 | + if (ok) |
| 244 | + memcpy(last, &tv, sizeof(tv)); |
| 245 | + |
237 | 246 | memset(&tv, 0, sizeof(tv));
|
238 | 247 |
|
239 | 248 | return ok;
|
@@ -372,7 +381,7 @@ extract_data(FState * st, unsigned count, uint8 *dst)
|
372 | 381 | unsigned block_nr = 0;
|
373 | 382 |
|
374 | 383 | /* Can we reseed? */
|
375 |
| - if (st->pool0_bytes >= POOL0_FILL && !too_often(st)) |
| 384 | + if (st->pool0_bytes >= POOL0_FILL && enough_time_passed(st)) |
376 | 385 | reseed(st);
|
377 | 386 |
|
378 | 387 | /* Is counter initialized? */
|
|
0 commit comments