OLD | NEW |
(Empty) | |
| 1 # |
| 2 # Random/OSRNG/nt.py : OS entropy source for MS Windows |
| 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 |
| 24 |
| 25 __revision__ = "$Id$" |
| 26 __all__ = ['WindowsRNG'] |
| 27 |
| 28 import winrandom |
| 29 from rng_base import BaseRNG |
| 30 |
| 31 class WindowsRNG(BaseRNG): |
| 32 |
| 33 name = "<CryptGenRandom>" |
| 34 |
| 35 def __init__(self): |
| 36 self.__winrand = winrandom.new() |
| 37 BaseRNG.__init__(self) |
| 38 |
| 39 def flush(self): |
| 40 """Work around weakness in Windows RNG. |
| 41 |
| 42 The CryptGenRandom mechanism in some versions of Windows allows an |
| 43 attacker to learn 128 KiB of past and future output. As a workaround, |
| 44 this function reads 128 KiB of 'random' data from Windows and discards |
| 45 it. |
| 46 |
| 47 For more information about the weaknesses in CryptGenRandom, see |
| 48 _Cryptanalysis of the Random Number Generator of the Windows Operating |
| 49 System_, by Leo Dorrendorf and Zvi Gutterman and Benny Pinkas |
| 50 http://eprint.iacr.org/2007/419 |
| 51 """ |
| 52 if self.closed: |
| 53 raise ValueError("I/O operation on closed file") |
| 54 data = self.__winrand.get_bytes(128*1024) |
| 55 assert (len(data) == 128*1024) |
| 56 BaseRNG.flush(self) |
| 57 |
| 58 def _close(self): |
| 59 self.__winrand = None |
| 60 |
| 61 def _read(self, N): |
| 62 # Unfortunately, research shows that CryptGenRandom doesn't provide |
| 63 # forward secrecy and fails the next-bit test unless we apply a |
| 64 # workaround, which we do here. See http://eprint.iacr.org/2007/419 |
| 65 # for information on the vulnerability. |
| 66 self.flush() |
| 67 data = self.__winrand.get_bytes(N) |
| 68 self.flush() |
| 69 return data |
| 70 |
| 71 def new(*args, **kwargs): |
| 72 return WindowsRNG(*args, **kwargs) |
| 73 |
| 74 # vim:set ts=4 sw=4 sts=4 expandtab: |
OLD | NEW |