posix.py 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. #
  2. # Random/OSRNG/posix.py : OS entropy source for POSIX systems
  3. #
  4. # Written in 2008 by Dwayne C. Litzenberger <dlitz@dlitz.net>
  5. #
  6. # ===================================================================
  7. # The contents of this file are dedicated to the public domain. To
  8. # the extent that dedication to the public domain is not available,
  9. # everyone is granted a worldwide, perpetual, royalty-free,
  10. # non-exclusive license to exercise all rights associated with the
  11. # contents of this file for any purpose whatsoever.
  12. # No rights are reserved.
  13. #
  14. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  15. # EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  16. # MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  17. # NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  18. # BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  19. # ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  20. # CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  21. # SOFTWARE.
  22. # ===================================================================
  23. __revision__ = "$Id$"
  24. __all__ = ['DevURandomRNG']
  25. import errno
  26. import os
  27. import stat
  28. from rng_base import BaseRNG
  29. from Crypto.Util.py3compat import b
  30. class DevURandomRNG(BaseRNG):
  31. def __init__(self, devname=None):
  32. if devname is None:
  33. self.name = "/dev/urandom"
  34. else:
  35. self.name = devname
  36. # Test that /dev/urandom is a character special device
  37. f = open(self.name, "rb", 0)
  38. fmode = os.fstat(f.fileno())[stat.ST_MODE]
  39. if not stat.S_ISCHR(fmode):
  40. f.close()
  41. raise TypeError("%r is not a character special device" % (self.name,))
  42. self.__file = f
  43. BaseRNG.__init__(self)
  44. def _close(self):
  45. self.__file.close()
  46. def _read(self, N):
  47. # Starting with Python 3 open with buffering=0 returns a FileIO object.
  48. # FileIO.read behaves like read(2) and not like fread(3) and thus we
  49. # have to handle the case that read returns less data as requested here
  50. # more carefully.
  51. data = b("")
  52. while len(data) < N:
  53. try:
  54. d = self.__file.read(N - len(data))
  55. except IOError, e:
  56. # read(2) has been interrupted by a signal; redo the read
  57. if e.errno == errno.EINTR:
  58. continue
  59. raise
  60. if d is None:
  61. # __file is in non-blocking mode and no data is available
  62. return data
  63. if len(d) == 0:
  64. # __file is in blocking mode and arrived at EOF
  65. return data
  66. data += d
  67. return data
  68. def new(*args, **kwargs):
  69. return DevURandomRNG(*args, **kwargs)
  70. # vim:set ts=4 sw=4 sts=4 expandtab: