/** * Jason Rohrer * 7-5-99 * Key generation piece of HardEncrypt package * * mods: * 7-15-99: Jason Rohrer * added failed allocation detection * 8-22-99: Jason Rohrer * removed precompiler directives (#if, #endif) to * improve multiplatform compilability * 8-23-99: Jason Rohrer * added support for outputting multiple key files * in one session. (i.e., "Enter number of key files to output:") * 8-27-99: Jason Rohrer * Fixed portability problem with rand() usage. * 255*rand() causes overflow on platforms with a 32-bit RAND_MAX * 7-19-03: Jason Rohrer * Fixed an indexing bug. * */ #include #include #include // prototypes inline long XOR( long A, long B); int main() { //void main () { printf(".oO() HardenedCriminal Software (TM) ()Oo.\n\n"); printf("HardEncrypt Package (c)1999 Little (open source, end to tyranny)\n"); printf("GenKeyFile: hardcore seeded keyfile generator\n"); printf("Version 1.1\n"); srand((unsigned)time( NULL )); // seed the pseudo-random number generator int quit = 0; unsigned char *keyFileName = new unsigned char[99]; unsigned char *seedFileName = new unsigned char[99]; unsigned char *badInputBuffer = new unsigned char[99]; FILE *keyFile = NULL; FILE *seedFile = NULL; int numberOfKeys = 0; int sizeOfKey = 0; int numSuccessfulRead = 0; // number of integers read from user console input // loop until a proper number of key files are entered while( numSuccessfulRead == 0 ) { printf("\nEnter number of key files to make: "); numSuccessfulRead = scanf("%d", &numberOfKeys); if( numSuccessfulRead == 0) { scanf("%98s", &badInputBuffer); printf("\nERROR: must input a whole, positive number\n"); } else if(numberOfKeys <= 0) { printf("\nERROR: must input a whole, positive number\n"); numSuccessfulRead = 0; } } // loop until good key postfix is entered char keyFileOpened = false; while( !keyFileOpened && !quit ) { printf("\nEnter postfix name for key files (i.e. myKeyFile): "); scanf("%98s", keyFileName); if( (keyFileName[0] == 'q' || keyFileName[0] == 'Q') && keyFileName[1] == '\0' ) { quit = 1; break; } else if( keyFileName[0] != '\0') { // make sure user enters something keyFileOpened = true; } } // loop until a good seed file is opened while( seedFile == NULL && !quit ) { printf("\nEnter name of seed file: "); scanf("%98s", seedFileName); if( (seedFileName[0] == 'q' || seedFileName[0] == 'Q') && seedFileName[1] == '\0' ) { quit = 1; break; } seedFile = fopen((const char*)seedFileName, "rb"); if( seedFile == NULL ) { printf("\nERROR: can't open seed file %s\n", seedFileName); } } if( !quit ) { numSuccessfulRead = 0; // number of integers read from user console input // loop until a proper size of key file entered while( numSuccessfulRead == 0 ) { printf("\nEnter size of each key file (in bytes): "); numSuccessfulRead = scanf("%d", &sizeOfKey); if( numSuccessfulRead == 0) { scanf("%98s", &badInputBuffer); printf("\nERROR: must input a whole, positive number\n"); } else if(sizeOfKey <= 0) { printf("\nERROR: must input a whole, positive number\n"); numSuccessfulRead = 0; } } int sizeBuff = 5000; int halfSizeBuff = sizeBuff/2; int numKeyBytesWritten = 0; int numSeedBytesRead = sizeBuff; unsigned char *keyFileBuffer = new unsigned char[sizeBuff]; // store written key file bytes unsigned char *seedFileBuffer = new unsigned char[sizeBuff]; // store read seed file bytes char *seedByteUsedBuffer = new char[sizeBuff]; // store used seed byte flags // check for failed memory alocation if( keyFileBuffer == NULL || seedFileBuffer == NULL || seedByteUsedBuffer == NULL) { printf("\nERROR: not enough memory to allocate the file read-write buffers.\n"); printf("Free up memory or increase GenKeyFile's partition and try again.\n"); } else { // write out random key char seedFileGoodSoFar = true; // does seed file seem to contain random, well-distributed data? long totalBytesWritten = 0; long totalBytesRead = 0; int sizeLastRead = 0; unsigned long startTime = (unsigned)time( NULL ); unsigned long netTime; int currentSeedBuffIndex = 0; int numUnsuccessfulRandJumps = 0; int maxUnsuccessfulRandJumps = sizeBuff; // walk linearly after how many unsuccessful rand jumps? for(int n=0; n 0) { repeatedByte = seedFileBuffer[0]; } for(int j=0; j