Skip to content

Commit 0f1623e

Browse files
committed
refactoring chacha
1 parent 3574756 commit 0f1623e

File tree

1 file changed

+7
-7
lines changed

1 file changed

+7
-7
lines changed

firebirdsql/chacha.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -95,12 +95,11 @@ def quaterround_u32(a, b, c, d):
9595

9696

9797
class ChaCha20:
98-
def __init__(self, key, nonce, pos=0):
99-
pos_len = 16 - len(nonce)
98+
def __init__(self, key, nonce, counter=0):
99+
self.counter_len = 16 - len(nonce)
100100
assert len(key) == 32
101-
assert pos_len == 4 or pos_len == 8
102-
pos_bytes = int_to_bytes(pos, pos_len)
103-
block_bytes = sigma + key + pos_bytes + nonce
101+
assert self.counter_len in (4, 8)
102+
block_bytes = sigma + key + int_to_bytes(counter, self.counter_len) + nonce
104103
assert len(block_bytes) == 64
105104

106105
state = []
@@ -109,7 +108,6 @@ def __init__(self, key, nonce, pos=0):
109108
self.state = state
110109
self.block = self.chacha20_round_bytes()
111110
self.block_pos = 0
112-
self.pos_len = pos_len
113111

114112
def chacha20_round_bytes(self):
115113
x = copy.copy(self.state)
@@ -141,9 +139,11 @@ def translate(self, plain):
141139
enc += chr(ord(plain[i]) ^ ord(self.block[self.block_pos]))
142140
self.block_pos += 1
143141
if len(self.block) == self.block_pos:
142+
# inclement counter
144143
self.state[12] = add_u32(self.state[12], 1)
145-
if self.pos_len == 8 and self.state[12] == 0:
144+
if self.counter_len == 8 and self.state[12] == 0:
146145
self.state[13] = add_u32(self.state[13], 1)
146+
147147
self.block = self.chacha20_round_bytes()
148148
self.block_pos = 0
149149

0 commit comments

Comments
 (0)