Exemple #1
0
void Rijndael::GenerateTables()
{
  unsigned char pow[512],log[256];
  int i = 0, w = 1; 
  do
  {   
    pow[i] = (byte)w;
    pow[i + 255] = (byte)w;
    log[w] = (byte)i++;
    w ^=  (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  } while (w != 1);
 
  for (int i = 0,w = 1; i < sizeof(rcon)/sizeof(rcon[0]); i++)
  {
    rcon[i] = w;
    w = (w << 1) ^ (w & ff_hi ? ff_poly : 0);
  }
  for(int i = 0; i < 256; ++i)
  {   
    unsigned char b=S[i]=fwd_affine(FFinv((byte)i));
    T1[i][1]=T1[i][2]=T2[i][2]=T2[i][3]=T3[i][0]=T3[i][3]=T4[i][0]=T4[i][1]=b;
    T1[i][0]=T2[i][1]=T3[i][2]=T4[i][3]=FFmul02(b);
    T1[i][3]=T2[i][0]=T3[i][1]=T4[i][2]=FFmul03(b);
    S5[i] = b = FFinv(inv_affine((byte)i));
    U1[b][3]=U2[b][0]=U3[b][1]=U4[b][2]=T5[i][3]=T6[i][0]=T7[i][1]=T8[i][2]=FFmul0b(b);
    U1[b][1]=U2[b][2]=U3[b][3]=U4[b][0]=T5[i][1]=T6[i][2]=T7[i][3]=T8[i][0]=FFmul09(b);
    U1[b][2]=U2[b][3]=U3[b][0]=U4[b][1]=T5[i][2]=T6[i][3]=T7[i][0]=T8[i][1]=FFmul0d(b);
    U1[b][0]=U2[b][1]=U3[b][2]=U4[b][3]=T5[i][0]=T6[i][1]=T7[i][2]=T8[i][3]=FFmul0e(b);
  }
}
Exemple #2
0
void
Flash_decrypt(unsigned char out[37], unsigned char in[37], Flash_Complete_Key PK)
{
	unsigned char u[37], tmp[37], tmp2[37], tmp3[37], tmp4[37];
	int i,j;

	unsigned char temp,zz[73];

	/*inv_affine correspond à T^-1 */
	inv_affine(out,in,PK->IT1,PK->T2,PK->M);

	memset(zz,0,73);
	for(i=0;i<37;i++)
	{
			zz[2*i] = PK->M[(((unsigned long) out[i])<<7) + (unsigned long) out[i]];
	}
	for(i=72;i>36;i--)
	{
		if(zz[i])
		{
			temp = zz[i];
			zz[i-(37-12)] ^= temp;
			zz[i-(37-10)] ^= temp;
			zz[i-(37- 2)] ^= temp;
			zz[i-(37- 0)] ^= temp;
		}
	}
	memcpy(u,zz,37);
	memset(zz,0,73);
	for(i=0;i<37;i++)
	{
			zz[2*i] = PK->M[(((unsigned long) u[i])<<7) + (unsigned long) u[i]];
	}
	for(i=72;i>36;i--)
	{
		if(zz[i])
		{
			temp = zz[i];
			zz[i-(37-12)] ^= temp;
			zz[i-(37-10)] ^= temp;
			zz[i-(37- 2)] ^= temp;
			zz[i-(37- 0)] ^= temp;
		}
	}
	memcpy(u,zz,37);
	//sqsq37(u,out,PK->M);
	//sqsq37(u,u,PK->M);

	memset(zz,0,73);

	for(i=0;i<37;i++)
	{
		for(j=0;j<37;j++)
		{
			zz[i+j] ^= PK->M[(((unsigned long) u[i])<<7) + (unsigned long) out[j]];
		}
	}

	for(i=72;i>36;i--)
	{
		if(zz[i])
		{
			temp = zz[i];
			zz[i-(37-12)] ^= temp;
			zz[i-(37-10)] ^= temp;
			zz[i-(37- 2)] ^= temp;
			zz[i-(37- 0)] ^= temp;
		}
	}

	memcpy(tmp,zz,37);
//	mul37(tmp,u,out,PK->M);

	memset(zz,0,73);
	for(i=0;i<37;i++)
	{
			zz[2*i] = PK->M[(((unsigned long) u[i])<<7) + (unsigned long) u[i]];
	}
	for(i=72;i>36;i--)
	{
		if(zz[i])
		{
			temp = zz[i];
			zz[i-(37-12)] ^= temp;
			zz[i-(37-10)] ^= temp;
			zz[i-(37- 2)] ^= temp;
			zz[i-(37- 0)] ^= temp;
		}
	}
	memcpy(out,zz,37);
	memset(zz,0,73);
	for(i=0;i<37;i++)
	{
			zz[2*i] = PK->M[(((unsigned long) out[i])<<7) + (unsigned long) out[i]];
	}
	for(i=72;i>36;i--)
	{
		if(zz[i])
		{
			temp = zz[i];
			zz[i-(37-12)] ^= temp;
			zz[i-(37-10)] ^= temp;
			zz[i-(37- 2)] ^= temp;
			zz[i-(37- 0)] ^= temp;
		}
	}
	memcpy(out,zz,37);
	//sqsq37(out,u,PK->M);
	//sqsq37(out,out,PK->M);
	
	memset(zz,0,73);
	for(i=0;i<37;i++)
	{
		for(j=0;j<37;j++)
		{
			zz[i+j] ^= PK->M[(((unsigned long) tmp[i])<<7) + (unsigned long) out[j]];
		}
	}
	for(i=72;i>36;i--)
	{
		if(zz[i])
		{
			temp = zz[i];
			zz[i-(37-12)] ^= temp;
			zz[i-(37-10)] ^= temp;
			zz[i-(37- 2)] ^= temp;
			zz[i-(37- 0)] ^= temp;
		}
	}
	memcpy(tmp2,zz,37);
	//mul37(tmp2,tmp,out,PK->M);

	sqsq37(tmp4,tmp2,PK->M);
	mul37(tmp4,tmp4,tmp2,PK->M);
	
	sqsq37(tmp2,out,PK->M);
	sqsq37(tmp2,tmp2,PK->M);

	//lineaire(u,tmp2,PK->M2);	
	sqsq37(u,tmp2,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	mul37(tmp,u,tmp4,PK->M);

	sqsq37(u,tmp,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	mul37(tmp3,u,tmp,PK->M);
	sqsq37(u,tmp3,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	mul37(tmp3,u,tmp,PK->M);
	sqsq37(u,tmp3,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);
	sqsq37(u,u,PK->M);

	mul37(tmp4,tmp4,u,PK->M);
	//lineaire(tmp3,tmp4,PK->M2);
	sqsq37(tmp3,tmp4,PK->M);
	sqsq37(tmp3,tmp3,PK->M);
	sqsq37(tmp3,tmp3,PK->M);
	sqsq37(tmp3,tmp3,PK->M);
	sqsq37(tmp3,tmp3,PK->M);
	sqsq37(tmp3,tmp3,PK->M);
	sqsq37(tmp4,tmp3,PK->M);
	lineaire(tmp3,tmp4,PK->M1);

	lineaire(tmp,tmp2,PK->M1);
	mul37(tmp4,tmp,u,PK->M);
	mul37(out,tmp4,tmp2,PK->M);
	mul37(tmp4,out,tmp3,PK->M);

	lineaire(tmp2,tmp4,PK->M1);
	//lineaire(tmp,tmp2,PK->M2);
	lineaire(tmp,tmp2,PK->M1);
	sqsq37(tmp,tmp,PK->M);
	sqsq37(tmp,tmp,PK->M);
	sqsq37(tmp,tmp,PK->M);
	sqsq37(tmp,tmp,PK->M);
	sqsq37(tmp,tmp,PK->M);
	sqsq37(tmp,tmp,PK->M);
	sqsq37(tmp,tmp,PK->M);
	mul37(tmp2,tmp,tmp3,PK->M);
	lineaire(tmp,tmp2,PK->M1);
	mul37(tmp2,tmp,tmp4,PK->M);

	/*multiplication matricielle par M1 qui représente la mise à la puissance 2^7^10*/
	/*Mis à la puissance 64*/
	/*mis à la puissance 2^77-1
	exponent(u,tmp,exp2,77,PK->M);
	for(i=0;i<18;i++)
	{
		/*mis à la puissance 2^7^22 de tmp 
		lineaire(out,tmp,PK->M2,PK->M);
		/*
		mul37(tmp,out,u,PK->M);
	}
   /* inversion affine de S*/	
	inv_affine(out,tmp2,PK->IS1,PK->S2,PK->M);
}