import sys from struct import pack, unpack The Salsa20/12 quarter-round function def…
- User
import sys from struct import pack, unpack
The Salsa20/12 quarter-round function
def quarter_round(y0, y1, y2, y3): z1 = y1 ^ ((y0 + y3) & 0xffffffff) << 7 & 0xffffffff z2 = y2 ^ ((z1 + y0) & 0xffffffff) << 9 & 0xffffffff z3 = y3 ^ ((z2 + z1) & 0xffffffff) << 13 & 0xffffffff z0 = y0 ^ ((z3 + z2) & 0xffffffff) << 18 & 0xffffffff return z0, z1, z2, z3
The Salsa20/12 row-round function
def row_round(y): z = [0] * 16 z[0], z[1], z[2], z[3] = quarter_round(y[0], y[1], y[2], y[3]) z[5], z[6], z[7], z[4] = quarter_round(y[5], y[6], y[7], y[4]) z[10], z[11], z[8], z[9] = quarter_round(y[10], y[11], y[8], y[9]) z[15], z[12], z[13], z[14] = quarter_round(y[15], y[12], y[13], y[14]) return z
The Salsa20/12 column-round function
def column_round(x): y = [0] * 16 y[0], y[4], y[8], y[12] = quarter_round(x[0], x[4], x[8], x[12]) y[5], y[9], y[13], y[1] = quarter_round(x[5], x[9], x[13], x[1]) y[10], y[14], y[2], y[6] = quarter_round(x[10], x[14], x[2], x[6]) y[15], y[3], y[7], y[11] = quarter_round(x[15], x[3], x[7], x[11]) return y
The Salsa20/12 double-round function
def double_round(x): return row_round(column_round(x))
The Salsa20/12 hash function
def salsa20_12_hash(x): z = list(x) # Convert to list to allow modifications for i in range(6): # 12 rounds --> 6 double-rounds z = double_round(z) return [(z[i] + x[i]) & 0xffffffff for i in range(16)]
The Salsa20/12 expansion function
def salsa20_12_expand(key, nonce, block_index): # Constants defined by the Salsa20 algorithm sigma = b'expand 32-byte k' if len(key) == 32 else b'expand 16-byte k' # Split the key for the 16 byte version using the sigma constant if len(key) == 16: k0, k1 = key[:16], key[:16] else: k0, k1 = key[:16], key[16:] # Set up the block index (nonce expansion) n = pack('<Q', block_index)
code# Salsa20 state composition return (sigma[:4] + k0 + sigma[4:8] + nonce + n + sigma[8:12] + k1 + sigma[12:])Salsa20/12 encryption/decryption (identical operations due to XOR)
def salsa20_12_crypt(key, nonce, text): block_size = 64 # 64 bytes encrypted_text = b'' num_blocks = (len(text) + block_size - 1) // block_size # Calculate the number of blocks
code - Assistant