Skip to content

Commit da9ba56

Browse files
zx2c4tytso
authored andcommitted
random: add get_random_{bytes,u32,u64,int,long,once}_wait family
These functions are simple convenience wrappers that call wait_for_random_bytes before calling the respective get_random_* function. Signed-off-by: Jason A. Donenfeld <[email protected]> Signed-off-by: Theodore Ts'o <[email protected]>
1 parent e297a78 commit da9ba56

File tree

3 files changed

+29
-0
lines changed

3 files changed

+29
-0
lines changed

include/linux/net.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -274,6 +274,8 @@ do { \
274274

275275
#define net_get_random_once(buf, nbytes) \
276276
get_random_once((buf), (nbytes))
277+
#define net_get_random_once_wait(buf, nbytes) \
278+
get_random_once_wait((buf), (nbytes))
277279

278280
int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
279281
size_t num, size_t len);

include/linux/once.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,5 +53,7 @@ void __do_once_done(bool *done, struct static_key *once_key,
5353

5454
#define get_random_once(buf, nbytes) \
5555
DO_ONCE(get_random_bytes, (buf), (nbytes))
56+
#define get_random_once_wait(buf, nbytes) \
57+
DO_ONCE(get_random_bytes_wait, (buf), (nbytes)) \
5658

5759
#endif /* _LINUX_ONCE_H */

include/linux/random.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,31 @@ static inline unsigned long get_random_long(void)
5858
#endif
5959
}
6060

61+
/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
62+
* Returns the result of the call to wait_for_random_bytes. */
63+
static inline int get_random_bytes_wait(void *buf, int nbytes)
64+
{
65+
int ret = wait_for_random_bytes();
66+
if (unlikely(ret))
67+
return ret;
68+
get_random_bytes(buf, nbytes);
69+
return 0;
70+
}
71+
72+
#define declare_get_random_var_wait(var) \
73+
static inline int get_random_ ## var ## _wait(var *out) { \
74+
int ret = wait_for_random_bytes(); \
75+
if (unlikely(ret)) \
76+
return ret; \
77+
*out = get_random_ ## var(); \
78+
return 0; \
79+
}
80+
declare_get_random_var_wait(u32)
81+
declare_get_random_var_wait(u64)
82+
declare_get_random_var_wait(int)
83+
declare_get_random_var_wait(long)
84+
#undef declare_get_random_var
85+
6186
unsigned long randomize_page(unsigned long start, unsigned long range);
6287

6388
u32 prandom_u32(void);

0 commit comments

Comments
 (0)