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>
#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*1024L // 1GB
@ -39,7 +40,8 @@ public:
private:
fileDescriptor randomSrcFileDiscr;
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 ulDriveByteOverallCount = 0; //all bytes shredded in all iterations + checking -> used for progress calculation
double d32Percent = 0.0;

View File

@ -402,7 +402,7 @@ void reHDD::startShredAllDrives(list <Drive>* plistDrives)
ostringstream address;
address << (void const *)&(*pTmpDrive);
Logger::logThis()->info("Started shred (all) for: " + pTmpDrive->getModelName() + "-" + pTmpDrive->getSerial() + " @" + address.str());
#endif
#endif
pTmpDrive->state = Drive::TaskState::SHRED_ACTIVE;
thread(ThreadShred, pTmpDrive).detach();
}

View File

@ -2,7 +2,7 @@
* @file shred.cpp
* @brief shred drive
* @author hendrik schutter
* @date 03.05.2020
* @date 22.08.2022
*/
#include "../include/reHDD.h"
@ -17,22 +17,8 @@ extern "C" {
const static char *randomsrc = (char*) "/dev/urandom";
#define DATASIZE 65536
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()
@ -65,6 +51,7 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
#ifndef DRYRUN
const char *cpDrivePath = drive->getPath().c_str();
unsigned char ucKey[TFNG_KEY_SIZE];
//open random source
randomSrcFileDiscr = open(randomsrc, O_RDONLY | O_LARGEFILE);
@ -88,6 +75,19 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
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);
drive->sShredSpeed.chronoShredTimestamp = std::chrono::system_clock::now();; //set inital timestamp for 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++)
{
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))
{
//last shred iteration --> overwrite with zeros instead with random data
memset(caChunk, 0U, CHUNK_DIMENSION*CHUNK_SIZE);
//last shred iteration --> overwrite (just the write chunk) bytes with zeros instead with random data
memset(caTfngData, 0U, CHUNK_SIZE);
}
while (ulDriveByteCounter < ulDriveByteSize)
{
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
unsigned long ulBytesInChunkBuffer = 0U;
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
//NOT last shred iteration --> generate new random data
tfng_prng_genrandom(caTfngData, TFNG_DATA_SIZE);
}
if((ulDriveByteSize-ulDriveByteCounter) < CHUNK_SIZE)
@ -149,7 +125,7 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
iBytesToShred = CHUNK_SIZE;
}
int iByteShredded = write(driveFileDiscr, caChunk[u32ChunkDimensionIndex], iBytesToShred);
int iByteShredded = write(driveFileDiscr, caTfngData, iBytesToShred);
if(iByteShredded <= 0)
{
@ -160,7 +136,6 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
return -1;
}
u32ChunkDimensionIndex = (u32ChunkDimensionIndex+1)%CHUNK_DIMENSION;
ulDriveByteCounter += iByteShredded;
ulDriveByteOverallCount += iByteShredded;
d32Percent = this->calcProgress();
@ -196,13 +171,19 @@ int Shred::shredDrive(Drive* drive, int* ipSignalFd)
cleanup();
return -1;
}
}//end one chunk write
//end one chunk write
}
if(0 != iRewindDrive(driveFileDiscr))
{
Logger::logThis()->error("Shred-Task: Unable to rewind drive! - Drive: " + drive->getSerial());
cleanup();
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
drive->u32DriveChecksumAferShredding = uiCalcChecksum(driveFileDiscr, drive, ipSignalFd);
@ -289,10 +270,10 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file,Drive* drive, int* ipSign
{
iBytesToCheck = CHUNK_SIZE;
}
int iReadBytes = read(file, caChunk, iBytesToCheck);
int iReadBytes = read(file, caReadBuffer, iBytesToCheck);
for (int iReadBytesCounter = 0U; iReadBytesCounter < iReadBytes; iReadBytesCounter++)
{
uiChecksum += caChunk[0][iReadBytesCounter];
uiChecksum += caReadBuffer[iReadBytesCounter];
}
ulDriveByteCounter += iReadBytes;
ulDriveByteOverallCount += iReadBytes;
@ -313,5 +294,5 @@ unsigned int Shred::uiCalcChecksum(fileDescriptor file,Drive* drive, int* ipSign
void Shred::cleanup()
{
close(driveFileDiscr);
close( randomSrcFileDiscr);
close(randomSrcFileDiscr);
}