2022-01-24 12:07:52 +08:00
|
|
|
# Copyright (C) Dnspython Contributors, see LICENSE for text of ISC license
|
|
|
|
|
|
|
|
# Copyright (C) 2009-2017 Nominum, Inc.
|
2018-11-01 00:08:29 +08:00
|
|
|
#
|
|
|
|
# Permission to use, copy, modify, and distribute this software and its
|
|
|
|
# documentation for any purpose with or without fee is hereby granted,
|
|
|
|
# provided that the above copyright notice and this permission notice
|
|
|
|
# appear in all copies.
|
|
|
|
#
|
|
|
|
# THE SOFTWARE IS PROVIDED "AS IS" AND NOMINUM DISCLAIMS ALL WARRANTIES
|
|
|
|
# WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
|
|
|
# MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL NOMINUM BE LIABLE FOR
|
|
|
|
# ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
|
|
|
# WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
|
|
|
# ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
|
|
|
|
# OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
import hashlib
|
2024-03-04 01:15:23 +08:00
|
|
|
import os
|
2018-11-01 00:08:29 +08:00
|
|
|
import random
|
2024-03-04 01:15:23 +08:00
|
|
|
import threading
|
2018-11-01 00:08:29 +08:00
|
|
|
import time
|
2024-03-04 01:15:23 +08:00
|
|
|
from typing import Any, Optional
|
2022-01-24 12:07:52 +08:00
|
|
|
|
2018-11-01 00:08:29 +08:00
|
|
|
|
2022-01-24 12:07:52 +08:00
|
|
|
class EntropyPool:
|
|
|
|
# This is an entropy pool for Python implementations that do not
|
|
|
|
# have a working SystemRandom. I'm not sure there are any, but
|
|
|
|
# leaving this code doesn't hurt anything as the library code
|
|
|
|
# is used if present.
|
2018-11-01 00:08:29 +08:00
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def __init__(self, seed: Optional[bytes] = None):
|
2018-11-01 00:08:29 +08:00
|
|
|
self.pool_index = 0
|
2024-03-04 01:15:23 +08:00
|
|
|
self.digest: Optional[bytearray] = None
|
2018-11-01 00:08:29 +08:00
|
|
|
self.next_byte = 0
|
2024-03-04 01:15:23 +08:00
|
|
|
self.lock = threading.Lock()
|
2022-01-24 12:07:52 +08:00
|
|
|
self.hash = hashlib.sha1()
|
|
|
|
self.hash_len = 20
|
2024-03-04 01:15:23 +08:00
|
|
|
self.pool = bytearray(b"\0" * self.hash_len)
|
2018-11-01 00:08:29 +08:00
|
|
|
if seed is not None:
|
2024-03-04 01:15:23 +08:00
|
|
|
self._stir(seed)
|
2018-11-01 00:08:29 +08:00
|
|
|
self.seeded = True
|
|
|
|
self.seed_pid = os.getpid()
|
|
|
|
else:
|
|
|
|
self.seeded = False
|
|
|
|
self.seed_pid = 0
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def _stir(self, entropy: bytes) -> None:
|
2022-01-24 12:07:52 +08:00
|
|
|
for c in entropy:
|
|
|
|
if self.pool_index == self.hash_len:
|
|
|
|
self.pool_index = 0
|
2024-03-04 01:15:23 +08:00
|
|
|
b = c & 0xFF
|
2022-01-24 12:07:52 +08:00
|
|
|
self.pool[self.pool_index] ^= b
|
|
|
|
self.pool_index += 1
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def stir(self, entropy: bytes) -> None:
|
2022-01-24 12:07:52 +08:00
|
|
|
with self.lock:
|
|
|
|
self._stir(entropy)
|
2018-11-01 00:08:29 +08:00
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def _maybe_seed(self) -> None:
|
2018-11-01 00:08:29 +08:00
|
|
|
if not self.seeded or self.seed_pid != os.getpid():
|
|
|
|
try:
|
|
|
|
seed = os.urandom(16)
|
2022-01-24 12:07:52 +08:00
|
|
|
except Exception: # pragma: no cover
|
2018-11-01 00:08:29 +08:00
|
|
|
try:
|
2024-03-04 01:15:23 +08:00
|
|
|
with open("/dev/urandom", "rb", 0) as r:
|
2018-11-01 00:08:29 +08:00
|
|
|
seed = r.read(16)
|
|
|
|
except Exception:
|
2024-03-04 01:15:23 +08:00
|
|
|
seed = str(time.time()).encode()
|
2018-11-01 00:08:29 +08:00
|
|
|
self.seeded = True
|
|
|
|
self.seed_pid = os.getpid()
|
|
|
|
self.digest = None
|
|
|
|
seed = bytearray(seed)
|
2022-01-24 12:07:52 +08:00
|
|
|
self._stir(seed)
|
2018-11-01 00:08:29 +08:00
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def random_8(self) -> int:
|
2022-01-24 12:07:52 +08:00
|
|
|
with self.lock:
|
2018-11-01 00:08:29 +08:00
|
|
|
self._maybe_seed()
|
|
|
|
if self.digest is None or self.next_byte == self.hash_len:
|
2022-01-24 12:07:52 +08:00
|
|
|
self.hash.update(bytes(self.pool))
|
2018-11-01 00:08:29 +08:00
|
|
|
self.digest = bytearray(self.hash.digest())
|
2022-01-24 12:07:52 +08:00
|
|
|
self._stir(self.digest)
|
2018-11-01 00:08:29 +08:00
|
|
|
self.next_byte = 0
|
|
|
|
value = self.digest[self.next_byte]
|
|
|
|
self.next_byte += 1
|
|
|
|
return value
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def random_16(self) -> int:
|
2018-11-01 00:08:29 +08:00
|
|
|
return self.random_8() * 256 + self.random_8()
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def random_32(self) -> int:
|
2018-11-01 00:08:29 +08:00
|
|
|
return self.random_16() * 65536 + self.random_16()
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
def random_between(self, first: int, last: int) -> int:
|
2018-11-01 00:08:29 +08:00
|
|
|
size = last - first + 1
|
2022-01-24 12:07:52 +08:00
|
|
|
if size > 4294967296:
|
2024-03-04 01:15:23 +08:00
|
|
|
raise ValueError("too big")
|
2018-11-01 00:08:29 +08:00
|
|
|
if size > 65536:
|
|
|
|
rand = self.random_32
|
2022-01-24 12:07:52 +08:00
|
|
|
max = 4294967295
|
2018-11-01 00:08:29 +08:00
|
|
|
elif size > 256:
|
|
|
|
rand = self.random_16
|
|
|
|
max = 65535
|
|
|
|
else:
|
|
|
|
rand = self.random_8
|
|
|
|
max = 255
|
|
|
|
return first + size * rand() // (max + 1)
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
2018-11-01 00:08:29 +08:00
|
|
|
pool = EntropyPool()
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
system_random: Optional[Any]
|
2018-11-01 00:08:29 +08:00
|
|
|
try:
|
|
|
|
system_random = random.SystemRandom()
|
2022-01-24 12:07:52 +08:00
|
|
|
except Exception: # pragma: no cover
|
2018-11-01 00:08:29 +08:00
|
|
|
system_random = None
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
|
|
|
def random_16() -> int:
|
2018-11-01 00:08:29 +08:00
|
|
|
if system_random is not None:
|
|
|
|
return system_random.randrange(0, 65536)
|
|
|
|
else:
|
|
|
|
return pool.random_16()
|
|
|
|
|
2024-03-04 01:15:23 +08:00
|
|
|
|
|
|
|
def between(first: int, last: int) -> int:
|
2018-11-01 00:08:29 +08:00
|
|
|
if system_random is not None:
|
|
|
|
return system_random.randrange(first, last + 1)
|
|
|
|
else:
|
|
|
|
return pool.random_between(first, last)
|