07.07.2023 Views

Implementing-cryptography-using-python

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

146 Chapter 5 ■ Stream Ciphers and Block Ciphers

In Chapter 4, you first learned about pseudorandomness. A linear-feedback

shift register (LFSR) is an algorithm for generating pseudorandom numbers.

The sequence of pseudorandom numbers generated by an LFSR can be used as

the one-time pad for an encryption algorithm. However, it has a major weakness:

the numbers generated are periodic, and an attacker can figure out the key

using a known plaintext attack. In the previous section, you used a PRNG to

make a stream cipher. The next level of complexity that you can utilize would

be something similar to the Trivium stream cipher, which uses a CSPRNG that

generates one bit at a time. It tries to make the LFSR idea more secure by having

multiple registers that interfere with each other. The intuitive notion is that

LFSRs yield to linear algebra, so let’s add just enough complexity to be nonlinear.

Figure 5.1 represents a schematic of a three-register Trivium implementation.

Cell 1

1

66 69

Cell 93

91 92 93

Cell 1

1

Cell 84

69 78 82 83 84

Output

Cell 1 Cell 111

1 66

87 109110111

Figure 5.1: Three-register representation of Trivium

When looking at Figure 5.1, you see it as three separate registers that each

produce their own output. The final output bit is the XOR of all three output bits.

The output of each register is also used to help form the input of another register.

For the initialization, to kick-start Trivium, it accepts two inputs: an 80-bit

key and an 80-bit IV. The 80-bit key is loaded into the leftmost 80 bits of the first

register. The 80-bit IV is loaded into the leftmost 80 bits of the second register.

Finally, the final 3 bits of the third register are set to 1 (the rightmost bits).

The stream is then run 4 × 288 times with the output discarded; this is now

the opening state.

Let’s do the first (tossed-out) run using an all 1 key and an all 1 IV, to show the

idea. Bits 1−80 are all 1, bits 94−173 are all 80, and bits 286−288 are all 1; everything

else is 0. The first output bit is an XOR of three different bits, so let’s look

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!