示例#1
0
unsigned char* AES::InvCipher(unsigned char* input)
{
	unsigned char state[4][4];
	int i,r,c;

	for(r=0; r<4; r++)
	{
		for(c=0; c<4 ;c++)
		{
			state[r][c] = input[c*4+r];
		}
	}

	AddRoundKey(state, w[10]);
	for(i=9; i>=0; i--)
	{
		InvShiftRows(state);
		InvSubBytes(state);
		AddRoundKey(state, w[i]);
		if(i)
		{
			InvMixColumns(state);
		}
	}
	
	for(r=0; r<4; r++)
	{
		for(c=0; c<4 ;c++)
		{
			input[c*4+r] = state[r][c];
		}
	}

	return input;
}
示例#2
0
void AES_dec(BYTE* input,BYTE* output,BYTE *w,BYTE Nr)
{
	BYTE gState[4][4];
	DWORD i,round;
	AES_Memset(&gState[0][0],0,16);
	for ( i = 0; i < (4 * NB); i++)
	{
		gState[i % 4][ i / 4] = input[i];
	}
	
	AddRoundKey(Nr,w,gState);
	
	for ( round = Nr-1; round >= 1; round--)  // main round loop
	{
        InvShiftRows(gState);
        InvSubBytes(gState);
        AddRoundKey(round,w,gState);
        InvMixColumns(gState);
	}  // end main round loop for InvCipher
	
	InvShiftRows(gState);
	InvSubBytes(gState);
	AddRoundKey(0,w,gState);
	
	// output = state
	for (i = 0; i < (4 * NB); i++)
	{
        output[i] =  gState[i % 4][ i / 4];
	}
}
示例#3
0
文件: 1.cpp 项目: Lirida/AES_console
void InvCipher()
{
int i, j, round=0;

	for(i=0; i<4; i++)
	{
		for(j=0; j<4; j++)
		{
			state[j][i] = in[i*4 + j];
		}
	}

	AddRoundKey(Nr); 

	for(round=Nr-1; round>0; round--)
	{
		InvShiftRows();
		InvSubBytes();
		AddRoundKey(round);
		InvMixColumns();
	}
	InvShiftRows();
	InvSubBytes();
	AddRoundKey(0);
	
	for(i=0; i<4; i++)
	{
		for(j=0; j<4; j++)
		{
			out[i*4+j]=state[j][i];
		}
	}
}
示例#4
0
void aeslite_break(unsigned char plaintext[], unsigned char ciphertext[], unsigned char out[])
{
   unsigned char plain[4][4];
   unsigned char cipher[4][4];

   // Copy the input to the plain.
   build_state_array_from_string(plaintext, plain);
   build_state_array_from_string(ciphertext, cipher);
   
   InvMixColumns(cipher);
   InvShiftRows(cipher);
   InvSubBytes(cipher);

   // Copy the state to the output array
   out[0] = plain[0][0] ^ cipher[0][0];
   out[1] = plain[1][0] ^ cipher[1][0];
   out[2] = plain[2][0] ^ cipher[2][0];
   out[3] = plain[3][0] ^ cipher[3][0];
   out[4] = plain[0][1] ^ cipher[0][1];
   out[5] = plain[1][1] ^ cipher[1][1];
   out[6] = plain[2][1] ^ cipher[2][1];
   out[7] = plain[3][1] ^ cipher[3][1];
   out[8] = plain[0][2] ^ cipher[0][2];
   out[9] = plain[1][2] ^ cipher[1][2];
   out[10] = plain[2][2] ^ cipher[2][2];
   out[11] = plain[3][2] ^ cipher[3][2];
   out[12] = plain[0][3] ^ cipher[0][3];
   out[13] = plain[1][3] ^ cipher[1][3];
   out[14] = plain[2][3] ^ cipher[2][3];
   out[15] = plain[3][3] ^ cipher[3][3];
}
示例#5
0
void
AES_CPU_Impl_O0::DecryptBlock(UINT8 *dst, const UINT8 *src)
{
    UINT8 m_state[m_Nb][4];
    for( UINT32 i = 0; i < 4; i++ )
        for( UINT32 j = 0; j < 4; j++ )
            m_state[j][i] = src[i*4 + j];

    // Add the First round key to the state before starting the rounds.
    AddRoundKey(m_state, m_Nr); 

    // There will be m_Nr rounds.
    // The first m_Nr-1 rounds are identical.
    // These m_Nr-1 rounds are executed in the loop below.
    for( UINT32 round = m_Nr-1; round > 0; round-- )
    {
        InvShiftRows(m_state);
        InvSubBytes(m_state);
        AddRoundKey(m_state, round);
        InvMixColumns(m_state);
    }

    // The last round is given below.
    // The MixColumns function is not here in the last round.
    InvShiftRows(m_state);
    InvSubBytes(m_state);
    AddRoundKey(m_state, 0);

    for( UINT32 i = 0; i < 4; i++ )
        for( UINT32 j = 0; j < 4; j++ )
            dst[i*4 + j] = m_state[j][i];
}
示例#6
0
文件: aeslib.c 项目: altuzar/aesenc
static void decryptBlock(unsigned char *in, unsigned char *out, 
						 unsigned char *w, int Nr)
{
	int round, i, j, n = 0;
	state_t state;

	for (i = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			state[i][j] = in[n++];
		}
	}

	AddRoundKey(&state, w, Nr);

	for (round = Nr-1; round > 0; round--) {
		SubInvShiftRows(&state);
		AddRoundKey(&state, w, round);
		InvMixColumns(&state);
	}

	SubInvShiftRows(&state);
	AddRoundKey(&state, w, 0);

	for (i = 0, n = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			out[n++] = state[i][j];
		}
	}
}
示例#7
0
文件: aes.c 项目: SamuelXu/clibs
void aes_invcipher(ctx_aes* aes, uint8_t* input, uint8_t* output)	 // decipher 16-bit input
{
	// state = input
	int i; 
	int round;
	memset(&aes->State[0][0],0,16);
	for (i = 0; i < (4 * aes->Nb); i++)
	{
		aes->State[i % 4][ i / 4] = input[i];
	}
	
	AddRoundKey(aes, aes->Nr);
	
	for (round = aes->Nr-1; round >= 1; round--)  // main round loop
	{
        InvShiftRows(aes);
        InvSubBytes(aes);
        AddRoundKey(aes, round);
        InvMixColumns(aes);
	}  // end main round loop for InvCipher
	
	InvShiftRows(aes);
	InvSubBytes(aes);
	AddRoundKey(aes, 0);
	
	// output = state
	for (i = 0; i < (4 * aes->Nb); i++)
	{
        output[i] =  aes->State[i % 4][ i / 4];
	}
	
}  // InvCipher()
示例#8
0
文件: aes.c 项目: LinLL/ipc
void InvCipher(unsigned char * input, unsigned char * output)  // decipher 16-bit input
{
	// state = input
	int i, round; 
	memset(&State[0][0],0,16);
	for (i = 0; i < (4 * Nb); i++)
	{
		State[i % 4][ i / 4] = input[i];
	}

	AddRoundKey(Nr);

	for (round = Nr-1; round >= 1; round--)  // main round loop
	{
		InvShiftRows();
		InvSubBytes();
		AddRoundKey(round);
		InvMixColumns();
	}  // end main round loop for InvCipher

	InvShiftRows();
	InvSubBytes();
	AddRoundKey(0);

	// output = state
	for (i = 0; i < (4 * Nb); i++)
	{
		output[i] =  State[i % 4][ i / 4];
	}

}  // InvCipher()
示例#9
0
文件: AES.c 项目: MFreeze/m2moca
// Manque une fonction de dechiffrement
void Decipher(unsigned char *out, unsigned char in[], unsigned char Key[], int Nr)
{

	int i,j,round=0;
	unsigned char T[16],U[16], V[16],W[16];
	unsigned char RoundKey[240];

	//On fabrique les clefs de ronde
	KeyExpansion( RoundKey, Key, 10);

	//On copide le messge dans le bloc d'etat 
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			W[4*j+i] = in[4*j + i];
		}
	}

	// On "pele" derniere ronde
	// La fonction MixColumns n'est pas dans la derniere ronde
	AddRoundKey(V,W,RoundKey,Nr);
	InvShiftRows(U,V);
	InvSubBytes(T,U);


	// Il y a Nr rondes.
	// Les Nr-1 premieres rondes sont identiques
	// Ces Nr-1 rondes sont dechiffrees dans la boucle for ci-dessous
	for(round=Nr-1;round>0;round--)
	{
	  AddRoundKey(W,T,RoundKey,round);
	  InvMixColumns(V,W);
	  InvShiftRows(U,V);
	  InvSubBytes(T,U);

	}	

	// AddKey de la premiere clef
	AddRoundKey(W,T,RoundKey,0); 

	// Le processus de dechiffremen est fini
	// On copie le bloc d'etat du message dans le bloc de message clair (out)
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			out[j*4+i]=W[4*j+i];
		}
	}
}
示例#10
0
文件: aes.cpp 项目: Thobian/xlib
line AesDecrypt(const void*     decrypt_data,
                const size_t    decrypt_data_size,
                const AesKey&   decrypt_key)
  {
  unsigned char data[bytes_row_size][bytes_columns_size];
  unsigned char expandkey[expand_key_size][bytes_row_size][bytes_columns_size];
  unsigned char okdata[bytes_row_size*bytes_columns_size];
  line ret;
  const unsigned char* lp_decrypt = (const unsigned char*)decrypt_data;

  KeyExpansion(decrypt_key._key,expandkey);

  for(size_t encrypted = 0;
    encrypted < decrypt_data_size;
    encrypted += (bytes_row_size*bytes_columns_size))
    {
    for(size_t row = 0; row < bytes_row_size; ++row)
      {
      for(size_t col = 0; col < bytes_columns_size; ++col)
        {
        data[row][col] = lp_decrypt[row + col * bytes_columns_size];
        }
      }

    AddRoundKey(data,expandkey[expand_key_size-1]);

    for(intptr_t i = expand_key_size - 2; i >= 0; --i)
      {
      InvShiftRows(data);
      InvSubstituteBytes(data);
      AddRoundKey(data,expandkey[i]);
      if(i > 0)
        InvMixColumns(data);
      }

    for(size_t row = 0; row < bytes_row_size; ++row)
      {
      for(size_t col = 0; col < bytes_columns_size; ++col)
        {
        okdata[row + col * bytes_columns_size] = data[row][col];
        }
      }
    ret.append(okdata,sizeof(okdata));
    lp_decrypt += bytes_row_size * bytes_columns_size;
    }

  return ret;
  }
示例#11
0
// InvCipher is the main function that decrypts the CipherText.
void InvCipher(char in[])
{
	int i,j,round=0;
	int q,z;
	///sharan testing

	//Copy the input CipherText to state array.
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			state[j][i] = in[i*4 + j];
		}
	}

	// Add the First round key to the state before starting the rounds.
	AddRoundKey(Nr); 

	// There will be Nr rounds.
	// The first Nr-1 rounds are identical.
	// These Nr-1 rounds are executed in the loop below.
	for(round=Nr-1;round>0;round--)
	{
		InvShiftRows();
		InvSubBytes();
		AddRoundKey(round);
		InvMixColumns();
	}
	
	// The last round is given below.
	// The MixColumns function is not here in the last round.
	InvShiftRows();
	InvSubBytes();
	AddRoundKey(0);

	// The decryption process is over.
	// Copy the state array to output array.
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			out[i*4+j]=state[j][i];
		}
	}
	strcat(out2,out);
}
示例#12
0
文件: aes.c 项目: DDTLK/AUTH
// decrypt is the main function that decrypts the CipherText.
void decrypt(uint8_t *out, uint8_t *in, uint8_t *expanded_key)
{
    int i,j,round=0;
    uint8_t state[4][4];

    int Nr = 128/32+6;// replace 128 by 192, 256 for larger keys

    //Copy the input CipherText to state array.
    for(i=0; i<4; i++)  {
        for(j=0; j<4; j++)  {
            state[j][i] = in[i*4 + j];
        }
    }

    // Add the First round key to the state before starting the rounds.
    AddRoundKey(Nr,state,expanded_key);

    // There will be Nr rounds.
    // The first Nr-1 rounds are identical.
    // These Nr-1 rounds are executed in the loop below.
    for(round=Nr-1; round>0; round--)  {
        InvShiftRows(state);
        InvSubBytes(state);
        AddRoundKey(round,state,expanded_key);
        InvMixColumns(state);
    }

    // The last round is given below.
    // The MixColumns function is not here in the last round.
    InvShiftRows(state);
    InvSubBytes(state);
    AddRoundKey(0,state,expanded_key);

    // The decryption process is over.
    // Copy the state array to output array.
    for(i=0; i<4; i++)  {
        for(j=0; j<4; j++)  {
            out[i*4+j]=state[j][i];
        }
    }
}
示例#13
0
文件: aes.c 项目: pidh/toyCrypt
void
AES_InvCipher(void *_ctx, u1 *data)
{
	char	r;
	AES_CTX	*ctx = (AES_CTX *)_ctx;
	u4	*w = ctx->W;

	w = &w[ctx->Nr * Nb];
	AddRoundKey((u4 *)data, w);
	w -= 4;

	for(r = 1;r < ctx->Nr;r++) {
		InvShiftRows(data);
		InvSubBytes(data);
		AddRoundKey((u4 *)data, w);
		InvMixColumns(data);
		w -= 4;
	}

	InvShiftRows(data);
	InvSubBytes(data);
	AddRoundKey((u4 *)data, w);
}
示例#14
0
void DecryptBlock(void* pEncrypted, const u_int32_ard *pKeys)
{
  // XOR the first key to the first state. 
  AddRoundKey(pEncrypted, pKeys, ROUNDS);

  #if defined(unroll_decrypt_loop)
  dtransform(pEncrypted, pKeys, 9);
  dtransform(pEncrypted, pKeys, 8);
  dtransform(pEncrypted, pKeys, 7);
  dtransform(pEncrypted, pKeys, 6);
  dtransform(pEncrypted, pKeys, 5);
  dtransform(pEncrypted, pKeys, 4);
  dtransform(pEncrypted, pKeys, 3);
  dtransform(pEncrypted, pKeys, 2);
  dtransform(pEncrypted, pKeys, 1);
  #else
  int round;
  for(round=ROUNDS-1; round>0; round--)
  {
    // FIXME: use non-arduino specific debug
    #ifdef verbose_debug
    Serial.print("Encryption round " );
    Serial.println(round);
    #endif

    InvSubAndShift(pEncrypted);
    AddRoundKey(pEncrypted, pKeys, round);
    InvMixColumns(pEncrypted);
  }
  #endif

  // The last round is different (Round 0) -- there is no MixColumns.
  InvSubAndShift(pEncrypted);
  AddRoundKey(pEncrypted, pKeys, 0);


} // DecryptBlock()
示例#15
0
文件: AES_in_c.c 项目: blostic/AES
int main(int argc, char** argv){

  print(state);

  KeySchedule();
  AddRoundKey();

  for(iteracja = 1; iteracja < ROUND_cnt; ++iteracja){
    SubBytes();
    ShiftRows();
    MixColumns();
   print(state);
   AddRoundKey();   
  }
  SubBytes();
  ShiftRows()
  AddRoundKey();

  print(state); /*encrypted*/

  AddRoundKey();
  InvShiftRows();
  InvSubBytes();
  for(iteracja = ROUND_cnt-1; iteracja > 0; --iteracja){
    AddRoundKey();
    InvMixColumns();
    InvShiftRows();
    InvSubBytes();
  }
  AddRoundKey();

  print(state);

  return 0;

}
示例#16
0
文件: main.c 项目: Guy0m/aes-project
void  dechiffrerBloc(unsigned char mat[TAILLEMATRICE][TAILLEMATRICE], unsigned char cleEtendue[TAILLEMATRICE*(NBROUND+1)][TAILLEMATRICE]){

    int ligne, colonne, round;
    // Traitement du bloc


   //BLOC CHIFFRE------------------------------------------
   printf("\n---------------------------------\n");
   printf("\nBLOC CHIFFRE \n",round);

    for (ligne=0; ligne<TAILLEMATRICE; ligne++){

        for (colonne=0; colonne<TAILLEMATRICE; colonne++){

          printf("0x%.2X\t"  ,mat[colonne][ligne]);
        }

         printf("\n");
    }
    printf("\n");

     //FIN BLOC CHIFFRE-----------------------------------



    AddRoundKey(cleEtendue,NBROUND,mat);

   /* //ADD ROUND KEY------------------------------------------
    printf("ADD ROUND KEY [%d]  \n",round);

    for (ligne=0; ligne<TAILLEMATRICE; ligne++){

        for (colonne=0; colonne<TAILLEMATRICE; colonne++){

          printf("0x%.2X\t"  ,mat[colonne][ligne]);
        }

        printf("\n");
    }
    printf("\n---------------------------------\n");

    //FIN ADD ROUND KEY-----------------------------------*/

    for(round=NBROUND-1; round>=0; round--){


        InvShiftRow(mat);


       /* //INV SHIFT ROW------------------------------------------
        printf("SHIFT ROW [%d]  \n",round);

        for (ligne=0; ligne<TAILLEMATRICE; ligne++){

            for (colonne=0; colonne<TAILLEMATRICE; colonne++){

              printf("0x%.2X\t"  ,mat[colonne][ligne]);
            }

             printf("\n");
        }

        printf("\n---------------------------------\n");
        //INV SHIFT ROW-----------------------------------*/


        InvSubBytes(mat);

       /*//INV SUB BYTES------------------------------------------
        printf("INV SUBBYTES[%d]  \n",round);

        for (ligne=0; ligne<TAILLEMATRICE; ligne++){

            for (colonne=0; colonne<TAILLEMATRICE; colonne++){

              printf("0x%.2X\t"  ,mat[colonne][ligne]);
            }

             printf("\n");
        }
        printf("\n---------------------------------\n");
        //INV SUBBYTES------------------------------------------*/

        AddRoundKey(cleEtendue,round,mat);

        /* //ADD ROUND KEY------------------------------------------
        printf("ADD ROUND KEY [%d]  \n",round);

        for (ligne=0; ligne<TAILLEMATRICE; ligne++){

            for (colonne=0; colonne<TAILLEMATRICE; colonne++){

              printf("0x%.2X\t"  ,mat[colonne][ligne]);
            }

             printf("\n");
        }

        printf("\n---------------------------------\n");
        //FIN ADD ROUND KEY-----------------------------------*/



        if(round!=(0))
        {
            InvMixColumns(mat);

            /*//DEBUG INV MIXCOLUMNS------------------------------------------

            printf("DEBUG INV MIXCOLUMNS [%d] \n",round);

            for (ligne=0; ligne<TAILLEMATRICE; ligne++){

                for (colonne=0; colonne<TAILLEMATRICE; colonne++){

                   printf("0x%.2X\t"  ,mat[colonne][ligne]);
                }

                 printf("\n");
            }

             printf("\n---------------------------------\n");
            // FIN DEBUG INV MIXCOLUMNS---------------------------------------*/
        }
    }



       //DEBUG  MESSAGE CRYPTE EN HEXA------------------------------------------

        printf("BLOC DECHIFRRE EN HEXA   \n",round);

        for (ligne=0; ligne<TAILLEMATRICE; ligne++){

            for (colonne=0; colonne<TAILLEMATRICE; colonne++){

              printf("0x%.2X\t"  ,mat[colonne][ligne]);
            }

             printf("\n");
        }

}