Commit 753d433b authored by Tobin C. Harding's avatar Tobin C. Harding Committed by Theodore Ts'o

random: Return nbytes filled from hw RNG

Currently the function get_random_bytes_arch() has return value 'void'.
If the hw RNG fails we currently fall back to using get_random_bytes().
This defeats the purpose of requesting random material from the hw RNG
in the first place.

There are currently no intree users of get_random_bytes_arch().

Only get random bytes from the hw RNG, make function return the number
of bytes retrieved from the hw RNG.
Acked-by: default avatarTheodore Ts'o <tytso@mit.edu>
Reviewed-by: default avatarSteven Rostedt (VMware) <rostedt@goodmis.org>
Signed-off-by: default avatarTobin C. Harding <me@tobin.cc>
Signed-off-by: default avatarTheodore Ts'o <tytso@mit.edu>
parent 8ddd6efa
...@@ -1725,26 +1725,28 @@ EXPORT_SYMBOL(del_random_ready_callback); ...@@ -1725,26 +1725,28 @@ EXPORT_SYMBOL(del_random_ready_callback);
* key known by the NSA). So it's useful if we need the speed, but * key known by the NSA). So it's useful if we need the speed, but
* only if we're willing to trust the hardware manufacturer not to * only if we're willing to trust the hardware manufacturer not to
* have put in a back door. * have put in a back door.
*
* Return number of bytes filled in.
*/ */
void get_random_bytes_arch(void *buf, int nbytes) int __must_check get_random_bytes_arch(void *buf, int nbytes)
{ {
int left = nbytes;
char *p = buf; char *p = buf;
trace_get_random_bytes_arch(nbytes, _RET_IP_); trace_get_random_bytes_arch(left, _RET_IP_);
while (nbytes) { while (left) {
unsigned long v; unsigned long v;
int chunk = min(nbytes, (int)sizeof(unsigned long)); int chunk = min_t(int, left, sizeof(unsigned long));
if (!arch_get_random_long(&v)) if (!arch_get_random_long(&v))
break; break;
memcpy(p, &v, chunk); memcpy(p, &v, chunk);
p += chunk; p += chunk;
nbytes -= chunk; left -= chunk;
} }
if (nbytes) return nbytes - left;
get_random_bytes(p, nbytes);
} }
EXPORT_SYMBOL(get_random_bytes_arch); EXPORT_SYMBOL(get_random_bytes_arch);
......
...@@ -38,7 +38,7 @@ extern void get_random_bytes(void *buf, int nbytes); ...@@ -38,7 +38,7 @@ extern void get_random_bytes(void *buf, int nbytes);
extern int wait_for_random_bytes(void); extern int wait_for_random_bytes(void);
extern int add_random_ready_callback(struct random_ready_callback *rdy); extern int add_random_ready_callback(struct random_ready_callback *rdy);
extern void del_random_ready_callback(struct random_ready_callback *rdy); extern void del_random_ready_callback(struct random_ready_callback *rdy);
extern void get_random_bytes_arch(void *buf, int nbytes); extern int __must_check get_random_bytes_arch(void *buf, int nbytes);
#ifndef MODULE #ifndef MODULE
extern const struct file_operations random_fops, urandom_fops; extern const struct file_operations random_fops, urandom_fops;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment