Skip to content

Commit

Permalink
random: add get_random_{bytes,u32,u64,int,long,once}_wait family
Browse files Browse the repository at this point in the history
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 <Jason@zx2c4.com>
Signed-off-by: Theodore Ts'o <tytso@mit.edu>
  • Loading branch information
Jason A. Donenfeld authored and Theodore Ts'o committed Jun 20, 2017
1 parent e297a78 commit da9ba56
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
2 changes: 2 additions & 0 deletions include/linux/net.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,8 @@ do { \

#define net_get_random_once(buf, nbytes) \
get_random_once((buf), (nbytes))
#define net_get_random_once_wait(buf, nbytes) \
get_random_once_wait((buf), (nbytes))

int kernel_sendmsg(struct socket *sock, struct msghdr *msg, struct kvec *vec,
size_t num, size_t len);
Expand Down
2 changes: 2 additions & 0 deletions include/linux/once.h
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,7 @@ void __do_once_done(bool *done, struct static_key *once_key,

#define get_random_once(buf, nbytes) \
DO_ONCE(get_random_bytes, (buf), (nbytes))
#define get_random_once_wait(buf, nbytes) \
DO_ONCE(get_random_bytes_wait, (buf), (nbytes)) \

#endif /* _LINUX_ONCE_H */
25 changes: 25 additions & 0 deletions include/linux/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,31 @@ static inline unsigned long get_random_long(void)
#endif
}

/* Calls wait_for_random_bytes() and then calls get_random_bytes(buf, nbytes).
* Returns the result of the call to wait_for_random_bytes. */
static inline int get_random_bytes_wait(void *buf, int nbytes)
{
int ret = wait_for_random_bytes();
if (unlikely(ret))
return ret;
get_random_bytes(buf, nbytes);
return 0;
}

#define declare_get_random_var_wait(var) \
static inline int get_random_ ## var ## _wait(var *out) { \
int ret = wait_for_random_bytes(); \
if (unlikely(ret)) \
return ret; \
*out = get_random_ ## var(); \
return 0; \
}
declare_get_random_var_wait(u32)
declare_get_random_var_wait(u64)
declare_get_random_var_wait(int)
declare_get_random_var_wait(long)
#undef declare_get_random_var

unsigned long randomize_page(unsigned long start, unsigned long range);

u32 prandom_u32(void);
Expand Down

0 comments on commit da9ba56

Please sign in to comment.