123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 |
-
-
-
- #include "onetimepad.hpp"
- #include "timing.hpp"
-
-
-
-
-
-
- #ifdef WIN32
-
-
-
- #include <windows.h>
- #include <wincrypt.h>
-
- PadBuffer OneTimePad::Entropy(int Length) {
- PadBuffer Buffer(Length, 0);
- HCRYPTPROV provider = 0;
-
- if(
- !CryptAcquireContext(
- &provider, NULL, NULL, PROV_INTEL_SEC, CRYPT_VERIFYCONTEXT)) {
-
- if(
- !CryptAcquireContext(
- &provider, NULL, NULL, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT)) {
-
- provider = 0;
- }
- }
-
- if(0 != provider) {
- CryptGenRandom (provider, Length, (BYTE*)&Buffer[0]);
- CryptReleaseContext(provider,0);
- StrongEntropyFlag = true;
- }
- else {
- StrongEntropyFlag = false;
- }
-
- return Buffer;
- }
-
- #else
-
-
-
- #include <fstream>
-
- PadBuffer OneTimePad::Entropy(int Length) {
- PadBuffer Buffer(Length, 0);
- try {
- ifstream Source("/dev/urandom", ios::binary);
- Source.read(reinterpret_cast<char*>(&Buffer[0]), Length);
- if(!Source.bad() && Source.gcount() == Length) {
- StrongEntropyFlag = true;
- } else {
- StrongEntropyFlag = false;
- }
- Source.close();
- }
-
- catch(...) {
- StrongEntropyFlag = false;
- }
- return Buffer;
- }
-
- #endif
-
-
-
-
-
-
-
-
-
-
-
-
- Timer OneTimePadRunTimer;
- msclock LightweightEntropyBuffer;
-
- void OneTimePad::addLightweightEntropy() {
- msclock StartFill = OneTimePadRunTimer.getStartClock();
- msclock ElapsedFill = OneTimePadRunTimer.getElapsedTime();
- msclock CombinedFill = StartFill ^ ElapsedFill;
- CombinedFill = CombinedFill ^ LightweightEntropyBuffer;
- unsigned char* PrimerBuffer = (unsigned char*) &CombinedFill;
- unsigned char* EntropyBuffer = (unsigned char*) &LightweightEntropyBuffer;
- for(int i = 0; i < sizeof(msclock); i++) {
- EntropyBuffer[i] +=
- PadGenerator.Encrypt(
- PadGenerator.Encrypt(PrimerBuffer[i]));
- }
- }
-
- void OneTimePad::addEntropy() {
- PadBuffer Fill = Entropy();
- for(int i = 0; i < Fill.size(); i++) {
- PadGenerator.Encrypt(
- PadGenerator.Encrypt(Fill.at(i)));
- }
- }
-
- void OneTimePad::addEntropy(PadBuffer Entropy) {
- addLightweightEntropy();
- for(int i = 0; i < Entropy.size(); i++) {
- PadGenerator.Encrypt(
- PadGenerator.Encrypt(Entropy.at(i)));
- }
- }
-
- PadBuffer OneTimePad::Pad(int Length) {
- addLightweightEntropy();
- PadBuffer Output; Output.reserve(Length);
- unsigned char x;
- for(int i = 0; i < Length; i++)
- Output.push_back(x = PadGenerator.Encrypt(x));
- return Output;
- }
-
- void* OneTimePad::fill(void* Object, int Size) {
- PadBuffer FillData = Pad(Size);
- unsigned char* Ptr = reinterpret_cast<unsigned char*>(Object);
- for(int i = 0; i < Size; i++) Ptr[i] = FillData.at(i);
- return Object;
- }
-
- bool OneTimePad::isStrong() { return StrongEntropyFlag; }
-
- OneTimePad::OneTimePad() {
- addLightweightEntropy();
- addEntropy();
- unsigned char x;
- for(int i = 0; i < 1024; i++) {
- x = PadGenerator.Encrypt(x);
- }
- }
|