Create AES on C/JAVA || SubBytes / ShiftRows / Vulnerabilities (second part)
1 part - https://dev.to/dima853/create-aes-on-cjava-forward-s-box-first-part-4lef 1. SubBytes: Non-linear byte substitution Mathematical basis: Each byte of the State is replaced according to the S-box table. The S-box is being built in 2 stages: Finding the inverse element in the Galois field GF(28). Affine transformation (matrix multiplication + XOR with a constant). The S-box formula: S(x) = M ⋅ x-1 + C where: x is the input byte (8 bits) x⁻1 is the inverse element in the Galois field GF(2⁸) M is an affine transformation matrix (8×8 bits) C is the constant 0x63 (01100011 in binary) + is the XOR operation (addition in GF(2)) · - matrix multiplication (multiplication of bit strings) The multiplicative inverse element of byte x in the finite Galois field GF(28), where multiplication and addition are performed modulo an irreducible polynomial.” 2. ShiftRows: Cyclic row shifting The rows of the State matrix are shifted by 0, 1, 2, 3 bytes to the left, respectively. The goal is to shuffle bytes between columns to enhance diffusion. Visualization: Before ShiftRows: | a00 a01 a02 a03 | | a10 a11 a12 a13 | | a20 a21 a22 a23 | | a30 a31 a32 a33 | After ShiftRows: | a00 a01 a02 a03 | // Line 0: Without shift | a11 a12 a13 a10 | // Line 1: shift by 1 | a22 a23 a20 a21 | // Line 2: shift by 2 | a33 a30 a31 a32 | // Line 3: shift by 3 Code #include // Include the header file for uint8_t (unsigned 8-bit integer type) // Function: sub_bytes // Performs the SubBytes transformation in AES by substituting each byte in the state matrix // with the corresponding byte from the S-box. void sub_bytes(uint8_t state[4][4]) // The state is a 4x4 matrix of bytes (uint8_t) { for (int i = 0; i 4][state[i][j] & 0x0F]; // Substitute the byte at state[i][j] with the value from the sbox // Explanation of sbox indexing: // state[i][j] >> 4: Gets the upper nibble (4 bits) of the byte at state[i][j]. This is used as the row index for sbox. // state[i][j] & 0x0F: Gets the lower nibble (4 bits) of the byte at state[i][j]. This is used as the column index for sbox. // The value in the sbox at the row and column determined by these nibbles is then used to replace the original value in state. } } } // Function: swap // Swaps the values of two uint8_t variables using pointers. void swap(uint8_t *a, uint8_t *b) // Takes two pointers to uint8_t as input { uint8_t temp = *a; // Store the value pointed to by 'a' in a temporary variable 'temp' *a = *b; // Assign the value pointed to by 'b' to the memory location pointed to by 'a' *b = temp; // Assign the value in 'temp' (the original value of 'a') to the memory location pointed to by 'b' } // Function: shift_rows // Performs the ShiftRows transformation in AES by cyclically shifting the rows of the state matrix. void shift_rows(uint8_t state[4][4]) // The state is a 4x4 matrix of bytes (uint8_t) { // Row 1: cyclic shift by 1 byte to the left uint8_t tmp = state[1][0]; // Store the first element of the second row in a temporary variable state[1][0] = state[1][1]; // Shift the second element to the first position state[1][1] = state[1][2]; // Shift the third element to the second position state[1][2] = state[1][3]; // Shift the fourth element to the third position state[1][3] = tmp; // Assign the temporary variable (original first element) to the fourth position // Row 2: cyclic shift by 2 bytes to the left (implemented with swap) swap(&state[2][0], &state[2][2]); // Swap the first and third elements of the third row swap(&state[2][1], &state[2][3]); // Swap the second and fourth elements of the third row // Row 3: cyclic shift by 3 bytes to the left (equivalent to shifting 1 byte to the right) tmp = state[3][3]; // Store the last element of the fourth row in a temporary variable state[3][3] = state[3][2]; // Shift the third element to the fourth position state[3][2] = state[3][1]; // Shift the second element to the third position state[3][1] = state[3][0]; // Shift the first element to the second position state[3][0] = tmp; // Assign the temporary variable (original last element) to the first position } 3. AES vulnerabilities Although AES (Advanced Encryption Standard) is one of the most reliable and widely used encryption algorithms, it is not completely invulnerable. There are various types of attacks that can be applied to AES, although many of them are complex and require significant resources or specific conditions. It is important to understand that most successful attacks on AES exploit not the flaws of the algorithm itself, but weaknesses in its implementation or in protocols using AES. Here is an overview of the main types of vulnerabilities and attacks on AES: 1. Side-Channel Attacks: Timing Attacks: These attacks use

1 part - https://dev.to/dima853/create-aes-on-cjava-forward-s-box-first-part-4lef
1. SubBytes: Non-linear byte substitution
Mathematical basis:
- Each byte of the State is replaced according to the S-box table.
- The S-box is being built in 2 stages:
- Finding the inverse element in the Galois field GF(28).
- Affine transformation (matrix multiplication + XOR with a constant).
The S-box formula:
S(x) = M ⋅ x-1 + C
where:
- x is the input byte (8 bits)
- x⁻1 is the inverse element in the Galois field GF(2⁸)
- M is an affine transformation matrix (8×8 bits)
- C is the constant 0x63 (01100011 in binary)
- + is the XOR operation (addition in GF(2))
- · - matrix multiplication (multiplication of bit strings)
- The multiplicative inverse element of byte x in the finite Galois field GF(28), where multiplication and addition are performed modulo an irreducible polynomial.”
2. ShiftRows: Cyclic row shifting
The rows of the State matrix are shifted by 0, 1, 2, 3 bytes to the left, respectively.
The goal is to shuffle bytes between columns to enhance diffusion.
Visualization:
Before ShiftRows:
| a00 a01 a02 a03 |
| a10 a11 a12 a13 |
| a20 a21 a22 a23 |
| a30 a31 a32 a33 |
After ShiftRows:
| a00 a01 a02 a03 | // Line 0: Without shift
| a11 a12 a13 a10 | // Line 1: shift by 1
| a22 a23 a20 a21 | // Line 2: shift by 2
| a33 a30 a31 a32 | // Line 3: shift by 3
Code
#include // Include the header file for uint8_t (unsigned 8-bit integer type)
// Function: sub_bytes
// Performs the SubBytes transformation in AES by substituting each byte in the state matrix
// with the corresponding byte from the S-box.
void sub_bytes(uint8_t state[4][4]) // The state is a 4x4 matrix of bytes (uint8_t)
{
for (int i = 0; i < 4; i++) // Iterate through the rows of the state matrix
{
for (int j = 0; j < 4; j++) // Iterate through the columns of the state matrix
{
state[i][j] = sbox[state[i][j] >> 4][state[i][j] & 0x0F]; // Substitute the byte at state[i][j] with the value from the sbox
// Explanation of sbox indexing:
// state[i][j] >> 4: Gets the upper nibble (4 bits) of the byte at state[i][j]. This is used as the row index for sbox.
// state[i][j] & 0x0F: Gets the lower nibble (4 bits) of the byte at state[i][j]. This is used as the column index for sbox.
// The value in the sbox at the row and column determined by these nibbles is then used to replace the original value in state.
}
}
}
// Function: swap
// Swaps the values of two uint8_t variables using pointers.
void swap(uint8_t *a, uint8_t *b) // Takes two pointers to uint8_t as input
{
uint8_t temp = *a; // Store the value pointed to by 'a' in a temporary variable 'temp'
*a = *b; // Assign the value pointed to by 'b' to the memory location pointed to by 'a'
*b = temp; // Assign the value in 'temp' (the original value of 'a') to the memory location pointed to by 'b'
}
// Function: shift_rows
// Performs the ShiftRows transformation in AES by cyclically shifting the rows of the state matrix.
void shift_rows(uint8_t state[4][4]) // The state is a 4x4 matrix of bytes (uint8_t)
{
// Row 1: cyclic shift by 1 byte to the left
uint8_t tmp = state[1][0]; // Store the first element of the second row in a temporary variable
state[1][0] = state[1][1]; // Shift the second element to the first position
state[1][1] = state[1][2]; // Shift the third element to the second position
state[1][2] = state[1][3]; // Shift the fourth element to the third position
state[1][3] = tmp; // Assign the temporary variable (original first element) to the fourth position
// Row 2: cyclic shift by 2 bytes to the left (implemented with swap)
swap(&state[2][0], &state[2][2]); // Swap the first and third elements of the third row
swap(&state[2][1], &state[2][3]); // Swap the second and fourth elements of the third row
// Row 3: cyclic shift by 3 bytes to the left (equivalent to shifting 1 byte to the right)
tmp = state[3][3]; // Store the last element of the fourth row in a temporary variable
state[3][3] = state[3][2]; // Shift the third element to the fourth position
state[3][2] = state[3][1]; // Shift the second element to the third position
state[3][1] = state[3][0]; // Shift the first element to the second position
state[3][0] = tmp; // Assign the temporary variable (original last element) to the first position
}
3. AES vulnerabilities
Although AES (Advanced Encryption Standard) is one of the most reliable and widely used encryption algorithms, it is not completely invulnerable. There are various types of attacks that can be applied to AES, although many of them are complex and require significant resources or specific conditions. It is important to understand that most successful attacks on AES exploit not the flaws of the algorithm itself, but weaknesses in its implementation or in protocols using AES.
Here is an overview of the main types of vulnerabilities and attacks on AES:
1. Side-Channel Attacks:
- Timing Attacks: These attacks use information about the timing of encryption operations to reveal the secret key. Different operations in AES can take different time depending on the key, and these differences can be measured.
- Energy Consumption Attacks (Power Analysis Attacks): These attacks analyze the device's power consumption during encryption. Differences in power consumption may be related to the secret key. There are simple Power consumption attacks (SPA) and Differential Power Consumption attacks (DPA), which are more complex and effective.
- Electromagnetic Radiation Attacks (Electromagnetic Analysis - EMA): Similar to attacks on energy consumption, but they analyze the electromagnetic radiation of the device.
- Error Injection Attacks: These attacks intentionally introduce errors into the encryption process (for example, by changing the supply voltage or using a laser) and analyze the output data with errors in order to obtain information about the key.
- Acoustic attacks: In rare cases, you can get information about the key from the sounds made by the device during encryption.
Countermeasures against attacks through third-party channels:
- Masking: Splitting secret data into random parts to hide the relationship between data and power consumption or execution time.
- Hiding: Equalization of power consumption or execution time of all operations to make analysis more difficult.
- Using algorithm-specific countermeasures: For example, the use of time-attack-resistant search tables.
2. Key Recovery Attacks:
- Brute-Force Attack: Iterate through all possible keys until the correct one is found. The effectiveness of the brute-force attack depends on the key length. AES with 128, 192, and 256-bit keys is considered resistant to brute-force attacks using modern computing power.
- Meet-in-the-Middle Attack: This attack applies to encryption schemes that use multiple encryption steps. She is trying to find a key that matches both encryption and decryption, "meeting in the middle."
- Related-Key Attacks: These attacks exploit vulnerabilities in key management schemes. If an attacker can gain access to multiple keys that are linked together (for example, keys derived from a single master key), they can use this information to recover the secret key.
3. Mathematical Attacks (Cryptanalytic Attacks):
- Linear Cryptanalysis: An attempt to build linear approximations (approximations) for encryption operations and use them to recover the key.
- Differential Cryptanalysis: Analysis of how differences in input data affect differences in output data. An attempt to find patterns that can be used to recover the key.
- Algebraic Attacks: Representation of the encryption algorithm in the form of a system of algebraic equations and an attempt to solve this system to recover the key.
It is important to note:
- AES is considered resistant to mathematical attacks: To date, there are no known practical mathematical attacks on AES that would be more effective than brute-force. However, research in this area continues.
- Complexity of implementation: Implementing AES in practice can be challenging, and implementation errors can lead to vulnerabilities. For example, improper use of the encryption mode, weak random number generation for the IV (Initialization Vector), or insecure key storage can create serious problems.
- Attacks on protocols using AES: Vulnerabilities are often found not in AES itself, but in the protocols that use it. For example, vulnerabilities in the TLS (Transport Layer Security) protocol can allow an attacker to bypass AES encryption.
Operating scenarios:
Most successful attacks on AES occur in the following scenarios:
- Embedded systems and devices with limited resources: These devices are often vulnerable to third-party attacks due to their limited protection capabilities.
- Incorrect implementations: Errors in the software or hardware implementation of AES can open the door to various attacks.
- Keys stored in an unsafe location: If an attacker can gain access to the key (for example, if it is stored in memory or on disk in an unencrypted form), then AES becomes useless.
Output:
AES remains a strong encryption algorithm, but its security depends on existing protocols.
(we will analyze in detail some of the methods with examples of attacks)