Example #1
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];
		}
	}
}
Example #2
0
static void encryptBlock(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, 0);

	for (round = 1; round < Nr; round++) {
		SubShiftRows(&state);
		MixColumns(&state);
		AddRoundKey(&state, w, round);
	}

	SubShiftRows(&state);
	AddRoundKey(&state, w, Nr);

	for (i = 0, n = 0; i < 4; i++) {
		for (j = 0; j < 4; j++) {
			out[n++] = state[i][j];
		}
	}
	
}
Example #3
0
File: aes.c Project: 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()
/**
 * @brief Encrypts 16 bytes of data.
 *
 * Based on Advanced Encryption Standard specification.
 * 
 * @param dataToEncrypt Data to encrypt, must be at least 16 bytes in size.
 * @param result Destination to store encrypted data, must be at least 16 bytes in size.
 * @param numRounds Number of rounds.
 * @param roundKey Round key.
 */
void ThreadMessageItemEncrypt::Cipher(unsigned char * dataToEncrypt,unsigned char * result, unsigned char numRounds, unsigned char * roundKey)
{
	// Copy data into state for manipulation
	for(size_t r = 0;r<EncryptKey::WORD_SIZE;r++)
	{
		for(size_t c = 0;c<EncryptKey::WORD_SIZE;c++)
		{
			state[r][c] = dataToEncrypt[r+(c*EncryptKey::WORD_SIZE)];
		}
	}

	// Perform encryption operations
	XorRoundKey(0,roundKey);

	for(size_t r = 1; r<numRounds; r++)
	{
		SubBytes();
		ShiftRowsLeft();
		MixColumns();
		XorRoundKey(r,roundKey);
	}

	SubBytes();
	ShiftRowsLeft();
	XorRoundKey(numRounds,roundKey);
	
	// Copy state into data now that we have finished
	for(int r = 0;r<EncryptKey::WORD_SIZE;r++)
	{
		for(int c = 0;c<EncryptKey::WORD_SIZE;c++)
		{
			result[r+(c*EncryptKey::WORD_SIZE)] = state[r][c];
		}
	}
}
Example #5
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;
}
Example #6
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];
	}

}
Example #7
0
File: aes.c Project: 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()
Example #8
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];
}
Example #9
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];
        }
    }
}
void proest_inverse_permute(proest_ctx *x) {
    int round;

    for (round = PROEST_NROUNDS-1; round >= 0; --round) {
        AddConstant(x, round);
        ShiftRegistersInverse(x, round);
        MixColumns(x);
        SubBits(x);
    }
}
// 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];
		}
	}
}
void proest_permute(proest_ctx *x)
{
  int round;

  for(round=0;round<PROEST_NROUNDS;round++)
  {
    SubBits(x);
    MixColumns(x);
    ShiftRegisters(x,round);
    AddConstant(x,round);
  }
}
Example #13
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);
}
Example #14
0
File: aes.cpp Project: 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;
  }
Example #15
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 ] );
}
Example #16
0
/**
 *  EncryptBlock
 *
 *  Encrypt a single block, stored in the buffer text. The buffer MUST be 16 
 *  bytes in length!
 *  pKeys stores a complete key schedule for the round.
 *  The algorithm, call order and function names, follows the reference of 
 *  FIPS-197, section 5.1.
 *
 *  The encryption loop can be unrolled or left as is by using the 
 *  unroll_encrypt_loop define.
 *
 *  The encrypted data is returned in the text buffer.
 *
 *  Note: Only 10 rounds and 128 bit keys are supported in this implementation.
 */
void EncryptBlock(void *pBlock, const u_int32_ard *pKeys) 
{
	// FIXME -- Use non-arduino-specific debug
	#ifdef verbose_debug
	Serial.println("\n\nStarting encrypt, plaintext is");
	printBytes((byte_ard*)pBlock,16,16);
	#endif 
  
    // XOR the first key to the first state
	AddRoundKey(pBlock, pKeys, 0);
    
    #if defined(unroll_encrypt_loop)
    ntransform(pBlock, pKeys, 1);
    ntransform(pBlock, pKeys, 2);
    ntransform(pBlock, pKeys, 3);
    ntransform(pBlock, pKeys, 4);
    ntransform(pBlock, pKeys, 5);
    ntransform(pBlock, pKeys, 6);
    ntransform(pBlock, pKeys, 7);
    ntransform(pBlock, pKeys, 8);
    ntransform(pBlock, pKeys, 9);
    #else

	int round;
	for (round=1; round<ROUNDS; round++)
	{
		// Fixme: Use non-arduino-specific debug
		#ifdef verbose_debug
		Serial.print("Encryption round ");
		Serial.println(round);
		#endif
  
		SubAndShift(pBlock); 
		MixColumns(pBlock);  
		AddRoundKey(pBlock, pKeys, round); 
	}
    #endif

	// Fixme: Use non-arduino-specific debug
	#ifdef verbose_debug
	Serial.println("Encryption round 10");
	#endif

	// Now, do the final round of encryption
	SubAndShift(pBlock);
	AddRoundKey(pBlock, pKeys, 10);  // add the last round key from the schedule
} //EncryptBlock()
Example #17
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);
}
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]);
}
Example #19
0
File: aes.c Project: 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
}
Example #20
0
void test_MixColumns_full_input_col2(void)
{
	int i;
	unsigned char expected[] = {
		0x00,   3, 0x00, 0x00,
		0x00,   4, 0x00, 0x00,
		0x00,   9, 0x00, 0x00,
		0x00,  10, 0x00, 0x00};

	unsigned char actual[] = {
		0x00, 0x01, 0x00, 0x00,
		0x00, 0x02, 0x00, 0x00,
		0x00, 0x03, 0x00, 0x00,
		0x00, 0x04, 0x00, 0x00};
	MixColumns(actual);

	for (i = 0; i < 4*4; i++)
	{
	  CU_ASSERT_INT_EQ(expected[i], actual[i]);
	}
}
Example #21
0
void test_MixColumns_full_input_low_values(void)
{
	int i;
	unsigned char expected[] = {
		 23,   3,  15,  11,
         16,   4,   8,  12,
         45,   9,  21,   1,
         54,  10,  30,   2};

	unsigned char actual[] = {
		 13,   1,   5,   9,
		 14,   2,   6,  10,
		 15,   3,   7,  11,
		 16,   4,   8,  12};
	MixColumns(actual);

	for (i = 0; i < 4*4; i++)
	{
	  CU_ASSERT_INT_EQ(expected[i], actual[i]);
	}
}
Example #22
0
File: aes.c Project: 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);
}
Example #23
0
File: aes.c Project: 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];
        }
    }
}
Example #24
0
void test_MixColumns_full_input_high_values(void)
{
	int i;
	unsigned char expected[] = {
		142, 159,   1, 198,
		 77, 220,   1, 198,
		161,  88,   1, 198,
		188, 157,   1, 198};

	unsigned char actual[] = {
	    219, 242,   1, 198,
		 19,  10,   1, 198,
		 83,  34,   1, 198,
		 69,  92,   1, 198};

	MixColumns(actual);

	for (i = 0; i < 4*4; i++)
	{
	  CU_ASSERT_INT_EQ(expected[i], actual[i]);
	}
}
Example #25
0
File: aes.c Project: 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);
}
Example #26
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);
}
Example #27
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];
}
Example #28
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;

}
Example #29
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]);
}
Example #30
0
File: AES.c Project: 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];
		}
	}
}