Esempio n. 1
0
//==============================================================================
void Decrypt( uint8_t *keyblok, uint8_t *buff, uint8_t *result) //Функция расшифровывания
{
  uint8_t i,j; //Счетчики
 
  KeyExpansion( keyblok, DECRYPT);  //Вычисление раундовых ключей
 
  for(i=j=0; i<Nb; i++, j+=4) s[i]= pack((uint8_t *)&buff[j]); //Заполнение промежуточного массива
  i=0;
 
  AddRoundKey(s , rkey , 0); ////Операция исключающее или с раундовым ключем
  
  for(i=1; i< Nr; i++)
  {
    ShiftRows((uint8_t*)s, DECRYPT);
    SubBytes((uint8_t*)s, DECRYPT);
    AddRoundKey(s, rkey, i);
    MixColums(s, Nb, DECRYPT);
  }
  
  ShiftRows((uint8_t*)s, DECRYPT);
  SubBytes((uint8_t*)s, DECRYPT);
  AddRoundKey(s, rkey, Nr);
  
  for(i=j=0;i<Nb;i++,j+=4) unpack(s[i], (uint8_t*)& result[j]);
 
}
Esempio n. 2
0
void
AES_CPU_Impl_O0::EncryptBlock(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, 0); 

    // 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 = 1; round < m_Nr; round++ )
    {
        SubBytes(m_state);
        ShiftRows(m_state);
        MixColumns(m_state);
        AddRoundKey(m_state, round);
    }

    // The last round is given below.
    // The MixColumns function is not here in the last round.
    SubBytes(m_state);
    ShiftRows(m_state);
    AddRoundKey(m_state, m_Nr);

    for( UINT32 i = 0; i < 4; i++ )
        for( UINT32 j = 0; j < 4; j++ )
            dst[i*4 + j] = m_state[j][i];
}
Esempio n. 3
0
File: aes.c Progetto: SamuelXu/clibs
void aes_cipher(ctx_aes* aes, uint8_t* input, uint8_t* output)  // encipher 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, 0);
	for (round = 1; round <= (aes->Nr - 1); round++)  // main round loop
	{
        SubBytes(aes); 
        ShiftRows(aes);  
        MixColumns(aes); 
        AddRoundKey(aes, round);
	}  // main round loop
	
	SubBytes(aes);
	ShiftRows(aes);
	AddRoundKey(aes, aes->Nr);
	
	// output = state
	for (i = 0; i < (4 * aes->Nb); i++)
	{
        output[i] =  aes->State[i % 4][ i / 4];
	}
	
}  // Cipher()
Esempio n. 4
0
void AES_enc(BYTE* input, BYTE* output,BYTE *w,BYTE Nr)
{
	DWORD i;
	DWORD round;
	BYTE gState[4][4];
	AES_Memset(&gState[0][0],0,16);
	for( i=0;i<4*NB;i++)
	{
		gState[i%4][i/4]=input[i];
	}
	AddRoundKey(0,w,gState);
	
	for ( round = 1; round <= (Nr - 1); round++)
	{
        SubBytes(gState);
        ShiftRows(gState);
        MixColumns(gState);
        AddRoundKey(round,w,gState);
	}  // main round loop
	
	SubBytes(gState);
	ShiftRows(gState);
	AddRoundKey(Nr,w,gState);
	
	// output = state
	for (i = 0; i < (4 * NB); i++)
	{
        output[i] =  gState[i % 4][ i / 4];
	}

}
Esempio n. 5
0
File: aes.c Progetto: LinLL/ipc
void Cipher(unsigned char* input, unsigned char* output)  // encipher 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];
		//State[i / 4][ i % 4] = input[i];
	}
	//Dump();
	AddRoundKey(0);

	for (round = 1; round <= (Nr - 1); round++)  // main round loop
	{
		SubBytes(); 
		ShiftRows();  
		MixColumns(); 
		AddRoundKey(round);
	}  // main round loop

	SubBytes();
	ShiftRows();
	AddRoundKey(Nr);

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

}  // Cipher()
Esempio n. 6
0
void Cipher()
{
	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(0); 

	for(round=1; round<Nr; round++)
	{
		SubBytes();
		ShiftRows();
		MixColumns();
		AddRoundKey(round);
	}
	
	SubBytes();
	ShiftRows();
	AddRoundKey(Nr);

	for(i=0; i<4; i++)
	{
		for(j=0; j<4; j++)
		{
			out[i*4+j]=state[j][i];
		}
	}
}
Esempio n. 7
0
// Cipher is the main function that encrypts the PlainText.
void Cipher(unsigned char out[16],
	    unsigned char in[16], unsigned char RoundKey[240], int Nr)
{
  unsigned char state[4][4];
  int i,j,round=0;
  //Copy the input PlainText 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(state, RoundKey, 0);
#ifdef DEBUG_PRINTOUT
  std::cerr << "  ka="; printState(state); std::cerr << std::endl;
#endif
  // There will be Nr rounds.
  // The first Nr-1 rounds are identical.
  // These Nr-1 rounds are executed in the loop below.
  for(round=1;round<Nr;round++)
    {
      SubBytes(state);
#ifdef DEBUG_PRINTOUT
      std::cerr << "  sb="; printState(state); std::cerr << std::endl;
#endif
      ShiftRows(state);
      MixColumns(state);
#ifdef DEBUG_PRINTOUT
      std::cerr << "  mc="; printState(state); std::cerr << std::endl;
#endif
      AddRoundKey(state, RoundKey, round);
#ifdef DEBUG_PRINTOUT
      std::cerr << "  ka="; printState(state); std::cerr << std::endl;
#endif
    }
  // The last round is given below.
  // The MixColumns function is not here in the last round.
  SubBytes(state);
  ShiftRows(state);
#ifdef DEBUG_PRINTOUT
  std::cerr << "  sr="; printState(state); std::cerr << std::endl;
#endif
  AddRoundKey(state, RoundKey, Nr);
#ifdef DEBUG_PRINTOUT
  std::cerr << "  ka="; printState(state); std::cerr << std::endl;
#endif
  // The encryption 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];
        }
    }
}
// Cipher is the main function that encrypts the PlainText.
void Cipher()
{
	int i,j,round=0;

	//Copy the input PlainText 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(0); 
	
	// There will be Nr rounds.
	// The first Nr-1 rounds are identical.
	// These Nr-1 rounds are executed in the loop below.
	for(round=1;round<Nr;round++)
	{
		SubBytes();
		ShiftRows();
		MixColumns();
		AddRoundKey(round);
		// for(i=0;i<4;i++)
		// {
		// 	for(j=0;j<4;j++)
		// 	{
		// 	//	out[i*4+j]=state[j][i];
		// 		printf("%02x",state[j][i]);
		// 	}

		// }
		printf("\n");

	}
	
	// The last round is given below.
	// The MixColumns function is not here in the last round.
	SubBytes();
	ShiftRows();
	AddRoundKey(Nr);

	// The encryption 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];
		}
	}
}
Esempio n. 9
0
void AES128::encrypt16( unsigned char buffer[ 16 ] )
{
	AddRoundKey( buffer, key_schedule[ 0 ] );
	
	for ( register unsigned short r = 1; r < 10; ++r )
	{
		Substitution( buffer );
		ShiftRows( buffer );
		MixColumns( buffer );
		AddRoundKey( buffer, key_schedule[ r ] );
	}
	
	Substitution( buffer );
	ShiftRows( buffer );
	AddRoundKey( buffer, key_schedule[ 10 ] );
}
Esempio n. 10
0
unsigned char* AES::Cipher(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[0]);

	for(i=1; i<=10; i++)
	{
		SubBytes(state);
		ShiftRows(state);
		if(i!=10)MixColumns(state);
		AddRoundKey(state,w[i]);
	}

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

	return input;
}
Esempio n. 11
0
static void Cipher(uint8x16_t RoundKey_v[])
{
	uint8_t round = 0;

	AddRoundKey(RoundKey_v[0]); 

	for(round = 1; round < rounds; ++round)
	{
		SubBytes();
		ShiftRows();
		MixColumns();
		AddRoundKey(RoundKey_v[round]);
	}

	SubBytes();
	ShiftRows();
	AddRoundKey(RoundKey_v[rounds]);
}
Esempio n. 12
0
void RijndaelEncrypt( uint8_t state[AES_BLOCK_SIZE], uint8_t eKey[AES_MAX_KEY_SIZE],int32_t Nr )
{
    int32_t round = 0;

    AddRoundKey(state,eKey,round);

    for ( round = 1; round < Nr; ++round )
    {
        SubBytes(state);
        ShiftRows(state);
        MixColumns(state);
        AddRoundKey(state,eKey,round);
    }

    SubBytes(state);
    ShiftRows(state);
    AddRoundKey(state,eKey,Nr);
}
Esempio n. 13
0
File: aes.c Progetto: rpicard/aes.c
// this is the function that takes a block of plaintext and the key schedule
// and encrypts it. the next level of abstraction would be to implement block
// modes using this function on each block
void EncryptBlock(uint8_t* block, uint8_t* key_schedule)
{
    // the state is always 128 bits for AES. we are going to represent that as
    // an array of 16 bytes. conceptually it can be useful to think of it as a
    // 4x4 matrix
    //
    // step 1 of the cipher is to initialize the state as the input block of
    // plaintext. we are just going to operate directly on the input block

    // step 2 is to do an initial round key addition. the first round key is
    // added by a simple bitwise xor operation
    AddRoundKey(block, key_schedule[0, (4*4) - 1]);

    // step 3 is Nr-1 rounds where Nr is the total number of rounds we will be
    // performing. Nr is 10, 12, and 14 for keysizes of 128, 192, and 256
    // respectively.
    for (uint8_t round = 0; round < (10 - 1); round++) {

        // the round function consists of four operations
        //
        // SubBytes subsitutes bytes in the state based on the standardized
        // substitution boxes or S-Boxes
        SubBytes(block);

        // ShiftRows cyclically shifts each of the last three rows of state
        // over by a different offset
        ShiftRows(block);

        // MixColumns does some math on the state, column by column
        MixColumns(block);

        // finally, we add the next round key to the state
        AddRoundKey(block, key_schedule[round * (4*4), (round + 1) * 4 - 1]);
    }

    // step 4 is the final round. the only difference is that we do not
    // perform the MixColumns operation on this one
    SubBytes(block);
    ShiftRows(block);
    AddRoundKey(block, key_schedule[10 * (4*4), (10 + 1) * (4*4) - 1]);

    // all of that fiddling with the state leaves us with the encrypted
    // block
}
Esempio n. 14
0
void Cipher(WORD block[Nb], WORD key[Nb*(Nr+1)])
{
    for(int round=1; round<Nr; round++)
    {
        log_it("Round %d started with:\n", block);

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

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

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

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

    for(int i=0; i<Nb; i++)
    {
        BYTE* temp = (BYTE*)&block[i];
        for (int j = 0; j < 4; j++)
            temp[j] = SubBytesTab[temp[j]];
        block[i] = pack(temp);
    }

    ShiftRows(block);

    AddRoundKey(block, &key[4*Nr]);
    log_it("After AddRoundKey\n", block);
}
Esempio n. 15
0
File: aes.c Progetto: DDTLK/AUTH
// encrypt is the main function that encrypts the PlainText.
void encrypt(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 PlainText 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(0,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=1; round<Nr; round++)  {
        SubBytes(state);
        ShiftRows(state);
        MixColumns(state);
        AddRoundKey(round,state,expanded_key);
    }

    // The last round is given below.
    // The MixColumns function is not here in the last round.
    SubBytes(state);
    ShiftRows(state);
    AddRoundKey(Nr,state,expanded_key);

    // The encryption 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];
        }
    }
}
Esempio n. 16
0
File: aes.c Progetto: prabhaks/AES
/*
 * This method performs encrypt operation given plaintext.
 * It read tables from tf and plaintext from fp.
 * Encrypts plainetxt using key
 */
void ProcessEncrypt(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 encryption 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);
	printOut(state, "input", 0);
	AddRoundKey(state, word, 0);
	printWord(word, "k_sch", 0, 0);
	for (round = 1; round < Nr; round++) {
		printOut(state, "start", round);
		SubBytes(state);
		printOut(state, "s_box", round);
		ShiftRows(state);
		printOut(state, "s_row", round);
		MixColumns(state, P);
		printOut(state, "m_col", round);
		AddRoundKey(state, word, round * Nb);
		printWord(word, "k_sch", round * Nb, round);
	}
	printOut(state, "start", round);
	SubBytes(state);
	printOut(state, "s_box", round);
	ShiftRows(state);
	printOut(state, "s_row", round);
	AddRoundKey(state, word, Nr * Nb);
	printWord(word, "k_sch", Nr * Nb, round);
	printOut(state, "output", round);
}
Esempio n. 17
0
File: aes.c Progetto: pidh/toyCrypt
void
AES_Cipher(void *_ctx, u1 *data)
{
	char	r;
	AES_CTX	*ctx = (AES_CTX *)_ctx;
	u4	*w = ctx->W;

	AddRoundKey((u4 *)data, w);
	w += 4;
	for(r = 1;r < ctx->Nr;r++) {
		SubBytes(data);
		ShiftRows(data);
		MixColumns(data);
		AddRoundKey((u4 *)data, w);

		w += 4;
	}

	SubBytes(data);
	ShiftRows(data);
	AddRoundKey((u4 *)data, w);
}
Esempio n. 18
0
static void
buildAESCircuit(GarbledCircuit *gc, block *inputLabels)
{
	GarblingContext ctxt;

	int q = 50000; //Just an upper bound
	int r = 50000;
    int *addKeyInputs = calloc(2 * m, sizeof(int));
    int *addKeyOutputs = calloc(m, sizeof(int));
    int *subBytesOutputs = calloc(m, sizeof(int));
    int *shiftRowsOutputs = calloc(m, sizeof(int));
    int *mixColumnOutputs = calloc(m, sizeof(int));
    block *outputMap = allocate_blocks(2 * m);

	createEmptyGarbledCircuit(gc, n, m, q, r, inputLabels);
	startBuilding(gc, &ctxt);

	countToN(addKeyInputs, 256);

	for (int round = 0; round < roundLimit; round++) {

		AddRoundKey(gc, &ctxt, addKeyInputs, addKeyOutputs);

		for (int i = 0; i < 16; i++) {
			SubBytes(gc, &ctxt, addKeyOutputs + 8 * i, subBytesOutputs + 8 * i);
		}

		ShiftRows(gc, &ctxt, subBytesOutputs, shiftRowsOutputs);

		for (int i = 0; i < 4; i++) {
			if (round != roundLimit - 1)
				MixColumns(gc, &ctxt, shiftRowsOutputs + i * 32,
                           mixColumnOutputs + 32 * i);
		}
		for (int i = 0; i < 128; i++) {
			addKeyInputs[i] = mixColumnOutputs[i];
			addKeyInputs[i + 128] = (round + 2) * 128 + i;
		}
	}

	finishBuilding(gc, &ctxt, outputMap, mixColumnOutputs);
	/* writeCircuitToFile(gc, AES_CIRCUIT_FILE_NAME); */

    free(addKeyInputs);
    free(addKeyOutputs);
    free(subBytesOutputs);
    free(shiftRowsOutputs);
    free(mixColumnOutputs);
    free(outputMap);
}
Esempio n. 19
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;

}
Esempio n. 20
0
File: aes.cpp Progetto: Thobian/xlib
line AesEncrypt(const void*     encrypt_data,
                const size_t    encrypt_data_size,
                const AesKey&   encrypt_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_encrypt = (const unsigned char*)encrypt_data;

  KeyExpansion(encrypt_key._key,expandkey);

  for(size_t encrypted = 0;
    encrypted < encrypt_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_encrypt[row + col * bytes_columns_size];
        }
      }

    AddRoundKey(data,expandkey[0]);

    for(size_t i = 1; i < expand_key_size; ++i)
      {
      SubstituteBytes(data);
      ShiftRows(data);
      if(i != (expand_key_size - 1))
        MixColumns(data);
      AddRoundKey(data,expandkey[i]);
      }

    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_encrypt += bytes_row_size * bytes_columns_size;
    }

  return ret;
  }
Esempio n. 21
0
/* encrypt one 128 bit block */
void HAM_CALLCONV aes_encrypt (uchar *in, uchar *expkey, uchar *out)
{
    uchar state[Nb * 4];
    unsigned round;

    memcpy (state, in, Nb * 4);
    AddRoundKey ((unsigned *)state, (unsigned *)expkey);

    for( round = 1; round < Nr + 1; round++ ) {
        if( round < Nr )
            MixSubColumns (state);
        else
            ShiftRows (state);

        AddRoundKey ((unsigned *)state, (unsigned *)expkey + round * Nb);
    }

    memcpy (out, state, sizeof(state));
}
Esempio n. 22
0
void test_ShiftRows(void)
{
	int i;
	unsigned char expected[] = {
		0x00, 0x01, 0x02, 0x03,
		0x11, 0x12, 0x13, 0x10,
		0x22, 0x23, 0x20, 0x21,
		0x33, 0x30, 0x31, 0x32};

	unsigned char actual[] = {
		0x00, 0x01, 0x02, 0x03,
		0x10, 0x11, 0x12, 0x13,
		0x20, 0x21, 0x22, 0x23,
		0x30, 0x31, 0x32, 0x33};
	ShiftRows(actual);

	for (i = 0; i < 4*4; i++)
	{
	  CU_ASSERT_INT_EQ(expected[i], actual[i]);
	}
}
Esempio n. 23
0
///////////////////////////////////////////////////////////////////////////////
//	函数名:	BlockDecrypt
//	描述:		对单块数据解密。
//	输入参数:	pState -- 状态数据。
//	输出参数:	pState -- 解密后的状态数据。
//	返回值:	无。
///////////////////////////////////////////////////////////////////////////////
static void BlockDecrypt(unsigned char *pState)
{
	unsigned char i;
	
	AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]);
	
	for (i = Nr; i > 0; i--)	// i = [Nr, 1]
	{
		ShiftRows(pState, 1);
		SubBytes(pState, 4*Nb, 1);
		AddRoundKey(pState, &g_roundKeyTable[4*Nb*(i-1)]);

		if (i != 1)
		{
			MixColumns(pState, 1);
		}
	}
	
// 为了节省代码,合并到循化执行
//  ShiftRows(pState, 1);
//  SubBytes(pState, 4*Nb, 1);
//  AddRoundKey(pState, g_roundKeyTable);
}
Esempio n. 24
0
///////////////////////////////////////////////////////////////////////////////
//	函数名:	BlockEncrypt
//	描述:		对单块数据加密。
//	输入参数:	pState -- 状态数据。
//	输出参数:	pState -- 加密后的状态数据。
//	返回值:	无。
///////////////////////////////////////////////////////////////////////////////
static void BlockEncrypt(unsigned char *pState)
{
	unsigned char i;
	
	AddRoundKey(pState, g_roundKeyTable);
	
	for (i = 1; i <= Nr; i++)	// i = [1, Nr]
	{
		SubBytes(pState, 4*Nb, 0);
		ShiftRows(pState, 0);

		if (i != Nr)
		{
			MixColumns(pState, 0);
		}

		AddRoundKey(pState, &g_roundKeyTable[4*Nb*i]);
	}
	
// 为了节省代码,合并到循化执行
// 	SubBytes(pState, 4*Nb);
//	ShiftRows(pState, 0);
// 	AddRoundKey(pState, &g_roundKeyTable[4*Nb*Nr]);
}
Esempio n. 25
0
/*
 * One AES round
 */
void AES_round( word P, word K, word C)
{
  int i,j;
  unsigned char state[4][4];

  // Convert the 16 byte sequence to a 4x4 matrix
  for(i=0; i<4; i++)
    for(j=0; j<4; j++)
      state[i][j] = P[ 4 * j + i ];

  // Apply keyless AES round
  SubBytes  ( state );
  ShiftRows ( state );
  MixColumns( state );

  // Convert the 4x4 matrix to 16-byte sequence
  for(i=0; i<4; i++)
    for(j=0; j<4; j++)
      C[ 4 * j + i ] = state[i][j];

  // Xor the key
  for(i=0; i<16; i++)
    C[i] ^= K[i];
}
Esempio n. 26
0
File: AES.c Progetto: MFreeze/m2moca
// La fonction cipher sert a chiffrer le bloc de message
void Cipher(unsigned char *C, unsigned char M[], 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] = M[4*j + i];
		}
	}


//    for (i=0; i<4;i++) {
//        for (j=16; j<24; j++) {
//            if (!(j%4))
//                printf("  ");    
//            printf("%02x ", RoundKey[j*4 + i]);
//        }
//        printf("\n");
//    }

    //printf("\n");

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

	// Il y a Nr rondes.
	// Les Nr-1 premieres rondes sont identiques
	// Ces Nr-1 rondes sont effectuées dans la boucle for ci-dessous
	for(round=1;round<Nr;round++)
	{
	  SubBytes(U,T);
	  ShiftRows(V,U);
	  MixColumns(W,V);
	  AddRoundKey(T,W,RoundKey,round);
	}	
	// Derniere ronde
	// La fonction MixColumns n'est pas dans la derniere ronde
	SubBytes(U,T);
	ShiftRows(V,U);
	AddRoundKey(W,V,RoundKey,Nr);


	// Le processus de chiffremen est fini
	// On copie le bloc d'etat du message dans le bloc de message chiffre
	for(i=0;i<4;i++)
	{
		for(j=0;j<4;j++)
		{
			C[j*4+i]=W[4*j+i];
		}
	}
}
void encrypt() {
    bzero(StateArray, 4*4*sizeof(unsigned char));
    bzero(ExpandedKey, 11*4*sizeof(unsigned char));

#if (AES_PRINT & AES_PRINT_MAIN)
    xil_printf("-- Test Encyption Key \r\n\n");
    AES_printf(Key);
    xil_printf("-------------------------\r\n\n");

    xil_printf("-- Test Plaintext \r\n\n");
    AES_printf(PlainText);
    xil_printf("-------------------------\r\n\n");
#endif

#if (AES_PRINT & AES_PRINT_MAIN)
    xil_printf("-- Starting Key Expension   \r\n\n");
#endif


    ExpandKey(Key, ExpandedKey);  //


#if (AES_PRINT & AES_PRINT_MAIN)
    xil_printf("-- Starting Encryption  \r\n\n");
#endif

    long int x;
    for(x=0; x<100; x++) {
        memcpy(StateArray, PlainText, 4*4*sizeof(unsigned char));

#if (AES_PRINT & AES_PRINT_DETAILS)
        xil_printf("-- Test State - Start of Round 0 \r\n\n");
        AES_printf(StateArray);
        xil_printf("-------------------------\r\n\n");
#endif

        AddRoundKey(ExpandedKey[0], StateArray);  //


#if (AES_PRINT & AES_PRINT_DETAILS)
        xil_printf("-- Test State - Start of Round 0 \r\n\n");
        AES_printf(StateArray);
        xil_printf("-------------------------\r\n\n");
#endif

        int i;

        //Rounds
        for(i=1; i<=10; i++) {

            SubBytes(StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
            xil_printf("-- Test State - Round %d after SubBytes \r\n\n", i);
            AES_printf(StateArray);
            xil_printf("-------------------\r\n\n");
#endif

            ShiftRows(StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
            xil_printf("-- Test State - Round %d after shiftRows \r\n\n", i);
            AES_printf(StateArray);
            xil_printf("-----------------------------\r\n\n");
#endif

            if(i != 10) {

                MixColumns(StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
                xil_printf("-- Test State - Round %d after MixColumns \r\n\n", i);
                AES_printf(StateArray);
                xil_printf("-----------------------------\r\n\n");
#endif
            }

            AddRoundKey(ExpandedKey[i], StateArray);
#if (AES_PRINT & AES_PRINT_DETAILS)
            xil_printf("-- Test State - Round %d after RoundKeyValue \r\n\n", i);
            AES_printf(StateArray);
            xil_printf("-----------------------------\r\n\n");
#endif
        }
    }

#if (AES_PRINT & AES_PRINT_DETAILS)
    xil_printf("-- AES key expansion and encryption test completed.   \r\n\n");
    xil_printf("-- Test State - End  \r\n\n");
    AES_printf(StateArray);
    xil_printf("-----------------------------\r\n\n");
#endif

    xil_printf("****************Encryption*********************\r\n");

}