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.

onetimepad.hpp 2.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. // onetimepad.hpp
  2. // Copyright (C) 2006 - 2007 MicroNeil Research Corporation
  3. //
  4. // This module leverages the Mangler encryption engine to create
  5. // cryptographically strong one-time pads and random numbers upon request.
  6. // The engine is seeded by /dev/urandom on *nix machines and by CryptGenRandom
  7. // on win32 machines. Additionally, each call to get new data introduces a
  8. // small amount of entropy based on the jitter in timing between calls and the
  9. // amount of time the application has been running since the generator was
  10. // started. Additional entropy can be provided by the application or again from
  11. // one of the core entropy generators (/dev/urandom or CryptGenRandom).
  12. #ifndef onetimepad_included
  13. #define onetimepad_included
  14. #include <vector>
  15. #include "mangler.hpp"
  16. using namespace std;
  17. typedef vector<unsigned char> PadBuffer;
  18. class OneTimePad { // One Time Pad generator.
  19. private:
  20. MANGLER PadGenerator; // MANGLER as a PRNG.
  21. void addLightweightEntropy(); // Add light weight entropy bits.
  22. PadBuffer Entropy(int Length = 1024); // System entropy source.
  23. void* fill(void* Object, int Size); // Internal method to fill an object.
  24. bool StrongEntropyFlag; // True if strong entropy is used.
  25. public:
  26. OneTimePad(); // Constructor initializes w/ Entropy.
  27. bool isStrong(); // True if strong entropy is available.
  28. PadBuffer Pad(int Length); // Get a pad of Length.
  29. void addEntropy(); // Add entropy from the system source.
  30. void addEntropy(PadBuffer Entropy); // Add entropy from this source.
  31. template <typename T> // Fill any kind of object
  32. T& fill(T& Object) { // with random bytes.
  33. fill((void*) &Object, sizeof(Object)); // Get a void ptr to it, fill it,
  34. return Object; // and return it to the caller.
  35. }
  36. };
  37. #endif