Jason A. Donenfeld 7c03613344 random: use chacha20 for get_random_int/long
commit f5b98461cb upstream.

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 <>
Suggested-by: Theodore Ts'o <>
Cc: Theodore Ts'o <>
Cc: Hannes Frederic Sowa <>
Cc: Andy Lutomirski <>
Signed-off-by: Theodore Ts'o <>
Signed-off-by: Greg Kroah-Hartman <>
2017-04-12 12:41:15 +02:00
Kconfig Re-enable CONFIG_MODVERSIONS in a slightly weaker form 2016-11-29 16:01:30 -08:00
Makefile kbuild: allow archs to select link dead code/data elimination 2016-09-09 10:47:00 +02:00
calibrate.c kernel: add calibration_delay_done() 2014-06-16 12:47:39 -06:00
do_mounts.c init/do_mounts.c: add create_dev() failure log 2015-06-25 17:00:42 -07:00
do_mounts.h init/do_mounts: initrd_load() can be boolean 2016-01-20 17:09:18 -08:00
do_mounts_initrd.c init/do_mounts: initrd_load() can be boolean 2016-01-20 17:09:18 -08:00
do_mounts_md.c init: disable sparse checking of the mount.o source files 2012-05-31 17:49:27 -07:00
do_mounts_rd.c init: use pr_cont() when displaying rotator during ramdisk loading. 2016-11-24 09:32:20 -08:00
init_task.c sched/core: Allow putting thread_info into task_struct 2016-09-15 08:25:13 +02:00
initramfs.c kexec: split kexec_load syscall from kexec core code 2015-09-10 13:29:01 -07:00
main.c random: use chacha20 for get_random_int/long 2017-04-12 12:41:15 +02:00
noinitramfs.c init: mark __user address space on string literals 2010-10-26 16:52:15 -07:00
version.c copy address of proc_ns_ops into ns_common 2014-12-04 14:34:47 -05:00