| [2352] | 1 | /* |
|---|
| 2 | * $Id: EntropySource.cs 2055 2010-05-04 05:51:04Z lowjoel $ |
|---|
| 3 | * Copyright 2008-2010 The Eraser Project |
|---|
| 4 | * Original Author: Joel Low <lowjoel@users.sourceforge.net> |
|---|
| [2357] | 5 | * Modified By: |
|---|
| [2352] | 6 | * |
|---|
| 7 | * This file is part of Eraser. |
|---|
| 8 | * |
|---|
| 9 | * Eraser is free software: you can redistribute it and/or modify it under the |
|---|
| 10 | * terms of the GNU General Public License as published by the Free Software |
|---|
| 11 | * Foundation, either version 3 of the License, or (at your option) any later |
|---|
| 12 | * version. |
|---|
| 13 | * |
|---|
| 14 | * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY |
|---|
| 15 | * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR |
|---|
| 16 | * A PARTICULAR PURPOSE. See the GNU General Public License for more details. |
|---|
| 17 | * |
|---|
| 18 | * A copy of the GNU General Public License can be found at |
|---|
| 19 | * <http://www.gnu.org/licenses/>. |
|---|
| 20 | */ |
|---|
| 21 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Linq; |
|---|
| 25 | using System.Text; |
|---|
| 26 | using System.Runtime.InteropServices; |
|---|
| 27 | |
|---|
| 28 | namespace Eraser.Plugins.ExtensionPoints |
|---|
| 29 | { |
|---|
| 30 | /// <summary> |
|---|
| 31 | /// Provides an abstract interface to allow multiple sources of entropy into |
|---|
| 32 | /// the EntropyPoller class. |
|---|
| 33 | /// </summary> |
|---|
| [2439] | 34 | public interface IEntropySource : IRegisterable |
|---|
| [2352] | 35 | { |
|---|
| 36 | /// <summary> |
|---|
| 37 | /// Constructor. |
|---|
| 38 | /// </summary> |
|---|
| 39 | protected EntropySource() |
|---|
| 40 | { |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | /// <summary> |
|---|
| 44 | /// The name of the entropy source |
|---|
| 45 | /// </summary> |
|---|
| 46 | public abstract string Name |
|---|
| 47 | { |
|---|
| 48 | get; |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | /// <summary> |
|---|
| 52 | /// The guid representing this entropy source |
|---|
| 53 | /// </summary> |
|---|
| 54 | public abstract Guid Guid |
|---|
| 55 | { |
|---|
| 56 | get; |
|---|
| 57 | } |
|---|
| 58 | |
|---|
| 59 | /// <summary> |
|---|
| 60 | /// Gets a primer to add to the pool when this source is first initialised, to |
|---|
| 61 | /// further add entropy to the pool. |
|---|
| 62 | /// </summary> |
|---|
| 63 | /// <returns>A byte array containing the entropy.</returns> |
|---|
| 64 | public abstract byte[] GetPrimer(); |
|---|
| 65 | |
|---|
| 66 | /// <summary> |
|---|
| 67 | /// Retrieve entropy from a source which will have slow rate of |
|---|
| 68 | /// entropy polling. |
|---|
| 69 | /// </summary> |
|---|
| 70 | /// <returns></returns> |
|---|
| 71 | public abstract byte[] GetSlowEntropy(); |
|---|
| 72 | |
|---|
| 73 | /// <summary> |
|---|
| 74 | /// Retrieve entropy from a soruce which will have a fast rate of |
|---|
| 75 | /// entropy polling. |
|---|
| 76 | /// </summary> |
|---|
| 77 | /// <returns></returns> |
|---|
| 78 | public abstract byte[] GetFastEntropy(); |
|---|
| 79 | |
|---|
| 80 | /// <summary> |
|---|
| 81 | /// Gets entropy from the entropy source. This will be called repetitively. |
|---|
| 82 | /// </summary> |
|---|
| 83 | /// <returns>A byte array containing the entropy, both slow rate and fast rate.</returns> |
|---|
| 84 | public abstract byte[] GetEntropy(); |
|---|
| 85 | |
|---|
| 86 | /// <summary> |
|---|
| 87 | /// Converts value types into a byte array. This is a helper function to allow |
|---|
| 88 | /// inherited classes to convert value types into byte arrays which can be |
|---|
| 89 | /// returned to the EntropyPoller class. |
|---|
| 90 | /// </summary> |
|---|
| 91 | /// <typeparam name="T">Any value type</typeparam> |
|---|
| 92 | /// <param name="entropy">A value which will be XORed with pool contents.</param> |
|---|
| 93 | protected static byte[] StructToBuffer<T>(T entropy) where T : struct |
|---|
| 94 | { |
|---|
| 95 | int sizeofObject = Marshal.SizeOf(entropy); |
|---|
| 96 | IntPtr memory = Marshal.AllocHGlobal(sizeofObject); |
|---|
| 97 | try |
|---|
| 98 | { |
|---|
| 99 | Marshal.StructureToPtr(entropy, memory, false); |
|---|
| 100 | byte[] dest = new byte[sizeofObject]; |
|---|
| 101 | |
|---|
| 102 | //Copy the memory |
|---|
| 103 | Marshal.Copy(memory, dest, 0, sizeofObject); |
|---|
| 104 | return dest; |
|---|
| 105 | } |
|---|
| 106 | finally |
|---|
| 107 | { |
|---|
| 108 | Marshal.FreeHGlobal(memory); |
|---|
| 109 | } |
|---|
| 110 | } |
|---|
| 111 | } |
|---|
| 112 | } |
|---|