Skip to content

Commit

Permalink
* cosmetic
Browse files Browse the repository at this point in the history
  • Loading branch information
jkmnt committed Nov 26, 2021
1 parent fc7f32a commit 1ec6761
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 17 deletions.
10 changes: 5 additions & 5 deletions eax.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,13 @@ def ctr(cfg, key, data, nonce):
enc = cfg.ECB(key)
out = b''

nonce_int = int.from_bytes(nonce, byteorder=cfg.ENDIAN, signed=False)
nonce_int = int.from_bytes(nonce, cfg.ENDIAN, signed=False)

cnt = 0
for i in range(0, len(data), cfg.BLOCKSIZE):
block = data[i:i+cfg.BLOCKSIZE]
k = (nonce_int + cnt) & cfg.BLOCKSIZE_MASK
k = k.to_bytes(cfg.BLOCKSIZE, byteorder=cfg.ENDIAN)
k = k.to_bytes(cfg.BLOCKSIZE, cfg.ENDIAN)
xorbuf = enc.run(k)
out += xorstrings(block, xorbuf)
cnt += 1
Expand All @@ -41,13 +41,13 @@ def omac(cfg, key, data, k):
enc = cfg.ECB(key)

L = enc.run(bytes([0] * cfg.BLOCKSIZE))
L_int = int.from_bytes(L, byteorder=cfg.ENDIAN, signed=False)
L_int = int.from_bytes(L, cfg.ENDIAN, signed=False)

L2_int = gf_double(L_int, cfg.BLOCKSIZE)
L4_int = gf_double(L2_int, cfg.BLOCKSIZE)

L2 = L2_int.to_bytes(cfg.BLOCKSIZE, byteorder=cfg.ENDIAN)
L4 = L4_int.to_bytes(cfg.BLOCKSIZE, byteorder=cfg.ENDIAN)
L2 = L2_int.to_bytes(cfg.BLOCKSIZE, cfg.ENDIAN)
L4 = L4_int.to_bytes(cfg.BLOCKSIZE, cfg.ENDIAN)

data = bytes([0] * (cfg.BLOCKSIZE - 1) + [k]) + data
data = bytearray(data)
Expand Down
10 changes: 5 additions & 5 deletions eax_stream.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,14 @@ class OMAC_stream:
def __init__(self, cfg, key, k):
enc = cfg.ECB(key)
L = enc.run(bytes([0] * cfg.BLOCKSIZE))
L_int = int.from_bytes(L, byteorder=cfg.ENDIAN, signed=False)
L_int = int.from_bytes(L, cfg.ENDIAN, signed=False)

L2_int = gf_double(L_int, cfg.BLOCKSIZE)
L4_int = gf_double(L2_int, cfg.BLOCKSIZE)

self.cfg = cfg
self.L2 = L2_int.to_bytes(cfg.BLOCKSIZE, byteorder=cfg.ENDIAN)
self.L4 = L4_int.to_bytes(cfg.BLOCKSIZE, byteorder=cfg.ENDIAN)
self.L2 = L2_int.to_bytes(cfg.BLOCKSIZE, cfg.ENDIAN)
self.L4 = L4_int.to_bytes(cfg.BLOCKSIZE, cfg.ENDIAN)

self.enc = enc
self.readyblock = bytes([0] * (cfg.BLOCKSIZE - 1) + [k])
Expand Down Expand Up @@ -52,7 +52,7 @@ def digest(self):
class CTR_stream:
def __init__(self, cfg, key, nonce):
enc = cfg.ECB(key)
nonce_int = int.from_bytes(nonce, byteorder=cfg.ENDIAN, signed=False)
nonce_int = int.from_bytes(nonce, cfg.ENDIAN, signed=False)

self.cfg = cfg
self.enc = enc
Expand All @@ -64,7 +64,7 @@ def process_byte(self, byte):
cfg = self.cfg
if self.pos % cfg.BLOCKSIZE == 0:
counter = (self.nonce + self.pos // cfg.BLOCKSIZE) & cfg.BLOCKSIZE_MASK
counter = counter.to_bytes(cfg.BLOCKSIZE, byteorder=cfg.ENDIAN)
counter = counter.to_bytes(cfg.BLOCKSIZE, cfg.ENDIAN)
self.xorbuf = self.enc.run(counter)

pt = self.xorbuf[self.pos % cfg.BLOCKSIZE] ^ byte
Expand Down
19 changes: 12 additions & 7 deletions xtea.py
Original file line number Diff line number Diff line change
@@ -1,24 +1,29 @@
import struct

MASK32 = (1 << 32) - 1

class XTEA:
def __init__(self, key, rounds=32):
self.keywords = struct.unpack('<IIII', key)
if len(key) != 16:
raise Exception('Expecting the 128 bit (16 bytes) key')

key = int.from_bytes(key, 'little', signed=False)
keywords = (key >> 0) & MASK32, (key >> 32) & MASK32, (key >> 64) & MASK32, (key >> 96) & MASK32,

schkey = []
# schedule the key for 32 rounds to move it out of enc loop
sum = 0
delta = 0x9E3779B9
for round in range(rounds):
k0 = (sum + self.keywords[sum & 3]) & MASK32
k0 = (sum + keywords[sum & 3]) & MASK32
sum = (sum + delta) & MASK32
k1 = (sum + self.keywords[(sum>>11) & 3]) & MASK32
k1 = (sum + keywords[(sum>>11) & 3]) & MASK32
schkey.append((k0, k1))
self.schkey = schkey

def encrypt(self, pt):
v0, v1 = struct.unpack('<II', pt)
pt = int.from_bytes(pt, 'little', signed=False)
v0, v1 = (pt >> 0) & MASK32, (pt >> 32) & MASK32
for schkey in self.schkey:
v0 = (v0 + ((((v1<<4) ^ (v1>>5)) + v1) ^ schkey[0])) & MASK32
v1 = (v1 + ((((v0<<4) ^ (v0>>5)) + v0) ^ schkey[1])) & MASK32
return struct.pack('<II', v0, v1)
ct = (v1 << 32) | v0
return ct.to_bytes(8, 'little')

0 comments on commit 1ec6761

Please sign in to comment.