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_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
Exemplo n.º 4
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.º 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
from cipher_description import CipherDescription

rectangle_sbox = [
    0x6, 0x5, 0xC, 0xA, 0x1, 0xE, 0x7, 0x9, 0xB, 0x0, 0x3, 0xD, 0x8, 0xF, 0x4,
    0x2
]
rectangle_permutations = [\
  ['s16', 's17', 's18','s19','s20','s21','s22','s23','s24','s25','s26','s27','s28','s29','s30','s31'],
['s32', 's44', 's40','s36'],
['s33', 's45', 's41','s37'],
['s34', 's46', 's42','s38'],
['s35', 's47', 's43','s39'],
['s48', 's61', 's58','s55','s52','s49','s62','s59','s56','s53','s50','s63','s60','s57','s54','s51'],
                          ]

rectangle = CipherDescription(64)
rectangle.add_sbox('S-box', rectangle_sbox)
for i in range(16):
    bits = [
        "s{}".format(i + 0), "s{}".format(i + 16), "s{}".format(i + 32),
        "s{}".format(i + 48)
    ]
    rectangle.apply_sbox('S-box', bits, bits)
for p in rectangle_permutations:
    rectangle.apply_permutation(p)
Exemplo n.º 7
0
['s2', 's32', 's8'],
['s3', 's48', 's12'],
['s5', 's17', 's20'],
['s6', 's33', 's24'],
['s7', 's49', 's28'],
['s9', 's18', 's36'],
['s10', 's34', 's40'],
['s11', 's50', 's44'],
['s13', 's19', 's52'],
['s14', 's35', 's56'],
['s15', 's51', 's60'],
['s22', 's37', 's25'],
['s23', 's53', 's29'],
['s26', 's38', 's41'],
['s27', 's54', 's45'],
['s30', 's39', 's57'],
['s31', 's55', 's61'],
['s43', 's58', 's46'],
['s47', 's59', 's62']]

present = CipherDescription(64)
present.add_sbox('S-box', present_sbox)
for i in range(16):
    bits = [
        "s{}".format(4 * i + 0), "s{}".format(4 * i + 1),
        "s{}".format(4 * i + 2), "s{}".format(4 * i + 3)
    ]
    present.apply_sbox('S-box', bits, bits)
for p in present_permutations:
    present.apply_permutation(p)
Exemplo n.º 8
0
# 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")

# Compute Feedback Bit
# f = s0 + ~s107 + maj(s244, s23, s160) + ch(s230, s111, s66) + s196 + k
maj(acorn, "s244", "s23", "s160", "tmaj2")
ch(acorn, "s230", "s111", "s66", "tch")
acorn.apply_xor("s0", "s107", "t1")  # TODO: Add not to second parameter
acorn.apply_xor("t1", "tmaj2", "t2")
acorn.apply_xor("t2", "tch", "t3")
acorn.apply_xor("t3", "s196", "t4")
acorn.apply_xor("t4", "tk", "s0")  # s0 gets feedback bit

# Shift everything
permutation = tuple("s{}".format(i) for i in range(292, -1, -1))
acorn.apply_permutation(permutation)

acorn.set_rounds(1536)
Exemplo n.º 9
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.º 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
        for bit in range(lanesize):
            x = "s{}".format(bit + lanesize * col + (5 * row) * lanesize)
            D = "tD{}".format(bit + lanesize * col)
            keccak.apply_xor(x, D, x)

# Rho
for row in range(5):
    for col in range(5):
        bit = col * lanesize + (5 * row * lanesize)
        rot_perm = [(i + rot_const[col][row]) % lanesize
                    for i in range(lanesize)]
        for cycle in decompose_permutation(rot_perm):
            cycle_bits = []
            for v in cycle:
                cycle_bits.append("s{}".format(v + bit))
            keccak.apply_permutation(cycle_bits)

# Pi
pi_perm = [
    1, 15, 22, 16, 12, 13, 3, 20, 11, 23, 6, 9, 4, 10, 8, 14, 18, 17, 2, 5, 19,
    7, 24, 21
]

for bit in range(lanesize):
    perm = ['s{}'.format(i * lanesize + bit) for i in pi_perm]
    keccak.apply_permutation(perm)

for row in range(5):
    for bit in range(lanesize):
        bits = [
            "s{}".format(bit + (5 * row) * lanesize),