Changeset 2567


Ignore:
Timestamp:
3/21/2012 11:31:28 AM (3 years ago)
Author:
lowjoel
Message:
  • Fixed entropy poller registration: The plugins rewrite broke entropy source and entropy poller integration.
  • Rewrote the MixPool? routine to use max/min instead of repeated ternary operators for clarity.
  • Fixed the circular buffer which is the pool: the previous code always wrote new entropy to the start of the pool.
  • Every time the entropy pool wraps, mix the pool so that previous entropy is not wasted.
Location:
trunk/eraser/Eraser.Manager
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/eraser/Eraser.Manager/EntropyPoller.cs

    r2566 r2567  
    4949            Pool = new byte[sizeof(uint) << 7]; 
    5050 
     51            //Handle the Entropy Source Registered event. 
     52            Host.Instance.EntropySources.Registered += OnEntropySourceRegistered; 
     53 
     54            //Meanwhile, add all entropy sources already registered. 
     55            foreach (IEntropySource source in Host.Instance.EntropySources) 
     56                AddEntropySource(source); 
     57 
    5158            //Then start the thread which maintains the pool. 
    5259            Thread = new Thread(Main); 
     
    7885                Thread.Sleep(2000 + (int)(DateTime.Now.Ticks % 2999)); 
    7986 
    80                 // Send entropy to the PRNGs for new seeds. 
     87                //Send entropy to the PRNGs for new seeds. 
    8188                DateTime now = DateTime.Now; 
    8289                if (now - lastAddedEntropy > managerEntropySpan) 
     
    94101        { 
    95102            Thread.Abort(); 
     103        } 
     104 
     105        /// <summary> 
     106        /// Handles the OnEntropySourceRegistered event so we can register them with 
     107        /// ourselves. 
     108        /// </summary> 
     109        /// <param name="sender">The object which was registered.</param> 
     110        /// <param name="e">Event argument.</param> 
     111        private void OnEntropySourceRegistered(object sender, EventArgs e) 
     112        { 
     113            AddEntropySource((IEntropySource)sender); 
    96114        } 
    97115 
     
    177195                for (; i < Pool.Length - hashSize; i += hashSize) 
    178196                    Buffer.BlockCopy(hash.ComputeHash(Pool, i, 
    179                         i + mixBlockSize >= Pool.Length ? Pool.Length - i : mixBlockSize), 
    180                         0, Pool, i, i + hashSize >= Pool.Length ? Pool.Length - i : hashSize); 
     197                        Math.Min(mixBlockSize, Pool.Length - i)), 0, Pool, i, 
     198                        Math.Min(hashSize, Pool.Length - i)); 
    181199 
    182200                //Mix the remaining blocks which require copying from the front 
     
    184202                for (; i < Pool.Length; i += hashSize) 
    185203                { 
    186                     Buffer.BlockCopy(Pool, i, combinedBuffer, 0, Pool.Length - i); 
    187  
    188                     Buffer.BlockCopy(Pool, 0, combinedBuffer, Pool.Length - i, 
    189                                 mixBlockSize - (Pool.Length - i)); 
     204                    int remainder = Pool.Length - i; 
     205                    Buffer.BlockCopy(Pool, i, combinedBuffer, 0, remainder); 
     206                    Buffer.BlockCopy(Pool, 0, combinedBuffer, remainder, 
     207                        mixBlockSize - remainder); 
    190208 
    191209                    Buffer.BlockCopy(hash.ComputeHash(combinedBuffer, 0, mixBlockSize), 0, 
    192                         Pool, i, Pool.Length - i > hashSize ? hashSize : Pool.Length - i); 
     210                        Pool, i, Math.Min(hashSize, remainder)); 
    193211                } 
    194212            } 
     
    222240                        //Bring the pool position back to the front if we are at our end 
    223241                        if (PoolPosition >= Pool.Length) 
     242                        { 
    224243                            PoolPosition = 0; 
     244                            MixPool(); 
     245                        } 
    225246 
    226247                        int amountToMix = Math.Min(size, Pool.Length - PoolPosition); 
     
    228249                        mpEntropy = mpEntropy + amountToMix; 
    229250                        size -= amountToMix; 
     251                        PoolPosition += amountToMix; 
    230252                    } 
    231253                } 
  • trunk/eraser/Eraser.Manager/ManagerLibrary.cs

    r2516 r2567  
    4444            Instance = this; 
    4545            Settings = new ManagerSettings(persistentStore); 
    46             entropyPoller = new EntropyPoller(); 
    4746            Host.Initialise(persistentStore); 
    4847            Host.Instance.PluginLoad += OnPluginLoad; 
    4948            Host.Instance.Load(); 
     49 
     50            //Initialise the Entropy Poller last since it depends on the Host. 
     51            entropyPoller = new EntropyPoller(); 
    5052        } 
    5153 
Note: See TracChangeset for help on using the changeset viewer.