示例#1
0
static void getlist_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	struct keygen keygen_for_check;
	TCLIST *list = tclistnew();
	TCLIST *recs;
	int i;

	keygen_init(&keygen, seed);
	keygen_init(&keygen_for_check, seed);

	for (i = 0; i < num; i++) {
		tclistpush2(list, keygen_next_key(&keygen));

		if (tclistnum(list) >= batch) {
			recs = do_tcadbmisc(adb, command, list);
			check_records(recs, &keygen_for_check, vsiz,
					tclistnum(list));
			tclistdel(recs);
			tclistclear(list);
		}
	}
	if (tclistnum(list)) {
		recs = do_tcadbmisc(adb, command, list);
		check_records(recs, &keygen_for_check, vsiz, tclistnum(list));
		tclistdel(recs);
	}

	tclistdel(list);
}
示例#2
0
文件: keygen.c 项目: Detegr/tapi2p
int generate(const char* path, unsigned int keys)
{
	int pathlen=strnlen(path, PATH_MAX);
	if(pathlen>PATH_MAX+4)
	{
		fprintf(stderr, "Path: %s is too long\n", path);
	}

	keygen_init();
	char path2[pathlen+5];
	memset(path2, 0, pathlen+5);
	memcpy(path2, path, pathlen);
	if(keys & T2PPUBLICKEY)
	{
		memcpy(path2+pathlen, ".pub", 4);
		if(keygen_bio_init(path2) != 0) return -1;
		if(generate_pubkey() != 0) return -1;
	}
	if(keys & T2PPRIVATEKEY)
	{
		if(keygen_bio_init(path) != 0) return -1;
		if(generate_privkey() != 0) return -1;
	}
	keygen_free();

	return 0;
}
示例#3
0
unsigned char * execute_challenge(FILE* fp, int j, char * kjc, int u, unsigned long * indices) {
	unsigned char message[v*32];
	unsigned char codeword[w*32];

	int pos = 0;
	int i,p;
	keygen_init();
	seeding(kjc);
	for(p=0;p<v;p++) {
		unsigned long randomIndex;
		char rand[8];
		keygen(rand, 8);
		indices[p] = *(unsigned long *)rand%t;
	}
	int index = 0;
	for (i=0;i<v;i++) {
		fseek(fp,indices[i]*32,SEEK_SET);
		unsigned char buffer[32];
		fread(buffer, 32, 1, fp);
		for(p=0;p<32;p++) {
			message[index] = buffer[p];
			index++;
		}
	}
	concat_encode(message,codeword);
	for (i=0;i<32;i++) {
		uth[i] = codeword[32*u+i];
	}
	return uth;
}
示例#4
0
static void putlist_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	char *value = xmalloc(vsiz);
	TCLIST *list = tclistnew();
	int i;

	keygen_init(&keygen, seed);

	for (i = 0; i < num; i++) {
		tclistpush2(list, keygen_next_key(&keygen));
		tclistpush(list, value, vsiz);

		if (tclistnum(list) / 2 >= batch) {
			tclistdel(do_tcadbmisc(adb, command, list));
			tclistclear(list);
		}
	}
	if (tclistnum(list))
		tclistdel(do_tcadbmisc(adb, command, list));

	tclistdel(list);
	free(value);
}
示例#5
0
static void fwmkeys_test(void *db, int num, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	char prefix[KEYGEN_PREFIX_SIZE + 1];
	TCLIST *list;

	keygen_init(&keygen, seed);

	list = tcadbfwmkeys2(adb, keygen_prefix(&keygen, prefix), -1);
	check_keys(list, num, seed);

	tclistdel(list);
}
示例#6
0
static void put_test(void *db, int num, int vsiz, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	char *value = xmalloc(vsiz);
	int i;

	keygen_init(&keygen, seed);

	for (i = 0; i < num; i++) {
		const char *key = keygen_next_key(&keygen);

		tcadbput(adb, key, strlen(key), value, vsiz);
	}

	free(value);
}
示例#7
0
static void rangeout_test(void *db, const char *command, int num, int vsiz,
			int batch, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	TCLIST *args = tclistnew();
	char start_key[KEYGEN_PREFIX_SIZE + 1];
	char max[100];
	char end_key[KEYGEN_PREFIX_SIZE + 1];
	char binc[2];

	keygen_init(&keygen, seed);

	keygen_prefix(&keygen, start_key);
	sprintf(max, "%d", batch);
	keygen_prefix(&keygen, end_key);
	end_key[KEYGEN_PREFIX_SIZE - 1] = '-' + 1;
	sprintf(binc, "0");

	tclistpush2(args, start_key);
	tclistpush2(args, max);
	tclistpush2(args, end_key);
	tclistpush2(args, binc);

	while (1) {
		TCLIST *recs;

		recs = do_tcadbmisc(adb, command, args);
		if (tclistnum(recs) == 0)
			break;

		if (debug) {
			const char *num_recs = tclistval2(recs, 0);
			num -= atoi(num_recs);
			if (num != 0 && atoi(num_recs) != batch)
				die("Unexpected number of records are deleted");
		}

		tclistdel(recs);
	}
	if (debug && num != 0)
		die("Unexpected number of records are deleted");

	tclistdel(args);
}
示例#8
0
static void range_atomic_test(void *db, int num, int vsiz, int batch,
			unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	TCLIST *args = tclistnew();
	char start_key[KEYGEN_PREFIX_SIZE + 1];
	char max[100];
	char end_key[KEYGEN_PREFIX_SIZE + 1];
	char binc[2];

	keygen_init(&keygen, seed);

	keygen_prefix(&keygen, start_key);
	sprintf(max, "%d", batch);
	keygen_prefix(&keygen, end_key);
	end_key[KEYGEN_PREFIX_SIZE - 1] = '-' + 1;
	sprintf(binc, "0");

	tclistpush2(args, start_key);
	tclistpush2(args, max);
	tclistpush2(args, end_key);
	tclistpush2(args, binc);

	while (1) {
		TCLIST *recs;
		int num_recs;

		recs = do_tcadbmisc(adb, "range_atomic", args);
		num_recs = tclistnum(recs) / 2;
		if (!num_recs)
			break;

		check_records(recs, &keygen, vsiz, num < batch ? num : batch);
		tclistover2(args, 0, tclistval2(recs, 2 * (num_recs - 1)));
		tclistdel(recs);
		num -= num_recs;
	}
	if (debug && num)
		die("Unexpected record num: %d", num);

	tclistdel(args);
}
示例#9
0
static void get_test(void *db, int num, int vsiz, unsigned int seed)
{
	TCADB *adb = db;
	struct keygen keygen;
	int i;

	keygen_init(&keygen, seed);

	for (i = 0; i < num; i++) {
		const char *key = keygen_next_key(&keygen);
		void *value;
		int siz;

		value = tcadbget(adb, key, strlen(key), &siz);
		if (debug && vsiz != siz)
			die("Unexpected value size: %d", siz);
			
		free(value);
	}
}
示例#10
0
static void check_keys(TCLIST *list, int num, unsigned int seed)
{
	int i;
	struct keygen keygen;

	if (!debug)
		return;

	keygen_init(&keygen, seed);

	if (num != tclistnum(list))
		die("Unexpected key num: %d", tclistnum(list));

	for (i = 0; i < num; i++) {
		int ksiz;
		const char *key = tclistval(list, i, &ksiz);

		if (strncmp(keygen_next_key(&keygen), key, ksiz))
			die("Unexpected key");
	}
}
示例#11
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);


}
示例#12
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;
}