Cryptography - Shiftrows Transformation



One linear unit of symmetric encryption methods in AES is the ShiftRows transformation. It is a transposition operation in which the state's rows are shifted repeatedly multiple times. The purpose of this function is to provide diffusion of bits across multiple rounds by randomly arranging the bits within each 128-bit block.

The state rows are shifted as follows as a result of this transformation: the first row remains unchanged, the second row is shifted to the left by one byte, the third row is shifted to the left by two bytes, and the last row is shifted to the left by three bytes.

AES without ShiftRows stage are greater than AES; they hardly change in value during rounds. This is attributed to poor encryption quality.

The most difficult part of the AES algorithm is linear and differential cryptanalysis, where common approaches for both can be used to solve Rijndael.

The arbitrary unknown and key-dependent substitution and permutation transformations are thought to be a good factor in enhancing the resistance of the block cipher against the differential and linear attacks, since the attacks require known transformations, according to the analysis of resistance against differential and linear cryptanalysis.

How it Works?

One of the components of the AES (Advanced Encryption Standard) algorithm is the ShiftRows transformation. It is a step in the data encryption process. The AES state is a grid of bytes, and in this stage, the rows are cyclically shifted. Let us simplify it −

  • State Matrix − Data is arranged by the AES algorithm into a state matrix, which is a grid of bytes. Usually, there are four rows and four columns in this matrix.
  • ShiftRows Step − The bytes in each row of the state matrix are moved to the left in this phase. There is no shift in the first row. One position has been relocated to the left in the second row, two positions have been moved to the left in the third row, and three locations have been moved to the left in the fourth row. The bytes that are moved out of one end of the row are placed back in at the other end since this shifting is done cyclically.

Example − Let's use the following state matrix as an example −

0x01  0x02  0x03  0x04
0x05  0x06  0x07  0x08
0x09  0x0A  0x0B  0x0C
0x0D  0x0E  0x0F  0x10

After the ShiftRows stage, it becomes −

0x01  0x02  0x03  0x04
0x06  0x07  0x08  0x05
0x0B  0x0C  0x09  0x0A
0x10  0x0D  0x0E  0x0F

Each row, as you can see, has been moved a specific number of positions to the left.

  • Purpose − The ShiftRows stage increases the difficulty of identifying patterns in the data by including diffusion into the encryption process.

Implementation using Python

The shift_rows function defined in code is used to implement the ShiftRows transformation within the context of the AES (Advanced Encryption Standard) algorithm. The shift_rows function takes a state matrix as input, performs the ShiftRows transformation, and outputs the changed state matrix.

Example

def shift_rows(state):
   for i in range(1, 4):
      state[i] = state[i][i:] + state[i][:i]
   return state

# function execution
state_matrix = [
   [0x01, 0x02, 0x03, 0x04],
   [0x05, 0x06, 0x07, 0x08],
   [0x09, 0x0A, 0x0B, 0x0C],
   [0x0D, 0x0E, 0x0F, 0x10]
]

shifted_state = shift_rows(state_matrix)
for row in shifted_state:
   print(' '.join(format(x, '02X') for x in row))

Following is the output of the above example −

Input/Output

01 02 03 04
06 07 08 05
0B 0C 09 0A
10 0D 0E 0F

Implementation using Java

This Java code shows how to apply the ShiftRows transformation in the context of the AES encryption algorithm by changing the state matrix. It also provides an effective implementation of the change. See the code below −

Example

// AES Class for shiftrows transformation
public class AES {
   public static byte[][] shiftRows(byte[][] state) {
      for (int i = 1; i < 4; i++) {
         byte[] temp = new byte[4];
         for (int j = 0; j < 4; j++) {
            temp[j] = state[i][(j - i + 4) % 4]; 
         }
         state[i] = temp;
      }
      return state;
   }
   // Main function
   public static void main(String[] args) {
      byte[][] stateMatrix = {
         {0x01, 0x02, 0x03, 0x04},
         {0x05, 0x06, 0x07, 0x08},
         {0x09, 0x0A, 0x0B, 0x0C},
         {0x0D, 0x0E, 0x0F, 0x10}
      };

      byte[][] shiftedState = shiftRows(stateMatrix);

      for (byte[] row : shiftedState) {
         for (byte b : row) {
            System.out.print(String.format("%02X ", b));
         }
         System.out.println();
      }
   }
}

Following is the output of the above example −

Input/Output

01 02 03 04 
08 05 06 07 
0B 0C 09 0A 
0E 0F 10 0D 

Implementation using C++

The ShiftRows transformation, an important phase in the AES encryption process, is demonstrated in this C++ code. C++ vectors and the std::rotate function are used to perform the cyclic left shifts. Below is the implementation using C++ −

Example

#include <iostream>
#include <vector>
#include <algorithm> // Include the algorithm header

std::vector<std::vector<int>> shiftRows(std::vector<std::vector<int>> state) {
   for (int i = 1; i < 4; i++) {
      std::rotate(state[i].begin(), state[i].begin() + i, state[i].end());
   }
   return state;
}

int main() {
   std::vector<std::vector<int>> stateMatrix = {
      {0x01, 0x02, 0x03, 0x04},
      {0x05, 0x06, 0x07, 0x08},
      {0x09, 0x0A, 0x0B, 0x0C},
      {0x0D, 0x0E, 0x0F, 0x10}
   };

   auto shiftedState = shiftRows(stateMatrix);

   for (const auto& row : shiftedState) {
      for (int val : row) {
         std::cout << std::hex << val << " ";
      }
      std::cout << std::endl;
   }
   return 0;
}

Following is the output of the above example −

Input/Output

1 2 3 4 
6 7 8 5 
b c 9 a 
10 d e f 

Summary

This chapter provides a description of the ShiftRows transformation, an important phase in the symmetric encryption process known as AES (Advanced Encryption Standard). The ShiftRows transformation in AES encryption requires cyclically shifting the state matrix's rows. The chapter also includes implementations of the ShiftRows transformation in Python, Java, and C++, demonstrating how to apply it in the context of AES encryption using a variety of programming languages and methodologies.

Advertisements