Skip to content

Commit

Permalink
random: Add support for architectural random hooks
Browse files Browse the repository at this point in the history
Add support for architecture-specific hooks into the kernel-directed
random number generator interfaces.  This patchset does not use the
architecture random number generator interfaces for the
userspace-directed interfaces (/dev/random and /dev/urandom), thus
eliminating the need to distinguish between them based on a pool
pointer.

Changes in version 3:
- Moved the hooks from extract_entropy() to get_random_bytes().
- Changes the hooks to inlines.

Signed-off-by: H. Peter Anvin <hpa@linux.intel.com>
Cc: Fenghua Yu <fenghua.yu@intel.com>
Cc: Matt Mackall <mpm@selenic.com>
Cc: Herbert Xu <herbert@gondor.apana.org.au>
Cc: "Theodore Ts'o" <tytso@mit.edu>
  • Loading branch information
H. Peter Anvin committed Jul 31, 2011
1 parent 02f8c6a commit 63d7717
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 3 deletions.
25 changes: 22 additions & 3 deletions drivers/char/random.c
Original file line number Diff line number Diff line change
Expand Up @@ -932,7 +932,21 @@ static ssize_t extract_entropy_user(struct entropy_store *r, void __user *buf,
*/
void get_random_bytes(void *buf, int nbytes)
{
extract_entropy(&nonblocking_pool, buf, nbytes, 0, 0);
char *p = buf;

while (nbytes) {
unsigned long v;
int chunk = min(nbytes, (int)sizeof(unsigned long));

if (!arch_get_random_long(&v))
break;

memcpy(buf, &v, chunk);
p += chunk;
nbytes -= chunk;
}

extract_entropy(&nonblocking_pool, p, nbytes, 0, 0);
}
EXPORT_SYMBOL(get_random_bytes);

Expand Down Expand Up @@ -1635,8 +1649,13 @@ DEFINE_PER_CPU(__u32 [4], get_random_int_hash);
unsigned int get_random_int(void)
{
struct keydata *keyptr;
__u32 *hash = get_cpu_var(get_random_int_hash);
int ret;
__u32 *hash;
unsigned int ret;

if (arch_get_random_int(&ret))
return ret;

hash = get_cpu_var(get_random_int_hash);

keyptr = get_keyptr();
hash[0] += current->pid + jiffies + get_cycles();
Expand Down
13 changes: 13 additions & 0 deletions include/linux/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,19 @@ static inline void prandom32_seed(struct rnd_state *state, u64 seed)
state->s3 = __seed(i, 15);
}

#ifdef CONFIG_ARCH_RANDOM
# include <asm/archrandom.h>
#else
static inline int arch_get_random_long(unsigned long *v)
{
return 0;
}
static inline int arch_get_random_int(unsigned int *v)
{
return 0;
}
#endif

#endif /* __KERNEL___ */

#endif /* _LINUX_RANDOM_H */

0 comments on commit 63d7717

Please sign in to comment.