October 2014
S M T W T F S
« Sep    
 1234
567891011
12131415161718
19202122232425
262728293031  

Recent Posts

DiceLockIVDigestedNoOMCheck, C++ program for Linux to verify DiceLock encryption cipher architecture with DiceLockIVDigested class and stream ciphers

DiceLockIVDigestedNoOMCheck is a C++ program for Linux to verify that DiceLockIVDigested class implementing one of DiceLock different architectures is working as expected.

DiceLockIVDigested class is the class that implements DiceLock architecture with the following characteristics:
– base encryption cipher/decipher is any symmetric cipher (stream cipher or block cipher operation mode) working with symmetric key, and initialization vector (IV) are needed,
– any hash digest algorithm is used to generate message hash digest from plaintext to be encrypted,
ciphertext plus ciphered hash digest of plaintext is checked for random number test properties,
– any function that is able to change original symmetric key with new values,
bit random number tests that can verify that encrypted ciphertext is at random.

Current algorithm classes that can be used with DiceLockIVDigested architecture are:

– as base encryption cipher/decipher:

  • stream ciphers with initialization vector as:
      HC 128,
      HC 256,
      Rabbit,
      Salsa 20/20 128,
      Salsa 20/20 256,
      Sosemanuk 128,
      Sosemanuk 256,
  • block cipher operatin modes as:
      CBC (cipher block chaining),
      CFB (cipher feedback),
      OFB (output feedback),
  • with any of following block ciphers:
      AES (Advanced Encryption Standard) with 128, 192 or 256 key bits,
      Camellia with 128, 192 or 256 key bits,
      Serpent with 128, 192 or 256 key bits,
      Twofish with 128, 192 or 256 key bits, or
      TDEA (Triple Data Encryption Algorithm) with 192 key bits,
  • – hash digest algorithms: Sha 1, Sha 224, Sha 256, Sha 384, Sha 512, Ripemd 128, Ripemd 160, Ripemd 256, Ripemd 320, Md 2, Md 4 and Md5,
    – symmetric key changers: IncreaseKeyModifier_6_0_0_1 and DecreaseKeyModifier_6_0_0_1,
    – random number tests: Frequency, Block Frequency, Cumulative Sum Forward, Cumulative Sum Reverse, Runs, Longest Run Of Ones, Rank, Universal, Approximate Entropy, Serial, Discrete Fourier Transform tests and random number test Suite with any or all previous enumerated random number tests.

    Because of so many alternative encryption/decryption engines can be used, we have split DiceLockIVDigested class tests in two main C++ program tests:
    DiceLockIVDigestedNoOMCheck C++ Source Code program making use of stream ciphers, and
    DiceLockIVDigestedOMCheck C++ Source Code program making use of block cipher operation modes with multiple block ciphers.

    In this test we verify DiceLockIVDigestedNoOMCheck C++ Source Code program making use of stream ciphers with the configuration shown below.

    In order to verify that DiceLockIVDigested class is performing the following program has been used. Execution call must be:
    DiceLockIVDigestedNoOMCheck [stream cipher][hash algorithm][key modifier][stream type] log_output_file, where the different values can be easily infered from source code.

    Test program in C++ language:

    // DiceLockIVDigestedNoOMCheck.cpp : Defines the entry point for the console application.
    //
    
    #include 
    #include 
    #include "DiceLockCipher.h"
    
    using namespace DiceLockSecurity::Cipher::SymmetricCipher::DiceLock;
    using namespace DiceLockSecurity::Cipher::SymmetricCipher::SymmetricStreamer::StreamCipher;
    
    #define MIN_LENGTH_TESTS	8
    #define MAX_LENGTH_TESTS	500000
    #define RANDOM_ALPHA		0.001
    #define FILE_NAME_LENGTH	300
    
    
    BaseSymmetricStreamer*	SetSymmetricStreamerCipherType(unsigned char cipher) {
    	BaseSymmetricStreamer* streamCipherObject;
    
    	streamCipherObject = NULL;
    	switch (cipher) {
    		case '0' :	streamCipherObject = new HC128();
    			break;
    		case '1' :	streamCipherObject = new HC256();
    			break;
    		case '2' :	streamCipherObject = new Rabbit();
    			break;
    		case '3' :	streamCipherObject = new Salsa20_20_128();
    			break;
    		case '4' :	streamCipherObject = new Salsa20_20_256();
    			break;
    		case '5' :	streamCipherObject = new Sosemanuk128();
    			break;
    		case '6' :	streamCipherObject = new Sosemanuk256();
    			break;
    	}
    	return streamCipherObject;
    }
    
    void RemoveSymmetricStreamerCipherType(BaseSymmetricStreamer* streamer, unsigned char cipher) {
    
    	switch (cipher) {
    		case '0' :	delete (HC128*)streamer;
    			break;
    		case '1' :	delete (HC256*)streamer;
    			break;
    		case '2' :	delete (Rabbit*)streamer;
    			break;
    		case '3' :	delete (Salsa20_20_128*)streamer;
    			break;
    		case '4' :	delete (Salsa20_20_256*)streamer;
    			break;
    		case '5' :	delete (Sosemanuk128*)streamer;
    			break;
    		case '6' :	delete (Sosemanuk256*)streamer;
    			break;
    	}
    }
    
    BaseHash* SetHash(unsigned char hash) {
    	BaseHash* hashObject;
    
    	hashObject = NULL;
    	switch (hash) {
    		case '0' :	hashObject = new Sha1();
    			break;
    		case '1' :	hashObject = new Sha224();
    			break;
    		case '2' :	hashObject = new Sha256();
    			break;
    		case '3' :	hashObject = new Sha384();
    			break;
    		case '4' :	hashObject = new Sha512();
    			break;
    		case '5' :	hashObject = new Ripemd128();
    			break;
    		case '6' :	hashObject = new Ripemd160();
    			break;
    		case '7' :	hashObject = new Ripemd256();
    			break;
    		case '8' :	hashObject = new Ripemd320();
    			break;
    		case '9' :	hashObject = new Md2();
    			break;
    		case 'A' :	hashObject = new Md4();
    			break;
    		case 'B' :	hashObject = new Md5();
    			break;
    	}
    	return hashObject;
    }
    
    BaseKeyModifier* SetKeyModifier(unsigned long int indexer) {
    	BaseKeyModifier* modifier;
    
    	modifier = NULL;
    	if ( indexer == '0' ) {
    		modifier = new IncreaseKeyModifier_6_0_0_1();
    	}
    	if ( indexer == '1' ) {
    		modifier = new DecreaseKeyModifier_6_0_0_1();
    	}
    	return modifier;
    }
    
    void RemoveKeyModifier(BaseKeyModifier* modifier, unsigned long int modify) {
    
    	if ( modify == '0' ) {
    		delete (IncreaseKeyModifier_6_0_0_1*)modifier;
    	}
    	if ( modify == '1' ) {
    		delete (DecreaseKeyModifier_6_0_0_1*)modifier;
    	}
    }
    
    BaseCryptoRandomStream*	SetStreamType(unsigned char stream, unsigned long int bitLength) {
    	BaseCryptoRandomStream* streamObject;
    
    	streamObject = NULL;
    	switch (stream) {
    		case '0' :	streamObject = new DefaultCryptoRandomStream(bitLength);
    			break;
    		case '1' :	streamObject = new PhysicalCryptoRandomStream(bitLength);
    			break;
    	}
    	return streamObject;
    }
    
    void RemoveStreamType(BaseCryptoRandomStream* streamObject, unsigned char stream) {
    
    	switch (stream) {
    		case '0' :	delete (DefaultCryptoRandomStream*)streamObject;
    			break;
    		case '1' :	delete (PhysicalCryptoRandomStream*)streamObject;
    			break;
    	}
    }
    
    void SetRandomTestsAndAlpha(RandomTestSuite* suite) {
    
    	suite->AddFrequencyTest();
    	suite->AddBlockFrequencyTest();
    	suite->AddCumulativeSumForwardTest();
    	suite->AddLongestRunOfOnesTest();
    	suite->AddRunsTest();
    	suite->SetAlpha(RANDOM_ALPHA);
    }
    
    void VerifyDecryption(BaseCryptoRandomStream* plaintext, BaseCryptoRandomStream* decipheredtext, unsigned long int* correctDecipher, unsigned long int* incorrectDecipher) {
    
    	if ( plaintext->Equals(decipheredtext) ) {
    		(*correctDecipher)++;
    	}
    	else {
    		(*incorrectDecipher)++;
    	}
    }
    
    int main(int argc, char* argv[])
    {
    
    	unsigned long int performedTests;
    	unsigned long int length;
    	unsigned long int reciphers;
    	unsigned long int maxReciphers;
    	unsigned long int correctDecipher;
    	unsigned long int incorrectDecipher;
    	unsigned char cipher;
    	unsigned char hash;
    	unsigned char keyModifier;
    	unsigned char dataStream;
    	char reciphersFile[FILE_NAME_LENGTH];
    	FILE* fp;
    	FILE* fr;
    
    	DiceLockIVDigested* diceLock;
    	BaseSymmetricStreamer* streamerCipher;
    	RandomTestSuite* randomSuite;
    	BaseHash*		 hashBase;
    	BaseKeyModifier* modifier;
    	BaseCryptoRandomStream* hashDigest;
    	BaseCryptoRandomStream* key;
    	BaseCryptoRandomStream* iv;
    	BaseCryptoRandomStream* plaintext;
    	BaseCryptoRandomStream* ciphertext;
    	BaseCryptoRandomStream* decipheredtext;
    
    	// Gets execution parameters from command line
    	cipher = argv[1][0];
    	hash = argv[1][1];
    	keyModifier = argv[1][2];
    	dataStream = argv[1][3];
    
    	// Pseudo-random generation, fixed intitialitation seed allows 
    	// re-run same test with identical data
    	srand(1);
    
    	performedTests = 0;
    	reciphers = 0;
    	maxReciphers = 0;
    	memset(reciphersFile, 0, FILE_NAME_LENGTH);
    	strcat(reciphersFile, argv[2]);
    	strcat(reciphersFile, ".reciphered");
    	if ( (fr = fopen(reciphersFile, "w")) == NULL ) {
    		printf(" ERROR OPENING FILE\n");
    		return 1;
    	}
    	fprintf(fr, "keyUC, ivUC, plainUC, length, reciphers\n");
    	correctDecipher = 0;
    	incorrectDecipher = 0;
    
    	for ( length = MIN_LENGTH_TESTS; length <= MAX_LENGTH_TESTS; length = length + 8 ) {
    
    		printf("length: %lu\n", length);
    		performedTests++;
    
    		// Encryption
    		diceLock =  new DiceLockIVDigested();
    		streamerCipher = SetSymmetricStreamerCipherType(cipher);
    		diceLock->SetSymmetricStreamer(streamerCipher);
    		randomSuite = new RandomTestSuite();
    		SetRandomTestsAndAlpha(randomSuite);
    		diceLock->SetRandomTestSuite(randomSuite);
    		hashBase = SetHash(hash);
    		hashDigest = SetStreamType(dataStream, hashBase->GetBitHashLength());
    		hashBase->SetMessageDigest(hashDigest);
    		diceLock->SetHashDigester(hashBase);
    		modifier = SetKeyModifier(keyModifier);
    		diceLock->SetKeyModifier(modifier);
    
    		key = SetStreamType(dataStream, diceLock->GetBitKeyLength());
    		iv = SetStreamType(dataStream, diceLock->GetBitIVLength());
    		plaintext = SetStreamType(dataStream, length);
    		ciphertext = SetStreamType(dataStream, diceLock->GetBitCiphertextLength(plaintext->GetBitLength()));
    		
    		key->FillUC((unsigned char)(rand() & 0x000000FF));
    		iv->FillUC((unsigned char)(rand() & 0x000000FF));
    		plaintext->FillUC((unsigned char)(rand() & 0x000000FF));
    
    		diceLock->Initialize();
    		diceLock->SetSymmetricKey(key);
    		diceLock->SetIV(iv);
    		diceLock->Cipher(plaintext, ciphertext);
    
    		if ( diceLock->GetReciphers() > 0 ) {
    			reciphers++;
    			if ( maxReciphers < diceLock->GetReciphers() ) {
    				maxReciphers = diceLock->GetReciphers();
    			}
    			fprintf(fr, "%02x,%02x,%02x,%lu,%lu\n", key->GetUCPosition(0), iv->GetUCPosition(0), plaintext->GetUCPosition(0), length, diceLock->GetReciphers());
    		}
    
    		RemoveKeyModifier(modifier, keyModifier);
    		RemoveStreamType(hashDigest, dataStream);
    		delete hashBase;
    		delete randomSuite;
    		RemoveSymmetricStreamerCipherType(streamerCipher, cipher);
    		delete diceLock;
    
    		// Decryption
    		diceLock =  new DiceLockIVDigested();
    		streamerCipher = SetSymmetricStreamerCipherType(cipher);
    		diceLock->SetSymmetricStreamer(streamerCipher);
    		hashBase = SetHash(hash);
    		hashDigest = SetStreamType(dataStream, hashBase->GetBitHashLength());
    		hashBase->SetMessageDigest(hashDigest);
    		diceLock->SetHashDigester(hashBase);
    		modifier = SetKeyModifier(keyModifier);
    		diceLock->SetKeyModifier(modifier);
    
    		decipheredtext = SetStreamType(dataStream, diceLock->GetBitDecipheredtextLength(ciphertext->GetBitLength()));
    
    		diceLock->Initialize();
    		diceLock->SetSymmetricKey(key);
    		diceLock->SetIV(iv);
    		diceLock->Decipher(ciphertext, decipheredtext);
    
    		RemoveKeyModifier(modifier, keyModifier);
    		RemoveStreamType(hashDigest, dataStream);
    		delete hashBase;
    		RemoveSymmetricStreamerCipherType(streamerCipher, cipher);
    		delete diceLock;
    
    		// Verifying plaintext and decipheredtext
    		VerifyDecryption(plaintext, decipheredtext, &correctDecipher, &incorrectDecipher);
    
    		RemoveStreamType(key, dataStream);
    		RemoveStreamType(iv, dataStream);
    		RemoveStreamType(plaintext, dataStream);
    		RemoveStreamType(ciphertext, dataStream);
    		RemoveStreamType(decipheredtext, dataStream);
    	}
    	fclose(fr);
    
    	// Total Output 
    	if ( (fp = fopen(argv[2], "w")) == NULL ) {
    		printf(" ERROR OPENING FILE\n");
    		return 1;
    	}
    	fprintf(fp, "Verified:\n");
    	fprintf(fp, "---------\n");
    	fprintf(fp, "%s\n", argv[2]);
    	fprintf(fp, "\n");
    	fprintf(fp, "Number of streams tested: %lu\n", performedTests);
    	fprintf(fp, "\n");
    	fprintf(fp, "From length of shorter stream tested in bits: %d\n", MIN_LENGTH_TESTS);
    	fprintf(fp, "Up to length of larger stream tested in bits: %d\n", MAX_LENGTH_TESTS);
    	fprintf(fp, "\n");
    	fprintf(fp, "\n");
    	fprintf(fp, "Number of correct streams deciphered: %lu\n", correctDecipher);
    	fprintf(fp, "Number of incorrect streams deciphered: %lu\n", incorrectDecipher);
    	if ( incorrectDecipher == 0 ) {
    		fprintf(fp, "Incorrect deciphered streams = %lu ==> ---OK---\n", incorrectDecipher);
    	}
    	else {
    		fprintf(fp, "Incorrect deciphered streams = %lu ==> ---ERROR---\n", incorrectDecipher);
    	}
    	fprintf(fp, "\n");
    	fprintf(fp, "Total encrypted streams reciphered at least once: %lu\n", reciphers);
    	fprintf(fp, "Maximum number of reciphers performed over one stream being reciphered: %lu\n", maxReciphers);
    	fprintf(fp, "\n");
    	fprintf(fp, "File with reciphered stream data:\n");
    	fprintf(fp, "%s\n", reciphersFile);
    	fprintf(fp, "\n");
    	fclose(fp);
    
    	return 0;
    }
    
    

    All different tests are performed over 62.500 streams and we will post each one result promptly.

    321 comments to DiceLockIVDigestedNoOMCheck, C++ program for Linux to verify DiceLock encryption cipher architecture with DiceLockIVDigested class and stream ciphers

    Leave a Reply