Skip to content

Commit

Permalink
---
Browse files Browse the repository at this point in the history
yaml
---
r: 346498
b: refs/heads/master
c: 6582c66
h: refs/heads/master
v: v3
  • Loading branch information
Akinobu Mita authored and Linus Torvalds committed Dec 18, 2012
1 parent 3fa5219 commit 2218c1f
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion [refs]
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
---
refs/heads/master: 496f2f93b1cc286f5a4f4f9acdc1e5314978683f
refs/heads/master: 6582c665d6b882dad8329e05749fbcf119f1ab88
2 changes: 2 additions & 0 deletions trunk/include/linux/random.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ unsigned int get_random_int(void);
unsigned long randomize_range(unsigned long start, unsigned long end, unsigned long len);

u32 prandom_u32(void);
void prandom_bytes(void *buf, int nbytes);
void prandom_seed(u32 seed);

/*
Expand All @@ -36,6 +37,7 @@ void prandom_seed(u32 seed);
#define srandom32(seed) prandom_seed(seed)

u32 prandom_u32_state(struct rnd_state *);
void prandom_bytes_state(struct rnd_state *state, void *buf, int nbytes);

/*
* Handle minimum values for seeds
Expand Down
49 changes: 49 additions & 0 deletions trunk/lib/random32.c
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,55 @@ u32 prandom_u32(void)
}
EXPORT_SYMBOL(prandom_u32);

/*
* prandom_bytes_state - get the requested number of pseudo-random bytes
*
* @state: pointer to state structure holding seeded state.
* @buf: where to copy the pseudo-random bytes to
* @bytes: the requested number of bytes
*
* This is used for pseudo-randomness with no outside seeding.
* For more random results, use prandom_bytes().
*/
void prandom_bytes_state(struct rnd_state *state, void *buf, int bytes)
{
unsigned char *p = buf;
int i;

for (i = 0; i < round_down(bytes, sizeof(u32)); i += sizeof(u32)) {
u32 random = prandom_u32_state(state);
int j;

for (j = 0; j < sizeof(u32); j++) {
p[i + j] = random;
random >>= BITS_PER_BYTE;
}
}
if (i < bytes) {
u32 random = prandom_u32_state(state);

for (; i < bytes; i++) {
p[i] = random;
random >>= BITS_PER_BYTE;
}
}
}
EXPORT_SYMBOL(prandom_bytes_state);

/**
* prandom_bytes - get the requested number of pseudo-random bytes
* @buf: where to copy the pseudo-random bytes to
* @bytes: the requested number of bytes
*/
void prandom_bytes(void *buf, int bytes)
{
struct rnd_state *state = &get_cpu_var(net_rand_state);

prandom_bytes_state(state, buf, bytes);
put_cpu_var(state);
}
EXPORT_SYMBOL(prandom_bytes);

/**
* prandom_seed - add entropy to pseudo random number generator
* @seed: seed value
Expand Down

0 comments on commit 2218c1f

Please sign in to comment.