Пример #1
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;
}
void displayVm(Vm* vm)
{
	printf("PC = %d ", vm->PC);
	printf("IR = ");
	displayCharArray(vm->IR, 6);
	printf(";  ");
	printf("%s\n", INSTRUCTION_NAMES[getOpCode(vm->IR)]);
}
void displayVmFinal(Vm* vm)
{
    printf("\nCPU:\n");
    printf("PC=%d SP=%d ACC=%d PSW=%s", vm->PC, vm->SP, vm->ACC, vm->PSW);
    printf(" IR=");
    displayCharArray(vm->IR, 6);
    printf("\n");
    printf("\nREGISTERS:\n");
    printf("P0=%d P1=%d P2=%d P3=%d\n", vm->P0, vm->P1, vm->P2, vm->P3);
    printf("R0=%d R1=%d R2=%d R3=%d\n", vm->R0, vm->R1, vm->R2, vm->R3);
    printf("");
    printf("\nMEMORY:\n");
    displayMemory(vm->memory, 100, 6);
    printf("--------------------------------------------------------------\n");
}
Пример #4
0
int checkFile(FILE* fp1,unsigned char * mac) {
	unsigned char newMac[16];
	unsigned char buf[BLOCK_SIZE];
	int i,j;
	FILE * orifp;
	fseek(fp1,0,SEEK_SET);
	if ((orifp = fopen("original", "w+b")) == NULL){
		printf("couldn't open input file for reading.\n");
		return -1;
        }
	for (i=0;i<m;i++) {
		readStartTime = getCPUTime();
		fread(buf, BLOCK_SIZE, 1, fp1);
		readEndTime = getCPUTime();
		readTime += readEndTime-readStartTime;
		writeStartTime = getCPUTime();
   	fwrite(buf,BLOCK_SIZE,1,orifp);
      	writeEndTime = getCPUTime();
		writeTime += writeEndTime-writeStartTime;
	}
	fclose(orifp);
	macStartTime = getCPUTime();
	printf("HMAC using key: ");
	displayCharArray(k_mac,16);	
	hmac("original",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;
}
Пример #5
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);


}
Пример #6
0
int outer_decoding(FILE* orifp, FILE* parifp, FILE *output, FILE* tempfp, decoded_blocks *db)
{
	int i,j,stripes,fileLen,index;
	int* prp_table;
	int* reverse_prp_table;
	char message[k];
	char codeword[n];

	//unsigned char decodedfile[stripes][k];
	int erasure[1];
	//FILE* tempfp = fopen("tempfile", "w+b");
	if (tempfp  == NULL){
 		printf("couldn't open temperory file for writing.\n");
 		return -1;
    	}

	//find the number of stripes in the file 
	fseek(parifp,0,SEEK_END);
	fileLen = ftell(parifp);
	printf("display fileLen of parity %d\n",fileLen);
	//if(fileLen % n==0) 
		stripes = fileLen/d; 
	//else
		//stripes = fileLen/n+1;
	printf("number of stripes for outer decoding %d\n",stripes);
	fflush(stdout);
	unsigned char ** parity;//[stripes][d]; 
	parity = (unsigned char **) malloc(stripes*sizeof(unsigned char *));  
	for (i = 0; i < stripes; i++) {
   	parity[i] = (unsigned char *) malloc(d*sizeof(unsigned char));  
   	}
	//call prp
	prp_table =malloc(sizeof(int)*stripes);
	reverse_prp_table = malloc(sizeof(int)*stripes);
	
	//perform reverse prp
	prp_table = prp(stripes, k_ecc_perm);
	reverseprp(stripes,prp_table,reverse_prp_table);
	/*for(i=0;i<stripes;i++){
		printf("prp %d: %d\n",i,prp_table[i]);
		printf("rev prp %d: %d\n",i,reverse_prp_table[i]);
	}*/
	//free(prp_table);
	printf("check1\n");
	enc_init(k_ecc_enc);
	
	//decrypt parity part
	rewind(parifp);
	rewind(orifp);
	///number of parity blocks = stripes/2
	//read parity parts directly

	//int parity_start = fileLen-(stripes/2)*d;
	//fseek(temp_fp,parity_start,SEEK_SET);
	unsigned char paritybytes[stripes][d];
	for (i=0;i<stripes;i++) {
		unsigned char ct[d];
		//unsigned char pt[d];	
		
		fread(ct,sizeof(ct),1,parifp);
		
		decrypt(ct,paritybytes[prp_table[i]],sizeof(ct)); 
		//printf("prp rev for %d: ",i);
		//displayCharArray(paritybytes[i],sizeof(ct));
		//for(j=0;j<d;j++){
		//	parity[i][j]=ct[j];
		//}
	}
	for (i=0;i<stripes;i++) {
		printf("prp rev for %d: ",i);
		displayCharArray(paritybytes[i],sizeof(paritybytes[i]));
	}
	free(reverse_prp_table);
	reverse_prp_table = malloc(sizeof(int)*m);
	prp_table =malloc(sizeof(int)*m);
	prp_table = prp(m, k_file_perm);
	reverseprp(m,prp_table,reverse_prp_table);
	//get the message and the parity, create codeword and decode it
	//rewind(temp_fp);
	FILE * prp = fopen("prp","w+b");
	for(i=0;i<m;i++) {
		unsigned char prpbuf[BLOCK_SIZE];
		fseek(orifp,prp_table[i]*32,SEEK_SET);
		size_t br = fread(prpbuf,1,BLOCK_SIZE,orifp);
		fwrite(prpbuf,1,BLOCK_SIZE,prp);
	}
	fclose(prp);
	prp = fopen("prp","r+b");
	for(i=0;i<stripes;i++) {
		index=0;
		unsigned char code[n];

		size_t br = fread(code,1,k,prp);
		int pad = k-br;
		int ii;
		for (ii=0;ii<pad;ii++)
			code[br+ii] = 0;
			
		for(j=0;j<d;j++) {
			code[k+j] = paritybytes[i][j]; 
		}
		printf("whole code %d: ",i);
		displayCharArray(code,n);
		decode_data(code, n);
		int syn = check_syndrome ();
		printf("syndrome: %d\n",syn);
		if (syn != 0) {
			correct_errors_erasures(code,n,0,erasure);
		}
		printf("decoded code %d: ",i);
		displayCharArray(code,n);
		//calculate block index of the parity part
		fwrite(code,1,br,tempfp);
	}
	fclose(tempfp);
	for (i = 0; i < stripes; i++){  
   	free(parity[i]);  
	}  
	free(parity);
	if ((tempfp = fopen("tempfile", "r+b")) == NULL){
 		printf("couldn't open temperory file for writing.\n");
 		return -1;
    	}
	// write data to the file by applying second level of permutation
	//free(prp_table);
	//free(reverse_prp_table);
	//printf("display m = %lu\n",m);

	rewind(tempfp);
	rewind(output);
	unsigned char buf[BLOCK_SIZE];
	for(i=0;i<m;i++) {
		readStartTime = getCPUTime();
		fseek(tempfp,reverse_prp_table[i]*32,SEEK_SET);
		fread(buf, BLOCK_SIZE, 1, tempfp);
		readEndTime = getCPUTime();
		readTime += readEndTime-readStartTime;
		writeStartTime = getCPUTime();
   	fwrite(buf,BLOCK_SIZE,1,output);
      	writeEndTime = getCPUTime();
		writeTime += writeEndTime-writeStartTime;		
	}
	printf("check4\n");
	fclose(tempfp);
	fclose(output);
	fclose(orifp);
	fclose(parifp);
	printf("check5\n");
	//delete(temp_fp);
	return 0;
}
Пример #7
0
int main(int argc,char** argv)
{

	totalStartTime = getCPUTime();
	//endTime = getCPUTime();
	printf("start POR extract...\n");
	char * filename = argv[1];
    FILE *fp1,*fp2,*temp_fp;
	//open encoded file for reading
  	if ((fp1 = fopen(argv[1], "r")) == NULL){
        printf("couldn't open input file for reading.\n");
        return -1;
        }
    
	int p;
	//t = atoi(argv[3]);
	unsigned long fileLen;
	fseek(fp1,0,SEEK_END);
	fileLen = ftell(fp1);
	t = (fileLen-16-oldq*BLOCK_SIZE) / BLOCK_SIZE;
	m = t*k/n;
	printf("extract for file \"%s\" with size=%lu m=%lu and t=%lu\n",filename,fileLen,m,t);
	//use master key and call keygen to generate all the keys here.	
	master_keygen(argv[2]);
	
	//read mac from the end of the old file	
	unsigned char originalmac[16];
	int bufLength=16;
	readStartTime = getCPUTime();
	fseek(fp1, fileLen-bufLength, SEEK_SET);
	fread(originalmac, sizeof(originalmac), 1, fp1);
	readEndTime = getCPUTime();
	readTime += readEndTime-readStartTime;
	printf("\nMAC attached at the end of the file: ");
	displayCharArray(originalmac,16);
	
	if (checkFile(fp1,originalmac)==0) {
		printf("File is intact.\n");
		fclose(fp1);
		totalEndTime = getCPUTime();
		totalTime = totalEndTime - totalStartTime;
		printf("#RESULT#\n");
		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",writeTime);
		exit(0);
	}
	else if (initial_outer_decoding(fp1,originalmac)==0){
		//initial_outer_decoding(fp1, newfp);
		//if (checkFile(newfp,originalmac)==0) {
			printf("File is recovered after outer encoding.\n");
			fclose(fp1);
			totalEndTime = getCPUTime();
			totalTime = totalEndTime - totalStartTime;
			printf("#RESULT#\n");
			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",writeTime);
			exit(0);
		//}
	}
	//else {
		//printf("File is corrupted.\n");
		//exit(0);
	//}
	int tempv = v;
	q = alpha * t / tempv;
	decoded_blocks * db;
	unsigned int i,j,u,size,index;
	char * codeword,mac;
	chal c[q];
	unsigned char k_j_c[16]; 
	char str[999]; 
	char * temp_block;
	// after writing new file, delete old file

	char * r_file="recovered";
	char * temp ="temp";
	unsigned int * v_chal_indices;


	//open output file for writing
	if ((fp2 = fopen(r_file, "w+")) == NULL){
	         printf("couldn't open output file for writing.\n");
	         return -1;
    }

	//open temp file for writing
	if ((temp_fp = fopen(temp, "w+")) == NULL){
	         printf("couldn't open temperory file for writing.\n");
	         return -1;
    	}

	//allocate memory for d1
	db = malloc (sizeof(struct d_b)*t);  
	//for(i=0;i<t;i++) {
	//	printf("for db[%d], sizeof frequency=%lu\n",i,sizeof(db[i].frequency));
	//	printf("display frequency[5] %d\n",db[i].frequency[5]);
	//}
	if(db == NULL) {
		printf("failed to allocate memory for d.\n");
		return -1;
	}

	//total number of challenges		
	size = alpha*(t/v); 

	//allocate memory for the challenge set
	//if ((c = (chal *)malloc(sizeof(chal)*size))== NULL) {
	//	fprintf(stderr, "failed to allocate memory for challenges.\n");
	//	return -1;
	//}
	
	// populate challenge set
	printf("\nstart inner layer decoding...\n");
	printf("generate %lu challenges\n",q);
	keygen_init();
	seeding(k_chal);	
	for (j=0;j<q;j++){
		keygen(c[j].k_j_c, 16);
		c[j].j = j;
	}
	printf("kjc for each challenge generated\n");
	
    // for each challenge
	for (i=0;i<q;i++){
		
		//unsigned char * codeword = (unsigned char *) malloc(sizeof(unsigned char)*32*w);
		unsigned char codeword[32*w];
		unsigned long indices[v];

		index = 0;
		//execute each challenge w times
		for(u=0;u<w;u++){
			unsigned char * subcode = execute_challenge(fp1,c[i].j, c[i].k_j_c, u, indices);
			//printf("%d-th sub code\n",u);
			//displayCharArray(subcode,32);
			int tempI;
			for(tempI=0;tempI<32;tempI++)
				codeword[index++] = subcode[tempI];
		}
		//printf("codeword for challenge #%d\n",i);
		//displayCharArray(codeword,4096);
		// inner code decoding
		printf("start decoding for challenge #%d\n",i);
		inner_GMD(db,codeword,indices,fp1); 
		printf("finish decoding for challenge %d\n",i);

		//free the memory
		//free(codeword);
		//free(indices);

		//delete old file
			
		//remove(filename);
	}
	
	for (i=0;i<t;i++){
		int max_frequency=0;
		int max_index=0;
		
		for(j=0;j<sizeof(db[i].frequency);j++){
			if(db[i].frequency[j] > max_frequency){
				max_frequency = db[i].frequency[j];
				max_index = j;
			}
		}
		if(max_frequency==0) {
			fseek(fp1,i*32,SEEK_SET);
			unsigned char buffer[32];
			fread(buffer, 32, 1, fp1);
			fwrite(buffer,32,1,temp_fp);
		 }
		else {
		//check if the location can be corrected or has erasure 
		if(ceil(max_frequency / sizeof(db[i].frequency)) > (delta+0.5)){
			fwrite(db[i].file_blocks[max_index],32,1,temp_fp);
		}else{
			fwrite(db[i].file_blocks[max_index],32,1,temp_fp);
			//where to get the index from
			//db[i].file_blocks[0]=NULL;
	
			//-1 indicating erasure
			db[i].frequency[0]=-1;
		}
        }
	}
    fclose(fp1);
	fclose(temp_fp);
	
	//perform outer decoding
	//outer_decoding(temp_fp,fp2,db);
	
	//compute mac
	unsigned char newmac[16]; 
	//hmac("temp",newmac,k_mac);
    //printf("display MAC\n");
    //displayCharArray(newmac,16);
	
	//if verified, print the file. Else output the error
	int flag=1;	
	for(i=0;i<MACSIZE;i++){
		if(newmac[i]!=originalmac[i]){	
			flag=0;
			break;
		}
	}
	if (flag==1){
		printf("Your file is recovered\n");
    		//while (fscanf(fp2, "%s", str)!=EOF){
        //		printf("%s",str);
		//}
	}else{
		printf("Your file can not be recovered.\n");
		//return -1;
	}

	fclose(fp2);
    printf("#RESULT#\n");
    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",writeTime);

	return 0;
}