def generate_Kreyvium_version(rounds): kreyvium = CipherDescription(416) for i in range(128): kreyvium.apply_mov("s{}".format(93+i), "s{}".format(288+i)) for r in range(rounds): kreyvium.apply_xor("s65", "s92", "t1") kreyvium.apply_xor("s161", "s176", "t2") kreyvium.apply_xor("s242", "s287", "t3") kreyvium.apply_and("s90", "s91", "tand1") kreyvium.apply_and("s174", "s175", "tand2") kreyvium.apply_and("s285", "s286", "tand3") kreyvium.apply_xor("t1", "tand1", "t1") kreyvium.apply_xor("t1", "s415", "t1") kreyvium.apply_xor("t1", "s170", "s92") kreyvium.apply_xor("t2", "tand2", "t2") kreyvium.apply_xor("t2", "s263", "s176") kreyvium.apply_xor("t3", "tand3", "t3") kreyvium.apply_xor("t3", "s68", "s287") switch_last_bits = ("s92", "s176", "s287") kreyvium.apply_permutation(switch_last_bits) permutation_1 = tuple("s{}".format(i) for i in range(93)) permutation_2 = tuple("s{}".format(i) for i in range(93, 177)) permutation_3 = tuple("s{}".format(i) for i in range(177, 288)) permutation_4 = tuple("s{}".format(i) for i in range(288, 416)) kreyvium.apply_permutation(permutation_1) kreyvium.apply_permutation(permutation_2) kreyvium.apply_permutation(permutation_3) kreyvium.apply_permutation(permutation_4) return kreyvium
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
from cipher_description import CipherDescription size = 64 TWINE = CipherDescription(size) s = ['s{}'.format(i) for i in range(size)] t = ['t{}'.format(i) for i in range(size)] Sbox = [0xC, 0, 0xF, 0xA, 2, 0xB, 9, 5, 8, 3, 0xD, 7, 1, 0xE, 6, 4] p = [5, 0, 1, 4, 7, 12, 3, 8, 13, 6, 9, 2, 15, 10, 11, 14] TWINE.add_sbox('sbox', Sbox) for i in range(8): for j in range(4): TWINE.apply_mov(s[8 * i + j], t[4 * i + j]) bits = t[4 * i:4 * (i + 1)] bits.reverse() TWINE.apply_sbox('sbox', bits, bits) for j in range(4): TWINE.apply_xor(t[4 * i + j], s[8 * i + 4 + j], s[8 * i + 4 + j]) TWINE.shufflewords(p, 4, 0)
def generate_misty(R): misty = CipherDescription(64) S7 = [ 54, 50, 62, 56, 22, 34, 94, 96, 38, 6, 63, 93, 2, 18,123, 33, 55,113, 39,114, 21, 67, 65, 12, 47, 73, 46, 27, 25,111,124, 81, 53, 9,121, 79, 52, 60, 58, 48,101,127, 40,120,104, 70, 71, 43, 20,122, 72, 61, 23,109, 13,100, 77, 1, 16, 7, 82, 10,105, 98, 117,116, 76, 11, 89,106, 0,125,118, 99, 86, 69, 30, 57,126, 87, 112, 51, 17, 5, 95, 14, 90, 84, 91, 8, 35,103, 32, 97, 28, 66, 102, 31, 26, 45, 75, 4, 85, 92, 37, 74, 80, 49, 68, 29,115, 44, 64,107,108, 24,110, 83, 36, 78, 42, 19, 15, 41, 88,119, 59, 3] S9 = [ 167,239,161,379,391,334, 9,338, 38,226, 48,358,452,385, 90,397, 183,253,147,331,415,340, 51,362,306,500,262, 82,216,159,356,177, 175,241,489, 37,206, 17, 0,333, 44,254,378, 58,143,220, 81,400, 95, 3,315,245, 54,235,218,405,472,264,172,494,371,290,399, 76, 165,197,395,121,257,480,423,212,240, 28,462,176,406,507,288,223, 501,407,249,265, 89,186,221,428,164, 74,440,196,458,421,350,163, 232,158,134,354, 13,250,491,142,191, 69,193,425,152,227,366,135, 344,300,276,242,437,320,113,278, 11,243, 87,317, 36, 93,496, 27, 487,446,482, 41, 68,156,457,131,326,403,339, 20, 39,115,442,124, 475,384,508, 53,112,170,479,151,126,169, 73,268,279,321,168,364, 363,292, 46,499,393,327,324, 24,456,267,157,460,488,426,309,229, 439,506,208,271,349,401,434,236, 16,209,359, 52, 56,120,199,277, 465,416,252,287,246, 6, 83,305,420,345,153,502, 65, 61,244,282, 173,222,418, 67,386,368,261,101,476,291,195,430, 49, 79,166,330, 280,383,373,128,382,408,155,495,367,388,274,107,459,417, 62,454, 132,225,203,316,234, 14,301, 91,503,286,424,211,347,307,140,374, 35,103,125,427, 19,214,453,146,498,314,444,230,256,329,198,285, 50,116, 78,410, 10,205,510,171,231, 45,139,467, 29, 86,505, 32, 72, 26,342,150,313,490,431,238,411,325,149,473, 40,119,174,355, 185,233,389, 71,448,273,372, 55,110,178,322, 12,469,392,369,190, 1,109,375,137,181, 88, 75,308,260,484, 98,272,370,275,412,111, 336,318, 4,504,492,259,304, 77,337,435, 21,357,303,332,483, 18, 47, 85, 25,497,474,289,100,269,296,478,270,106, 31,104,433, 84, 414,486,394, 96, 99,154,511,148,413,361,409,255,162,215,302,201, 266,351,343,144,441,365,108,298,251, 34,182,509,138,210,335,133, 311,352,328,141,396,346,123,319,450,281,429,228,443,481, 92,404, 485,422,248,297, 23,213,130,466, 22,217,283, 70,294,360,419,127, 312,377, 7,468,194, 2,117,295,463,258,224,447,247,187, 80,398, 284,353,105,390,299,471,470,184, 57,200,348, 63,204,188, 33,451, 97, 30,310,219, 94,160,129,493, 64,179,263,102,189,207,114,402, 438,477,387,122,192, 42,381, 5,145,118,180,449,293,323,136,380, 43, 66, 60,455,341,445,202,432, 8,237, 15,376,436,464, 59,461] misty.add_sbox('S7', S7) misty.add_sbox('S9', S9) shuffle = [1,0] def FL(bits): t = ["t{}".format(i) for i in range(16)] for i in range(16): misty.apply_mov(bits[i],t[i]) #key and ? for i in range(16): misty.apply_xor(t[i],bits[i+16],bits[i+16]) for i in range(16): misty.apply_mov(bits[i+16],t[i]) #key or ? for i in range(16): misty.apply_xor(t[i],bits[i],bits[i]) def FI(bits): misty.apply_sbox('S9', bits[8::-1],bits[8::-1]) for i in range(7): misty.apply_xor(bits[9+i],bits[i],bits[i]) misty.apply_sbox('S7', bits[:8:-1],bits[:8:-1]) for i in range(7): misty.apply_xor(bits[9+i],bits[i],bits[i+9]) misty.apply_sbox('S9', bits[8::-1],bits[8::-1]) for i in range(7): misty.apply_xor(bits[9+i],bits[i],bits[i]) t = ["t{}".format(i+128) for i in range(16)] for i in range(16): misty.apply_mov(bits[i],t[i]) for i in range(16): misty.apply_mov(t[(i+9)%16],bits[i]) def FO(bits): FI(bits[:16]) for i in range(16): misty.apply_xor(bits[16+i],bits[i],bits[i]) FI(bits[16:]) for i in range(16): misty.apply_xor(bits[16+i],bits[i],bits[i+16]) FI(bits[:16]) for i in range(16): misty.apply_xor(bits[16+i],bits[i],bits[i]) t = ["t{}".format(i+64) for i in range(16)] for i in range(16): misty.apply_mov(bits[i],t[i]) misty.apply_mov(bits[i+16],bits[i]) misty.apply_mov(t[i],bits[i+16]) state = ["s{}".format(i) for i in range(64)] t = ["t{}".format(i) for i in range(32)] for r in range(R): if r&1==0: FL(state[:32]) FL(state[32:]) for i in range(32): misty.apply_mov(state[i], t[i]) FO(t) for i in range(32): misty.apply_xor(state[i+32],t[i],state[i+32]) misty.shufflewords(shuffle,32,1) return misty
wordsize = 4 mibs = CipherDescription(size) s = ['s{}'.format(i) for i in range(size)] t = ['t{}'.format(i) for i in range(size // 2)] y = [['t{}'.format(i + wordsize * j) for i in range(wordsize)] for j in range(size // wordsize)] s1 = [4, 15, 3, 8, 13, 10, 12, 0, 11, 5, 7, 14, 2, 6, 1, 9] mibs.add_sbox('S1', s1) shuffle1 = [1, 0] shuffle2 = [1, 7, 0, 2, 5, 6, 3, 4] shuffle2_inv = [2, 0, 3, 6, 7, 4, 5, 1] # Copy of the left side pass through the round function for i in range(32): mibs.apply_mov(s[i], t[i]) # Applying sboxes for i in range(8): bits = t[4 * i:4 * (i + 1)] bits.reverse() mibs.apply_sbox('S1', bits, bits) # Applying mixing layer for bit in range(wordsize): y1 = "t{}".format(bit) y2 = "t{}".format(bit + wordsize) y3 = "t{}".format(bit + 2 * wordsize) y4 = "t{}".format(bit + 3 * wordsize) y5 = "t{}".format(bit + 4 * wordsize) y6 = "t{}".format(bit + 5 * wordsize)
from cipher_description import CipherDescription size = 64 RoadRunnerR = CipherDescription(size) s = ['s{}'.format(i) for i in range(size)] t = ['t{}'.format(i) for i in range(size)] sbox = [0, 8, 6, 0xD, 5, 0xF, 7, 0xC, 4, 0xE, 2, 3, 9, 1, 0xB, 0xA] RoadRunnerR.add_sbox('Sbox', sbox) for i in range(size / 2): RoadRunnerR.apply_mov(s[i], t[i]) def S(): for i in range(8): bits = ['t{}'.format(i + 8 * j) for j in range(4)] bits.reverse() RoadRunnerR.apply_sbox('Sbox', bits, bits) def L(): for i in range(size / 2): RoadRunnerR.apply_mov(t[i], t[size / 2 + i]) for i in range(size / 2): RoadRunnerR.apply_xor(t[i], t[size / 2 + 8 * (i / 8) + (i + 1) % 8], t[i]) RoadRunnerR.apply_xor(t[i], t[size / 2 + 8 * (i / 8) + (i + 2) % 8], t[i]) S()
u = u[r:] + u[:r] u.reverse() return u s = ['s{}'.format(i) for i in range(128)] t = ['t{}'.format(i) for i in range(128)] d = s[:n] c = s[n:2 * n] b = s[2 * n:3 * n] a = s[3 * n:] #Step 1 for i in range(n): BelT.apply_mov(d[i], t[i]) BelT.apply_mov(a[i], t[3 * n + i]) 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)
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])
DES.add_sbox('S-box4', S4) DES.add_sbox('S-box5', S5) DES.add_sbox('S-box6', S6) DES.add_sbox('S-box7', S7) DES.add_sbox('S-box8', S8) #F function #Expansion E = [ 32, 1, 2, 3, 4, 5, 4, 5, 6, 7, 8, 9, 8, 9, 10, 11, 12, 13, 12, 13, 14, 15, 16, 17, 16, 17, 18, 19, 20, 21, 20, 21, 22, 23, 24, 25, 24, 25, 26, 27, 28, 29, 28, 29, 30, 31, 32, 1 ] for i in range(48): DES.apply_mov(s[32 + E[i] - 1], t[i]) #S-box layer for i in range(1, 9): input_bits = t[(i - 1) * 6:i * 6] output_bits = t[(i - 1) * 4:i * 4] DES.apply_sbox('S-box{}'.format(i), input_bits, output_bits) #P-box P = [ 16, 7, 20, 21, 29, 12, 28, 17, 1, 15, 23, 26, 5, 18, 31, 10, 2, 8, 24, 14, 32, 27, 3, 9, 19, 13, 30, 6, 22, 11, 4, 25 ] for i in range(32):
s3 = [7, 6, 8, 11, 0, 15, 3, 14, 9, 10, 12, 13, 5, 2, 4, 1] s4 = [14, 5, 15, 0, 7, 2, 12, 13, 1, 8, 4, 9, 11, 10, 6, 3] s5 = [2, 13, 11, 12, 15, 14, 0, 9, 7, 10, 6, 3, 1, 8, 4, 5] s6 = [11, 9, 4, 14, 0, 15, 10, 13, 6, 12, 5, 7, 3, 8, 1, 2] s7 = [13, 10, 15, 0, 14, 4, 9, 11, 2, 1, 8, 3, 7, 5, 12, 6] LBlock.add_sbox('S0', s0) LBlock.add_sbox('S1', s1) LBlock.add_sbox('S2', s2) LBlock.add_sbox('S3', s3) LBlock.add_sbox('S4', s4) LBlock.add_sbox('S5', s5) LBlock.add_sbox('S6', s6) LBlock.add_sbox('S7', s7) shuffle1 = [1, 0] shuffle2 = [1, 2, 3, 0] shuffle3 = [4, 12, 0, 8, 20, 28, 16, 24] LBlock.shufflewords(shuffle2, 8, 0) for i in range(size / 2): LBlock.apply_mov(s[i + 32], t[i]) for i in range(8): bits = t[4 * i:4 * (i + 1)] bits.reverse() LBlock.apply_sbox('S{}'.format(i), bits, bits) for i in range(size / 2): LBlock.apply_xor(t[shuffle3[i / 4] + i % 4], s[i], s[i]) LBlock.shufflewords(shuffle1, 32, 0)
def sparx_version(size, r, Ae): sparx = CipherDescription(size) s = ['s{}'.format(i) for i in range(size)] if size == 64: rs = 3 elif size == 128: rs = 4 def A(h): n = 16 a = 7 b = 2 x = s[h + n:h + 2 * n] y = s[h:h + n] shift = ['s{}'.format(h + n + (i * (n - a)) % n) for i in range(n)] sparx.apply_permutation(shift) sparx.add_mod(x, y, x, n, size + 2 * h) for j in range(b): shift = ['s{}'.format(h + j + i * b) for i in range(n / b)] sparx.apply_permutation(shift) for i in range(n): sparx.apply_xor(x[i], y[i], y[i]) for rnd in range(r): for i in range(rs): for j in range(size / 32): A(j * 32) t = ['t{}'.format(i) for i in range(size)] for i in range(size / 2): sparx.apply_mov(s[i + size / 2], t[i]) for i in range(size / 4): sparx.apply_xor(t[i], t[i + 16], t[i + size / 2]) if size == 128: for i in range(16): sparx.apply_xor(t[i + size / 2], t[i + 16 + size / 2], t[i + size / 2]) if size == 64: sw = [0, 1] elif size == 128: sw = [0, 3, 2, 1] for j in range(size / 32): for i in range(16): sparx.apply_xor(t[size / 2 + (i + 8) % 16], t[i + 16 * sw[j]], t[i + 16 * sw[j]]) for i in range(size / 2): sparx.apply_xor(s[i], t[i], s[i]) swap = [1, 0] sparx.shufflewords(swap, size / 2, 0) for i in range(Ae): for j in range(size / 32): A(j * 32) return sparx