-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathchacha.py
More file actions
65 lines (49 loc) · 1.73 KB
/
Copy pathchacha.py
File metadata and controls
65 lines (49 loc) · 1.73 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
import struct
rotl32 = lambda v, c: ((v << c) & 0xFFFFFFFF) | (v >> (32 - c))
def chacha_block(state):
x = state[:]
for _ in range(10):
# quarter round on columns
x[0], x[4], x[8], x[12] = quarter_round(x[0], x[4], x[8], x[12])
x[1], x[5], x[9], x[13] = quarter_round(x[1], x[5], x[9], x[13])
x[2], x[6], x[10], x[14] = quarter_round(x[2], x[6], x[10], x[14])
x[3], x[7], x[11], x[15] = quarter_round(x[3], x[7], x[11], x[15])
# diagonal
x[0], x[5], x[10], x[15] = quarter_round(x[0], x[5], x[10], x[15])
x[1], x[6], x[11], x[12] = quarter_round(x[1], x[6], x[11], x[12])
x[2], x[7], x[8], x[13] = quarter_round(x[2], x[7], x[8], x[13])
x[3], x[4], x[9], x[14] = quarter_round(x[3], x[4], x[9], x[14])
for i in range(16):
x[i] = (x[i] + state[i]) & 0xFFFFFFFF
return x
def quarter_round(a, b, c, d):
a = (a + b) & 0xFFFFFFFF
d = rotl32(d ^ a, 16)
c = (c + d) & 0xFFFFFFFF
b = rotl32(b ^ c, 12)
a = (a + b) & 0xFFFFFFFF
d = rotl32(d ^ a, 8)
c = (c + d) & 0xFFFFFFFF
b = rotl32(b ^ c, 7)
return a, b, c, d
def chacha20(constant, key, nonce, counter):
# initial state
state = [0] * 16
# constants
state[0] = struct.unpack('<I', constant[0:4])[0]
state[1] = struct.unpack('<I', constant[4:8])[0]
state[2] = struct.unpack('<I', constant[8:12])[0]
state[3] = struct.unpack('<I', constant[12:16])[0]
# key
for i in range(8):
state[4 + i] = struct.unpack('<I', key[i*4:(i+1)*4])[0]
state[12] = counter & 0xFFFFFFFF # counter
# nonce
state[13] = struct.unpack('<I', nonce[0:4])[0]
state[14] = struct.unpack('<I', nonce[4:8])[0]
state[15] = struct.unpack('<I', nonce[8:12])[0]
output_state = chacha_block(state)
output = b''
for word in output_state:
output += struct.pack('<I', word)
return output