Exemple #1
0
BOOL aes_init(aes* a,int mode,int nk,char *key,char *iv)
{   /* Key=nk bytes */
    /* currently NB,nk = 16, 24 or 32          */
    /* Key Scheduler. Create expanded encryption key */
    int i,j,k,N,nr;
    WORD CipherKey[8];

    nk/=4;

    if (nk!=4 && nk!=6 && nk!=8) return FALSE;

    /* nr is number of rounds */
    nr=6+nk;

    a->Nk=nk;
    a->Nr=nr;

    aes_reset(a,mode,iv);

    N=NB*(nr+1);

    for (i=j=0; i<nk; i++,j+=4)
    {
        CipherKey[i]=pack((BYTE *)&key[j]);
    }
    for (i=0; i<nk; i++) a->fkey[i]=CipherKey[i];
    for (j=nk,k=0; j<N; j+=nk,k++)
    {
        a->fkey[j]=a->fkey[j-nk]^SubByte(ROTL24(a->fkey[j-1]))^rco[k];
        if (nk<=6)
        {
            for (i=1; i<nk && (i+j)<N; i++)
                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
        }
        else
        {
            for (i=1; i<4 && (i+j)<N; i++)
                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
            if ((j+4)<N) a->fkey[j+4]=a->fkey[j+4-nk]^SubByte(a->fkey[j+3]);
            for (i=5; i<nk && (i+j)<N; i++)
                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
        }
    }

    /* now for the expanded decrypt key in reverse order */

    for (j=0; j<NB; j++) a->rkey[j+N-NB]=a->fkey[j];
    for (i=NB; i<N-NB; i+=NB)
    {
        k=N-NB-i;
        for (j=0; j<NB; j++) a->rkey[k+j]=InvMixCol(a->fkey[i+j]);
    }
    for (j=N-NB; j<N; j++) a->rkey[j-N+NB]=a->fkey[j];

    return TRUE;
}
Exemple #2
0
/* Initialise cipher */
int MCL_AES_init(mcl_aes* a,int mode,int nk,char *key,char *iv)
{ /* Key length Nk=16, 24 or 32 bytes */
  /* Key Scheduler. Create expanded encryption key */
    int i,j,k,N,nr;
    unsign32 CipherKey[8];
    
    nk/=4;

	if (nk!=4 && nk!=6 && nk!=8) return 0;

	nr=6+nk;

    a->Nk=nk; a->Nr=nr;

    MCL_AES_reset(a,mode,iv);

    N=NB*(nr+1);
    
    for (i=j=0;i<nk;i++,j+=4)
    {
        CipherKey[i]=pack((uchar *)&key[j]);
    }
    for (i=0;i<nk;i++) a->fkey[i]=CipherKey[i];
    for (j=nk,k=0;j<N;j+=nk,k++)
    {
        a->fkey[j]=a->fkey[j-nk]^SubByte(ROTL24(a->fkey[j-1]))^rco[k];
		if (nk<=6)
		{
			for (i=1;i<nk && (i+j)<N;i++)
				a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
		}
		else
        {
            for (i=1;i<4 && (i+j)<N;i++)
                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
            if ((j+4)<N) a->fkey[j+4]=a->fkey[j+4-nk]^SubByte(a->fkey[j+3]);
            for (i=5;i<nk && (i+j)<N;i++)
                a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];
        }		
  
	}
 /* now for the expanded decrypt key in reverse order */

    for (j=0;j<NB;j++) a->rkey[j+N-NB]=a->fkey[j]; 
    for (i=NB;i<N-NB;i+=NB)
    {
        k=N-NB-i;
        for (j=0;j<NB;j++) a->rkey[k+j]=InvMixCol(a->fkey[i+j]);
    }
    for (j=N-NB;j<N;j++) a->rkey[j-N+NB]=a->fkey[j];
	return 1;
}
Exemple #3
0
//加密运算,inputSize必须为128,192,256位
void Encrypt(RijndaelContextPtr context,void* input)
{
	unsigned int round;
	//明文的最大长度
	unsigned long state[8] = {0};
	unsigned char* inputStringPtr = (unsigned char*)input;
	unsigned int nb = context->nb;
	//经过这个过程,由a0 a1 a2 a3 a4 a5组成的字节序列就组成了
	// a0 a4 a8  a12 a16 ...
	// a1 a5 a9  a13 a17 ...
	// a2 a6 a10 a14 a18 ...
	// a3 a7 a11 a15 a19 ...
	//这样的字节矩阵
	state[0] = GETINT32(inputStringPtr,0,nb);
	state[1] = GETINT32(inputStringPtr,1,nb);
	state[2] = GETINT32(inputStringPtr,2,nb);
	state[3] = GETINT32(inputStringPtr,3,nb);
	if (nb >= (KeySize_192/4))
	{
		state[4] = GETINT32(inputStringPtr,4,nb);
		state[5] = GETINT32(inputStringPtr,5,nb);
	}

	if (nb >= (KeySize_256/4))
	{
		state[6] = GETINT32(inputStringPtr,6,nb);
		state[7] = GETINT32(inputStringPtr,7,nb);
	}
	AddRoundKey(context,0,state);
	for (round = 1; round<context->nr; round++)
	{
		SubByte(context,state);
		ShiftRow(context,state);
		MixColumn(context,state);
		AddRoundKey(context,round,state);
	}
	SubByte(context,state);
	ShiftRow(context,state);
	AddRoundKey(context,context->nr,state);
	//就这个格式输出的密文,其实也没有关系的
	//将格式纠正
	StateToChars((unsigned char*)input,state,context->nb);
}
Exemple #4
0
/* Initialise cipher */
void AES_init(aes* a,int mode,char *key,char *iv)
{ /* Key=16 bytes */
  /* Key Scheduler. Create expanded encryption key */
    int i,j,k,N,nk;
    unsign32 CipherKey[4];

    nk=4;

    AES_reset(a,mode,iv);

    N=NB*(ROUNDS+1);

    for (i=j=0;i<nk;i++,j+=4)
    {
        CipherKey[i]=pack((uchar *)&key[j]);
    }
    for (i=0;i<nk;i++) a->fkey[i]=CipherKey[i];
    for (j=nk,k=0;j<N;j+=nk,k++)
    {
        a->fkey[j]=a->fkey[j-nk]^SubByte(ROTL24(a->fkey[j-1]))^rco[k];

        for (i=1;i<nk && (i+j)<N;i++)
            a->fkey[i+j]=a->fkey[i+j-nk]^a->fkey[i+j-1];

	}
 /* now for the expanded decrypt key in reverse order */

    for (j=0;j<NB;j++) a->rkey[j+N-NB]=a->fkey[j];
    for (i=NB;i<N-NB;i+=NB)
    {
        k=N-NB-i;
        for (j=0;j<NB;j++) a->rkey[k+j]=InvMixCol(a->fkey[i+j]);
    }
    for (j=N-NB;j<N;j++) a->rkey[j-N+NB]=a->fkey[j];

}
Exemple #5
0
void gkey(int nb,int nk,char *key)
{ /* blocksize=32*nb bits. Key=32*nk bits */
  /* currently nb,bk = 4, 6 or 8          */
  /* key comes as 4*Nk bytes              */
  /* Key Scheduler. Create expanded encryption key */
    int i,j,k,m,N;
    int C1,C2,C3;
    u32 CipherKey[8];

    Nb=nb; Nk=nk;

  /* Nr is number of rounds */
    if (Nb>=Nk) Nr=6+Nb;
    else        Nr=6+Nk;

    C1=1;
    if (Nb<8) { C2=2; C3=3; }
    else      { C2=3; C3=4; }

  /* pre-calculate forward and reverse increments */
    for (m=j=0;j<nb;j++,m+=3)
    {
        fi[m]=(j+C1)%nb;
        fi[m+1]=(j+C2)%nb;
        fi[m+2]=(j+C3)%nb;
        ri[m]=(nb+j-C1)%nb;
        ri[m+1]=(nb+j-C2)%nb;
        ri[m+2]=(nb+j-C3)%nb;
    }

    N=Nb*(Nr+1);

    for (i=j=0;i<Nk;i++,j+=4)
    {
        CipherKey[i]=pack((u8 *)&key[j]);
    }
    for (i=0;i<Nk;i++) fkey[i]=CipherKey[i];
    for (j=Nk,k=0;j<N;j+=Nk,k++)
    {
        fkey[j]=fkey[j-Nk]^SubByte(ROTL24(fkey[j-1]))^rco[k];
        if (Nk<=6)
        {
            for (i=1;i<Nk && (i+j)<N;i++)
                fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
        }
        else
        {
            for (i=1;i<4 &&(i+j)<N;i++)
                fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
            if ((j+4)<N) fkey[j+4]=fkey[j+4-Nk]^SubByte(fkey[j+3]);
            for (i=5;i<Nk && (i+j)<N;i++)
                fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
        }

    }

 /* now for the expanded decrypt key in reverse order */

    for (j=0;j<Nb;j++) rkey[j+N-Nb]=fkey[j];
    for (i=Nb;i<N-Nb;i+=Nb)
    {
        k=N-Nb-i;
        for (j=0;j<Nb;j++) rkey[k+j]=InvMixCol(fkey[i+j]);
    }
    for (j=N-Nb;j<N;j++) rkey[j-N+Nb]=fkey[j];
}
Exemple #6
0
void Compare(byte* Register, byte Operand) {
	byte Temp=*Register;
	SubByte(&Temp, Operand);
        FlagN=1;
	TStates+=4;
}
/*获得密钥*/
void gkey(int nb,int nk,char *key)
{
	int i,j,k,m,N;
	int C1,C2,C3;
	WORD CipherKey[8];
	Nb=nb;
	Nk=nk;
/* Nr is number of rounds Nr是加密的轮数*/
	if (Nb>=Nk)
		Nr=6+Nb;
	else
		Nr=6+Nk;
	C1=1;
	if (Nb<8)
	{
		C2=2;
		C3=3;
	}
	else
	{
		C2=3;
		C3=4;
	}
	for (m=j=0;j<nb;j++,m+=3)
{
		fi[m]=(j+C1)%nb;
		fi[m+1]=(j+C2)%nb;
		fi[m+2]=(j+C3)%nb;
		ri[m]=(nb+j-C1)%nb;
		ri[m+1]=(nb+j-C2)%nb;
		ri[m+2]=(nb+j-C3)%nb;
	}
	N=Nb*(Nr+1);
	for (i=j=0;i<Nk;i++,j+=4)
	{
		CipherKey[i]=pack((BYTE *)&key[j]);
	}
	for (i=0;i<Nk;i++)
		fkey[i]=CipherKey[i];
	for (j=Nk,k=0;j<N;j+=Nk,k++)
	{
		fkey[j]=fkey[j-Nk]^SubByte(ROTL24(fkey[j-1]))^rco[k];
		if (Nk<=6)
		{
			for (i=1;i<Nk && (i+j)<N;i++)
				fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
		}
		else
		{
			for (i=1;i<4 &&(i+j)<N;i++)
				fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
			if ((j+4)<N)
				fkey[j+4]=fkey[j+4-Nk]^SubByte(fkey[j+3]);
			for (i=5;i<Nk && (i+j)<N;i++)
				fkey[i+j]=fkey[i+j-Nk]^fkey[i+j-1];
		}
	}
	for (j=0;j<Nb;j++)
		rkey[j+N-Nb]=fkey[j];
	for (i=Nb;i<N-Nb;i+=Nb)
	{
		k=N-Nb-i;
		for (j=0;j<Nb;j++)
			rkey[k+j]=InvMixCol(fkey[i+j]);
	}
	for (j=N-Nb;j<N;j++)
		rkey[j-N+Nb]=fkey[j];
}
Exemple #8
0
/*  **************** key expand ************************ */
int
KeySchedule (int type, int key[32])
{
  unsigned nk, nb, round;
  unsigned i, j;
  int temp[4];
  switch (type)
    {
    case 128128:
      nk = 4;
      nb = 4;
      round = 10;
      break;
    case 128192:
      nk = 4;
      nb = 6;
      round = 12;
      break;
    case 128256:
      nk = 4;
      nb = 8;
      round = 14;
      break;
    case 192128:
      nk = 6;
      nb = 4;
      round = 12;
      break;
    case 192192:
      nk = 6;
      nb = 6;
      round = 12;
      break;
    case 192256:
      nk = 6;
      nb = 8;
      round = 14;
      break;
    case 256128:
      nk = 8;
      nb = 4;
      round = 14;
      break;
    case 256192:
      nk = 8;
      nb = 6;
      round = 14;
      break;
    case 256256:
      nk = 8;
      nb = 8;
      round = 14;
      break;
    default:
      return -1;
    }
  for (j = 0; j < nk; ++j)
    for (i = 0; i < 4; ++i)
/* 0 word */
      word[i][j] = key[i + j * 4];

/* expanded key is generated */
  for (j = nk; j < nb * (round + 1); ++j)
    {

/* RotByte */
      if ((j % nk) == 0)
	{
	  temp[0] = SubByte (word[1][j - 1]) ^ Rcon0[(j / nk) - 1];
	  temp[1] = SubByte (word[2][j - 1]);
	  temp[2] = SubByte (word[3][j - 1]);
	  temp[3] = SubByte (word[0][j - 1]);
	}
      if ((j % nk) != 0)
	{
	  temp[0] = word[0][j - 1];
	  temp[1] = word[1][j - 1];
	  temp[2] = word[2][j - 1];
	  temp[3] = word[3][j - 1];
	}
      if (nk > 6 && j % nk == 4)
	for (i = 0; i < 4; ++i)
	  temp[i] = SubByte (temp[i]);
      for (i = 0; i < 4; ++i)
	word[i][j] = word[i][j - nk] ^ temp[i];
    }
  return 0;
}