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

@@ -29,6 +29,8 @@ class OneTimePad {
#include <windows.h>
#include <wincrypt.h>

namespace codedweller {

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

} // End namespace codddweller

#else

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

#include <fstream>

namespace codedweller {

PadBuffer OneTimePad::Entropy(int Length) { // Get Length bytes of strong entropy.
PadBuffer Buffer(Length, 0); // Initialize a buffer to hold them.
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.
if(!Source.bad() && Source.gcount() == Length) { // If we got what we came for then
StrongEntropyFlag = true; // we have strong cryptography.
@@ -82,11 +88,15 @@ PadBuffer OneTimePad::Entropy(int Length) {
return Buffer; // Return the buffer.
}

} // End namespace codedweller

#endif

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

namespace codedweller {

// 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
// milliseconds, the number and times of calls to addLightweightEntropy(), and
@@ -105,7 +115,7 @@ void OneTimePad::addLightweightEntropy() {
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* 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
PadGenerator.Encrypt( // the results and using one extra
PadGenerator.Encrypt(PrimerBuffer[i])); // round per byte to increase the
@@ -114,7 +124,7 @@ void OneTimePad::addLightweightEntropy() {

void OneTimePad::addEntropy() { // Add strong entropy if available.
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(Fill.at(i))); // round per byte to increase the
} // amount of guessing an attacker
@@ -122,7 +132,7 @@ void OneTimePad::addEntropy() {

void OneTimePad::addEntropy(PadBuffer Entropy) { // Add entropy from a given source.
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.at(i))); // extra round per byte to increase
} // the amount of guessing an attacker
@@ -155,3 +165,4 @@ OneTimePad::OneTimePad() {
} // initial Mangler state.
} // The OneTimePad object is ready.

} // End namespace codedweller

+ 4
- 5
onetimepad.hpp View File

@@ -10,15 +10,14 @@
// started. Additional entropy can be provided by the application or again from
// one of the core entropy generators (/dev/urandom or CryptGenRandom).
#ifndef onetimepad_included
#define onetimepad_included
#pragma once
#include <vector>
#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.
private:
@@ -47,4 +46,4 @@ class OneTimePad {
};
#endif
} // End namespace codedweller

Loading…
Cancel
Save