summaryrefslogtreecommitdiff
path: root/src/basic/random-util.h
Commit message (Collapse)AuthorAgeFilesLines
* random-seed: move pool size determination to random-util.[ch]Lennart Poettering2019-07-251-0/+6
| | | | That way we can reuse it elsewhere.
* random-util: rename RANDOM_DONT_DRAIN → RANDOM_MAY_FAILLennart Poettering2019-05-071-2/+2
| | | | | | | | | | | The old flag name was a bit of a misnomer, as /dev/urandom cannot be "drained". Once it's initialized it's initialized and then is good forever. (Only /dev/random has a concept of 'draining', but we never use that, as it's an obsolete interface). The flag is still useful though, since it allows us to suppress accesses to the random pool while it is not initialized, as that trips up the kernel and it logs about any such attempts, which we really don't want.
* random-util: allow RDRAND to be used in 32-bit x86 binariesMike Gilbert2018-11-101-1/+1
| | | | | | | | | | | Rename rdrand64 to rdrand, and switch from uint64_t to unsigned long. This produces code that will compile/assemble on both x86-64 and x86-32. This could be useful when running a 32-bit copy of systemd on a modern Intel processor. RDRAND is inherently arch-specific, so relying on the compiler-defined 'long' type seems reasonable.
* random-util: optionally allow randomness to be generated via RDRANDLennart Poettering2018-11-081-0/+1
| | | | | | | | We only use this when we don't require the best randomness. The primary usecase for this is UUID generation, as this means we don't drain randomness from the kernel pool for them. Since UUIDs are usually not secrets RDRAND should be goot enough for them to avoid real-life collisions.
* random-util: introduce RANDOM_DONT_DRAINLennart Poettering2018-11-081-0/+1
| | | | | | | | | | | | Originally, the high_quality_required boolean argument controlled two things: whether to extend any random data we successfully read with pseudo-random data, and whether to return -ENODATA if we couldn't read any data at all. The boolean got replaced by RANDOM_EXTEND_WITH_PSEUDO, but this name doesn't really cover the second part nicely. Moreover hiding both changes of behaviour under a single flag is confusing. Hence, let's split this part off under a new flag, and use it from random_bytes().
* random-util: optionally enable blocking getrandom() behaviourLennart Poettering2018-11-081-0/+1
| | | | | When generating the salt for the firstboot password logic, let's use getrandom() blocking mode, and insist in the very best entropy.
* random-util: change high_quality_required bool parameter into a flags parameterLennart Poettering2018-11-081-3/+7
| | | | No change in behaviour, just some refactoring.
* random-util: rename acquire_random_bytes() → genuine_random_bytes()Lennart Poettering2018-11-081-3/+4
| | | | | | | | | It's more descriptive, since we also have a function random_bytes() which sounds very similar. Also rename pseudorandom_bytes() to pseudo_random_bytes(). This way the two functions are nicely systematic, one returning genuine random bytes and the other pseudo random ones.
* random-util: use RDRAND for randomness if the kernel doesn't want to give us anyLennart Poettering2018-08-211-0/+2
| | | | | | | | | | | Pretty much all intel cpus have had RDRAND in a long time. While CPU-internal RNG are widely not trusted, for seeding hash tables it's perfectly OK to use: we don't high quality entropy in that case, hence let's use it. This is only hooked up with 'high_quality_required' is false. If we require high quality entropy the kernel is the only source we should use.
* tree-wide: remove Lennart's copyright linesLennart Poettering2018-06-141-4/+0
| | | | | | | | | | | These lines are generally out-of-date, incomplete and unnecessary. With SPDX and git repository much more accurate and fine grained information about licensing and authorship is available, hence let's drop the per-file copyright notice. Of course, removing copyright lines of others is problematic, hence this commit only removes my own lines and leaves all others untouched. It might be nicer if sooner or later those could go away too, making git the only and accurate source of authorship information.
* tree-wide: drop 'This file is part of systemd' blurbLennart Poettering2018-06-141-2/+0
| | | | | | | | | | | | | | | | This part of the copyright blurb stems from the GPL use recommendations: https://www.gnu.org/licenses/gpl-howto.en.html The concept appears to originate in times where version control was per file, instead of per tree, and was a way to glue the files together. Ultimately, we nowadays don't live in that world anymore, and this information is entirely useless anyway, as people are very welcome to copy these files into any projects they like, and they shouldn't have to change bits that are part of our copyright header for that. hence, let's just get rid of this old cruft, and shorten our codebase a bit.
* tree-wide: drop license boilerplateZbigniew Jędrzejewski-Szmek2018-04-061-13/+0
| | | | | | | | | | Files which are installed as-is (any .service and other unit files, .conf files, .policy files, etc), are left as is. My assumption is that SPDX identifiers are not yet that well known, so it's better to retain the extended header to avoid any doubt. I also kept any copyright lines. We can probably remove them, but it'd nice to obtain explicit acks from all involved authors before doing that.
* Add SPDX license identifiers to source files under the LGPLZbigniew Jędrzejewski-Szmek2017-11-191-0/+1
| | | | | This follows what the kernel is doing, c.f. https://git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git/commit/?id=5fd54ace4721fc5ce2bb5aef6318fcf17f421460.
* basic/random-util: do not fall back to /dev/urandom if getrandom() returns shortZbigniew Jędrzejewski-Szmek2017-06-281-1/+2
| | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | | During early boot, we'd call getrandom(), and immediately fall back to reading from /dev/urandom unless we got the full requested number of bytes. Those two sources are the same, so the most likely result is /dev/urandom producing some pseudorandom numbers for us, complaining widely on the way. Let's change our behaviour to be more conservative: - if the numbers are only used to initialize a hash table, a short read is OK, we don't really care if we get the first part of the seed truly random and then some pseudorandom bytes. So just do that and return "success". - if getrandom() returns -EAGAIN, fall back to rand() instead of querying /dev/urandom again. The idea with those two changes is to avoid generating a warning about reading from an /dev/urandom when the kernel doesn't have enough entropy. - only in the cases where we really need to make the best effort possible (sd_id128_randomize and firstboot password hashing), fall back to /dev/urandom. When calling getrandom(), drop the checks whether the argument fits in an int — getrandom() should do that for us already, and we call it with small arguments only anyway. Note that this does not really change the (relatively high) number of random bytes we request from the kernel. On my laptop, during boot, PID 1 and all other processes using this code through libsystemd request: 74780 bytes with high_quality_required == false 464 bytes with high_quality_required == true and it does not eliminate reads from /dev/urandom completely. If the kernel was short on entropy and getrandom() would fail, we would fall back to /dev/urandom for those 464 bytes. When falling back to /dev/urandom, don't lose the short read we already got, and just read the remaining bytes. If getrandom() syscall is not available, we fall back to /dev/urandom same as before. Fixes #4167 (possibly partially, let's see).
* basic/random-util: use most of the pseudorandom bytes from rand()Zbigniew Jędrzejewski-Szmek2017-06-281-0/+1
| | | | | | The only implementation that we care about — glibc — provides us with 31 bits of entropy. Let's use 24 bits of that, instead of throwing all but 8 away.
* basic: include only what we useThomas Hindoe Paaboel Andersen2015-11-301-0/+1
| | | | | This is a cleaned up result of running iwyu but without forward declarations on src/basic.
* build-sys: split internal basic/ library from shared/Kay Sievers2015-06-111-0/+38
basic/ can be used by everything cannot use anything outside of basic/ libsystemd/ can use basic/ cannot use shared/ shared/ can use libsystemd/