def generate_speck_version(n, a, b): speck = CipherDescription(2 * n) s = ['s{}'.format(i) for i in range(2 * n)] ''' if n == 16: a = 7 b = 2 else: a = 8 b = 3 ''' x = s[n:] y = s[:n] if n % a == 0: for j in range(a): shift = [ 's{}'.format(n + j + (i * (n - a)) % n) for i in range(n / a) ] speck.apply_permutation(shift) else: shift = ['s{}'.format(n + (i * (n - a)) % n) for i in range(n)] speck.apply_permutation(shift) speck.add_mod(x, y, x, n, 0) if n % b == 0: for j in range(b): shift = ['s{}'.format(j + i * b) for i in range(n / b)] speck.apply_permutation(shift) else: shift = ['s{}'.format((i * b) % n) for i in range(n)] speck.apply_permutation(shift) for i in range(n): speck.apply_xor(x[i], y[i], y[i]) return speck
def generate_chacha(r): size = 512 chacha = CipherDescription(size) n = 32 s = ['s{}'.format(i) for i in range(size)] t = ['t{}'.format(i) for i in range(size)] v = [] for i in range(16): v.append(s[i*n:(i+1)*n]) for j in range(r): for i in range(4): a,b,c,d = v[0+i], v[4+(i+(j&1)*1)%4], v[8+(i+(j&1)*2)%4], v[12+(i+(j&1)*3)%4] chacha.add_mod(a,b,a,n,size) for l in range(n): chacha.apply_xor(d[l],a[l],t[l]) for l in range(n): chacha.apply_mov(t[(l-16)%n],d[l]) chacha.add_mod(c,d,c,n,size) for l in range(n): chacha.apply_xor(b[l],c[l],t[l]) for l in range(n): chacha.apply_mov(t[(l-12)%n],b[l]) chacha.add_mod(a,b,a,n,size) for l in range(n): chacha.apply_xor(d[l],a[l],t[l]) for l in range(n): chacha.apply_mov(t[(l-8)%n],d[l]) chacha.add_mod(c,d,c,n,size) for l in range(n): chacha.apply_xor(b[l],c[l],t[l]) for l in range(n): chacha.apply_mov(t[(l-7)%n],b[l]) return chacha
BelT.addconstant_mod(t[:n], t[:n], n, 4 * n) BelT.addconstant_mod(t[3 * n:], t[3 * n:], n, 8 * n) t[:n] = G(t[:n], 21) t[3 * n:] = G(t[3 * n:], 5) for i in range(n): BelT.apply_xor(c[i], t[i], c[i]) BelT.apply_xor(b[i], t[3 * n + i], b[i]) #Step 2 for i in range(n): BelT.apply_mov(b[i], t[2 * n + i]) BelT.addconstant_mod(t[2 * n:3 * n], t[2 * n:3 * n], n, 12 * n) t[2 * n:3 * n] = G(t[2 * n:3 * n], 13) BelT.addconstant_mod(a, a, n, 16 * n) BelT.add_mod(a, t[2 * n:3 * n], a, n, 20 * n) #Step 3 for i in range(n): BelT.apply_mov(b[i], t[2 * n + i]) BelT.apply_mov(c[i], t[n + i]) BelT.add_mod(t[n:2 * n], t[2 * n:3 * n], t[n:2 * n], n, 24 * n) BelT.addconstant_mod(t[n:2 * n], t[n:2 * n], n, 28 * n) t[n:2 * n] = G(t[n:2 * n], 21) BelT.add_mod(t[n:2 * n], b, b, n, 32 * n) BelT.addconstant_mod(c, c, n, 36 * n) BelT.add_mod(t[n:2 * n], c, c, n, 40 * n) #Step 4
from cipher_description import CipherDescription #Bit numbering #s128..s0 LEA = CipherDescription(128) wordsize = 32 s = ['s{}'.format(i) for i in range(128)] for i in range(3): x = s[wordsize * (2 - i):wordsize * (3 - i)] y = s[wordsize * (3 - i):wordsize * (4 - i)] LEA.add_mod(x, y, y, wordsize, 128) shuffle = [1, 2, 3, 0] LEA.shufflewords(shuffle, wordsize, 1) for i in range(wordsize): LEA.apply_mov(s[i], 't{}'.format(i)) LEA.apply_mov(s[i + 32], 't{}'.format(i + 32)) LEA.apply_mov(s[i + 64], 't{}'.format(i + 64)) for i in range(wordsize): LEA.apply_mov('t{}'.format(i), s[(i + 9) % wordsize]) LEA.apply_mov('t{}'.format(32 + i), s[32 + (i - 5) % wordsize]) LEA.apply_mov('t{}'.format(64 + i), s[64 + (i - 3) % wordsize])
n = 8 s = ['s{}'.format(i) for i in range(64)] t = ['t{}'.format(i) for i in range(64)] for i in range(n): HIGHT.apply_xor(s[(i+3)%n],s[(i+4)%n],'t{}'.format(i)) HIGHT.apply_xor(s[(i+6)%n],'t{}'.format(i),'t{}'.format(i)) HIGHT.apply_xor(s[16+(i+1)%n],s[16+(i+2)%n],'t{}'.format(16+i)) HIGHT.apply_xor(s[16+(i+7)%n],'t{}'.format(16+i),'t{}'.format(16+i)) HIGHT.apply_xor(s[32+(i+3)%n],s[32+(i+4)%n],'t{}'.format(32+i)) HIGHT.apply_xor(s[32+(i+6)%n],'t{}'.format(32+i),'t{}'.format(32+i)) HIGHT.apply_xor(s[48+(i+1)%n],s[48+(i+2)%n],'t{}'.format(48+i)) HIGHT.apply_xor(s[48+(i+7)%n],'t{}'.format(48+i),'t{}'.format(48+i)) HIGHT.add_mod(t[:8],s[8:16],s[8:16],n,64) HIGHT.add_mod(t[32:40],s[40:48],s[40:48],n,128) HIGHT.addconstant_mod(t[16:24],t[16:24],n,192) HIGHT.addconstant_mod(t[48:56],t[48:56],n,256) for i in range(n): HIGHT.apply_xor(t[16+i],s[24+i],s[24+i]) HIGHT.apply_xor(t[48+i],s[56+i],s[56+i]) shuffle = [7] + range(7) HIGHT.shufflewords(shuffle,n,1)
from cipher_description import CipherDescription size = 128 n = 32 chaskey = CipherDescription(size) s = ['s{}'.format(i) for i in range(size)] t = ['t{}'.format(i) for i in range(size)] v0 = s[0:32] v1 = s[32:64] v2 = s[64:96] v3 = s[96:128] chaskey.add_mod(v0, v1, v0, n, 0) chaskey.add_mod(v2, v3, v2, n, 0) shuffle = [i for i in range(size)] for i in range(n): shuffle[n + i] = n + (i + 5) % n shuffle[3 * n + i] = 3 * n + (i + 8) % n chaskey.shufflewords(shuffle, 1, 0) for i in range(n): chaskey.apply_xor(v0[i], v1[i], v1[i]) chaskey.apply_xor(v2[i], v3[i], v3[i]) shuffle = [i for i in range(size)] for i in range(n): shuffle[i] = (i + 16) % n chaskey.shufflewords(shuffle, 1, 0)