☕
Pause for minutes until .
Remaining:
Pius Walter
T4INF1202: Kryptologie
Part 6: AES and Cipher Implementation
2026-05-05
Recap
  • Last week, we talked about Feistel networks as a generalized way to construct invertible ciphers
  • The advantage was that the Feistel function need not be invertible
  • Today, we will cover AESAdvanced Encryption Standard, a substitution-permutation based cipher
  • We will also talk a bit about implementation in a programming language
Basic operations
  • Recall that in a Feistel network, the general construction was given, but the function Q4mta4Ck5NLHFZf1qH71g5gJY was opaque
  • There are various different types of Feistel networks, we've looked at Classical Feistel; others are e.g., Alternating, Numeric, Type-1, Type-2, Type-3 Feistel
  • Hoang and Rogaway 2010: On Generalized Feistel Networks
Basic operations
  • Inside the Feistel function Q4mta4Ck5NLHFZf1qH71g5gJY, we've seen:
    • Substitutions (e.g., S-boxes)
    • Permutations (e.g., bit shifts and rotations)
  • A cipher that only uses substitutions/permutations without the Feistel structure is called an SP-network (substitution-permutation)
  • Needs to ensure invertibility on its own
Standardization
  • After it was clear that DESData Encryption Standard is not secure enough anymore, NISTNational Institute of Standards and Technology held a cipher competition in 1997
  • The formal requirements for the algorithm were:
    • Block ciphers with 128 bit block size and 128, 192 and 256 bit keys (uncommon at the time)
    • Non patent-encumbered, royalty free
    • Good performance in software and hardware (size and speed)
    • Secure against all known attacks (e.g., differential cryptanalysis)
AES Candidates
AES Finalists
AES Candidates
  • Rijndael, named after their authors (Rijmen and Daemen) won the competition
  • It showed the best security-performance relationship
  • Rijndael is therefore now also known as AESAdvanced Encryption Standard
  • It uses 10, 12 or 14 rounds, depending on the key length (AES-128, AES-192, AES-256)
Algorithmic steps
  • AESAdvanced Encryption Standard has four internal primitive functions that are applied:
    • SubBytes: Substitution on byte basis
    • ShiftRows: Permutation on byte basis
    • MixColumns: Permutation on bit basis
    • AddRoundKey: XOR of bytes with the round-specific subkey
  • Application on an interative per-round basis
AES S-box construction
  • AESAdvanced Encryption Standard uses an Q4mta4Ck5NLH8 \times 8Zf1qH71g5gJY S-box (i.e., an uint8_t[256])
  • Arithmetic operations in Q4mta4Ck5NLH\mathbb{F}_{2^8}Zf1qH71g5gJY using the division polynomial Q4mta4Ck5NLHx^8 + x^4 + x^3 + x + 1Zf1qH71g5gJY (sometimes also called the field of AESAdvanced Encryption Standard)
  • Minimal irreducible polynomial (monic, least amount of terms, coefficients mainly in low exponents)
AES S-box construction
  • In this field, every element (but zero) has an inverse element
  • For AESAdvanced Encryption Standard, the input is first inverted and then an (invertible) affine transformation is applied
  • Essentially, this is a field inversion followed by a matrix multiplication
  • Both can be collapsed into a lookup table
AES S-box example
  • Q4mta4Ck5NLHq = \texttt{\{ab\}} = x^7 + x^5 + x^3 + x + 1Zf1qH71g5gJY
  • Q4mta4Ck5NLHq^{-1} = x^6 + x^3 + x = \texttt{\{4a\}}Zf1qH71g5gJY
  • Q4mta4Ck5NLHq \cdot q^{-1} = \texttt{\{ab\}} \cdot \texttt{\{4a\}} = 1 = \texttt{\{01\}}Zf1qH71g5gJY
QBEntEozXWio s(q) = \begin{pmatrix} 1 & 0 & 0 & 0 & 1 & 1 & 1 & 1\\ 1 & 1 & 0 & 0 & 0 & 1 & 1 & 1\\ 1 & 1 & 1 & 0 & 0 & 0 & 1 & 1\\ 1 & 1 & 1 & 1 & 0 & 0 & 0 & 1\\ 1 & 1 & 1 & 1 & 1 & 0 & 0 & 0\\ 0 & 1 & 1 & 1 & 1 & 1 & 0 & 0\\ 0 & 0 & 1 & 1 & 1 & 1 & 1 & 0\\ 0 & 0 & 0 & 1 & 1 & 1 & 1 & 1\\ \end{pmatrix} \cdot q^{-1} + \begin{pmatrix} 1\\ 1\\ 0\\ 0\\ 0\\ 1\\ 1\\ 0\\ \end{pmatrix} kIt7msRdzcHK
AES S-box example
  • Input and output bytes are read as column vectors
  • Least significant bit is at the top
  • Inversion and affine transformation are typically combined in one step (already precomputed in the S-box)
  • For decryption, an inverted S-box is necessary that performs the steps in the reversed order (invert the affine transformation and invert the substitution)
AES round functions
  • When applying the individual steps, the 16-byte blocks are typically read as a Q4mta4Ck5NLH4 \times 4Zf1qH71g5gJY array of one byte each
  • Only useful for visualisation, in memory blocks are simple arrays (uint8_t[16])
AES round functions
SubBytes
ShiftRows
  • The ShiftRows operation is a simple permutation on a byte basis
  • Then, each row is rotated for a specific amount:
    • first row is not rotated
    • second row is rotated 1 byte to the left
    • third row is rotated 2 bytes to the left
    • fourth row is rotated 3 bytes to the left
ShiftRows
  • Terminology may be a bit confusing: shifting usually discards everything that is shifted out of the register
  • This does not happen here; firstly because we do not shift bits, but bytes
  • Rotation is shifting but instead of discarding the bits, they are fed back into the other side of the register
  • Sometimes, it is more precisely synonymously referred to as a circular shift
ShiftRows
Shifting vs. rotation
  • Bit shifts are also frequently used in cryptography
  • Generally, shifting cannot be inverted because information may be lost (shifted out the register)
  • Some tricks need to be used (such as those used by Feistel networks)
  • Rotation can always be inverted (rotation to the other direction by the same amount of bits)
MixColumns
  • MixColumns is a bit more complicated; each column is interpreted as four polynomials in Q4mta4Ck5NLH\mathbb{F}_{2^8}Zf1qH71g5gJY
  • Then, a polynomial multiplication is performed: Q4mta4Ck5NLHb_3 X^3 + b_2 X^2 + b_1 X + b_0Zf1qH71g5gJY
  • The result is then reduced modulo Q4mta4Ck5NLHX^4 + 1Zf1qH71g5gJY
MixColumns
  • Polynomials with which the values are multiplied are part of an MDSMaximum distance separable matrix
  • This ensures good mixing properties: confusion and diffusion; avalanche property
  • There are other MDSMaximum distance separable matrices, but this one is, for the aforementioned reason, particularly efficient
MixColumns
QBEntEozXWio M = \begin{pmatrix} x & x+1 & 1 & 1\\ 1 & x & x+1 & 1\\ 1 & 1 & x & x+1\\ x+1 & 1 & 1 & x\\ \end{pmatrix} = kIt7msRdzcHK
QBEntEozXWio = \begin{pmatrix} \texttt{\{02\}} & \texttt{\{03\}} & \texttt{\{01\}} & \texttt{\{01\}}\\ \texttt{\{01\}} & \texttt{\{02\}} & \texttt{\{03\}} & \texttt{\{01\}}\\ \texttt{\{01\}} & \texttt{\{01\}} & \texttt{\{02\}} & \texttt{\{03\}}\\ \texttt{\{03\}} & \texttt{\{01\}} & \texttt{\{01\}} & \texttt{\{02\}}\\ \end{pmatrix} kIt7msRdzcHK
MixColumns
  • Note that literature sometimes switches back and forth between different conventions for representing these polynomials
  • Sometimes, the integer representation is used for convenience purposes (more on that later)
  • Can be a bit confusing, we will use mainly polynomial notation here
MixColumns
  • Each column vector Q4mta4Ck5NLH\vec{c}Zf1qH71g5gJY is multiplied with Q4mta4Ck5NLHMZf1qH71g5gJY individually and returns a new column vector Q4mta4Ck5NLH\vec{y}Zf1qH71g5gJY
  • QBEntEozXWio \vec{c}_i = \begin{pmatrix} x_{4i}\\ x_{4i+1}\\ x_{4i+2}\\ x_{4i+3}\\ \end{pmatrix},\ \ i = [0 \ldots 3] kIt7msRdzcHK
  • Q4mta4Ck5NLHM\vec{c} = \vec{y}Zf1qH71g5gJY
MixColumns
  • This gives a resulting polynomial with only four coefficients
  • They can be easily combined using XOR operations by using a small precomputation (terms are only Q4mta4Ck5NLH0, 1, x, x+1Zf1qH71g5gJY)
  • Multiplication with 0 and 1 are trivial; with Q4mta4Ck5NLHxZf1qH71g5gJY is a shift operation followed by modular reduction. Q4mta4Ck5NLHx+1Zf1qH71g5gJY is the XOR of Q4mta4Ck5NLHxZf1qH71g5gJY and Q4mta4Ck5NLH1Zf1qH71g5gJY
  • Effectively, this mixes bytes and bits within the columns of the block
MixColumns
MixColumns example
QBEntEozXWio B = \begin{pmatrix} \texttt{\{12\}} & \texttt{\{34\}} & \texttt{\{56\}} & \texttt{\{78\}}\\ \texttt{\{c0\}} & \texttt{\{ff\}} & \texttt{\{ee\}} & \texttt{\{11\}}\\ \texttt{\{de\}} & \texttt{\{ad\}} & \texttt{\{be\}} & \texttt{\{ef\}}\\ \texttt{\{ba\}} & \texttt{\{ad\}} & \texttt{\{c0\}} & \texttt{\{de\}}\\ \end{pmatrix} kIt7msRdzcHK
QBEntEozXWio \vec{c}_0 = \begin{pmatrix} \texttt{\{12\}}\\ \texttt{\{c0\}}\\ \texttt{\{de\}}\\ \texttt{\{ba\}}\\ \end{pmatrix} kIt7msRdzcHK
MixColumns example
QBEntEozXWio M\vec{c}_0 = \begin{pmatrix} \texttt{\{02\}} & \texttt{\{03\}} & \texttt{\{01\}} & \texttt{\{01\}}\\ \texttt{\{01\}} & \texttt{\{02\}} & \texttt{\{03\}} & \texttt{\{01\}}\\ \texttt{\{01\}} & \texttt{\{01\}} & \texttt{\{02\}} & \texttt{\{03\}}\\ \texttt{\{03\}} & \texttt{\{01\}} & \texttt{\{01\}} & \texttt{\{02\}}\\ \end{pmatrix} \cdot \begin{pmatrix} \texttt{\{12\}}\\ \texttt{\{c0\}}\\ \texttt{\{de\}}\\ \texttt{\{ba\}}\\ \end{pmatrix} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}}\ + kIt7msRdzcHK
QBEntEozXWio + \texttt{\{01\}} \cdot \texttt{\{de\}} + \texttt{\{01\}} \cdot \texttt{\{ba\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}}\ + kIt7msRdzcHK
QBEntEozXWio + \texttt{\{01\}} \cdot \texttt{\{de\}} + \texttt{\{01\}} \cdot \texttt{\{ba\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}}\ + kIt7msRdzcHK
QBEntEozXWio + \texttt{\{01\}} \cdot \texttt{\{de\}} + \texttt{\{01\}} \cdot \texttt{\{ba\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{de\}} + \texttt{\{ba\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}}\ + kIt7msRdzcHK
QBEntEozXWio + \texttt{\{01\}} \cdot \texttt{\{de\}} + \texttt{\{01\}} \cdot \texttt{\{ba\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{de\}} + \texttt{\{ba\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}}\ + kIt7msRdzcHK
QBEntEozXWio + \texttt{\{01\}} \cdot \texttt{\{de\}} + \texttt{\{01\}} \cdot \texttt{\{ba\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{de\}} + \texttt{\{ba\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{24\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{24\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{40\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{24\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{40\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = (\texttt{\{01\}} \cdot \texttt{\{c0\}} + \texttt{\{02\}} \cdot \texttt{\{c0\}}) + \texttt{\{40\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{24\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{40\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = (\texttt{\{01\}} \cdot \texttt{\{c0\}} + \texttt{\{02\}} \cdot \texttt{\{c0\}}) + \texttt{\{40\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{c0\}} + \texttt{\{c0\}} + \texttt{\{40\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = x \cdot \texttt{\{12\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{24\}} + \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{64\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{03\}} \cdot \texttt{\{c0\}} + \texttt{\{40\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = (\texttt{\{01\}} \cdot \texttt{\{c0\}} + \texttt{\{02\}} \cdot \texttt{\{c0\}}) + \texttt{\{40\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{c0\}} + \texttt{\{c0\}} + \texttt{\{40\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{c0\}} + \texttt{\{80\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{c0\}} + \texttt{\{80\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{c0\}} + \texttt{\{80\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{180\}} + \texttt{\{80\}} kIt7msRdzcHK
MixColumns example
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{02\}} \cdot \texttt{\{c0\}} + \texttt{\{80\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{180\}} + \texttt{\{80\}} kIt7msRdzcHK
QBEntEozXWio \vec{y}_{0,0} = \texttt{\{180\}} + \texttt{\{11b\}} + \texttt{\{80\}} = \texttt{\{1b\}} kIt7msRdzcHK
MixColumns example
uint8_t B[16] = {
    0x12, 0xc0, 0xde, 0xba,
    0x34, 0xff, 0xad, 0xad,
    0x56, 0xee, 0xbe, 0xc0,
    0x78, 0x11, 0xef, 0xde
};
aes_mix_columns(B);
for (int i = 0; i < 16; i++) {
    printf("%02x ", B[i]);
}
printf("\n");
// result: 1b 4a a0 47 72 90 66 4f fb 88 84 31 f2 ae d5 d1
AddRoundKey
  • AddRoundKey is probably the easiest operation of them all
  • Each round key is added on the block (each round key has also 128 bits) – the respective round key is added in Q4mta4Ck5NLH\mathbb{F}_{2^8}Zf1qH71g5gJY
  • Simple bytewise/blockwise XOR operation
AddRoundKey
Key schedule
  • The key schedule uses all these operations as well, which is why we cover it last
  • Additionally, it uses a fairly trivial RotWord operation which rotates words in the input block (similar to ShiftRows)
  • It uses the AESAdvanced Encryption Standard S-box and adds a predefined round constant at each round (different for each round)
  • In AESAdvanced Encryption Standard, Q4mta4Ck5NLHK_1 = KZf1qH71g5gJY
Representation of polynomials
  • As we have seen, bytes of blocks are regularly interpreted as polynomials in the AESAdvanced Encryption Standard field
  • Those have up to eight coefficients which are all in Q4mta4Ck5NLH\mathbb{F}_2Zf1qH71g5gJY
  • How would you efficiently implement this? What is the most compact representation?
Representation of polynomials
  • Since every coefficient is one bit, we need 8 bits for every polynomial in Q4mta4Ck5NLH\mathbb{F}_{2^8}Zf1qH71g5gJY
  • This means every polynomial is exactly one byte, and it's the preferred way of storing polynomials – also for other fields like the Q4mta4Ck5NLH\mathbb{F}_{2^{128}}Zf1qH71g5gJY used in AESAdvanced Encryption Standard-GCMGalois/Counter Mode
  • What notation maps bits to coefficients? In other words, does the value Q4mta4Ck5NLH\texttt{\{04\}}Zf1qH71g5gJY mean the polynomial Q4mta4Ck5NLHx^2Zf1qH71g5gJY or Q4mta4Ck5NLHx^5Zf1qH71g5gJY?
Representation of polynomials
Multiplication of polynomials
  • For now, let's assume that we have a polynomial given in byte form the way that it is typically used.
  • Let's say we want to multiply this polynomial by Q4mta4Ck5NLHxZf1qH71g5gJY – what is the operation we're doing on the byte?
Multiplication of polynomials
  • Multiplication by Q4mta4Ck5NLHxZf1qH71g5gJY is simply a shift-left by one instruction, which makes it delightfully efficient on computers
  • There is one caveat: What happens if we have a byte value (e.g., a uint8_t in C) of which the leftmost bit is 1 and we shift left by one?
  • The bit is lost! Is this what we want?
Multiplication of polynomials
  • Of course not! The proper way of doing this is to perform a modular reduction.
  • For example, say we've just multiplied Q4mta4Ck5NLHx^7Zf1qH71g5gJY by Q4mta4Ck5NLHxZf1qH71g5gJY, this gives Q4mta4Ck5NLHx^8Zf1qH71g5gJY
  • We need to do a modular reduction and, in this case, can simply subtract the field polynomial
  • Luckily, subtraction is addition is XOR – again, delightfully efficient
Multiplication of polynomials
  • Some cases require modular reduction, others don't.
  • In particular, for multiplying with Q4mta4Ck5NLHxZf1qH71g5gJY, modular reduction is only necessary if Q4mta4Ck5NLHx^7Zf1qH71g5gJY was present in the polynomial
  • How do we check this in a byte representation?
Multiplication of polynomials
Multiplication of polynomials
def mul_x(poly):
    poly <<= 1
    if poly & 0x100:
        poly ^= 0x11b
Bit operations
48
General bit operations
  • Bitwise AND (&): Mask bits. Everything gets set to zero but the mask.
  • Bitwise OR (|): Merge bits. The result has bits set where any of the operands had bits set.
  • Bitwise XOR (^): Toggle bits. The result flips bits exactly where the mask was set.
  • Bitwise shift (<< and >>): Shift bits in their position left or right.
Bit masking
Bit masking
Bit masking
Bit masking
Bit masking
Bit masking
Bit masking
Bit masking
Bit toggling
Bit toggling
Bit toggling
Bit toggling
Bit toggling
Bit toggling
Bit toggling
Bit toggling
From P-box to code
  • Let's say we have a given permutation that you should put into code
  • You can always find the canonical form by doing one bit at a time, but this is extremely inefficient (not just for the computer, but for you)
  • The trick is to combine all shifting operations that are identical and mask only once
From P-box to code
From P-box to code
  • First, take a look at all the bits that are not shifted at all
  • Those we only need to mask, and we're done!
From P-box to code
From P-box to code
outval = (inval & 0x0f)
From P-box to code
  • Then, look at all those bits that are shifted left by one bit
  • We need to first mask those together, then shift them.
  • Then we do a bitwise OR with the result of the previous exercise
From P-box to code
From P-box to code
outval = (inval & 0x0f)
         | ((inval & 0x50) << 1)
From P-box to code
  • Finally, there are two bits which need to be shifted right by one bit.
  • Again we need to mask those bits, then shift them right.
  • And again we do a bitwise OR with the result of the previous exercise
From P-box to code
From P-box to code
outval = (inval & 0x0f)
         | ((inval & 0x50) << 1)
         | ((inval & 0xa0) >> 1)
Trick: Set a particular bit
  • How do you set a bit at bitoffset 18 programmatically?
  • I.e., you can obviously write the constant 262144 or 0x40000 but it becomes difficult to read.
  • Is there a better way?
Trick: Set a particular bit
  • How do you set a bit at bitoffset 18 programmatically?
  • I.e., you can obviously write the constant 262144 or 0x40000 but it becomes difficult to read.
  • Is there a better way?
  • There is:
    1 << 18
Trick: Set a whole mask
  • What if you want to create a bitmask that is exactly 18 bits wide? I.e., all lower 18 bits should be set, all others not.
Trick: Set a whole mask
  • What if you want to create a bitmask that is exactly 18 bits wide? I.e., all lower 18 bits should be set, all others not.
  • Very similar to the previous trick:
    (1 << 18) - 1
Conclusion
65
Takeaway
  • You should understand and be comfortable with efficient implementation of field arithmetic in the AESAdvanced Encryption Standard field
  • Bit masking, combining and toggling operations should be known to you, and you should be able to put code into graph and vice versa
Thank you!
Any questions?