Exemplo n.º 1
0
        void invShiftRows(byte [][] inv_state)
        {
            ///no inverse shift row is to be applied on row 0/

            ///inverse shift row operation on row 1/
            for (ushort i = 3; i > 0; i--)
            {
                AES_CxDecipher.swap(ref inv_state [1] [i], ref inv_state [1] [i - 1]);
            }

            ///inverse shift row operation on row 2/
            for (ushort i = 0; i < 2; i++)
            {
                for (ushort j = 3; j > 0; j--)
                {
                    AES_CxDecipher.swap(ref inv_state [2] [j], ref inv_state [2] [j - 1]);
                }
            }

            ///inverse shift row operation on row 3/
            for (ushort i = 0; i < 3; i++)
            {
                for (ushort j = 3; j > 0; j--)
                {
                    AES_CxDecipher.swap(ref inv_state [3] [j], ref inv_state [3] [j - 1]);
                }
            }
        }
Exemplo n.º 2
0
        void shiftRows(byte [][] state)
        {
            ///no shift row is to be applied on row 0/

            ///shift row operation on row 1/
            for (ushort i = 0; i < 3; i++)
            {
                AES_CxDecipher.swap(ref state[1][i], ref state[1][i + 1]);
            }

            ///shift row operation on row 2/
            for (ushort i = 0; i < 2; i++)
            {
                for (ushort j = 0; j < 3; j++)
                {
                    AES_CxDecipher.swap(ref state [2] [j], ref state [2] [j + 1]);
                }
            }

            ///shift row operation on row 3/
            for (ushort i = 0; i < 3; i++)
            {
                for (ushort j = 0; j < 3; j++)
                {
                    AES_CxDecipher.swap(ref state [3] [j], ref state [3] [j + 1]);
                }
            }
        }
Exemplo n.º 3
0
 void invSubBytes(byte [][] inv_state)
 {
     for (ushort i = 0; i < 4; i++)
     {
         for (ushort j = 0; j < 4; j++)
         {
             inv_state [i] [j] = AES_CxDecipher.get_invSBOX_value(inv_state [i] [j]);
         }
     }
 }
Exemplo n.º 4
0
 void subBytes(byte [][] state)
 {
     for (ushort i = 0; i < 4; i++)
     {
         for (ushort j = 0; j < 4; j++)
         {
             state[i][j] = AES_CxDecipher.get_SBOX_value(state [i][j]);
         }
     }
 }
Exemplo n.º 5
0
        void keyExpansion(byte[] key, byte[] keySchedule)
        {
            byte[] temp = new byte[4];
            ushort i    = 0;

            for (; i < this.Nk; i++)
            {
                for (ushort j = 0; j < 4; j++)
                {
                    keySchedule [4 * i + j] = key [4 * i + j];
                }
            }

            i = (ushort)this.Nk;

            while (i < (4 * (this.Nr + 1)))
            {
                for (ushort j = 0; j < 4; j++)
                {
                    temp[j] = keySchedule[4 * (i - 1) + j];
                }

                if (i % this.Nk == 0)
                {
                    for (ushort j = 0; j < 3; j++)                    ///RotWord SubRoutine/
                    {
                        AES_CxDecipher.swap(ref temp [j], ref temp [j + 1]);
                    }

                    for (ushort j = 0; j < 4; j++)                    ///SubWord SubRoutine/
                    {
                        temp[j] = get_SBOX_value(temp[j]);
                    }

                    for (ushort j = 0; j < 4; j++)
                    {
                        if (j == 0)
                        {
                            temp[j] ^= R_CON[(i / this.Nk) - 1];
                        }

                        else
                        {
                            temp[j] ^= 0x00;
                        }
                    }
                }

                else if ((this.Nk > 6) && (i % this.Nk == 4))
                {
                    for (ushort j = 0; j < 4; j++)                    ///SubWord SubRoutine/
                    {
                        temp[j] = get_SBOX_value(temp[j]);
                    }
                }

                for (ushort j = 0; j < 4; j++)
                {
                    keySchedule [(4 * i) + j] = (byte)(keySchedule [(4 * (i - this.Nk)) + j] ^ temp [j]);
                }

                i += 1;
            }
        }