Skip to content

Commit c489934

Browse files
picnixzLamentXU123
andauthored
[3.13] gh-135244: generate UUID random Node ID with a CSPRNG as per RFC 9562, §6.10.3 (GH-135226) (#137408)
* [3.13] 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>
1 parent 25c221f commit c489934

File tree

2 files changed

+12
-6
lines changed

2 files changed

+12
-6
lines changed

Lib/uuid.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -595,18 +595,20 @@ def _windll_getnode():
595595

596596
def _random_getnode():
597597
"""Get a random node ID."""
598-
# RFC 4122, $4.1.6 says "For systems with no IEEE address, a randomly or
599-
# pseudo-randomly generated value may be used; see Section 4.5. The
600-
# multicast bit must be set in such addresses, in order that they will
601-
# never conflict with addresses obtained from network cards."
598+
# RFC 9562, §6.10-3 says that
599+
#
600+
# Implementations MAY elect to obtain a 48-bit cryptographic-quality
601+
# random number as per Section 6.9 to use as the Node ID. [...] [and]
602+
# implementations MUST set the least significant bit of the first octet
603+
# of the Node ID to 1. This bit is the unicast or multicast bit, which
604+
# will never be set in IEEE 802 addresses obtained from network cards.
602605
#
603606
# The "multicast bit" of a MAC address is defined to be "the least
604607
# significant bit of the first octet". This works out to be the 41st bit
605608
# counting from 1 being the least significant bit, or 1<<40.
606609
#
607610
# See https://en.wikipedia.org/w/index.php?title=MAC_address&oldid=1128764812#Universal_vs._local_(U/L_bit)
608-
import random
609-
return random.getrandbits(48) | (1 << 40)
611+
return int.from_bytes(os.urandom(6)) | (1 << 40)
610612

611613

612614
# _OS_GETTERS, when known, are targeted for a specific OS or platform.
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
:mod:`uuid`: when the MAC address cannot be determined, the 48-bit node
2+
ID is now generated with a cryptographically-secure pseudo-random number
3+
generator (CSPRNG) as per :rfc:`RFC 9562, §6.10.3 <9562#section-6.10-3>`.
4+
This affects :func:`~uuid.uuid1`.

0 commit comments

Comments
 (0)