public inbox for libc-help@sourceware.org
 help / color / mirror / Atom feed
From: Adhemerval Zanella <adhemerval.zanella@linaro.org>
To: Noah Goldstein <goldstein.w.n@gmail.com>,
	Florian Weimer <fweimer@redhat.com>
Cc: Fernando Gont <fernando@gont.com.ar>,
	Libc-help <libc-help@sourceware.org>
Subject: Re: getentropy() vs. getrandom() vs. arc4random()
Date: Wed, 15 Jun 2022 13:29:56 -0700	[thread overview]
Message-ID: <1C6880D9-7578-4EF6-8689-5AAD23E2618E@linaro.org> (raw)
In-Reply-To: <CAFUsyf+Xo8tzgbMPw2G8WFP15SEHu9DV8Oud9rymcTEABnVE2g@mail.gmail.com>



> On 15 Jun 2022, at 11:03, Noah Goldstein <goldstein.w.n@gmail.com> wrote:
> 
> On Wed, Jun 15, 2022 at 11:01 AM Adhemerval Zanella via Libc-help
> <libc-help@sourceware.org <mailto:libc-help@sourceware.org>> wrote:
>> 
>> 
>> 
>>> On 15 Jun 2022, at 07:24, Fernando Gont <fernando@gont.com.ar> wrote:
>>> 
>>> Hi,
>>> 
>>> I'm currently trying to grasp the functional differences in the different interfaces to generate pseudorandom numbers in different platforms. And I was wondering if you could shed some light on some questions I have.
>>> 
>>> 
>>> ** Brief Background: **
>>> 
>>> We're working on a document where we warn users about the security implications of using rand() and random() to generate pseudorandom numbers (in scenarios where cryptographically secure pseudorandom numbers are needed).
>>> 
>>> So we want to recommend better PRNG options for different operating systems. For example, in the case of OpenBSD, we recommend the use of arc4random(3), which provides a higher-level interface than the getentropy(2) system call.
>>> 
>>> However, we're unsure about what to recommend for the Linux case.
>>> 
>>> For the Linux case, I see that there's a lot of code using getrandom(2) -- a syscall --, which is kind of complex/too-low-level. And I see that Linux also has getentropy(3) library function, which is described in random(7) as a "more portable interface the underlying PRNG devices".
>>> 
>>> So, for the Linux case, I feel tempted to recommend the usage of getentropy(3) over getrandom(2), but since most code employs getrandom(2), I'm not sure whether I'm missing something.
>>> 
>>> Any thoughts?
>>> 
>>> Aside, it seems that for OpenBSD, getentropy(2) is a "low-level" syscall, while arc4random(3) is a high-level library function. But in the case of Linux, getentropy(3) is a high-level library function instead, while getrandom(2) is the low-level syscall. -- which means that usage of these interfaces would probably not be consistent across platforms.
>>> 
>>> Is this actually the case?
>> 
>> On glibc, getentropy and getrandom both end calling getrandom syscall although
>> with different flags. The getentropy calls getrandom without any flag which in turn
>> get entropy from /dev/urandom. The getrandom function allows us to specify
>> which source you use through GRND_RANDOM flag.
>> 
>> Also, getentropy current has a hard limit of maximum of 256 bytes and it is not
>> defined a cancelation entrypoint (so pthread_cancel does not act upon it).
>> 
>> So both functions drawn entropy direct from the kernel and with recent Linux
>> random number development to unify both random and urandom the difference
>> might ended up with just getentropy being a cancellation entrypoint.
>> 
>> The rand and random functions are both userspace only where caller should set
>> PRNG state and both returns predictable output based on the initial seed. On glibc
>> both are implemented with either a LGC or a polynomial generated, set by the
>> seed size. So the quality of the output will depend of the seed entropy and the
>> limitation of the polynomial used.
>> 
>> The arc4random is similar to getentropy and getrandom, but it tries to use kernel
>> entropy to initialize a PRNG. Also, the usual implementation that uses ChaCha20
>> (OpenBSD, FreeBSD) periodically feeds back kernel entropy to improve randomness.
>> The arc4random also provides some more guarantees, like fork-detection.
>> 
>> We are aiming to provide arc4random on new glibc version [1].
>> 
>> [1] https://patchwork.sourceware.org/project/glibc/list/?series=9540
> 
> Are there any blockers to this at the moment?
> 
> Wouldn't minding have some time before 2.36 to possibly look into the
> x86_64 implementations.

I think the last part is what missing some review (the TCB optimization to make it
lockless) and I am also waiting some feedback from Florian, since my proposal
is similar but uses a different strategy than his previous one.

  reply	other threads:[~2022-06-15 20:29 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-06-15 14:24 Fernando Gont
2022-06-15 18:00 ` Adhemerval Zanella
2022-06-15 18:03   ` Noah Goldstein
2022-06-15 20:29     ` Adhemerval Zanella [this message]
2022-06-16 17:12   ` Fernando Gont
2022-06-16 17:27     ` Yann Droneaud
2022-06-16 17:46       ` Adhemerval Zanella

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1C6880D9-7578-4EF6-8689-5AAD23E2618E@linaro.org \
    --to=adhemerval.zanella@linaro.org \
    --cc=fernando@gont.com.ar \
    --cc=fweimer@redhat.com \
    --cc=goldstein.w.n@gmail.com \
    --cc=libc-help@sourceware.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).