예제 #1
0
void checkEqsALL(std::string eq, int round, u64 *fixPL, u64 *fixPR, float &result, int fixn, int blocksize, int keysize){

	int totalcount = 0;
	int hit = 0;
	
	while (totalcount < 1000){

		// do simon encryption with random key
		u64 key[4] = {0};
		u64 *PL, *PR, *CL, *CR;
		PL = (u64*)malloc(sizeof(u64)*fixn);
		PR = (u64*)malloc(sizeof(u64)*fixn);
		CL = (u64*)malloc(sizeof(u64)*fixn);
		CR = (u64*)malloc(sizeof(u64)*fixn);

		u64 **ZR, **ZL;
		ZR = (u64 **) malloc(fixn*sizeof(u64*));
		ZL = (u64 **) malloc(fixn*sizeof(u64*));
		
		//TODO:: adjust keysize based on version

		key[0] = randu32();
		key[1] = randu32();
		key[2] = randu32();
		key[3] = randu32();

		for (int i = 0; i < fixn; i++){
			ZL[i] = (u64 *) malloc(round*sizeof(u64));
			ZR[i] = (u64 *) malloc(round*sizeof(u64));

			PL[i] = fixPL[i];
			PR[i] = fixPR[i];
			SimonEncryptBlockALL(PL[i],PR[i],CL[i],CR[i],ZR[i],ZL[i],key,blocksize,keysize,round);
		}

		// now we have all ZR[i][j], CL[i], CR[i], PL[i], PR[i] ... 

		// parse eq to find the right value
		int res = 0;
		char ** tokens = split(eq,'+');

		if (tokens)
		{
			for (int i = 0; *(tokens + i); i++)
			{		
				if (strstr(*(tokens + i), "k") != NULL ){
					char ** splitByU = split(*(tokens+i),'_');
					if (splitByU+1){
						int k_index = atoi(*(splitByU+1));
						// TODO change based on version!!!
						int k_part = k_index / 32;   
						int k_j = k_index % 32;
						//printf("K %d %d %d \n",k_index,k_part,k_j);
						res += getValueALL(key[k_part],k_j);
					}
				}
				// ZR1_003_025 
				else if (strstr(*(tokens + i), "Z") != NULL ){

					char ** splitByU = split(*(tokens+i),'_');
					int index1,index2,index3;

					index1 = atoi(*(splitByU)+2);
					index2 = atoi(*(splitByU+1));
					index3 = atoi(*(splitByU+2));
					//printf("ZR %d %d %d \n", index1, index2, index3);
					res+=getValueALL(ZR[index1][index2],index3);
				}
				else if (strstr(*(tokens + i), "C") != NULL){
					char ** splitByU = split(*(tokens+i),'_');
					int index1,index2;

					index1 = atoi(*(splitByU)+2);
					index2 = atoi(*(splitByU+1));
					//printf("CR %d %d \n", index1, index2);
					res += getValueALL(CR[index1],index2);
				}else {
					if (atoi(*(tokens+i))==1){
						res+=1;
					}
				}
			}
		}

		// mod 2 check equation
		if ((res%2)==0){
			hit++;
		}
		totalcount++;
		result = hit / totalcount * 100;
	}
	if (result > 0){
		printf("%s %4.2f\%\n",eq.c_str(), result);
	}
예제 #2
0
파일: random.c 프로젝트: dlubarov/k2
u32 randu32_range(u32 lower, u32 upper)
{
  return lower + randu32() % upper;
}
예제 #3
0
// fix 3 version
void checkEqs(std::string eq, int round, u32 *fixPL, u32 *fixPR, float &result){

	int totalcount = 0;
	int hit = 0;
	
	while (totalcount < 1000){

		// do simon encryption with random key
		u32 key[4] = {0};
		u32 PL[3] = {0}, PR[3] = {0}, CL[3]={0}, CR[3]={0};

		u32 **ZR, **ZL;
		ZR = (u32 **) malloc(3*sizeof(u32*));
		ZL = (u32 **) malloc(3*sizeof(u32*));

		key[0] = randu32();
		key[1] = randu32();
		key[2] = randu32();
		key[3] = randu32();

		for (int i = 0; i < 3; i++){
			ZL[i] = (u32 *) malloc(round*sizeof(u32));
			ZR[i] = (u32 *) malloc(round*sizeof(u32));

			PL[i] = fixPL[i];
			PR[i] = fixPR[i];
			SimonEncryptBlock64128(PL[i],PR[i],CL[i],CR[i],ZR[i],ZL[i],key,32,128,round);
		}

		// now we have all ZR[i][j], CL[i], CR[i], PL[i], PR[i] ... 

		// parse eq to find the right value
		int res = 0;
		char ** tokens = split(eq,'+');

		if (tokens)
		{
			for (int i = 0; *(tokens + i); i++)
			{		
				if (strstr(*(tokens + i), "k") != NULL ){
					char ** splitByU = split(*(tokens+i),'_');
					if (splitByU+1){
						int k_index = atoi(*(splitByU+1));
						int k_part = k_index / 32;
						int k_j = k_index % 32;
						//printf("K %d %d %d \n",k_index,k_part,k_j);
						res += getValue(key[k_part],k_j);
					}
				}
				// ZR1_003_025 
				else if (strstr(*(tokens + i), "Z") != NULL ){

					char ** splitByU = split(*(tokens+i),'_');
					int index1,index2,index3;

					index1 = atoi(*(splitByU)+2);
					index2 = atoi(*(splitByU+1));
					index3 = atoi(*(splitByU+2));
					//printf("ZR %d %d %d \n", index1, index2, index3);
					res+=getValue(ZR[index1][index2],index3);
				}
				else if (strstr(*(tokens + i), "C") != NULL){
					char ** splitByU = split(*(tokens+i),'_');
					int index1,index2;

					index1 = atoi(*(splitByU)+2);
					index2 = atoi(*(splitByU+1));
					//printf("CR %d %d \n", index1, index2);
					res += getValue(CR[index1],index2);
				}else {
					if (atoi(*(tokens+i))==1){
						res+=1;
					}
				}
			}
		}

		// mod 2 check equation
		if ((res%2)==0){
			hit++;
		}
		totalcount++;
		result = hit / totalcount * 100;
	}
	printf("%s %4.2f\%\n",eq.c_str(), result);
}
예제 #4
0
파일: random.c 프로젝트: dlubarov/k2
u32 randu32_limit(u32 upper)
{
  return randu32() % upper;
}