Ignore:
Timestamp:
2/12/2010 8:39:59 AM (4 years ago)
Author:
lowjoel
Message:

Removed CryptGenRandom? for a few reasons:

  • The only use was in the Entropy gathering code. We default to using RNGCryptoServiceProvider which is already a CSPRNG.
  • Since RNGCryptoServiceProvider doesn't accept seeds, we can't seed it with our entropy
  • CryptGenRandom? involved a lot of setting up -- certain computers do not have it and would result in a crash
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser6/Eraser.Util/Security.cs

    r1802 r1832  
    9292                out wasVerified) && wasVerified; 
    9393        } 
    94  
    95         /// <summary> 
    96         /// Randomises the provided buffer using CryptGenRandom. 
    97         /// </summary> 
    98         /// <param name="cryptGenRandom">The buffer which receives the random 
    99         /// data. The contents of this buffer can also be used as a random 
    100         /// seed.</param> 
    101         /// <returns>True if the operation suceeded.</returns> 
    102         public static bool Randomise(byte[] buffer) 
    103         { 
    104             return CryptApi.CryptGenRandom(buffer); 
    105         } 
    106     } 
    107  
    108     internal sealed class CryptApi : IDisposable 
    109     { 
    110         /// <summary> 
    111         /// Constructor. 
    112         /// </summary> 
    113         private CryptApi() 
    114         { 
    115             /* Intel i8xx (82802 Firmware Hub Device) hardware random number generator */ 
    116             const string IntelDefaultProvider = "Intel Hardware Cryptographic Service Provider"; 
    117  
    118             handle = new SafeCryptHandle(); 
    119             if (NativeMethods.CryptAcquireContext(out handle, null, 
    120                 IntelDefaultProvider, NativeMethods.PROV_INTEL_SEC, 0)) 
    121             { 
    122                 return; 
    123             } 
    124             else if (NativeMethods.CryptAcquireContext(out handle, null, 
    125                 null, NativeMethods.PROV_RSA_FULL, 0)) 
    126             { 
    127                 return; 
    128             } 
    129             else if (Marshal.GetLastWin32Error() == NativeMethods.NTE_BAD_KEYSET) 
    130             { 
    131                 //Default keyset doesn't exist, attempt to create a new one 
    132                 if (NativeMethods.CryptAcquireContext(out handle, null, null, 
    133                     NativeMethods.PROV_RSA_FULL, NativeMethods.CRYPT_NEWKEYSET)) 
    134                 { 
    135                     return; 
    136                 } 
    137             } 
    138  
    139             throw new NotSupportedException("Unable to acquire a cryptographic service provider."); 
    140         } 
    141  
    142         #region IDisposable Members 
    143         ~CryptApi() 
    144         { 
    145             Dispose(false); 
    146         } 
    147  
    148         private void Dispose(bool disposing) 
    149         { 
    150             //If we already have run Dispose, then handle will be null. 
    151             if (handle == null) 
    152                 return; 
    153  
    154             if (disposing) 
    155                 handle.Close(); 
    156  
    157             //Don't run Dispose again. 
    158             handle = null; 
    159         } 
    160  
    161         public void Dispose() 
    162         { 
    163             Dispose(true); 
    164             GC.SuppressFinalize(this); 
    165         } 
    166         #endregion 
    167  
    168         /// <summary> 
    169         /// The GenRandom function fills a buffer with cryptographically random bytes. 
    170         /// </summary> 
    171         /// <param name="buffer">Buffer to receive the returned data. This buffer 
    172         /// must be at least dwLen bytes in length. 
    173         ///  
    174         /// Optionally, the application can fill this buffer with data to use as 
    175         /// an auxiliary random seed.</param> 
    176         public static bool CryptGenRandom(byte[] buffer) 
    177         { 
    178             return NativeMethods.CryptGenRandom(instance.handle, (uint)buffer.Length, buffer); 
    179         } 
    180  
    181         /// <summary> 
    182         /// The HCRYPTPROV handle. 
    183         /// </summary> 
    184         private SafeCryptHandle handle; 
    185  
    186         /// <summary> 
    187         /// The global CryptAPI instance. 
    188         /// </summary> 
    189         private static CryptApi instance = new CryptApi(); 
    190     } 
    191  
    192     internal class SafeCryptHandle : SafeHandleZeroOrMinusOneIsInvalid 
    193     { 
    194         public SafeCryptHandle() 
    195             : base(true) 
    196         { 
    197         } 
    198  
    199         protected override bool ReleaseHandle() 
    200         { 
    201             NativeMethods.CryptReleaseContext(handle, 0u); 
    202             handle = IntPtr.Zero; 
    203             return true; 
    204         } 
    20594    } 
    20695} 
Note: See TracChangeset for help on using the changeset viewer.