|
Packit Bot |
95feaa |
/* Fast pseudo-random bits based on clock_gettime.
|
|
Packit Bot |
95feaa |
Copyright (C) 2019 Free Software Foundation, Inc.
|
|
Packit Bot |
95feaa |
This file is part of the GNU C Library.
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
The GNU C Library is free software; you can redistribute it and/or
|
|
Packit Bot |
95feaa |
modify it under the terms of the GNU Lesser General Public
|
|
Packit Bot |
95feaa |
License as published by the Free Software Foundation; either
|
|
Packit Bot |
95feaa |
version 2.1 of the License, or (at your option) any later version.
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
The GNU C Library is distributed in the hope that it will be useful,
|
|
Packit Bot |
95feaa |
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
Packit Bot |
95feaa |
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
Packit Bot |
95feaa |
Lesser General Public License for more details.
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
You should have received a copy of the GNU Lesser General Public
|
|
Packit Bot |
95feaa |
License along with the GNU C Library; if not, see
|
|
Packit Bot |
95feaa |
<http://www.gnu.org/licenses/>. */
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
#ifndef _RANDOM_BITS_H
|
|
Packit Bot |
95feaa |
# define _RANDOM_BITS_H
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
#include <time.h>
|
|
Packit Bot |
95feaa |
#include <stdint.h>
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
/* Provides fast pseudo-random bits through clock_gettime. It has unspecified
|
|
Packit Bot |
95feaa |
starting time, nano-second accuracy, its randomness is significantly better
|
|
Packit Bot |
95feaa |
than gettimeofday, and for mostly architectures it is implemented through
|
|
Packit Bot |
95feaa |
vDSO instead of a syscall. Since the source is a system clock, the upper
|
|
Packit Bot |
95feaa |
bits will have less entropy. */
|
|
Packit Bot |
95feaa |
static inline uint32_t
|
|
Packit Bot |
95feaa |
random_bits (void)
|
|
Packit Bot |
95feaa |
{
|
|
Packit Bot |
95feaa |
struct timespec tv;
|
|
Packit Bot |
95feaa |
__clock_gettime (CLOCK_MONOTONIC, &tv;;
|
|
Packit Bot |
95feaa |
/* Shuffle the lower bits to minimize the clock bias. */
|
|
Packit Bot |
95feaa |
uint32_t ret = tv.tv_nsec ^ tv.tv_sec;
|
|
Packit Bot |
95feaa |
ret ^= (ret << 24) | (ret >> 8);
|
|
Packit Bot |
95feaa |
return ret;
|
|
Packit Bot |
95feaa |
}
|
|
Packit Bot |
95feaa |
|
|
Packit Bot |
95feaa |
#endif
|