Ejemplo n.º 1
0
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];
		}
	}
}
Ejemplo n.º 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];
	}
}
Ejemplo n.º 3
0
Archivo: aes.c Proyecto: 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()
Ejemplo n.º 4
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];
}
Ejemplo n.º 5
0
Archivo: aes.c Proyecto: 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()
Ejemplo n.º 6
0
Archivo: AES.c Proyecto: 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];
		}
	}
}
Ejemplo n.º 7
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;
}
Ejemplo n.º 8
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];
}
Ejemplo n.º 9
0
void InvCipher(WORD block[Nb], WORD key[Nb*(Nr+1)])
{
    for(int round=Nr-1; round>0; round--)
    {
        log_it("Round %d started with:\n", block);

        // InvShiftRows
        InvShiftRows(block);
        log_it("After InvShiftRows\n", block);

        // InvByteSub
        for(int i=0; i<Nb; i++)
        {
            BYTE* temp = (BYTE*)&block[i];
            for (int j = 0; j < 4; j++)
                temp[j] = InvSubBytesTab[temp[j]];
            block[i] = pack(temp);
        }
        log_it("After InvByteSub\n", block);

        // AddRoundKey
        AddRoundKey(block, &key[4*round]);
        log_it("After AddRoundKey\n", block);

        // InvMixColumn
        InvMixColumn(block);
        log_it("After InvMixColumn\n", block);

    }
    // InvShiftRows
    InvShiftRows(block);
    log_it("After InvShiftRows\n", block);

    // InvByteSub
    for(int i=0; i<Nb; i++)
    {
        BYTE* temp = (BYTE*)&block[i];
        for (int j = 0; j < 4; j++)
            temp[j] = InvSubBytesTab[temp[j]];
        block[i] = pack(temp);
    }
    log_it("After InvByteSub\n", block);

    // AddRoundKey
    AddRoundKey(block, &key[0]);
    log_it("After AddRoundKey\n", block);
}
Ejemplo n.º 10
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);
}
Ejemplo n.º 11
0
Archivo: aes.c Proyecto: 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];
        }
    }
}
Ejemplo n.º 12
0
Archivo: aes.c Proyecto: prabhaks/AES
/**
 * This method performs decrypt operation given cipher.
 * It read tables from tf and ciphertext from fp.
 * Decrypts using key. Using fiestel structure, decrypts is nothigs but encryption
 * with reverse round keys input.
 */
void ProcessDecrypt(char *key, FILE *tf, FILE *fp) {
	table_check = 0;
	ProcessTableCheck(tf);
	char buf[16];
	int ret = fread(buf, 1, 16, fp);
	if (ret < 16) {
		fprintf(stderr,
				"Input size for decryption can not be less than 16 bytes\n");
		exit(1);
	}
	int i, Nr = 10, Nb = 4, round;
	unsigned char **state = (unsigned char **) malloc(
			sizeof(unsigned char *) * 4);
	for (i = 0; i < 4; i++)
		state[i] = (unsigned char *) malloc(sizeof(unsigned char) * 4);
	copyInStateArray(state, buf);
	unsigned char **word = doProcessKeyExpand(key);
	printOutD(state, "iinput", 0);
	AddRoundKey(state, word, Nr * Nb);
	printWordD(word, "ik_sch", Nr * Nb, 0);
	for (round = Nr - 1; round > 0; round--) {
		printOutD(state, "istart", Nr - round);
		InvShiftRows(state);
		printOutD(state, "is_row", Nr - round);
		InvSubBytes(state);
		printOutD(state, "is_box", Nr - round);
		AddRoundKey(state, word, round * Nb);
		printWordD(word, "ik_sch", round * Nb, Nr - round);
		printOutD(state, "ik_add", Nr - round);
		MixColumns(state, INVP);
	}
	printOutD(state, "istart", Nr - round);
	InvShiftRows(state);
	printOutD(state, "is_row", Nr - round);
	InvSubBytes(state);
	printOutD(state, "is_box", Nr - round);
	AddRoundKey(state, word, 0);
	printWordD(word, "ik_sch", round * Nb, Nr - round);
	printOutD(state, "ioutput", Nr - round);
}
Ejemplo n.º 13
0
Archivo: aes.c Proyecto: 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);
}
Ejemplo n.º 14
0
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;

}
Ejemplo n.º 15
0
Archivo: aes.cpp Proyecto: 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;
  }
Ejemplo n.º 16
0
void HAM_CALLCONV aes_decrypt (uchar *in, uchar *expkey, uchar *out)
{
    uchar state[Nb * 4];
    unsigned round;

    memcpy (state, in, sizeof(state));

    AddRoundKey ((unsigned *)state, (unsigned *)expkey + Nr * Nb);
    InvShiftRows(state);

    for( round = Nr; round--; )
    {
        AddRoundKey ((unsigned *)state, (unsigned *)expkey + round * Nb);
        if( round )
            InvMixSubColumns (state);
    }

    memcpy (out, state, sizeof(state));
}