-
-
Notifications
You must be signed in to change notification settings - Fork 32.1k
Use CSPRNG for random UUID node ID #135244
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Another CSPRNG( I've run the brench mark wieh pyperf. It seems like from os import urandom
return int.from_bytes(urandom(2), 'big') & 0x3fff so to sum up, may we use |
To be precise, I should have said that this is assuming that the UUIDs are generated sequentially and that the underlying MT-19937 state is not touched in between, so it's a bit more complex. |
I think I saw the benchmarks results somewhere. Can you post them in full as well? (I think you edited your message). |
I'll treat this as a bugfix but not a security bug fix as it's not critical enough IMO. |
os.urandom(): Mean +- std dev: 205 ns +- 6 ns pyperf on python 3.9 |
You're right. And there are also secruity warnings of |
Can you test using the latest main branch instead of Python 3.9? TiA |
I've done the benchmarks on my side and they are different: $ ./python -m pyperf timeit -s "import random" "random.getrandbits(14)"
Mean +- std dev: 14.0 ns +- 0.3 ns
$ ./python -m pyperf timeit -s "import os" "int.from_bytes(os.urandom(2)) & 0x3fff"
Mean +- std dev: 555 ns +- 5 ns
$ ./python -m pyperf timeit -s "import secrets" "secrets.randbits(14)"
Mean +- std dev: 599 ns +- 5 ns So we're still losing quite a lot of speed. |
This is a very huge drop of performance. We're 25x times slower now. Since the node ID is cached, I think we can live with a CSPRNG. However resampling the clock sequence every time is too much of a performance loss, so we won't use it. However, we can add a note. |
My benchmark on 3.13:
Well yes, we are losing a quiet lot of speed |
Correct. node ID is cached so even if its 25x slower we just need to generate it once. |
So the new changes would be only in _random_getnode:
and some notes in docs? |
Yes. But prefer using |
Ok. I will post the final changes soon at the linked PR |
…, §6.10.3 (#135226) This aligns with the recommendations of RFC 9562, Section 6.10, paragraph 3 [1]. [1]: https://www.rfc-editor.org/rfc/rfc9562.html#section-6.10-3. --------- Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…C 9562, §6.10.3 (pythonGH-135226) This aligns with the recommendations of RFC 9562, Section 6.10, paragraph 3 [1]. [1]: https://www.rfc-editor.org/rfc/rfc9562.html#section-6.10-3. --------- (cherry picked from commit 1cb716387255a7bdab5b580bcf8ac1b6fa32cc41) Co-authored-by: LamentXU <108666168+LamentXU123@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
…FC 9562, §6.10.3 (GH-135226) (#135255) gh-135244: generate UUID random Node ID with a CSPRNG as per RFC 9562, §6.10.3 (GH-135226) This aligns with the recommendations of RFC 9562, Section 6.10, paragraph 3 [1]. [1]: https://www.rfc-editor.org/rfc/rfc9562.html#section-6.10-3. --------- (cherry picked from commit 1cb7163) Co-authored-by: LamentXU <108666168+LamentXU123@users.noreply.github.com> Co-authored-by: Bénédikt Tran <10796600+picnixz@users.noreply.github.com>
For 3.13, I'll hold the backport because I need #134704, which I don't want to merge for a while (see #132901 (comment) for the rationale). |
Uh oh!
There was an error while loading. Please reload this page.
Feature or enhancement
Proposal:
Issue for: #135226
(I forgot to open issue first, sry)
I'll do the benchmark of the performance impact when switching to
secrets.getrandbits()
for the clock sequence soon. I'll post here.I totally agree with the previous discussion. Summary:
_random_getnode()
from PRNG to CSPRNG which randomly generates 48-bits note when python failed to get MAC addressclock_seq
from PRNG to CSPRNG in bothuuidv1()
anduuidv6()
. Well I think this is of great importance. Those two algorithm are time-based. The only random thing comes fromclock_seq
. Hackers can predict the next UUID if they got 624*32 random bits generated from the previous UUIDs (1427 UUIDs will be enough).note that nothing will be changed if the performance impact is unacceptable.
Has this already been discussed elsewhere?
I have already discussed this feature proposal on Discourse
Links to previous discussion of this feature:
#135226
Linked PRs
The text was updated successfully, but these errors were encountered: