// onetimepad.hpp // // Copyright (C) 2006-2020 MicroNeil Research Corporation. // // This software is released under the MIT license. See LICENSE.TXT. // // This module leverages the Mangler encryption engine to create // cryptographically strong one-time pads and random numbers upon request. // The engine is seeded by /dev/urandom on *nix machines and by CryptGenRandom // on win32 machines. Additionally, each call to get new data introduces a // small amount of entropy based on the jitter in timing between calls and the // amount of time the application has been running since the generator was // started. Additional entropy can be provided by the application or again from // one of the core entropy generators (/dev/urandom or CryptGenRandom). #pragma once #include #include "mangler.hpp" namespace codedweller { typedef std::vector PadBuffer; class OneTimePad { // One Time Pad generator. private: MANGLER PadGenerator; // MANGLER as a PRNG. void addLightweightEntropy(); // Add light weight entropy bits. PadBuffer Entropy(int Length = 1024); // System entropy source. void* fill(void* Object, int Size); // Internal method to fill an object. bool StrongEntropyFlag; // True if strong entropy is used. public: OneTimePad(); // Constructor initializes w/ Entropy. bool isStrong(); // True if strong entropy is available. PadBuffer Pad(int Length); // Get a pad of Length. void addEntropy(); // Add entropy from the system source. void addEntropy(PadBuffer Entropy); // Add entropy from this source. template // Fill any kind of object T& fill(T& Object) { // with random bytes. fill((void*) &Object, sizeof(Object)); // Get a void ptr to it, fill it, return Object; // and return it to the caller. } }; } // End namespace codedweller