Browse Source

cleaned up namespace in onetimepad

master
Pete McNeil 4 years ago
parent
commit
b38c32f496
2 changed files with 19 additions and 9 deletions
  1. 15
    4
      onetimepad.cpp
  2. 4
    5
      onetimepad.hpp

+ 15
- 4
onetimepad.cpp View File

#include <windows.h> #include <windows.h>
#include <wincrypt.h> #include <wincrypt.h>


namespace codedweller {

PadBuffer OneTimePad::Entropy(int Length) { // Get a PadBuffer full of randomness. PadBuffer OneTimePad::Entropy(int Length) { // Get a PadBuffer full of randomness.
PadBuffer Buffer(Length, 0); // Start by initializing the buffer. PadBuffer Buffer(Length, 0); // Start by initializing the buffer.
HCRYPTPROV provider = 0; // We will need a handle for the source. HCRYPTPROV provider = 0; // We will need a handle for the source.
return Buffer; // Return the data we got. return Buffer; // Return the data we got.
} }


} // End namespace codddweller

#else #else


//// *NIX Strong Entropy Source == /dev/urandom //////////////////////////////// //// *NIX Strong Entropy Source == /dev/urandom ////////////////////////////////


#include <fstream> #include <fstream>


namespace codedweller {

PadBuffer OneTimePad::Entropy(int Length) { // Get Length bytes of strong entropy. PadBuffer OneTimePad::Entropy(int Length) { // Get Length bytes of strong entropy.
PadBuffer Buffer(Length, 0); // Initialize a buffer to hold them. PadBuffer Buffer(Length, 0); // Initialize a buffer to hold them.
try { // Handle this in a try block. try { // Handle this in a try block.
ifstream Source("/dev/urandom", ios::binary); // Open /dev/urandom if possible.
std::ifstream Source("/dev/urandom", std::ios::binary); // Open /dev/urandom if possible.
Source.read(reinterpret_cast<char*>(&Buffer[0]), Length); // Read data into the buffer. Source.read(reinterpret_cast<char*>(&Buffer[0]), Length); // Read data into the buffer.
if(!Source.bad() && Source.gcount() == Length) { // If we got what we came for then if(!Source.bad() && Source.gcount() == Length) { // If we got what we came for then
StrongEntropyFlag = true; // we have strong cryptography. StrongEntropyFlag = true; // we have strong cryptography.
return Buffer; // Return the buffer. return Buffer; // Return the buffer.
} }


} // End namespace codedweller

#endif #endif


// End Platform Specific Bits // End Platform Specific Bits
//////////////////////////////////////////////////////////////////////////////// ////////////////////////////////////////////////////////////////////////////////


namespace codedweller {

// Lightweight entropy is built from a combination of the time in ms UTC that // Lightweight entropy is built from a combination of the time in ms UTC that
// the application was started, the number of milliseconds since that time in // the application was started, the number of milliseconds since that time in
// milliseconds, the number and times of calls to addLightweightEntropy(), and // milliseconds, the number and times of calls to addLightweightEntropy(), and
CombinedFill = CombinedFill ^ LightweightEntropyBuffer; // Pick up some previous state entropy. CombinedFill = CombinedFill ^ LightweightEntropyBuffer; // Pick up some previous state entropy.
unsigned char* PrimerBuffer = (unsigned char*) &CombinedFill; // Treat the value as a bunch of bytes. unsigned char* PrimerBuffer = (unsigned char*) &CombinedFill; // Treat the value as a bunch of bytes.
unsigned char* EntropyBuffer = (unsigned char*) &LightweightEntropyBuffer; // Likewise with the entropy buffer. unsigned char* EntropyBuffer = (unsigned char*) &LightweightEntropyBuffer; // Likewise with the entropy buffer.
for(int i = 0; i < sizeof(msclock); i++) { // Fold bytes into the mangler one
for(size_t i = 0; i < sizeof(msclock); i++) { // Fold bytes into the mangler one
EntropyBuffer[i] += // byte at a time, capturing the EntropyBuffer[i] += // byte at a time, capturing the
PadGenerator.Encrypt( // the results and using one extra PadGenerator.Encrypt( // the results and using one extra
PadGenerator.Encrypt(PrimerBuffer[i])); // round per byte to increase the PadGenerator.Encrypt(PrimerBuffer[i])); // round per byte to increase the


void OneTimePad::addEntropy() { // Add strong entropy if available. void OneTimePad::addEntropy() { // Add strong entropy if available.
PadBuffer Fill = Entropy(); // Grab the entropy bits to add. PadBuffer Fill = Entropy(); // Grab the entropy bits to add.
for(int i = 0; i < Fill.size(); i++) { // Pump them in one byte at a
for(size_t i = 0; i < Fill.size(); i++) { // Pump them in one byte at a
PadGenerator.Encrypt( // time and then run an extra PadGenerator.Encrypt( // time and then run an extra
PadGenerator.Encrypt(Fill.at(i))); // round per byte to increase the PadGenerator.Encrypt(Fill.at(i))); // round per byte to increase the
} // amount of guessing an attacker } // amount of guessing an attacker


void OneTimePad::addEntropy(PadBuffer Entropy) { // Add entropy from a given source. void OneTimePad::addEntropy(PadBuffer Entropy) { // Add entropy from a given source.
addLightweightEntropy(); // Start with some lightweight entropy. addLightweightEntropy(); // Start with some lightweight entropy.
for(int i = 0; i < Entropy.size(); i++) { // Then loop through the provided
for(size_t i = 0; i < Entropy.size(); i++) { // Then loop through the provided
PadGenerator.Encrypt( // entropy and mix it in with one PadGenerator.Encrypt( // entropy and mix it in with one
PadGenerator.Encrypt(Entropy.at(i))); // extra round per byte to increase PadGenerator.Encrypt(Entropy.at(i))); // extra round per byte to increase
} // the amount of guessing an attacker } // the amount of guessing an attacker
} // initial Mangler state. } // initial Mangler state.
} // The OneTimePad object is ready. } // The OneTimePad object is ready.


} // End namespace codedweller

+ 4
- 5
onetimepad.hpp View File

// started. Additional entropy can be provided by the application or again from // started. Additional entropy can be provided by the application or again from
// one of the core entropy generators (/dev/urandom or CryptGenRandom). // one of the core entropy generators (/dev/urandom or CryptGenRandom).
#ifndef onetimepad_included
#define onetimepad_included
#pragma once
#include <vector> #include <vector>
#include "mangler.hpp" #include "mangler.hpp"
using namespace std;
namespace codedweller {
typedef vector<unsigned char> PadBuffer;
typedef std::vector<unsigned char> PadBuffer;
class OneTimePad { // One Time Pad generator. class OneTimePad { // One Time Pad generator.
private: private:
}; };
#endif
} // End namespace codedweller

Loading…
Cancel
Save