| 1 |
"""The urandom() function, suitable for cryptographic use. |
| 2 |
""" |
| 3 |
|
| 4 |
from __future__ import with_statement |
| 5 |
import os, sys |
| 6 |
import errno |
| 7 |
|
| 8 |
from rpython.rtyper.lltypesystem import lltype, rffi |
| 9 |
from rpython.rlib.objectmodel import not_rpython |
| 10 |
from rpython.translator.tool.cbuild import ExternalCompilationInfo |
| 11 |
from rpython.rtyper.tool import rffi_platform |
| 12 |
|
| 13 |
|
| 14 |
if sys.platform == 'win32': |
| 15 |
from rpython.rlib import rwin32 |
| 16 |
|
| 17 |
eci = ExternalCompilationInfo( |
| 18 |
includes = ['windows.h', 'bcrypt.h'], |
| 19 |
libraries = ['Bcrypt'], |
| 20 |
) |
| 21 |
|
| 22 |
class CConfig: |
| 23 |
_compilation_info_ = eci |
| 24 |
|
| 25 |
globals().update(rffi_platform.configure(CConfig)) |
| 26 |
|
| 27 |
BCryptGenRandom = rffi.llexternal( |
| 28 |
'BCryptGenRandom', |
| 29 |
[rffi.VOIDP , rffi.CArrayPtr(rwin32.BYTE), rffi.ULONG, rffi.ULONG], |
| 30 |
rwin32.BOOL, |
| 31 |
calling_conv='win', |
| 32 |
compilation_info=eci, |
| 33 |
save_err=rffi.RFFI_SAVE_LASTERROR) |
| 34 |
|
| 35 |
def urandom(n, signal_checker=None): |
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
BCRYPT_USE_SYSTEM_PREFERRED_RNG = 0x00000002 |
| 40 |
|
| 41 |
with lltype.scoped_alloc(rffi.CArray(rwin32.BYTE), n, |
| 42 |
zero=True, |
| 43 |
) as buf: |
| 44 |
result = BCryptGenRandom(None, buf, n, BCRYPT_USE_SYSTEM_PREFERRED_RNG) |
| 45 |
if result != 0: |
| 46 |
raise rwin32.lastSavedWindowsError("BCryptGenRandom") |
| 47 |
|
| 48 |
return rffi.charpsize2str(rffi.cast(rffi.CCHARP, buf), n) |
| 49 |
|
| 50 |
else: |
| 51 |
SYS_getrandom = None |
| 52 |
|
| 53 |
if sys.platform.startswith('linux'): |
| 54 |
eci = ExternalCompilationInfo(includes=['sys/syscall.h']) |
| 55 |
class CConfig: |
| 56 |
_compilation_info_ = eci |
| 57 |
SYS_getrandom = rffi_platform.DefinedConstantInteger( |
| 58 |
'SYS_getrandom') |
| 59 |
globals().update(rffi_platform.configure(CConfig)) |
| 60 |
|
| 61 |
if SYS_getrandom is not None: |
| 62 |
from rpython.rlib.rposix import get_saved_errno, handle_posix_error |
| 63 |
import errno |
| 64 |
|
| 65 |
eci = eci.merge(ExternalCompilationInfo(includes=['linux/random.h'])) |
| 66 |
class CConfig: |
| 67 |
_compilation_info_ = eci |
| 68 |
GRND_NONBLOCK = rffi_platform.DefinedConstantInteger( |
| 69 |
'GRND_NONBLOCK') |
| 70 |
globals().update(rffi_platform.configure(CConfig)) |
| 71 |
if GRND_NONBLOCK is None: |
| 72 |
GRND_NONBLOCK = 0x0001 |
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
syscall = rffi.llexternal( |
| 77 |
'syscall', |
| 78 |
[lltype.Signed, rffi.CCHARP, rffi.LONG, rffi.INT], |
| 79 |
lltype.Signed, |
| 80 |
compilation_info=eci, |
| 81 |
save_err=rffi.RFFI_SAVE_ERRNO) |
| 82 |
|
| 83 |
class Works: |
| 84 |
status = True |
| 85 |
getrandom_works = Works() |
| 86 |
|
| 87 |
def _getrandom(n, result, signal_checker): |
| 88 |
if not getrandom_works.status: |
| 89 |
return n |
| 90 |
while n > 0: |
| 91 |
with rffi.scoped_alloc_buffer(n) as buf: |
| 92 |
got = syscall(SYS_getrandom, buf.raw, n, GRND_NONBLOCK) |
| 93 |
if got >= 0: |
| 94 |
s = buf.str(got) |
| 95 |
result.append(s) |
| 96 |
n -= len(s) |
| 97 |
continue |
| 98 |
err = get_saved_errno() |
| 99 |
if (err == errno.ENOSYS or err == errno.EPERM or |
| 100 |
err == errno.EAGAIN): |
| 101 |
getrandom_works.status = False |
| 102 |
return n |
| 103 |
if err == errno.EINTR: |
| 104 |
if signal_checker is not None: |
| 105 |
signal_checker() |
| 106 |
continue |
| 107 |
handle_posix_error("getrandom", got) |
| 108 |
raise AssertionError("unreachable") |
| 109 |
return n |
| 110 |
|
| 111 |
def urandom(n, signal_checker=None): |
| 112 |
"Read n bytes from /dev/urandom." |
| 113 |
|
| 114 |
|
| 115 |
result = [] |
| 116 |
if SYS_getrandom is not None: |
| 117 |
n = _getrandom(n, result, signal_checker) |
| 118 |
if n <= 0: |
| 119 |
return ''.join(result) |
| 120 |
|
| 121 |
|
| 122 |
|
| 123 |
|
| 124 |
|
| 125 |
fd = os.open("/dev/urandom", os.O_RDONLY, 0777) |
| 126 |
try: |
| 127 |
while n > 0: |
| 128 |
try: |
| 129 |
data = os.read(fd, n) |
| 130 |
except OSError as e: |
| 131 |
if e.errno != errno.EINTR: |
| 132 |
raise |
| 133 |
data = '' |
| 134 |
result.append(data) |
| 135 |
n -= len(data) |
| 136 |
finally: |
| 137 |
os.close(fd) |
| 138 |
return ''.join(result) |