summaryrefslogtreecommitdiff
path: root/include/linux/random.h
blob: 243c0602bbc6cc190c874d6aafcc508621f73777 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
/*
 * include/linux/random.h
 *
 * Include file for the random number generator.
 */
#ifndef _LINUX_RANDOM_H
#define _LINUX_RANDOM_H

#include <unistd.h>
#include <sys/syscall.h>
#include <linux/bug.h>

#ifdef __NR_getrandom
static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
{
	 return syscall(SYS_getrandom, buf, buflen, flags);
}
#else
extern int urandom_fd;

static inline int getrandom(void *buf, size_t buflen, unsigned int flags)
{
	return read(urandom_fd, buf, buflen);
}
#endif

static inline void get_random_bytes(void *buf, int nbytes)
{
	BUG_ON(getrandom(buf, nbytes, 0) != nbytes);
}

#define get_random_type(type)				\
static inline type get_random_##type(void)		\
{							\
	type v;						\
							\
	get_random_bytes(&v, sizeof(v));		\
	return v;					\
}

get_random_type(int);
get_random_type(long);
get_random_type(u64);

#endif /* _LINUX_RANDOM_H */