Skip to content

Commit f5b9846

Browse files
zx2c4tytso
authored andcommitted
random: use chacha20 for get_random_int/long
Now that our crng uses chacha20, we can rely on its speedy characteristics for replacing MD5, while simultaneously achieving a higher security guarantee. Before the idea was to use these functions if you wanted random integers that aren't stupidly insecure but aren't necessarily secure either, a vague gray zone, that hopefully was "good enough" for its users. With chacha20, we can strengthen this claim, since either we're using an rdrand-like instruction, or we're using the same crng as /dev/urandom. And it's faster than what was before. We could have chosen to replace this with a SipHash-derived function, which might be slightly faster, but at the cost of having yet another RNG construction in the kernel. By moving to chacha20, we have a single RNG to analyze and verify, and we also already get good performance improvements on all platforms. Implementation-wise, rather than use a generic buffer for both get_random_int/long and memcpy based on the size needs, we use a specific buffer for 32-bit reads and for 64-bit reads. This way, we're guaranteed to always have aligned accesses on all platforms. While slightly more verbose in C, the assembly this generates is a lot simpler than otherwise. Finally, on 32-bit platforms where longs and ints are the same size, we simply alias get_random_int to get_random_long. Signed-off-by: Jason A. Donenfeld <Jason@zx2c4.com> Suggested-by: Theodore Ts'o <tytso@mit.edu> Cc: Theodore Ts'o <tytso@mit.edu> Cc: Hannes Frederic Sowa <hannes@stressinduktion.org> Cc: Andy Lutomirski <luto@amacapital.net> Signed-off-by: Theodore Ts'o <tytso@mit.edu>
1 parent 5d0e5ea commit f5b9846

3 files changed

Lines changed: 43 additions & 43 deletions

File tree

‎drivers/char/random.c‎

Lines changed: 43 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2016,63 +2016,65 @@ struct ctl_table random_table[] = {
20162016
};
20172017
#endif /* CONFIG_SYSCTL */
20182018

2019-
static u32 random_int_secret[MD5_MESSAGE_BYTES / 4] ____cacheline_aligned;
2020-
2021-
int random_int_secret_init(void)
2022-
{
2023-
get_random_bytes(random_int_secret, sizeof(random_int_secret));
2024-
return 0;
2025-
}
2026-
2027-
static DEFINE_PER_CPU(__u32 [MD5_DIGEST_WORDS], get_random_int_hash)
2028-
__aligned(sizeof(unsigned long));
2019+
struct batched_entropy {
2020+
union {
2021+
unsigned long entropy_long[CHACHA20_BLOCK_SIZE / sizeof(unsigned long)];
2022+
unsigned int entropy_int[CHACHA20_BLOCK_SIZE / sizeof(unsigned int)];
2023+
};
2024+
unsigned int position;
2025+
};
20292026

20302027
/*
2031-
* Get a random word for internal kernel use only. Similar to urandom but
2032-
* with the goal of minimal entropy pool depletion. As a result, the random
2033-
* value is not cryptographically secure but for several uses the cost of
2034-
* depleting entropy is too high
2028+
* Get a random word for internal kernel use only. The quality of the random
2029+
* number is either as good as RDRAND or as good as /dev/urandom, with the
2030+
* goal of being quite fast and not depleting entropy.
20352031
*/
2036-
unsigned int get_random_int(void)
2032+
static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_long);
2033+
unsigned long get_random_long(void)
20372034
{
2038-
__u32 *hash;
2039-
unsigned int ret;
2035+
unsigned long ret;
2036+
struct batched_entropy *batch;
20402037

2041-
if (arch_get_random_int(&ret))
2038+
if (arch_get_random_long(&ret))
20422039
return ret;
20432040

2044-
hash = get_cpu_var(get_random_int_hash);
2045-
2046-
hash[0] += current->pid + jiffies + random_get_entropy();
2047-
md5_transform(hash, random_int_secret);
2048-
ret = hash[0];
2049-
put_cpu_var(get_random_int_hash);
2050-
2041+
batch = &get_cpu_var(batched_entropy_long);
2042+
if (batch->position % ARRAY_SIZE(batch->entropy_long) == 0) {
2043+
extract_crng((u8 *)batch->entropy_long);
2044+
batch->position = 0;
2045+
}
2046+
ret = batch->entropy_long[batch->position++];
2047+
put_cpu_var(batched_entropy_long);
20512048
return ret;
20522049
}
2053-
EXPORT_SYMBOL(get_random_int);
2050+
EXPORT_SYMBOL(get_random_long);
20542051

2055-
/*
2056-
* Same as get_random_int(), but returns unsigned long.
2057-
*/
2058-
unsigned long get_random_long(void)
2052+
#if BITS_PER_LONG == 32
2053+
unsigned int get_random_int(void)
20592054
{
2060-
__u32 *hash;
2061-
unsigned long ret;
2055+
return get_random_long();
2056+
}
2057+
#else
2058+
static DEFINE_PER_CPU(struct batched_entropy, batched_entropy_int);
2059+
unsigned int get_random_int(void)
2060+
{
2061+
unsigned int ret;
2062+
struct batched_entropy *batch;
20622063

2063-
if (arch_get_random_long(&ret))
2064+
if (arch_get_random_int(&ret))
20642065
return ret;
20652066

2066-
hash = get_cpu_var(get_random_int_hash);
2067-
2068-
hash[0] += current->pid + jiffies + random_get_entropy();
2069-
md5_transform(hash, random_int_secret);
2070-
ret = *(unsigned long *)hash;
2071-
put_cpu_var(get_random_int_hash);
2072-
2067+
batch = &get_cpu_var(batched_entropy_int);
2068+
if (batch->position % ARRAY_SIZE(batch->entropy_int) == 0) {
2069+
extract_crng((u8 *)batch->entropy_int);
2070+
batch->position = 0;
2071+
}
2072+
ret = batch->entropy_int[batch->position++];
2073+
put_cpu_var(batched_entropy_int);
20732074
return ret;
20742075
}
2075-
EXPORT_SYMBOL(get_random_long);
2076+
#endif
2077+
EXPORT_SYMBOL(get_random_int);
20762078

20772079
/**
20782080
* randomize_page - Generate a random, page aligned address

‎include/linux/random.h‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@ extern void get_random_bytes(void *buf, int nbytes);
3737
extern int add_random_ready_callback(struct random_ready_callback *rdy);
3838
extern void del_random_ready_callback(struct random_ready_callback *rdy);
3939
extern void get_random_bytes_arch(void *buf, int nbytes);
40-
extern int random_int_secret_init(void);
4140

4241
#ifndef MODULE
4342
extern const struct file_operations random_fops, urandom_fops;

‎init/main.c‎

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -879,7 +879,6 @@ static void __init do_basic_setup(void)
879879
do_ctors();
880880
usermodehelper_enable();
881881
do_initcalls();
882-
random_int_secret_init();
883882
}
884883

885884
static void __init do_pre_smp_initcalls(void)

0 commit comments

Comments
 (0)