////////////////////////////////////////////////////////////////////////// // File: DESmedium_SW2.c // Date: 14 December 2000 // Author: Antonio Esteves, GEC-DI-UM // // Software part of the hardware/software implementation of DES. // The hardware part is mapped into EDgAR-2 board. // The accesses to EDgAR-2 is done via WinDriver functions. // ////////////////////////////////////////////////////////////////////////// #include #include "DESmedium_SW.h" ////////////////////////////////////////////////////////////////////////// // Generate a pseudo-random 64-bits value based on a previus value. ////////////////////////////////////////////////////////////////////////// void pseudoRandom64 (DWORD dinH, DWORD dinL, DWORD *newDinH, DWORD *newDinL) { DWORD bit0, bit2, bit3, bit32, bit63; // newDin[63:0] = (din[63] xor din[3] xor din[2] xor din[0]) | din[63:1] bit0 = (dinL << 31 ); bit2 = (dinL & B2 ) << 29; bit3 = (dinL & B3 ) << 28; bit63 = (dinH & B31 ); bit63 = (bit63 ^ bit3) ^ (bit2 ^ bit0); bit32 = dinH & B0; if (bit32 == 0) *newDinL = (dinL >> 1); else *newDinL = (dinL >> 1) | B31; if (bit63 == 0) *newDinH = (dinH >> 1); else *newDinH = (dinH >> 1) | B31; } ////////////////////////////////////////////////////////////////////////// // Generate pseudo-random input data an input key to DES. // // numSamples -> number of 64-bit samples to be generated // dinH -> pointer to the array where most significant 32 bits of // data samples will be stored // dinL -> pointer to the array where least significant 32 bits of // data samples will be stored // keyInH -> pointer to the array where most significant 32 bits of // key samples will be stored // keyInL -> pointer to the array where least significant 32 bits of // key samples will be stored ////////////////////////////////////////////////////////////////////////// void generateRandData (int numSamples, DWORD *dinH, DWORD *dinL, DWORD *keyInH, DWORD *keyInL) { DWORD keyAuxH, keyAuxL; int i; // Initial data and key sample keyAuxH = 0x0b1a0d2a ; keyAuxL = 0x332b2b19 ; dinH[0] = 0xca2b5652 ; dinL[0] = 0x32ad54b7 ; // Reset the key's parity bits keyInH[0] = keyAuxH & PARITY; keyInL[0] = keyAuxL & PARITY; for (i=1;i pointer to the name of the file used to write data // encryptMode -> direction of encryption ('e'=encrypt, 'd'=decrypt) // numSamples -> number of 64-bit samples to store in file // dinH -> pointer to the array where most significant 32 bits of // input data samples are stored // dinL -> pointer to the array where least significant 32 bits of // input data samples are stored // doutH -> pointer to the array where most significant 32 bits of // output data samples are stored // doutL -> pointer to the array where least significant 32 bits of // output data samples are stored // keyInH -> pointer to the array where most significant 32 bits of // key samples are stored // keyInL -> pointer to the array where least significant 32 bits of // key samples are stored // // Return: true (success), false (not success) ////////////////////////////////////////////////////////////////////////// int saveDESdata (char* name, int encryptMode, int numSamples, DWORD* dinH, DWORD* dinL, DWORD* doutH, DWORD* doutL, DWORD* keyInH, DWORD* keyInL) { FILE *fp; int i; fp = fopen(name,"w"); if(fp == NULL) { printf("\n Error when opening file %s.",name); return (0); } if(fprintf(fp,"%s\n",(encryptMode=='e')?"ENCRYPTED":"DECRYPTED") < 0) { printf("\n Error when writting to file %s.",name); fclose(fp); return (0); } if(fprintf(fp,"%d VALUES\nMULTIPLE_KEY\n\n",numSamples) < 0) { printf("\n Error when writting to file %s.",name); fclose(fp); return (0); } if(fprintf(fp,"%s\n","INPUT DATA OUTPUT DATA INPUT KEY") < 0) { printf("\n Error when writting to file %s.",name); fclose(fp); return (0); } if(fprintf(fp,"%s\n","----------------- ----------------- -----------------") < 0) { printf("\n Error when writting to file %s.",name); fclose(fp); return (0); } for (i=0;i pointer to the name of the file used to write data // encryptMode -> direction of encryption ('e'=encrypt, 'd'=decrypt) // numSamples -> number of 64-bit samples read from file // dinH -> pointer to the array where most significant 32 bits of // input data samples will be stored (** previous output **) // dinL -> pointer to the array where least significant 32 bits of // input data samples will be stored (** previous output **) // doutH -> pointer to the array where most significant 32 bits of // output data samples will be stored (** previous input **) // doutL -> pointer to the array where least significant 32 bits of // output data samples will be stored (** previous input **) // keyInH -> pointer to the array where most significant 32 bits of // key samples will be stored // keyInL -> pointer to the array where least significant 32 bits of // key samples will be stored // // Return: true (success), false (not success) ////////////////////////////////////////////////////////////////////////// int readDESdata (char* name, int *encryptMode, int *numSamples, DWORD* dinH, DWORD* dinL, DWORD* doutH, DWORD* doutL, DWORD* keyInH, DWORD* keyInL) { FILE *fp; int i; char readText[20], typeKey, ch; DWORD keyH, keyL; /////////////////////////////////////////////////// FILE FORMATS // ENCRYPTED | DECRYPTED | NOT_PROCESSED // VALUES // MULTIPLE_KEY | SINGLE_KEY // // INPUT DATA // --------------------- --------------------- --------------------- // <76543210> <76543210> <76543210> // ... ... ... ... ... ... // <76543210> <76543210> <76543210> ////////////////////////////////////////////////// FORMAT 1 // NOT_PROCESSED // VALUES // SINGLE_KEY // // INPUT DATA // --------------------- // <76543210> // ... ... // <76543210> /////////////////////////////////////////////////////////// ////////////////////////////////////////////////// FORMAT 2 // NOT_PROCESSED // VALUES // MULTIPLE_KEY // // INPUT DATA // --------------------- --------------------- // <76543210> <76543210> // ... ... ... ... // <76543210> <76543210> /////////////////////////////////////////////////////////// ////////////////////////////////////////////////// FORMAT 3 // ENCRYPTED | DECRYPTED // VALUES // MULTIPLE_KEY // // INPUT DATA // --------------------- --------------------- --------------------- // <76543210> <76543210> <76543210> // ... ... ... ... ... ... // <76543210> <76543210> <76543210> /////////////////////////////////////////////////////////// fp = fopen(name,"r"); if(fp == NULL) { printf("\n Error when opening file %s.",name); return (0); } if(fscanf(fp,"%s\n",readText) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } if(strcmp(readText,"ENCRYPTED")==0) { *encryptMode = 'e'; } else if (strcmp(readText,"DECRYPTED")==0) { *encryptMode ='d'; } else if (strcmp(readText,"NOT_PROCESSED")==0) { *encryptMode = 'n'; } else { printf("\n Invalid field 1 on file %s.",name); fclose(fp); return (0); } if(fscanf(fp,"%d %s\n", numSamples, readText) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } if(fscanf(fp,"%s", readText) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } if(strcmp(readText,"MULTIPLE_KEY")==0) { typeKey = 'm'; fscanf(fp,"%c", &ch); fscanf(fp,"%c", &ch); } else if (strcmp(readText,"SINGLE_KEY")==0) { typeKey = 's'; fscanf(fp," %x %x\n", &keyH, &keyL); fscanf(fp,"%c", &ch); } else { printf("\n Invalid field 3 on file %s.",name); fclose(fp); return (0); } do { // read one header line fscanf(fp,"%c",&ch); } while (ch != '\n'); do { // read one header line fscanf(fp,"%c",&ch); } while (ch != '\n'); if ( (*encryptMode == 'n') && (typeKey == 's') ) // FORMAT 1 { // Read "INPUT DATA" samples from file into dinH,dinL // Fill keyInH,keyInL with fixed values keyH,keyL for (i=0;i<*numSamples;++i) { if(fscanf(fp,"%x %x\n", dinH+i, dinL+i) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } keyInH[i] = keyH; keyInL[i] = keyL; doutH[i] = 0; doutL[i] = 0; } } else if ( (*encryptMode == 'n') && (typeKey == 'm') ) // FORMAT 2 { // Read "INPUT DATA" samples from file into dinH,dinL // Read "INPUT KEY" samples from file into keyInH,keyInL for (i=0;i<*numSamples;++i) { if(fscanf(fp,"%x %x %x %x\n", dinH+i, dinL+i, keyInH+i, keyInL+i) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } doutH[i] = 0; doutL[i] = 0; } } if ( (*encryptMode == 'e') || (*encryptMode == 'd') ) // FORMAT 3 { // Read "OUTPUT DATA" samples from file into dinH,dinL // Read "INPUT DATA" samples from file into doutH,doutL // Read "INPUT KEY" samples from file into keyInH,keyInL for (i=0;i<*numSamples;++i) { if(fscanf(fp,"%x %x %x %x %x %x\n", doutH+i, doutL+i, dinH+i, dinL+i, keyInH+i, keyInL+i) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } } } fclose(fp); return (1); } ////////////////////////////////////////////////////////////////////////// // Read the number of 64-bit samples included on file. ////////////////////////////////////////////////////////////////////////// int readNumberSamples (char* name, int *numSamples) { FILE *fp; char readText[20]; fp = fopen(name,"r"); if(fp == NULL) { printf("\n Error when opening file %s.",name); return (0); } if(fscanf(fp,"%s\n",readText) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } if(fscanf(fp,"%d %s\n", numSamples, readText) < 0) { printf("\n Error when reading from file %s.",name); fclose(fp); return (0); } fclose(fp); return (1); } ////////////////////////////////////////////////////////////////////////// // Operations implemented in software for the DES example. ////////////////////////////////////////////////////////////////////////// void read_write (EDGAR2A_HANDLE hEDGAR2A, EDGAR2A_ADDR e2addrSpace) { DWORD *dinH, *dinL, *keyInH, *keyInL, encryptIn, dinValid; DWORD *doutH, *doutL, writeDone; DWORD valRead, val2send; int numSamples, fileSamples, encryptMode, inputMode, i, j, o; int fileEncMode, ch; char name[200]; //-------------------------------------------------------------------- // Read direction of encryption (encrypt or decrypt) do { printf ("\n\n [E]ncrypt or [D]ecrypt data: "); encryptMode = getchar (); encryptMode = tolower(encryptMode); } while ( (encryptMode!='e') && (encryptMode!='d') ); newline(); do { printf ("\n Generate input data [R]andomly or read data from [F]ile : "); inputMode = getchar (); inputMode = tolower(inputMode); } while ( (inputMode!='r') && (inputMode!='f') ); //-------------------------------------------------------------------- // Read number of samples to encrypt/decrypt if (inputMode == 'r') // Generate input data randomly { if (encryptMode=='e') // ENCRYPT { printf ("\n Number of 64-bit samples to encrypt [multiple of 4] : "); scanf ("%d",&numSamples); numSamples = (numSamples%4==0) ? numSamples : (numSamples/4+1)*4; encryptIn = 0x01010101 ; } else // (encryptMode=='d') * DECRYPT { printf ("\n Number of 64-bit samples to decrypt [multiple of 4] : "); scanf ("%d",&numSamples); numSamples = (numSamples%4==0) ? numSamples : (numSamples/4+1)*4; encryptIn = 0x00000000 ; } newline(); //-------------------------------------------------------------------- // Allocate space for input and output data dinH = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); dinL = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); keyInH = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); keyInL = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); doutH = (DWORD*) malloc (sizeof(DWORD)*numSamples); doutL = (DWORD*) malloc (sizeof(DWORD)*numSamples); //-------------------------------------------------------------------- // Generate pseudo-random input data an input key to DES. generateRandData (numSamples, dinH, dinL, keyInH, keyInL); } else // Read data from file { newline(); printf ("\n File with data to process: "); gets(name); readNumberSamples (name, &fileSamples); numSamples = (fileSamples%4==0) ? fileSamples : (fileSamples/4+1)*4; //-------------------------------------------------------------------- // Allocate space for input and output data dinH = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); dinL = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); keyInH = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); keyInL = (DWORD*) malloc (sizeof(DWORD)*(numSamples+4)); doutH = (DWORD*) malloc (sizeof(DWORD)*numSamples); doutL = (DWORD*) malloc (sizeof(DWORD)*numSamples); //-------------------------------------------------------------------- // Read input data from file readDESdata (name, &fileEncMode, &fileSamples, dinH, dinL, doutH, doutL, keyInH, keyInL); for(i=fileSamples; i> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b1_din, val2send); // Write byte 1 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b2_din, val2send); // Write byte 2 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b3_din, val2send); // Write byte 3 of "din" val2send = dinH[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b4_din, val2send); // Write byte 4 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b5_din, val2send); // Write byte 5 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b6_din, val2send); // Write byte 6 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b7_din, val2send); // Write byte 7 of "din" val2send = keyInL[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b0_keyIn, val2send); // Write byte 0 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b1_keyIn, val2send); // Write byte 1 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b2_keyIn, val2send); // Write byte 2 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b3_keyIn, val2send); // Write byte 3 of "keyIn" val2send = keyInH[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b4_keyIn, val2send); // Write byte 4 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b5_keyIn, val2send); // Write byte 5 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b6_keyIn, val2send); // Write byte 6 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b7_keyIn, val2send); // Write byte 7 of "keyIn" EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_encryptIn, encryptIn); // Write "encryptIn" EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_dinValid, dinValid); // Write "dinValid" } while (i> 24; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b1_dout); // Read byte 1 of "dout" valRead = (valRead >> 16) & 0x0000FF00; doutL[o] = doutL[o] | valRead; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b2_dout); // Read byte 2 of "dout" valRead = (valRead >> 8) & 0x00FF0000; doutL[o] = doutL[o] | valRead; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b3_dout); // Read byte 3 of "dout" valRead = valRead & 0xFF000000; doutL[o] = doutL[o] | valRead; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b4_dout); // Read byte 4 of "dout" doutH[o] = valRead >> 24; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b5_dout); // Read byte 5 of "dout" valRead = (valRead >> 16) & 0x0000FF00; doutH[o] = doutH[o] | valRead; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b6_dout); // Read byte 6 of "dout" valRead = (valRead >> 8) & 0x00FF0000; doutH[o] = doutH[o] | valRead; valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_b7_dout); // Read byte 7 of "dout" valRead = valRead & 0xFF000000; doutH[o] = doutH[o] | valRead; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_writeDone, writeDone); // Write "writeDone" // --------------------------------------------------------------- // Read "readNewData" from stage 0 valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_readNewData); valRead = valRead & 0x00000001; while (valRead == 0) { // printf("\n ... readNewData is zero ..."); valRead = EDGAR2A_ReadDword(hEDGAR2A, e2addrSpace, ADD_R_readNewData); valRead = valRead & 0x00000001; } // --------------------------------------------------------------- // When (readNewData = 1) write "din", "keyIn", "encryptIn" and // finaly "dinValid" into stage 0 val2send = dinL[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b0_din, val2send); // Write byte 0 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b1_din, val2send); // Write byte 1 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b2_din, val2send); // Write byte 2 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b3_din, val2send); // Write byte 3 of "din" val2send = dinH[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b4_din, val2send); // Write byte 4 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b5_din, val2send); // Write byte 5 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b6_din, val2send); // Write byte 6 of "din" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b7_din, val2send); // Write byte 7 of "din" val2send = keyInL[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b0_keyIn, val2send); // Write byte 0 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b1_keyIn, val2send); // Write byte 1 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b2_keyIn, val2send); // Write byte 2 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b3_keyIn, val2send); // Write byte 3 of "keyIn" val2send = keyInH[i] ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b4_keyIn, val2send); // Write byte 4 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b5_keyIn, val2send); // Write byte 5 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b6_keyIn, val2send); // Write byte 6 of "keyIn" val2send = val2send >> 8 ; EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_b7_keyIn, val2send); // Write byte 7 of "keyIn" EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_encryptIn, encryptIn); // Write "encryptIn" EDGAR2A_WriteDword(hEDGAR2A, e2addrSpace, ADD_W_dinValid, dinValid); // Write "dinValid" } } //-------------------------------------------------------------------- // Print processed data on the screen do { printf ("\n Print processed data on the screen [y/n]: "); ch = getchar (); ch = tolower(ch); } while ( (ch!='y') && (ch!='n') ); if (ch == 'y') { printf("\n %s", "INPUT DATA OUTPUT DATA INPUT KEY"); printf("\n %s\n","----------------- ----------------- -----------------"); for (i=0;i