using tfng instead of urandom

This commit is contained in:
Hendrik Schutter 2022-08-22 15:27:29 +02:00
parent 4cf1efea7a
commit 09446b52ca
3 changed files with 37 additions and 54 deletions

View File

@ -18,7 +18,8 @@
#include <string.h> #include <string.h>
#define CHUNK_SIZE 1024*1024*2 //amount of bytes that are overwritten at once --> 2MB #define CHUNK_SIZE 1024*1024*2 //amount of bytes that are overwritten at once --> 2MB
#define CHUNK_DIMENSION 100U //amount of chunks are read at once from random source //#define CHUNK_SIZE 1024U*4U //amount of bytes that are overwritten at once
#define TFNG_DATA_SIZE 65536U //amount of bytes used by tfng
//#define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB //#define DEMO_DRIVE_SIZE 1024*1024*256L // 256MB
//#define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB //#define DEMO_DRIVE_SIZE 1024*1024*1024L // 1GB
@ -39,7 +40,8 @@ public:
private: private:
fileDescriptor randomSrcFileDiscr; fileDescriptor randomSrcFileDiscr;
fileDescriptor driveFileDiscr; fileDescriptor driveFileDiscr;
unsigned char caChunk[CHUNK_DIMENSION][CHUNK_SIZE]; unsigned char caTfngData[TFNG_DATA_SIZE];
unsigned char caReadBuffer[CHUNK_SIZE];
unsigned long ulDriveByteSize; unsigned long ulDriveByteSize;
unsigned long ulDriveByteOverallCount = 0; //all bytes shredded in all iterations + checking -> used for progress calculation unsigned long ulDriveByteOverallCount = 0; //all bytes shredded in all iterations + checking -> used for progress calculation
double d32Percent = 0.0; double d32Percent = 0.0;

View File

@ -2,7 +2,7 @@
* @file shred.cpp * @file shred.cpp
* @brief shred drive * @brief shred drive
* @author hendrik schutter * @author hendrik schutter
* @date 03.05.2020 * @date 22.08.2022
*/ */
#include "../include/reHDD.h" #include "../include/reHDD.h"
@ -17,22 +17,8 @@ extern "C" {
const static char *randomsrc = (char*) "/dev/urandom"; const static char *randomsrc = (char*) "/dev/urandom";
#define DATASIZE 65536
Shred::Shred() Shred::Shred()
{ {
static char data[DATASIZE];
static char key[TFNG_KEY_SIZE];
tfng_prng_seedkey(key);
tfng_prng_genrandom(data, DATASIZE);
Logger::logThis()->info("RandomData: " + to_string(data[0]));
} }
Shred::~Shred() Shred::~Shred()
@ -65,6 +51,7 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
#ifndef DRYRUN #ifndef DRYRUN
const char *cpDrivePath = drive->getPath().c_str(); const char *cpDrivePath = drive->getPath().c_str();
unsigned char ucKey[TFNG_KEY_SIZE];
//open random source //open random source
randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE); randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE);
@ -88,6 +75,19 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
return -1; return -1;
} }
//read key for random generator
ssize_t readRet = read(randomSrcFileDiscr, ucKey, sizeof(ucKey)) ;
if (readRet <= 0)
{
std::string errorMsg(strerror(readRet));
Logger::logThis()->error("Shred-Task: Read random key failed! " + errorMsg + " - Drive: " + drive->getSerial());
perror(randomsrc);
cleanup();
return -1;
}
tfng_prng_seedkey(ucKey);
this->ulDriveByteSize = getDriveSizeInBytes(driveFileDiscr); this->ulDriveByteSize = getDriveSizeInBytes(driveFileDiscr);
drive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();; //set inital timestamp for speed metric drive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();; //set inital timestamp for speed metric
unsigned long ulSpeedMetricBytesWritten = 0U; //uses to calculate speed metric unsigned long ulSpeedMetricBytesWritten = 0U; //uses to calculate speed metric
@ -99,45 +99,21 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
for (unsigned int uiShredIterationCounter = 0U; uiShredIterationCounter < SHRED_ITERATIONS; uiShredIterationCounter++) for (unsigned int uiShredIterationCounter = 0U; uiShredIterationCounter < SHRED_ITERATIONS; uiShredIterationCounter++)
{ {
unsigned long ulDriveByteCounter = 0U; //used for one shred-iteration to keep track of the current drive position unsigned long ulDriveByteCounter = 0U; //used for one shred-iteration to keep track of the current drive position
uint32_t u32ChunkDimensionIndex = 0U;
if(uiShredIterationCounter == (SHRED_ITERATIONS-1)) if(uiShredIterationCounter == (SHRED_ITERATIONS-1))
{ {
//last shred iteration --> overwrite with zeros instead with random data //last shred iteration --> overwrite (just the write chunk) bytes with zeros instead with random data
memset(caChunk, 0U, CHUNK_DIMENSION*CHUNK_SIZE); memset(caTfngData, 0U, CHUNK_SIZE);
} }
while (ulDriveByteCounter < ulDriveByteSize) while (ulDriveByteCounter < ulDriveByteSize)
{ {
int iBytesToShred = 0; //Bytes that will be overwritten in this chunk-iteration int iBytesToShred = 0; //Bytes that will be overwritten in this chunk-iteration
if((u32ChunkDimensionIndex == 0U) && (uiShredIterationCounter != (SHRED_ITERATIONS-1))) if(uiShredIterationCounter != (SHRED_ITERATIONS-1))
{ {
//read new chunks from random source if needed and this is NOT the last shred iteration //NOT last shred iteration --> generate new random data
unsigned long ulBytesInChunkBuffer = 0U; tfng_prng_genrandom(caTfngData, TFNG_DATA_SIZE);
while (ulBytesInChunkBuffer < CHUNK_DIMENSION*CHUNK_SIZE)
{
//read new random bytes
int iReadBytes = read(randomSrcFileDiscr, caChunk, ((CHUNK_DIMENSION*CHUNK_SIZE)-ulBytesInChunkBuffer));
if (iReadBytes > 0)
{
ulBytesInChunkBuffer += iReadBytes;
}
else
{
std::string errorMsg(strerror(iReadBytes));
Logger::logThis()->error("Shred-Task: Read from random source failed! " + errorMsg + " - Drive: " + drive->getSerial());
perror("unable to read random data");
cleanup();
return -1;;
}
} //end chunk read
#ifdef LOG_LEVEL_HIGH
Logger::logThis()->info("Shred-Task: Read new random data - Drive: " + drive->getSerial());
#endif
} }
if((ulDriveByteSize-ulDriveByteCounter) < CHUNK_SIZE) if((ulDriveByteSize-ulDriveByteCounter) < CHUNK_SIZE)
@ -149,7 +125,7 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
iBytesToShred = CHUNK_SIZE; iBytesToShred = CHUNK_SIZE;
} }
int iByteShredded = write(driveFileDiscr, caChunk[u32ChunkDimensionIndex], iBytesToShred); int iByteShredded = write(driveFileDiscr, caTfngData, iBytesToShred);
if(iByteShredded <= 0) if(iByteShredded <= 0)
{ {
@ -160,7 +136,6 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
return -1; return -1;
} }
u32ChunkDimensionIndex = (u32ChunkDimensionIndex+1)%CHUNK_DIMENSION;
ulDriveByteCounter += iByteShredded; ulDriveByteCounter += iByteShredded;
ulDriveByteOverallCount += iByteShredded; ulDriveByteOverallCount += iByteShredded;
d32Percent = this->calcProgress(); d32Percent = this->calcProgress();
@ -196,13 +171,19 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
cleanup(); cleanup();
return -1; return -1;
} }
}//end one chunk write //end one chunk write
}
if(0 != iRewindDrive(driveFileDiscr)) if(0 != iRewindDrive(driveFileDiscr))
{ {
Logger::logThis()->error("Shred-Task: Unable to rewind drive! - Drive: " + drive->getSerial());
cleanup(); cleanup();
return -1; return -1;
} }
} //end one shred iteration //end one shred iteration
}
//end of all shred iteratio
tfng_prng_seedkey(NULL); //reset random generator
#ifdef ZERO_CHECK_ALERT #ifdef ZERO_CHECK_ALERT
drive->u32DriveChecksumAferShredding = uiCalcChecksum(driveFileDiscr, drive, ipSignalFd); drive->u32DriveChecksumAferShredding = uiCalcChecksum(driveFileDiscr, drive, ipSignalFd);
@ -289,10 +270,10 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file,Drive* drive, int* ipSign
{ {
iBytesToCheck = CHUNK_SIZE; iBytesToCheck = CHUNK_SIZE;
} }
int iReadBytes = read(file, caChunk, iBytesToCheck); int iReadBytes = read(file, caReadBuffer, iBytesToCheck);
for (int iReadBytesCounter = 0U; iReadBytesCounter < iReadBytes; iReadBytesCounter++) for (int iReadBytesCounter = 0U; iReadBytesCounter < iReadBytes; iReadBytesCounter++)
{ {
uiChecksum += caChunk[0][iReadBytesCounter]; uiChecksum += caReadBuffer[iReadBytesCounter];
} }
ulDriveByteCounter += iReadBytes; ulDriveByteCounter += iReadBytes;
ulDriveByteOverallCount += iReadBytes; ulDriveByteOverallCount += iReadBytes;