07.07.2023 Views

Implementing-cryptography-using-python

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Chapter 5 ■ Stream Ciphers and Block Ciphers 151

Once you have the library installed, you can use the XSalsa20_keystream

to generate a keystream of the desired length, or you can pass any message

(plaintext or ciphertext) to have it XOR'd with the keystream. All values must

be binary strings that include str for Python 2 or the byte for Python 3. Here,

you will see a Python implementation of the salsa20 library:

from salsa20 import XSalsa20_xor

from os import urandom

IV = urandom(24)

KEY = b'*secret**secret**secret**secret*'

ciphertext = XSalsa20_xor(b"IT'S A YELLOW SUBMARINE", IV, KEY)

print(XSalsa20_xor(ciphertext, IV, KEY).decode())

IT'S A YELLOW SUBMARINE

One of the reasons why you should be familiar with Salsa20 is that it is consistently

faster than AES. It is recommended to use Salsa20 for encryption in

typical cryptographic applications.

ChaCha Cipher

ChaCha is a modification of Salsa20 published by Bernstein in 2008. It uses a

new round function that increases diffusion and increases performance on

some architectures. The ChaCha cipher is a stream cipher that uses a 256-bit

key and a 64-bit nonce/IV. Currently the AES block cipher has a dominant role

in secret key encryption; in addition, many systems have started to implement

hardware acceleration for AES, thus adding to its growing adoption. With AES

owning the market share for secret key encryption, it would cause major issues

if AES becomes cryptographically insecure. AES has been shown to be weak

around cache-collision attacks; therefore, Google has proposed ChaCha20 as

an alternative and actively uses it within TLS connections. Currently ChaCha is

three times faster than software-enabled AES and, like Salsa20, is not vulnerable

to timing attacks. ChaCha operates by creating a keystream that is then XOR’d

with the plaintext and has been standardized with RFC 7539.

Some of the key differences between ChaCha and Salsa include improved diffusion

per round, which offers an enhanced resistance to cryptanalysis. ChaCha,

like Salsa20, builds a 4×4 matrix and adds the results to the original matrix to

obtain a 16-word or 64-byte output block. Three additional differences should

be noted. First, the ChaCha series permutes the order of words in the output

block to match the permutation; this is designed to save time on SIMD (single

instruction, multiple data) platforms but does not make any difference on speed

in other platforms. SIMD is a class of parallel computers that can perform the

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

Saved successfully!

Ooh no, something went wrong!