You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. // onetimepad.cpp
  2. // Copyright (C) 2006-2007 MicroNeil Research Corporation
  3. #include "onetimepad.hpp"
  4. #include "timing.hpp"
  5. using namespace std;
  6. /*
  7. class OneTimePad { // One Time Pad generator.
  8. private:
  9. MANGLER PagGenerator; // MANGLER as a PRNG.
  10. PadBuffer Entropy(int Length = 1024); // System entropy source.
  11. public:
  12. ** OneTimePad(); // Constructor initializes w/ Entropy.
  13. ** PadBuffer Pad(int Length); // Get a pad of Length.
  14. ** void addEntropy(); // Add entropy from the system source.
  15. ** void addEntropy(PadBuffer Entropy); // Add entropy from this source.
  16. };
  17. */
  18. ////////////////////////////////////////////////////////////////////////////////
  19. // Platform specific strong entropy sourcing.
  20. #ifdef WIN32
  21. //// WIN32 Strong Entropy Source == CryptGenRandom() ///////////////////////////
  22. #include <windows.h>
  23. #include <wincrypt.h>
  24. namespace CodeDweller {
  25. PadBuffer OneTimePad::Entropy(int Length) { // Get a PadBuffer full of randomness.
  26. PadBuffer Buffer(Length, 0); // Start by initializing the buffer.
  27. HCRYPTPROV provider = 0; // We will need a handle for the source.
  28. if( // Try a two different sources.
  29. !CryptAcquireContext( // If we can get a hardware source
  30. &provider, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT)) { // from an intel CPU then use that first.
  31. if( // If we can't use get that
  32. !CryptAcquireContext( // entropy source for some reason then
  33. &provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) { // try to use the RSA source. That should
  34. // always work, but if it doesn't we need
  35. provider = 0; // to know about it so we'll have a zero
  36. } // handle.
  37. }
  38. if(0 != provider) { // If we did get a good source then
  39. CryptGenRandom (provider, Length, (BYTE*)&Buffer[0]); // grab the random bit required and
  40. CryptReleaseContext(provider,0); // then let the provider go.
  41. StrongEntropyFlag = true; // We DID get strong entropy.
  42. }
  43. else { // If we did not get a good source
  44. StrongEntropyFlag = false; // then we are not strong.
  45. }
  46. return Buffer; // Return the data we got.
  47. }
  48. #else
  49. //// *NIX Strong Entropy Source == /dev/urandom ////////////////////////////////
  50. #include <fstream>
  51. namespace CodeDweller {
  52. PadBuffer OneTimePad::Entropy(int Length) { // Get Length bytes of strong entropy.
  53. PadBuffer Buffer(Length, 0); // Initialize a buffer to hold them.
  54. try { // Handle this in a try block.
  55. ifstream Source("/dev/urandom", ios::binary); // Open /dev/urandom if possible.
  56. Source.read(reinterpret_cast<char*>(&Buffer[0]), Length); // Read data into the buffer.
  57. if(!Source.bad() && Source.gcount() == Length) { // If we got what we came for then
  58. StrongEntropyFlag = true; // we have strong cryptography.
  59. } else { // If we didn't then we are not
  60. StrongEntropyFlag = false; // strong, and don't have things
  61. } // to make us go.
  62. Source.close(); // We're done, so close the stream.
  63. }
  64. catch(...) { // If we had an exception then we
  65. StrongEntropyFlag = false; // did not get strong entropy.
  66. }
  67. return Buffer; // Return the buffer.
  68. }
  69. #endif
  70. // End Platform Specific Bits
  71. ////////////////////////////////////////////////////////////////////////////////
  72. // Lightweight entropy is built from a combination of the time in ms UTC that
  73. // the application was started, the number of milliseconds since that time in
  74. // milliseconds, the number and times of calls to addLightweightEntropy(), and
  75. // the state of the MANGLER engine at each call -- that state is effected by
  76. // the combined previous use of the MANGLER and any other entropy that was
  77. // added including the timing of those events (since they all trigger the
  78. // addLightweightEntropy() function.
  79. Timer OneTimePadRunTimer; // Millisecond entropy source.
  80. msclock LightweightEntropyBuffer; // Lightweight entropy bucket.
  81. void OneTimePad::addLightweightEntropy() { // Add entropy based on the
  82. msclock StartFill = OneTimePadRunTimer.getStartClock(); // initial start time of the app
  83. msclock ElapsedFill = OneTimePadRunTimer.getElapsedTime(); // and the number of millisecs since.
  84. msclock CombinedFill = StartFill ^ ElapsedFill; // XOR the two together to combine.
  85. CombinedFill = CombinedFill ^ LightweightEntropyBuffer; // Pick up some previous state entropy.
  86. unsigned char* PrimerBuffer = (unsigned char*) &CombinedFill; // Treat the value as a bunch of bytes.
  87. unsigned char* EntropyBuffer = (unsigned char*) &LightweightEntropyBuffer; // Likewise with the entropy buffer.
  88. for(int unsigned i = 0; i < sizeof(msclock); i++) { // Fold bytes into the mangler one
  89. EntropyBuffer[i] += // byte at a time, capturing the
  90. PadGenerator.Encrypt( // the results and using one extra
  91. PadGenerator.Encrypt(PrimerBuffer[i])); // round per byte to increase the
  92. } // amount of guessing an attacker
  93. } // needs to do.
  94. void OneTimePad::addEntropy() { // Add strong entropy if available.
  95. PadBuffer Fill = Entropy(); // Grab the entropy bits to add.
  96. for(int unsigned i = 0; i < Fill.size(); i++) { // Pump them in one byte at a
  97. PadGenerator.Encrypt( // time and then run an extra
  98. PadGenerator.Encrypt(Fill.at(i))); // round per byte to increase the
  99. } // amount of guessing an attacker
  100. } // needs to do.
  101. void OneTimePad::addEntropy(PadBuffer Entropy) { // Add entropy from a given source.
  102. addLightweightEntropy(); // Start with some lightweight entropy.
  103. for(int unsigned i = 0; i < Entropy.size(); i++) { // Then loop through the provided
  104. PadGenerator.Encrypt( // entropy and mix it in with one
  105. PadGenerator.Encrypt(Entropy.at(i))); // extra round per byte to increase
  106. } // the amount of guessing an attacker
  107. } // needs to do.
  108. PadBuffer OneTimePad::Pad(int Length) { // Grab a pad of a specific length.
  109. addLightweightEntropy(); // Add some lightweight entropy.
  110. PadBuffer Output; Output.reserve(Length); // Create a buffer the right size.
  111. unsigned char x; // Starting with an uninitialized
  112. for(int i = 0; i < Length; i++) // char, fill the buffer with
  113. Output.push_back(x = PadGenerator.Encrypt(x)); // random bytes from the mangler.
  114. return Output; // Return the new pad.
  115. }
  116. void* OneTimePad::fill(void* Object, int Size) { // Fill *Object with random bytes.
  117. PadBuffer FillData = Pad(Size); // Get a Pad of the correct size.
  118. unsigned char* Ptr = reinterpret_cast<unsigned char*>(Object); // Reinterpret the pointer type.
  119. for(int i = 0; i < Size; i++) Ptr[i] = FillData.at(i); // Fill the object with the Pad.
  120. return Object; // Return the object.
  121. }
  122. bool OneTimePad::isStrong() { return StrongEntropyFlag; } // Tell them if I'm strong!
  123. OneTimePad::OneTimePad() { // Initialize the one time pad.
  124. addLightweightEntropy(); // Add lightweight entropy.
  125. addEntropy(); // Add cryptographic entropy.
  126. unsigned char x; // Starting with an uninitialized
  127. for(int i = 0; i < 1024; i++) { // character, run 1024 rounds to
  128. x = PadGenerator.Encrypt(x); // reduce the predictability of the
  129. } // initial Mangler state.
  130. } // The OneTimePad object is ready.
  131. }