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