shifting array elements in c for shift row in AES

2.3k views Asked by At

I am trying to implement the shift row functionality(of AES encryption), I have written the following function but for the array elements corresponding to positions 2,3 3,2 and 3,3 are not getting exchanged correctly. Kindly have a look and advise...

expected output http://en.wikipedia.org/wiki/File:AES-ShiftRows.svg

void shiftRow(unsigned char **state){

int i,j,n; unsigned char *temp;

    temp =(unsigned char *)malloc(sizeof(unsigned char));

    for(i=1;i<4;i++){
        *temp =state[i][0];
        printf("\t%x",*temp);
        for(j=0;j<4;j++){
            n = ((i+j)>3?(i+j)-4:i+j);
            state[i][j]=state[i][n];
        }
        printf("\t%x\n",*temp);
        state[i][j-i]=*temp;
    }


}
4

There are 4 answers

0
Himanshu Sourav On BEST ANSWER

some workaround I had to do, not perfect solution but did the job...

void shiftRow(unsigned char **state){
    int i,j,n; unsigned char temp;

    //  temp =(unsigned char *)malloc(sizeof(unsigned char));

        for(i=1;i<2;i++){
            temp =state[i][0];
            //printf("\t%x",temp);
            for(j=0;j<4;j++){
                n = ((i+j)>3?(i+j)-4:i+j);
                state[i][j]=state[i][n];        
            }
            printf("\t%x\n",temp);
            state[i][j-i]=temp;

        }

            temp =state[2][0];
            state[2][0]=state[2][2];
            state[2][2]=temp;
            temp =state[2][1];
            state[2][1]=state[2][3];
            state[2][3]=temp;

            temp =state[3][0];
            state[3][0]=state[3][3];
            state[3][3]=state[3][2];
            state[3][2]=state[3][1];
            state[3][1]=temp;

    } //shiftrow ends

any other solution/approach would be deeply appreciated

0
Dracul On

So this is the vector I have before shifting it.

This vector [0 .. 15]

std::vector n_S_BOX = [63 c0 ab 20 eb 2f 30 cb 9f 93 af 2b a0 92 c7 a2]

    int five = 5;
    for(int i = 0; i < 16; i += 4)
    {
            for(int t = 0; t < 4; ++t)
            {
                    shift_Rows_V.push_back(n_S_BOX[(i + five*t)%16]);
            }
    }

To this [63 2f af a2 eb 93 c7 20 9f 92 ab cb a0 c0 30 2b]

0
Matthias On

That´s how i did it . Maybe it´s helpful for someone.

void AES::shiftRows(byte state[4][4])
{
    int sub = 1;    // the subtraction of numCol
    byte tmp, tmp1, tmp2;

    for (int numRow = 1; numRow < 4; numRow++)
    {
        tmp = state[numRow][0];

        if (sub == 2) {
            tmp1 = state[numRow][1];
        }
        else if (sub == 3)
        {
            tmp1 = state[numRow][1];
            tmp2 = state[numRow][2];
        }

        for (int numCol = 1; numCol < 4; numCol++) {
            if (numCol - sub >= 0)
            {
                state[numRow][numCol - sub] = state[numRow][numCol];        // be careful don´t go out of range
            }
        }

        if (sub == 1) {
            state[1][3] = tmp;
        }
        else if (sub == 2) {
            state[2][2] = tmp;
            state[2][3] = tmp1;
        }
        else if (sub == 3)
        {
            state[3][1] = tmp;
            state[3][2] = tmp1;
            state[3][3] = tmp2;
        }
        sub++;
    }
}
0
heduncook On

Here is my solution:

void shiftRows(uint8_t state[4][4])
{
    uint8_t tmp[4];
    for(int i = 1; i < 4; i++)
    {
        for(int j = 0; j < 4; j++)
        {
            tmp[j] = state[i][(j+i)%4];
        }
        for(int j = 0; j < 4; j++)
        {
            state[i][j] = tmp[j];
        }
    }
}