Esempio n. 1
0
/**
 * Initialize the Packet Handler library
 * \param[in] txWinSize The transmission window size (number of tx packet buffers).
 * \param[in] streme A callback function for transmitting the packet.
 * \param[in] id The source ID of transmitter.
 * \return PHInstHandle The Pachet Handler instance data.
 * \return 0 Failure
 */
PHInstHandle PHInitialize(PacketHandlerConfig *cfg)
{
	// Allocate the primary structure
	PHPacketDataHandle data = pvPortMalloc(sizeof(PHPacketData));
	if (!data)
		return 0;
	data->cfg = *cfg;
	data->tx_seq_id = 0;

	// Allocate the packet windows
	data->tx_packets = pvPortMalloc(sizeof(PHPacket) * data->cfg.winSize);
	data->rx_packets = pvPortMalloc(sizeof(PHPacket) * data->cfg.winSize);

	// Initialize the windows
	data->tx_win_start = data->tx_win_end = 0;
	data->rx_win_start = data->rx_win_end = 0;
	for (uint8_t i = 0; i < data->cfg.winSize; ++i)
	{
		data->tx_packets[i].header.type = PACKET_TYPE_NONE;
		data->rx_packets[i].header.type = PACKET_TYPE_NONE;
	}

	// Create the lock
	data->lock = xSemaphoreCreateRecursiveMutex();

	// Initialize the ECC library.
	initialize_ecc();

	// Return the structure.
	return (PHInstHandle)data;
}
Esempio n. 2
0
// concatenate two RS code for encoding
void concat_encode(unsigned char * message,unsigned char* codeword) {
	unsigned char tmp_code[v*BLOCK_SIZE*n1/k1],stripe[k1],stripe_code[n1];
	int index,i,j;
	// read in the message part
	for (index=0;index<v*BLOCK_SIZE;index++) {
		tmp_code[index] = message[index];
	}
	// outer encoding on the message
	for (i=0;i<v;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = message[i*k1+j];
		}
		initialize_ecc();
		// encode for each outer stripe
		encode_data(stripe,k1,stripe_code);
		// append parity at the end
		for (j=0;j<n1-k1;j++) {
			tmp_code[index] = stripe_code[k1+j];
			index++;
		}
	}
	index = 0;
	// perform inner encoding
	for (i=0;i<v*n1/k1;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = tmp_code[i*k2+j];
		}
		// encode for each inner stripe
		encode_data(stripe,k2,stripe_code);
		for (j=0;j<n2;j++) {
			codeword[index] = stripe_code[j];
			index++;
		}
	}
}
Esempio n. 3
0
void concat_encode(unsigned char * message,unsigned char* codeword) {
	unsigned char tmp_code[v*32*n1/k1],stripe[k1],stripe_code[n1];
	int index,i,j;
	
	for (index=0;index<v*32;index++) {
		tmp_code[index] = message[index];
	}
	for (i=0;i<v;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = message[i*k1+j];
		}
		initialize_ecc();
		encode_data(stripe,k1,stripe_code);
		for (j=0;j<n1-k1;j++) {
			tmp_code[index] = stripe_code[k1+j];
			index++;
		}
	}
	index = 0;
	for (i=0;i<v*n1/k1;i++) {
		for (j=0;j<sizeof(stripe);j++) {
			stripe[j] = tmp_code[i*k2+j];
		}
		encode_data(stripe,k2,stripe_code);
		for (j=0;j<n2;j++) {
			codeword[index] = stripe_code[j];
			index++;
		}
	}
}
Esempio n. 4
0
int
main (int argc, char *argv[])
{
 
  int erasures[16];
  int nerasures = 0;

  /* Initialization the ECC library */
 
  initialize_ecc ();
 
  /* ************** */
 
  /* Encode data into codeword, adding NPAR parity bytes */
  encode_data(msg, sizeof(msg), codeword);
 
  printf("Encoded data is: \"%s\"\n", codeword);
  printf("Encoded data length: \"%d\"\n", sizeof(codeword));
  printf("msg length: \"%d\"\n", sizeof(msg));
  int i;
  		for (i = 0; (i < sizeof(msg)); i++) {
			printf("%02x", msg[i]);
		}
		printf("\n");
		for (i = 0; (i < sizeof(codeword)); i++) {
			printf("%02x", codeword[i]);
		}
		printf("\n");
 
#define ML (sizeof (msg) + NPAR)


  /* We need to indicate the position of the erasures.  Eraseure
     positions are indexed (1 based) from the end of the message... */

  erasures[nerasures++] = ML-17;
  erasures[nerasures++] = ML-19;

 
  /* Now decode -- encoded codeword size must be passed */
  decode_data(codeword, ML);

  /* check if syndrome is all zeros */
  if (check_syndrome () != 0) {
    correct_errors_erasures (codeword, 
			     ML,
			     nerasures, 
			     erasures);
 
    printf("Corrected codeword: \"%s\"\n", codeword);
  }
 
  exit(0);
}
Esempio n. 5
0
int initial_outer_decoding(FILE* fp1,unsigned char * mac) {
	unsigned char newMac[16];
	unsigned char buf[BLOCK_SIZE];
	int i,j;
	FILE * orifp, * parifp;

	if ((orifp = fopen("original", "r+b")) == NULL){
		printf("couldn't open input file for reading.\n");
		return -1;
        }
	if ((parifp = fopen("parity", "w+b")) == NULL){
		printf("couldn't open input file for reading.\n");
		return -1;
        }
	fseek(fp1,m*BLOCK_SIZE,SEEK_SET);
	for (i=0;i<t-m;i++) {
		readStartTime = getCPUTime();
		fread(buf, BLOCK_SIZE, 1, fp1);
		readEndTime = getCPUTime();
		readTime += readEndTime-readStartTime;
		writeStartTime = getCPUTime();
   	fwrite(buf,BLOCK_SIZE,1,parifp);
      	writeEndTime = getCPUTime();
		writeTime += writeEndTime-writeStartTime;
	}
	fclose(parifp);
	if ((parifp = fopen("parity", "r+b")) == NULL){
		printf("couldn't open input file for reading.\n");
		return -1;
        }
   FILE* outer;
   if ((outer = fopen("outerdec", "w+b")) == NULL){
		printf("couldn't open input file for reading.\n");
		return -1;
        }     
   FILE* tempfp = fopen("tempfile", "w+b");
   initialize_ecc();
   outer_decoding(orifp, parifp, outer, tempfp, NULL);
	macStartTime = getCPUTime();
	printf("HMAC using key: ");
	displayCharArray(k_mac,16);	
	hmac("outerdec",newMac,k_mac);
	macEndTime = getCPUTime();
	macTime = macEndTime - macStartTime;
	printf("MAC of the file part: ");
	displayCharArray(newMac,16);

	for (i=0;i<16;i++) {
		if (newMac[i]!=mac[i])
			return -1;
	}
	return 0;
}
ReedSolomon::ReedSolomon(int npar)
{
    this->npar = npar;
    //printf("npar = %d  ",npar); fflush(stdout);
    pBytes = new int[npar * 2];
    clear<int>(pBytes, npar * 2);
    synBytes = new int[npar * 2];
    clear<int>(synBytes, npar * 2);
    genPoly = new int[npar * 4];
    clear<int>(genPoly, npar * 4);

    berlekamp = new Berlekamp(npar, synBytes);
    initialize_ecc();
}
void ReedSolomon::reset_npar(int npar)
{
    if(this->npar == npar) return;
    this->npar = npar;
    //printf("npar = %d   ",npar);
    delete[] pBytes;
    delete[] synBytes;
    delete[] genPoly;
    delete berlekamp;
    pBytes = new int[npar * 2];
    clear<int>(pBytes, npar * 2);
    synBytes = new int[npar * 2];
    clear<int>(synBytes, npar * 2);
    genPoly = new int[npar * 4];
    clear<int>(genPoly, npar * 4);
    berlekamp = new Berlekamp(npar, synBytes);
    initialize_ecc();
}
Esempio n. 8
0
int main(int argc, char* argv[])
{
    totalStartTime = getCPUTime();
    printf("%lf\n",totalStartTime);
	int i;
	FILE* fp = fopen(argv[1],"a+b");
	if (fp==NULL) {
		printf("fopen error: cannot open file\n");
		exit(1);
	}
	int* prptable;
	unsigned char mac[MAXBLOCKSIZE];
    unsigned long fileLen1,fileLen2;
    // get the file size
	fseek(fp,0,SEEK_END);
	fileLen1 = ftell(fp);
    // generate keys
	master_keygen(argv[2]);

	printf("key for file permutation: ");
	displayCharArray(k_file_perm,16);

	printf("key for ecc permutation: ");
	displayCharArray(k_ecc_perm,16);

	printf("key for ecc encryption: ");
	displayCharArray(k_ecc_enc,16);

	printf("key for challenge generation: ");
	displayCharArray(k_chal,16);

	printf("key for random index generation: ");
	displayCharArray(k_ind,16);

	printf("key for response encryption: ");
	displayCharArray(k_enc,16);

	printf("key for MAC computation: ");
	displayCharArray(k_mac,16);	
	
    // blockize the file
	int blocks = blockize(fp);
	t = blocks;
	fclose(fp);
    fp = fopen(argv[1],"a+b");
    
    // computing MAC
	printf("\nComputing file's MAC...\n");
		printf("\nmac size %lu\n",sizeof(mac));
    macStartTime = getCPUTime();
	hmac(argv[1],mac,k_mac);
    macTime = getCPUTime() - macStartTime;
	printf("\nMAC = ");
	displayCharArray(mac,16);	
	
    // perform a file level PRP
	printf("\nSRF PRP for the entire file...\n");
    prpStartTime = getCPUTime();
	prptable = prp(blocks, k_file_perm);
    prpTime += getCPUTime() - prpStartTime;
    eccStartTime = getCPUTime();
	initialize_ecc();
	inc_encoding(fp,prptable);
    eccTime = getCPUTime() - eccStartTime - readTime;
	
	printf("\nFile blocks after outer layer encoding: %lu\n",t);

    // precompute q challenge and responsess
	printf("\nPrecomputation for %d challenges and responses\n",q);
    chalStartTime = getCPUTime();
	Chal c[q];
	int j,p;
    // use k_chal to generate kjc
	keygen_init();
	seeding(k_chal);
	unsigned char * kjc[q];
	for(j=0;j<q;j++) {
		kjc[j] = malloc(16*sizeof(unsigned char *));
		keygen(kjc[j], 16);
	}
    // use kjc to generate random indices
	for(j=0;j<q;j++) {
		keygen_init();
		seeding(kjc[j]);
		for(p=0;p<v;p++) {
			unsigned long randomIndex;
			char rand[8];
			keygen(rand, 8);
			randomIndex = *(unsigned long *)rand;	
			c[j].s[p] = randomIndex % t;
		}
	}
    // use k_ind to generate random index u
	keygen_init();
	seeding(k_ind);	
	for(j=0;j<q;j++) {
		unsigned int randomIndex;
		char rand[4];
		keygen(rand, 4);
		randomIndex = *(unsigned int *)rand;	
		c[j].u = randomIndex % w;
	}
	printf("Precomputation for challenges finishes\n");
    // precompute challenge responses
	precompute_response(fp,c,k_enc);
	printf("Precomputation for responses finishes\n");
    chalTime+=getCPUTime()-chalStartTime - write2Time;
    // append MAC at the end of the files
	printf("\nAppend MAC to the end of the file...\n");
    write2StartTime = getCPUTime();
    clock_gettime(CLOCK_MONOTONIC, &start);
	fwrite(mac,16,1,fp);
    clock_gettime(CLOCK_MONOTONIC, &finish);
    double addTime = finish.tv_sec - start.tv_sec;
    addTime += (finish.tv_nsec - start.tv_nsec)/1000000000.0;
    write2Time += getCPUTime() - write2StartTime+addTime;
    fseek(fp,0,SEEK_END);
	fileLen2 = ftell(fp);
	fclose(fp);

	printf("\nPOR encoding done\n");

	// display time performance
    totalTime = getCPUTime() - totalStartTime;
    printf("#RESULT#\n");
    printf("%lu\n",fileLen1);
    printf("%lu\n",fileLen2);
    printf("%lf\n",totalTime);
    printf("%lf\n",readTime);
    printf("%lf\n",prpTime);
    printf("%lf\n",eccTime);
    printf("%lf\n",macTime);
    printf("%lf\n",chalTime);
    printf("%lf\n",write1Time+write2Time);
    printf("%lf\n",encTime);


}
Esempio n. 9
0
int
main (int argc, char *argv[])
{
    int erasures[16];
    int nerasures = 0;
    char* filename = argv[1];
    char command[100];
    printf("reading the original file %s\n",argv[1]);

    //filename = argv[1];
    sprintf(command,"md5 %s",argv[1]);
    system(command);

    /* Initialization the ECC library */
    
    initialize_ecc ();
    
    //Open file
    file = fopen(argv[1], "r");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", argv[1]);
        exit(1);
    }
    
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);
    
    //Allocate memory
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
        exit(2);
    }
    
    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);

    fclose(file);

    long i = fileLen;
    long pos = 0;
    int writeFlag = TRUE;
    strcat(filename,".encode");
    printf("----------------------------------------------\n");
    printf("encoding the original file into %s\n",filename);

    while (i>0)
    {
        //system(command);

        int msgLen;
        if (i>256-NPAR) {
            msgLen = 256-NPAR;
        }
        else {
            msgLen = i;
        }
        unsigned char msg[msgLen];
        int k;

        for(k=0;k<msgLen;k++) {
            msg[k] = buffer[pos];
            pos++;
        }
        encode_data(msg, sizeof(msg), codeword);

        if (writeFlag) {
            file = fopen(filename, "w+");
            writeFlag = FALSE;
        }
        else {
            file = fopen(filename, "a+");
        }
        fwrite(codeword, msgLen+NPAR, 1, file);

        fclose(file);
        i = i - msgLen;
    }
    free(buffer);

    sprintf(command,"md5 %s",filename);
    system(command);

    file = fopen(filename, "r");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", filename);
        exit(1);
    }
    
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);
    //Allocate memory
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
        exit(2);
    }
    
    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);
    
    fclose(file);
    i = fileLen;
    pos = 0;
    writeFlag = TRUE;
    strcat(filename,".corrupt");
    printf("----------------------------------------------\n");
    printf("making some errors and write into the file %s\n",filename);

    int k;
    for(k = 0;k<10;k++) {
        long h = rand()%fileLen;
        buffer[h] = buffer[h] % 20;
    }
    file = fopen(filename, "w+");
    fwrite(buffer, fileLen, 1, file);
    fclose(file);

    free(buffer);

    sprintf(command,"md5 %s",filename);
    system(command);
    
    file = fopen(filename, "r");
    if (!file)
    {
        fprintf(stderr, "Unable to open file %s", filename);
        exit(1);
    }
    
    //Get file length
    fseek(file, 0, SEEK_END);
    fileLen=ftell(file);
    fseek(file, 0, SEEK_SET);
    //Allocate memory
    buffer=(unsigned char *)malloc(fileLen+1);
    if (!buffer)
    {
        fprintf(stderr, "Memory error!");
        fclose(file);
        exit(2);
    }
    
    //Read file contents into buffer
    fread(buffer, fileLen, 1, file);
    
    fclose(file);
    i = fileLen;
    pos = 0;
    writeFlag = TRUE;
    strcat(filename,".recover");
    printf("----------------------------------------------\n");
    printf("recovering corrupted file into %s\n",filename);
    while (i>0)
    {
        int msgLen;
        if (i>256) {
            msgLen = 256;
        }
        else {
            msgLen = i;
        }
        unsigned char msg[msgLen];
        int k;
        
        for(k=0;k<msgLen;k++) {
            msg[k] = buffer[pos];
            pos++;
        }
        decode_data(msg, msgLen);

        if (check_syndrome () != 0) {
            correct_errors_erasures (msg,msgLen,nerasures,erasures);
        }
        if (writeFlag) {
            file = fopen(filename, "w+");
            writeFlag = FALSE;
        }
        else {
            file = fopen(filename, "a+");
        }
        fwrite(msg, msgLen-NPAR, 1, file);
        
        fclose(file);
        i = i - msgLen;
    }
    sprintf(command,"md5 %s",filename);
    system(command);
    
    free(buffer);
    exit(0);
}
Esempio n. 10
0
 virtual void SetUp() {
   initialize_ecc();
 }