A few secure, random bytes without `pgcrypto`
brandur.org
brandur.org
Taking the first 5 bytes of a v6 UUID (time) and last 5 (node) would be a bad random day.
> Postgres 13’s gen_random_uuid() which generates a V6 UUID that’s secure...
gen_random_uuid gives you a version V4 UUID, not a V6 UUID (it's even in the code comments in the snipped included in the blog). I don't believe Postgres even has a function to generate a V6 UUID - which, indeed, would be a bad idea to use as a source of randomness.
What is happening right now?
Or take the red pill, pull back the curtains of reality, and see the machinery behind: https://news.ycombinator.com/item?id=41197775
Glitch in the matrix.
What is happening right now? Why is your comment marked four hours ago?
Sure, it wouldn't be portable to windows but that's more of a feature than a bug.
This is the ChatGPT answer that I was able to derive:
> You can read from `/dev/urandom` in a PostgreSQL query using `plperlu`, which allows executing unsafe Perl code. > Create a function to read random bytes:
CREATE EXTENSION plperlu;
CREATE OR REPLACE FUNCTION get_random_bytes(num_bytes int)
RETURNS bytea
LANGUAGE plperlu
AS $$
my $num_bytes = $_[0];
open my $urandom, '<', '/dev/urandom' or die "Cannot open /dev/urandom: $!";
read $urandom, my $bytes, $num_bytes;
close $urandom;
return $bytes;
$$;
SELECT get_random_bytes(16);Some systems have basically made them equivalent though.
I can't find that footnote anywhere
Having your security strategy rely on quirky behaviors of an implementation detail which might change is incredibly dangerous.
That being said, the PostgreSQL documentation doesn't say anything in particular about the predictability of `gen_random_uuid`, so the behavior is unspecified. But it's worth noting the function has an explicit guard to raise an error if secure random is not available, so they were conscious of this possibility and did not attempt any misguided fallbacks.
And unfortunately this requirement is not baked into the UUID spec either, which uses the word "should" instead of "must" when discussing CSPRNG usage.
You can send half-random input in and then send more half-random input in until you’re satisfied that the RNG has gotten a suitable amount of entropy. Do not chop, rearrange, hash, or bit shift the data trying to make it “stronger” the CSPRNG will do an infinitely better job of doing that for you. Just treat it like a Mr Fusion. Drop a can, a banana peel and the stale beer in and let it cook.
I gave a similar speech to a team trying to initialize SSL sessions on an embedded machine. “But what if we XOR…” No. Stahp.
Just poke in the setSeed function and see what it does.
This does not actually work. If an attacker can observe output of the CSPRNG, and knows the initial state (when it did not yet have enough entropy), then piecemeal addition of entropy allows the attacker to bruteforce what the added entropy was. To be safe, you need to add a significant amount of entropy at once, without allowing the attacker to observe output from an intermediate state. But after you've done that, you won't ever need to add entropy again.
GP does not suggest using the output before enough entropy had been gathered, eg see ‘until’ in:
> until you’re satisfied that the RNG has gotten a suitable amount of entropy.
Why are you equating that to a hacky attempt to make less random data more random?
Another responder suggested that the mention of v6 UUIDs is an error. Maybe. But that’s a truly bizarre typo to make. And they still haven’t fixed it.
setSeed(0); setSeed(1); rand()
and
setSeed(1); rand()
returning different values is not only a good idea but is already a thing. Am I wrong?
This would confuse the hell out of me, what specifically has this behaviour?
Adding entropy is a very different operation.