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

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