Exemplo n.º 1
0
def generate_simon_version(n, rounds, a=8, b=1, c=2):
    simon = CipherDescription(2*n)
    for i in range(n):
        input_1 = "s{}".format((i-a)%n+n)
        input_2 = "s{}".format((i-b)%n+n)
        product =  "t{}".format(2*i)
        simon.apply_and(input_1, input_2, product)
        input_3 = "s{}".format((i-c)%n+n)
        xor = "t{}".format(2*i+1)
        simon.apply_xor(product, input_3, xor)
        right_side = "s{}".format(i)
        simon.apply_xor(xor, right_side, right_side)
    for i in range(n):
        right_side = "s{}".format(i)
        left_side = "s{}".format(i+n)
        simon.apply_permutation( (right_side, left_side) )
    simon.set_rounds(rounds)
    return simon
Exemplo n.º 2
0
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
Exemplo n.º 3
0
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
Exemplo n.º 4
0
        0xd5, 0xdb, 0x37, 0x45, 0xde, 0xfd, 0x8e, 0x2f,
        0x03, 0xff, 0x6a, 0x72, 0x6d, 0x6c, 0x5b, 0x51,
        0x8d, 0x1b, 0xaf, 0x92, 0xbb, 0xdd, 0xbc, 0x7f,
        0x11, 0xd9, 0x5c, 0x41, 0x1f, 0x10, 0x5a, 0xd8,
        0x0a, 0xc1, 0x31, 0x88, 0xa5, 0xcd, 0x7b, 0xbd,
        0x2d, 0x74, 0xd0, 0x12, 0xb8, 0xe5, 0xb4, 0xb0,
        0x89, 0x69, 0x97, 0x4a, 0x0c, 0x96, 0x77, 0x7e,
        0x65, 0xb9, 0xf1, 0x09, 0xc5, 0x6e, 0xc6, 0x84,
        0x18, 0xf0, 0x7d, 0xec, 0x3a, 0xdc, 0x4d, 0x20,
        0x79, 0xee, 0x5f, 0x3e, 0xd7, 0xcb, 0x39, 0x48]


SM4.add_sbox('sbox',Sbox)

for i in range(32):
	SM4.apply_xor(s[32 + i], s[64 + i], t[i])
	SM4.apply_xor(t[i], s[96 + i], t[i])

# S-box Layer
for i in range(4):
	sbox_bits = t[8*i:8*(i + 1)]
	SM4.apply_sbox('sbox',sbox_bits,sbox_bits)

# Linear Layer
for i in range(32):
	SM4.apply_xor(t[i], s[i], s[i])
	SM4.apply_xor(s[i], t[(i + 2) % 32], s[i])
	SM4.apply_xor(s[i], t[(i + 10) % 32], s[i])
	SM4.apply_xor(s[i], t[(i + 18) % 32], s[i])
	SM4.apply_xor(s[i], t[(i + 24) % 32], s[i])
Exemplo n.º 5
0
def generate_skinny_version(wordsize, rounds):
    # State
    # 0  1   2  3
    # 4  5   6  7
    # 8  9  10 11
    # 12 13 14 15
    if wordsize == 4:
        skinny_sbox = [
            0xC, 0x6, 0x9, 0x0, 0x1, 0xa, 0x2, 0xb, 0x3, 0x8, 0x5, 0xd, 0x4,
            0xe, 0x7, 0xf
        ]
    elif wordsize == 8:
        skinny_sbox = [
            0x65, 0x4c, 0x6a, 0x42, 0x4b, 0x63, 0x43, 0x6b, 0x55, 0x75, 0x5a,
            0x7a, 0x53, 0x73, 0x5b, 0x7b, 0x35, 0x8c, 0x3a, 0x81, 0x89, 0x33,
            0x80, 0x3b, 0x95, 0x25, 0x98, 0x2a, 0x90, 0x23, 0x99, 0x2b, 0xe5,
            0xcc, 0xe8, 0xc1, 0xc9, 0xe0, 0xc0, 0xe9, 0xd5, 0xf5, 0xd8, 0xf8,
            0xd0, 0xf0, 0xd9, 0xf9, 0xa5, 0x1c, 0xa8, 0x12, 0x1b, 0xa0, 0x13,
            0xa9, 0x05, 0xb5, 0x0a, 0xb8, 0x03, 0xb0, 0x0b, 0xb9, 0x32, 0x88,
            0x3c, 0x85, 0x8d, 0x34, 0x84, 0x3d, 0x91, 0x22, 0x9c, 0x2c, 0x94,
            0x24, 0x9d, 0x2d, 0x62, 0x4a, 0x6c, 0x45, 0x4d, 0x64, 0x44, 0x6d,
            0x52, 0x72, 0x5c, 0x7c, 0x54, 0x74, 0x5d, 0x7d, 0xa1, 0x1a, 0xac,
            0x15, 0x1d, 0xa4, 0x14, 0xad, 0x02, 0xb1, 0x0c, 0xbc, 0x04, 0xb4,
            0x0d, 0xbd, 0xe1, 0xc8, 0xec, 0xc5, 0xcd, 0xe4, 0xc4, 0xed, 0xd1,
            0xf1, 0xdc, 0xfc, 0xd4, 0xf4, 0xdd, 0xfd, 0x36, 0x8e, 0x38, 0x82,
            0x8b, 0x30, 0x83, 0x39, 0x96, 0x26, 0x9a, 0x28, 0x93, 0x20, 0x9b,
            0x29, 0x66, 0x4e, 0x68, 0x41, 0x49, 0x60, 0x40, 0x69, 0x56, 0x76,
            0x58, 0x78, 0x50, 0x70, 0x59, 0x79, 0xa6, 0x1e, 0xaa, 0x11, 0x19,
            0xa3, 0x10, 0xab, 0x06, 0xb6, 0x08, 0xba, 0x00, 0xb3, 0x09, 0xbb,
            0xe6, 0xce, 0xea, 0xc2, 0xcb, 0xe3, 0xc3, 0xeb, 0xd6, 0xf6, 0xda,
            0xfa, 0xd3, 0xf3, 0xdb, 0xfb, 0x31, 0x8a, 0x3e, 0x86, 0x8f, 0x37,
            0x87, 0x3f, 0x92, 0x21, 0x9e, 0x2e, 0x97, 0x27, 0x9f, 0x2f, 0x61,
            0x48, 0x6e, 0x46, 0x4f, 0x67, 0x47, 0x6f, 0x51, 0x71, 0x5e, 0x7e,
            0x57, 0x77, 0x5f, 0x7f, 0xa2, 0x18, 0xae, 0x16, 0x1f, 0xa7, 0x17,
            0xaf, 0x01, 0xb2, 0x0e, 0xbe, 0x07, 0xb7, 0x0f, 0xbf, 0xe2, 0xca,
            0xee, 0xc6, 0xcf, 0xe7, 0xc7, 0xef, 0xd2, 0xf2, 0xde, 0xfe, 0xd7,
            0xf7, 0xdf, 0xff
        ]

    # ShiftRows
    shiftrows = []
    for bit in range(wordsize):
        # Second Row
        shiftrows.append([
            "s{}".format(wordsize * 4 + bit), "s{}".format(wordsize * 5 + bit),
            "s{}".format(wordsize * 6 + bit), "s{}".format(wordsize * 7 + bit)
        ])
        # Third Row
        shiftrows.append([
            "s{}".format(wordsize * 8 + bit), "s{}".format(wordsize * 10 + bit)
        ])
        shiftrows.append([
            "s{}".format(wordsize * 9 + bit), "s{}".format(wordsize * 11 + bit)
        ])

        # Fourth Row
        shiftrows.append([
            "s{}".format(wordsize * 12 + bit),
            "s{}".format(wordsize * 15 + bit),
            "s{}".format(wordsize * 14 + bit),
            "s{}".format(wordsize * 13 + bit)
        ])

    skinny = CipherDescription(wordsize * 16)
    skinny.add_sbox('S-box', skinny_sbox)

    # SubCells
    for word in range(16):
        bits = ["s{}".format(wordsize * word + i) for i in range(wordsize)]
        skinny.apply_sbox('S-box', bits, bits)

    # ShiftRows
    for shift in shiftrows:
        skinny.apply_permutation(shift)

    # MixColumns
    for col in range(4):
        for bit in range(wordsize):
            x0 = "s{}".format(bit + col * wordsize)
            x1 = "s{}".format(bit + 4 * wordsize + col * wordsize)
            x2 = "s{}".format(bit + 4 * 2 * wordsize + col * wordsize)
            x3 = "s{}".format(bit + 4 * 3 * wordsize + col * wordsize)

            skinny.apply_xor(x1, x2, x1)
            skinny.apply_xor(x2, x0, x2)
            skinny.apply_xor(x2, x3, x3)

            skinny.apply_permutation([x0, x1, x2, x3])

    return skinny
Exemplo n.º 6
0
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()
L()
S()
L()
S()
L()
S()

for i in range(size / 2):
    RoadRunnerR.apply_xor(s[size / 2 + i], t[i], s[size / 2 + i])
swap = [1, 0]
RoadRunnerR.shufflewords(swap, size / 2, 1)
Exemplo n.º 7
0
# Theta
for col in range(5):
    for bit in range(lanesize):
        row_1 = "s{}".format(bit + col * lanesize)
        row_2 = "s{}".format(bit + 5 * lanesize + col * lanesize)
        row_3 = "s{}".format(bit + 10 * lanesize + col * lanesize)
        row_4 = "s{}".format(bit + 15 * lanesize + col * lanesize)
        row_5 = "s{}".format(bit + 20 * lanesize + col * lanesize)

        #TODO: Add XOR with multiple inputs?
        xor_1 = "tC0{}".format(bit + lanesize * col)
        xor_2 = "tC1{}".format(bit + lanesize * col)
        xor_3 = "tC2{}".format(bit + lanesize * col)
        xor_C = "tC_{}".format(bit + lanesize * col)

        keccak.apply_xor(row_1, row_2, xor_1)
        keccak.apply_xor(xor_1, row_3, xor_2)
        keccak.apply_xor(xor_2, row_4, xor_3)
        keccak.apply_xor(xor_3, row_5, xor_C)

for col in range(5):
    for bit in range(lanesize):
        C = "tC_{}".format(bit + lanesize * ((col - 1) % 5))  # C[col-1]
        Crot = "tC_{}".format((bit - 1) % lanesize + lanesize *
                              ((col + 1) % 5))  # C[col+1] <<< 1
        xor_D = "tD{}".format(bit + lanesize * col)
        keccak.apply_xor(C, Crot, xor_D)

for row in range(5):
    for col in range(5):
        for bit in range(lanesize):
Exemplo n.º 8
0
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)
Exemplo n.º 9
0
c = 3
d = 26
e = 9
f = 0

# newz = rotate(x ^ (z << 1) ^ ((y&z) << a),d);
# newy = rotate(y ^ x        ^ ((x|z) << b),e);
# newx = rotate(z ^ y        ^ ((x&y) << c),f);

# new z
for bit in range(32):
    gimli.apply_and("s{}".format(wordsize + bit),
                    "s{}".format(2 * wordsize + bit), "tand0{}".format(bit))

for bit in range(a, 32):
    gimli.apply_xor("s{}".format(0 + bit), "tand0{}".format(bit - a),
                    "txor{}".format(bit))
for bit in range(0, a):
    gimli.apply_and("s{}".format(0 + bit), "s{}".format(0 + bit),
                    "txor{}".format(bit))

for bit in range(0, 1):
    gimli.apply_and("txor{}".format(bit), "txor{}".format(bit),
                    "tnewz{}".format(bit))
for bit in range(1, 32):
    gimli.apply_xor("txor{}".format(bit), "s{}".format(2 * wordsize + bit - 1),
                    "tnewz{}".format(bit))

# new z
for bit in range(32):
    gimli.apply_and("s{}".format(0 + bit), "s{}".format(2 * wordsize + bit),
                    "tor{}".format(bit))  # TODO: Replace with or?
Exemplo n.º 10
0
from cipher_description import CipherDescription

bivium = CipherDescription(177)
bivium.apply_xor("s65", "s92", "t0")
bivium.apply_and("s90", "s91", "t1")
bivium.apply_xor("t0", "t1", "t2")
bivium.apply_xor("t2", "s170", "s92")
bivium.apply_xor("s161", "s176", "t3")
bivium.apply_and("s174", "s175", "t4")
bivium.apply_xor("t3", "t4", "t5")
bivium.apply_xor("t5", "s68", "s176")
switch_last_bits = ("s92", "s176")
bivium.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))
bivium.apply_permutation(permutation_1)
bivium.apply_permutation(permutation_2)
bivium.set_rounds(708)
Exemplo n.º 11
0
    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):
    DES.apply_mov(t[P[i] - 1], t[32 + i])

#Compression
for i in range(32):
    DES.apply_xor(s[i], t[32 + i], s[i])
#swap
shuffle = [1, 0]
DES.shufflewords(shuffle, 32, 0)
Exemplo n.º 12
0
from cipher_description import CipherDescription
#Bit numbering
#s63..s0
HIGHT = CipherDescription(64)
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])
Exemplo n.º 13
0
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)
Exemplo n.º 14
0
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)

chaskey.add_mod(v2, v1, v2, n, 0)
chaskey.add_mod(v0, v3, v0, n, 0)

shuffle = [i for i in range(size)]
for i in range(n):
    shuffle[n + i] = n + (i + 7) % n
    shuffle[3 * n + i] = 3 * n + (i + 13) % n
chaskey.shufflewords(shuffle, 1, 0)
Exemplo n.º 15
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
Exemplo n.º 16
0
    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)
    y7 = "t{}".format(bit + 6 * wordsize)
    y8 = "t{}".format(bit + 7 * wordsize)

    mibs.apply_xor(y4, y8, y8)
    mibs.apply_xor(y3, y7, y7)
    mibs.apply_xor(y2, y6, y6)
    mibs.apply_xor(y1, y5, y5)
    mibs.apply_xor(y8, y2, y2)
    mibs.apply_xor(y7, y1, y1)
    mibs.apply_xor(y6, y4, y4)
    mibs.apply_xor(y5, y3, y3)
    mibs.apply_xor(y4, y5, y5)
    mibs.apply_xor(y3, y8, y8)
    mibs.apply_xor(y2, y7, y7)
    mibs.apply_xor(y1, y6, y6)
    mibs.apply_xor(y8, y4, y4)
    mibs.apply_xor(y7, y3, y3)
    mibs.apply_xor(y6, y2, y2)
    mibs.apply_xor(y5, y1, y1)
Exemplo n.º 17
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
Exemplo n.º 18
0
def generate_midori_version(wordsize, rounds):
    # State
    # 0 4  8 12
    # 1 5  9 13
    # 2 6 10 14
    # 3 7 11 15
    if wordsize == 4:
        midori_sbox = [
            0xC, 0xA, 0xD, 0x3, 0xE, 0xB, 0xF, 0x7, 0x8, 0x9, 0x1, 0x5, 0x0,
            0x2, 0x4, 0x6
        ]
    elif wordsize == 8:
        midori_sbox = [
            0x1, 0x0, 0x5, 0x3, 0xe, 0x2, 0xf, 0x7, 0xd, 0xa, 0x9, 0xb, 0xc,
            0x8, 0x4, 0x6
        ]

    # Shuffle Cells
    shuffle_cells = []
    for bit in range(wordsize):
        shuffle_cells.append([
            "s{}".format(wordsize * 10 + bit),
            "s{}".format(wordsize * 1 + bit), "s{}".format(wordsize * 7 + bit),
            "s{}".format(wordsize * 12 + bit)
        ])
        shuffle_cells.append([
            "s{}".format(wordsize * 5 + bit), "s{}".format(wordsize * 2 + bit),
            "s{}".format(wordsize * 14 + bit), "s{}".format(wordsize * 4 + bit)
        ])
        shuffle_cells.append([
            "s{}".format(wordsize * 15 + bit),
            "s{}".format(wordsize * 3 + bit), "s{}".format(wordsize * 9 + bit),
            "s{}".format(wordsize * 8 + bit)
        ])
        shuffle_cells.append([
            "s{}".format(wordsize * 11 + bit), "s{}".format(wordsize * 6 + bit)
        ])

    midori = CipherDescription(wordsize * 16)
    midori.add_sbox('S-box', midori_sbox)

    # SubCell
    for i in range(16):
        if wordsize == 4:
            bits = [
                "s{}".format(4 * i + 3), "s{}".format(4 * i + 2),
                "s{}".format(4 * i + 1), "s{}".format(4 * i + 0)
            ]
            midori.apply_sbox('S-box', bits, bits)
        else:
            # Apply bit permutation
            bit_perm = [sb_perm[i % 4][j] for j in range(8)]
            for cycle in decompose_permutation(bit_perm):
                cycle_bits = []
                for v in cycle:
                    cycle_bits.append("s{}".format(8 * i + v))
                midori.apply_permutation(cycle_bits)
            # Apply S-box
            bits = [
                "s{}".format(8 * i + 0), "s{}".format(8 * i + 1),
                "s{}".format(8 * i + 2), "s{}".format(8 * i + 3)
            ]
            midori.apply_sbox('S-box', bits, bits)
            bits = [
                "s{}".format(8 * i + 4), "s{}".format(8 * i + 5),
                "s{}".format(8 * i + 6), "s{}".format(8 * i + 7)
            ]
            midori.apply_sbox('S-box', bits, bits)
            # Apply bit permutation
            bit_perm = [8 * i + sb_perminv[i % 4][j] for j in range(8)]
            for cycle in decompose_permutation(bit_perm):
                cycle_bits = []
                for v in cycle:
                    cycle_bits.append("s{}".format(8 * i + v))
                midori.apply_permutation(cycle_bits)

    # ShuffleCell
    for shuffle in shuffle_cells:
        midori.apply_permutation(shuffle)

    # MixColumn
    for col in range(4):
        for bit in range(wordsize):
            x0 = "s{}".format(bit + col * wordsize * 4)
            x1 = "s{}".format(bit + wordsize + col * wordsize * 4)
            x2 = "s{}".format(bit + 2 * wordsize + col * wordsize * 4)
            x3 = "s{}".format(bit + 3 * wordsize + col * wordsize * 4)

            midori.apply_xor(x0, x1, "txor01")
            midori.apply_xor(x0, x2, "txor02")
            midori.apply_xor(x1, x2, "txor12")

            midori.apply_xor(x1, "txor12", "tx2")

            midori.apply_xor("txor12", x3, x0)
            midori.apply_xor("txor02", x3, x1)
            midori.apply_xor("txor01", x3, x2)
            midori.apply_xor("txor01", "tx2", x3)
            # midori.apply_xor(x0, x1, "txor01")
            # midori.apply_xor("txor01", x2, "tx3p")
            #
            # midori.apply_xor("txor01", x3, "tx2p")
            #
            # midori.apply_xor(x0, x2, "txor02")
            # midori.apply_xor("txor02", x3, "tx1p")
            #
            # midori.apply_xor(x1, x2, "txor12")
            # midori.apply_xor("txor12", x3, "tx0p")
            #
            # midori.apply_mov("tx0p", x0)
            # midori.apply_mov("tx1p", x1)
            # midori.apply_mov("tx2p", x2)
            # midori.apply_mov("tx3p", x3)

    return midori
Exemplo n.º 19
0
from cipher_description import CipherDescription

trivium = CipherDescription(288)
trivium.apply_xor("s65", "s92", "t1")
trivium.apply_xor("s161", "s176", "t2")
trivium.apply_xor("s242", "s287", "t3")

trivium.apply_and("s90", "s91", "tand1")
trivium.apply_and("s174", "s175", "tand2")
trivium.apply_and("s285", "s286", "tand3")

trivium.apply_xor("t1", "tand1", "t1")
trivium.apply_xor("t1", "s170", "s92")

trivium.apply_xor("t2", "tand2", "t2")
trivium.apply_xor("t2", "s263", "s176")

trivium.apply_xor("t3", "tand3", "t3")
trivium.apply_xor("t3", "s68", "s287")

switch_last_bits = ("s92", "s176", "s287")
trivium.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))
trivium.apply_permutation(permutation_1)
trivium.apply_permutation(permutation_2)
trivium.apply_permutation(permutation_3)
trivium.set_rounds(1152)
Exemplo n.º 20
0

def ch(cipher, a, b, c, out):
    cipher.apply_and(a, b, "t1")
    cipher.apply_and(a, c, "t2")  # TODO: Add not to first parameter
    cipher.apply_xor("t1", "t2", out)


acorn = CipherDescription(293)

# Compute Keystream Bit
# k = s12 + s154 + maj(s256, s61, s193)

# Majority
maj(acorn, "s256", "s61", "s193", "tmaj1")
acorn.apply_xor("s12", "s154", "t1")
acorn.apply_xor("t1", "tmaj1", "tk")

# Update State
acorn.apply_xor("s289", "s235", "t1")
acorn.apply_xor("t1", "s230", "s289")
acorn.apply_xor("s230", "s196", "t1")
acorn.apply_xor("t1", "s193", "s230")
acorn.apply_xor("s193", "s160", "t1")
acorn.apply_xor("t1", "s154", "s193")
acorn.apply_xor("s154", "s111", "t1")
acorn.apply_xor("t1", "s107", "s154")
acorn.apply_xor("s107", "s66", "t1")
acorn.apply_xor("t1", "s61", "s107")
acorn.apply_xor("s61", "s23", "t1")
acorn.apply_xor("t1", "s0", "s61")
Exemplo n.º 21
0
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)
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])
Exemplo n.º 22
0
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