We just saw in the previous episode that we can use a PRG (Pseudo Random Generator) to generate a long random sequence from an initial key. Now let’s try another approach with what are called block ciphers. 1/n
2
A block cipher is an algorithm to encrypt and decrypt a block where the input and output have always the exact same size. One well-known block cipher is AES with blocks of size 128 bits (16 bytes) and keys of size either 128, 192 or 256 bits. 2/n
3
A block cipher is built on two concepts, key expansion and rounds. Key expansion generates multiple keys from one key, and each round uses one key to modify the input iteratively. In AES-128, there are 10 rounds. 3/n
4
Each AES round performs three operations: substitutions, permutations, key XORing. Permutations are rows and columns shifts on a 4x4 U8 matrix. Substitution is table-based. And key expansion is very similar, based on 32bit words rotations and substitutions. 4/n
5
Fun fact, AES is so common these days that processors usually include dedicated instructions able to compute AES rounds on 128-bit registers. Same thing for AES key expansion. On Intel, these are the "aesenc", "aesenclast", and "aeskeygenassist" instructions. 5/n
6
Now that we can encrypt 1 block, how do we encrypt the whole plaintext? Remember that we don’t want our encrypting algorithm to be deterministic, so that an attacker cannot recognize patterns in the cipher text. Two common solutions are CBC and CTR. 6/n
7
CBC stands for "cipher block chaining". Just like in blockchains, we simply use the random output of one ciphertext block as input for the next plaintext block. So we just need one random "initialization vector" (IV) to start the chain and we are good. 7/n
8
CTR stands for "randomized CounTeR mode". In that case we simply initialize a counter randomly as our IV, then for each block, we encrypt the incremented counter with AES and XOR that result with the plaintext block. 8/n
9
Contrary to CBC, CTR is parallel since each block can be encrypted independently! Remark that since we don’t need to inverse the encryption, just do a XOR to decode a ciphertext with CTR, it is also more generalizable and can be used with PRG such as Salsa20! 9/n
10
Beware that block cipher are still vulnerable to attacks such as side channel attacks or fault attacks, so as usual, stick to the classic libraries!
That’s all for today! I hope you liked it. See you in the next episode! 10/10