示例#1
0
def generate_GIFT_version(size):
    GIFT = CipherDescription(size)
    s = ['s{}'.format(i) for i in range(size)]

    #Sbox layer
    sbox = [1, 0xa, 4, 0xc, 6, 0xf, 3, 9, 2, 0xd, 0xb, 7, 5, 0, 8, 0xe]
    GIFT.add_sbox('S_box', sbox)
    for i in range(size / 4):
        bits = s[i * 4:(i + 1) * 4]
        bits.reverse()
        GIFT.apply_sbox('S_box', bits, bits)

    #Bit permutation
    if size == 64:
        P = [
            0, 17, 34, 51, 48, 1, 18, 35, 32, 49, 2, 19, 16, 33, 50, 3, 4, 21,
            38, 55, 52, 5, 22, 39, 36, 53, 6, 23, 20, 37, 54, 7, 8, 25, 42, 59,
            56, 9, 26, 43, 40, 57, 10, 27, 24, 41, 58, 11, 12, 29, 46, 63, 60,
            13, 30, 47, 44, 61, 14, 31, 28, 45, 62, 15
        ]
    else:
        P = [
            0, 33, 66, 99, 96, 1, 34, 67, 64, 97, 2, 35, 32, 65, 98, 3, 4, 37,
            70, 103, 100, 5, 38, 71, 68, 101, 6, 39, 36, 69, 102, 7, 8, 41, 74,
            107, 104, 9, 42, 75, 72, 105, 10, 43, 40, 73, 106, 11, 12, 45, 78,
            111, 108, 13, 46, 79, 76, 109, 14, 47, 44, 77, 110, 15, 16, 49, 82,
            115, 112, 17, 50, 83, 80, 113, 18, 51, 48, 81, 114, 19, 20, 53, 86,
            119, 116, 21, 54, 87, 84, 117, 22, 55, 52, 85, 118, 23, 24, 57, 90,
            123, 120, 25, 58, 91, 88, 121, 26, 59, 56, 89, 122, 27, 28, 61, 94,
            127, 124, 29, 62, 95, 92, 125, 30, 63, 60, 93, 126, 31
        ]
    GIFT.shufflewords(P, 1, 1)

    return GIFT
示例#2
0
def generate_test_version(wordsize):
    # State
    # 0 4  8 12
    # 1 5  9 13
    # 2 6 10 14
    # 3 7 11 15
    test = CipherDescription(16*wordsize)
    

    midori_sbox = [0xC, 0xA, 0xD, 0x3, 0xE, 0xB, 0xF, 0x7,
                   0x8, 0x9, 0x1, 0x5, 0x0, 0x2, 0x4, 0x6]
                       
    shuffle = [0,10,5,15, 14,4,11,1, 9,3,12,6, 7,13,2,8]
    MC = [[0, 1, 1, 1],
          [1, 0, 1, 1],
          [1, 1, 0, 1],
          [1, 1, 1, 0]]
    Rp = 0
    test.add_sbox('S-box', midori_sbox)
    for i in range(16):
        bits = []
        for j in range(wordsize):
            bits.append("s{}".format(wordsize*i + wordsize - 1 - j))
        test.apply_sbox('S-box', bits, bits)
    
    
    test.shufflewords(shuffle,wordsize)
    
    
    # MixColumn
    test.apply_MC(wordsize, MC, Rp, 4, 4)
  
    return test
示例#3
0
def generate_Mantis_version(forward, backward):
    mantis = CipherDescription(64)
    wordsize = 4
    mantis.add_sbox('S-box', midori_sbox)

    def S():
        for i in range(16):
            bits = []
            for j in range(wordsize):
                bits.append("s{}".format(wordsize * i + wordsize - 1 - j))
            mantis.apply_sbox('S-box', bits, bits)

    def R():
        S()
        mantis.shufflewords(shuffle, wordsize, 1)
        mantis.apply_MC(wordsize, MC, Rp, 4, 4)

    def Ri():
        mantis.apply_MC(wordsize, MC, Rp, 4, 4)
        mantis.shufflewords(shufflei, wordsize, 1)
        S()

    #Forward rounds
    for i in range(forward):
        R()
    #Middle
    S()
    mantis.apply_MC(wordsize, MC, Rp, 4, 4)
    S()
    #Inverse rounds
    for i in range(backward):
        Ri()

    return mantis
示例#4
0
文件: AES.py 项目: siweisun/solvatore
def generate_AES():

    AES = CipherDescription(128)

    #Add the appropriate sbox

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

    AES.add_sbox('S-box', sbox)

    #Apply the sbox to all cells
    for i in range(16):
        bits = []
        for j in range(8):
            bits.append("s{}".format(8 * i + 7 - j))
        AES.apply_sbox('S-box', bits, bits)

    #Define and apply shiftrows
    tshuffle = []
    for i in range(4):
        tshuffle.append([((j + i) % 4) * 4 + i for j in range(4)])

    shuffle = []
    for i in range(4):
        for j in range(4):
            shuffle.append(tshuffle[j][i])

    AES.shufflewords(shuffle, 8, 1)

    #Apply MixColumns
    MC = [[2, 3, 1, 1], [1, 2, 3, 1], [1, 1, 2, 3], [3, 1, 1, 2]]
    IP = 0x1b
    AES.apply_MC(8, MC, IP, 4, 4)

    return AES
示例#5
0
def generate_spongent_version(b):
    spongent = CipherDescription(b)
    sbox = [0xE, 0xD, 0xB, 0, 2, 1, 4, 0xF, 7, 0xA, 8, 5, 9, 0xC, 3, 6]
    spongent.add_sbox('S-box', sbox)
    for i in range(b / 4):
        bits = []
        for j in range(4):
            bits.append("s{}".format(4 * i + 3 - j))
        spongent.apply_sbox('S-box', bits, bits)

    shuffle = [(j * b / 4) % (b - 1) for j in range(b - 1)]
    shuffle.append(b - 1)
    spongent.shufflewords(shuffle, 1, 1)
    return spongent
示例#6
0
def generate_photon_version(t):

    #define wordsize, statesize, and irreducible polynomial
    if t == 100:
        d = 5
        s = 4
        IP = 3
        Z = [1, 2, 9, 9, 2]
    elif t == 144:
        d = 6
        s = 4
        IP = 3
        Z = [1, 2, 8, 5, 8, 2]
    elif t == 196:
        d = 7
        s = 4
        IP = 3
        Z = [1, 4, 6, 1, 1, 6, 4]
    elif t == 256:
        d = 8
        s = 4
        IP = 3
        Z = [2, 4, 2, 11, 2, 8, 5, 6]
    elif t == 288:
        d = 6
        s = 8
        IP = 0x1b
        Z = [2, 3, 1, 2, 1, 4]
    photon = CipherDescription(t)

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

    #Apply the sbox to all cells
    for i in range(d * d):
        bits = []
        for j in range(s):
            bits.append("s{}".format(s * i + s - 1 - j))
        photon.apply_sbox('S-box', bits, bits)

    #Define and apply shiftrows
    tshuffle = []
    for i in range(d):
        tshuffle.append([((j + i) % d) * d + i for j in range(d)])

    shuffle = []
    for i in range(d):
        for j in range(d):
            shuffle.append(tshuffle[j][i])

    photon.shufflewords(shuffle, s, 1)
    '''
    #Apply MixColumns
    MC = []
    for i in range(d-1):
        temp = [0]
        for j in range(d-1):
            if i==j:
                temp.append(1)
            else: 
                temp.append(0)
        MC.append(temp)
    MC.append(Z)
    
    for i in range(d):
        photon.apply_MC(s,MC,IP,d,d)
    '''
    photon.apply_MC_serial(s, Z, IP)
    return photon
示例#7
0
def generate_Prince_version(forward, backward):
    prince = CipherDescription(64)
    wordsize = 4

    prince.add_sbox('S-box', sbox)
    prince.add_sbox('S-boxi', invsbox)

    M = []
    M.append([])
    for i in range(4):
        for j in range(4):
            M[0].append([])
            for c in range(16):
                M[0][i * 4 + j].append(m[(c / 4 + i) % 4][j][c % 4])

    M.append([])
    for i in range(4):
        for j in range(4):
            M[1].append([])
            for c in range(16):
                M[1][i * 4 + j].append(m[(c / 4 + i + 1) % 4][j][c % 4])

    def S():
        for i in range(16):
            bits = []
            for j in range(wordsize):
                bits.append("s{}".format(wordsize * i + wordsize - 1 - j))
            prince.apply_sbox('S-box', bits, bits)

    def Si():
        for i in range(16):
            bits = []
            for j in range(wordsize):
                bits.append("s{}".format(wordsize * i + wordsize - 1 - j))
            prince.apply_sbox('S-boxi', bits, bits)

    def Mhat():
        #Create temp variables
        for i in range(64):
            prince.apply_mov("s{}".format(i), "t{}".format(i))

        #Apply the M hat matrix
        for c in range(4):
            if c % 4 == 0:
                t = 0
            else:
                t = 1
            for i in range(16):
                k = M[t][i].index(1)
                prince.apply_mov("t{}".format(16 * c + k),
                                 "s{}".format(16 * c + i))
                for j in range(k + 1, 16):
                    if M[t][i][j] == 1:
                        prince.apply_xor("t{}".format(16 * c + j),
                                         "s{}".format(16 * c + i),
                                         "s{}".format(16 * c + i))

    def R():
        S()
        Mhat()
        prince.shufflewords(shuffle, 4, 1)

    def Ri():
        prince.shufflewords(shufflei, 4, 1)
        Mhat()
        Si()

    #Forward rounds
    for i in range(forward):
        R()

    #Middle
    S()
    Mhat()
    Si()

    #Inverse rounds
    for i in range(backward):
        Ri()

    return prince
示例#8
0
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()
示例#9
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)
示例#10
0
    0xF3, 0x3C, 0x65, 0x7B, 0x63, 0x7C, 0x30, 0x6A, 0xDD, 0x4E, 0xA7, 0x79,
    0x9E, 0xB2, 0x3D, 0x31, 0x3E, 0x98, 0xB5, 0x6E, 0x27, 0xD3, 0xBC, 0xCF,
    0x59, 0x1E, 0x18, 0x1F, 0x4C, 0x5A, 0xB7, 0x93, 0xE9, 0xDE, 0xE7, 0x2C,
    0x8F, 0x0C, 0x0F, 0xA6, 0x2D, 0xDB, 0x49, 0xF4, 0x6F, 0x73, 0x96, 0x47,
    0x06, 0x07, 0x53, 0x16, 0xED, 0x24, 0x7A, 0x37, 0x39, 0xCB, 0xA3, 0x83,
    0x03, 0xA9, 0x8B, 0xF6, 0x92, 0xBD, 0x9B, 0x1C, 0xE5, 0xD1, 0x41, 0x01,
    0x54, 0x45, 0xFB, 0xC9, 0x5E, 0x4D, 0x0E, 0xF2, 0x68, 0x20, 0x80, 0xAA,
    0x22, 0x7D, 0x64, 0x2F, 0x26, 0x87, 0xF9, 0x34, 0x90, 0x40, 0x55, 0x11,
    0xBE, 0x32, 0x97, 0x13, 0x43, 0xFC, 0x9A, 0x48, 0xA0, 0x2A, 0x88, 0x5F,
    0x19, 0x4B, 0x09, 0xA1, 0x7E, 0xCD, 0xA4, 0xD0, 0x15, 0x44, 0xAF, 0x8C,
    0xA5, 0x84, 0x50, 0xBF, 0x66, 0xD2, 0xE8, 0x8A, 0xA2, 0xD7, 0x46, 0x52,
    0x42, 0xA8, 0xDF, 0xB3, 0x69, 0x74, 0xC5, 0x51, 0xEB, 0x23, 0x29, 0x21,
    0xD4, 0xEF, 0xD9, 0xB4, 0x3A, 0x62, 0x28, 0x75, 0x91, 0x14, 0x10, 0xEA,
    0x77, 0x6C, 0xDA, 0x1D
]
BelT.add_sbox('H', H)


def G(u, r):
    u.reverse()
    for i in range(4):
        BelT.apply_sbox('H', u[i * 8:(i + 1) * 8], u[i * 8:(i + 1) * 8])

    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)]
示例#11
0
from cipher_description import CipherDescription

LED = CipherDescription(64)
s = 4
d = 4
IP = 3
Z = [4, 1, 2, 2]

#Add the appropriate sbox
sbox = [
    0xC, 0x5, 0x6, 0xB, 0x9, 0x0, 0xA, 0xD, 0x3, 0xE, 0xF, 0x8, 0x4, 0x7, 0x1,
    0x2
]
LED.add_sbox('S-box', sbox)

#Apply the sbox to all cells
for i in range(d * d):
    bits = []
    for j in range(s):
        bits.append("s{}".format(s * i + s - 1 - j))
    LED.apply_sbox('S-box', bits, bits)

#Define and apply shiftrows
tshuffle = []
for i in range(d):
    tshuffle.append([((j + i) % d) * d + i for j in range(d)])

shuffle = []
for i in range(d):
    for j in range(d):
        shuffle.append(tshuffle[j][i])
示例#12
0
# State size
b = 200
lanesize = b // 25

keccak_sbox = [
    0x0, 0x5, 0xa, 0xb, 0x14, 0x11, 0x16, 0x17, 0x9, 0xc, 0x3, 0x2, 0xd, 0x8,
    0xf, 0xe, 0x12, 0x15, 0x18, 0x1b, 0x6, 0x1, 0x4, 0x7, 0x1a, 0x1d, 0x10,
    0x13, 0x1e, 0x19, 0x1c, 0x1f
]

rot_const = [[0, 36, 3, 41, 18], [1, 44, 10, 45, 2], [62, 6, 43, 15, 61],
             [28, 55, 25, 21, 56], [27, 20, 39, 8, 14]]

keccak = CipherDescription(b)
keccak.add_sbox('S-box', keccak_sbox)

# 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)
示例#13
0
文件: sm4.py 项目: siweisun/solvatore
        0xad, 0x93, 0x32, 0x30, 0xf5, 0x8c, 0xb1, 0xe3,
        0x1d, 0xf6, 0xe2, 0x2e, 0x82, 0x66, 0xca, 0x60,
        0xc0, 0x29, 0x23, 0xab, 0x0d, 0x53, 0x4e, 0x6f,
        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])
示例#14
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
示例#15
0
文件: DES.py 项目: siweisun/solvatore
S7 = []
for j in range(2):
    for i in range(16):
        S7.extend([S7t[i + 32 * j], S7t[i + 32 * j + 16]])

S8t = [
    13, 2, 8, 4, 6, 15, 11, 1, 10, 9, 3, 14, 5, 0, 12, 7, 1, 15, 13, 8, 10, 3,
    7, 4, 12, 5, 6, 11, 0, 14, 9, 2, 7, 11, 4, 1, 9, 12, 14, 2, 0, 6, 10, 13,
    15, 3, 5, 8, 2, 1, 14, 7, 4, 10, 8, 13, 15, 12, 9, 0, 3, 5, 6, 11
]
S8 = []
for j in range(2):
    for i in range(16):
        S8.extend([S8t[i + 32 * j], S8t[i + 32 * j + 16]])

DES.add_sbox('S-box1', S1)
DES.add_sbox('S-box2', S2)
DES.add_sbox('S-box3', S3)
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
]
示例#16
0
from cipher_description import CipherDescription

size = 64
LBlock = CipherDescription(size)
s = ['s{}'.format(i) for i in range(size)]
t = ['t{}'.format(i) for i in range(size / 2)]

s0 = [14, 9, 15, 0, 13, 4, 10, 11, 1, 2, 8, 3, 7, 6, 12, 5]
s1 = [4, 11, 14, 9, 15, 13, 0, 10, 7, 12, 5, 6, 2, 8, 1, 3]
s2 = [1, 14, 7, 12, 15, 13, 0, 6, 11, 5, 9, 3, 2, 4, 8, 10]
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])
示例#17
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
示例#18
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
示例#19
0
def apply_sigma(cipher, bit, rot_a, rot_b):
    '''
    Sigma Function used in ASCON
    '''
    cipher.apply_xor("s{}".format((bit - rot_a) % 64), "s{}".format(
        (bit - rot_b) % 64), "tmp")
    cipher.apply_xor("s{}".format(bit), "tmp", "s{}".format(bit))


ascon_sbox = [
    4, 11, 31, 20, 26, 21, 9, 2, 27, 5, 8, 18, 29, 3, 6, 28, 30, 19, 7, 14, 0,
    13, 17, 24, 16, 12, 1, 25, 22, 10, 15, 23
]

ascon = CipherDescription(320)
ascon.add_sbox('S-box', ascon_sbox)

# Substitution Layer
for i in range(64):
    bits = [
        "s{}".format(i + 0), "s{}".format(i + 64), "s{}".format(i + 128),
        "s{}".format(i + 192), "s{}".format(i + 256)
    ]
    ascon.apply_sbox('S-box', bits, bits)

# Linear Layer
for i in range(64):
    apply_sigma(ascon, i, 19, 28)
    apply_sigma(ascon, i + 64, 61, 39)
    apply_sigma(ascon, i + 128, 1, 6)
    apply_sigma(ascon, i + 192, 10, 17)
示例#20
0
def generate_skipjack():
    skipjack = CipherDescription(64)

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

    def g(bits):
        t = ["t{}".format(i) for i in range(8)]
        for i in range(4):
            for j in range(8):
                skipjack.apply_mov(bits[j], t[j])
            skipjack.apply_sbox('F', t[::-1], t[::-1])
            for j in range(8):
                skipjack.apply_xor(bits[j + 8], t[j], bits[j + 8])

            shuffle = []
            for i in range(8):
                shuffle.append([bits[i], bits[i + 8]])
            for i in shuffle:
                skipjack.apply_permutation(i)

    def RuleA(bits):
        shuffle = [3, 0, 1, 2]
        g(bits[:16])
        for i in range(16):
            skipjack.apply_xor(bits[i], bits[48 + i], bits[48 + i])
        skipjack.shufflewords(shuffle, 16, 1)

    def RuleB(bits):
        shuffle = [3, 0, 1, 2]
        for i in range(16):
            skipjack.apply_xor(bits[i], bits[16 + i], bits[16 + i])
        g(bits[:16])
        skipjack.shufflewords(shuffle, 16, 1)

    bits = ["s{}".format(i) for i in range(64)]

    for i in range(8):
        RuleA(bits)
    for i in range(8):
        RuleB(bits)
    for i in range(8):
        RuleA(bits)
    for i in range(0):
        RuleB(bits)

    return skipjack
示例#21
0
# H. Hadipour
# 10 Nov 2018
from cipher_description import CipherDescription

size = 64
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)
示例#22
0
def generate_QARMA_version(wordsize,sigma,f,b):
    QARMA = CipherDescription(16*wordsize)

    # State
    # 0 4  8 12
    # 1 5  9 13
    # 2 6 10 14
    # 3 7 11 15

    s0 = [ 0, 14, 2, 10, 9, 15, 8, 11, 6, 4, 3, 7, 13, 12, 1, 5 ]
    s1 = [ 10, 13, 14, 6, 15, 7, 3, 5, 9, 8, 0, 12, 11, 1, 2, 4 ]
    s2 = [ 11, 6, 8, 15, 12, 0, 9, 14, 3, 7, 4, 5, 13, 2, 1, 10 ]
    s2i = [ 5, 14, 13, 8, 10, 11, 1, 9, 2, 6, 15, 0, 4, 12, 7, 3 ]

    IP = 1

    if sigma == 0:
        QARMA.add_sbox("S-box", s0)
        QARMA.add_sbox("S-boxinverse", s0)
    elif sigma == 1:
        QARMA.add_sbox("S-box", s1)
        QARMA.add_sbox("S-boxinverse", s1)
    else:
        QARMA.add_sbox("S-box", s2)
        QARMA.add_sbox("S-boxinverse", s2i)

    def S():
        if wordsize == 4:
            for i in range(16):
                bits = []
                for j in range(4):
                    bits.append("s{}".format(4*i + 3 - j))
                QARMA.apply_sbox('S-box', bits, bits)
        else:
            for i in range(32):
                bits = []
                for j in range(4):
                    bits.append("s{}".format(4*i + 3 - j))
                QARMA.apply_sbox('S-box', bits, bits)
                # Permute output bits

            for i in range(16):
                shuffle_bits_first_sbox = ["s{}".format(8*i + j) for j in [1, 2, 4]]
                shuffle_bits_secnd_sbox = ["s{}".format(8*i + j) for j in [3, 6, 5]]
                QARMA.apply_permutation(shuffle_bits_first_sbox)
                QARMA.apply_permutation(shuffle_bits_secnd_sbox)
            #     for j in range(8):
            #         QARMA.apply_mov("s{}".format(8*i+j),"t{}".format(j))
            #
            #     for j in range(4):
            #         QARMA.apply_mov("t{}".format(2*j),"s{}".format(8*i+j))
            #     for j in range(4):
            #         QARMA.apply_mov("t{}".format(2*j+1),"s{}".format(8*i+j+4))

    def Si():
        if wordsize == 4:
            for i in range(16):
                bits = []
                for j in range(4):
                    bits.append("s{}".format(4*i + 3 - j))
                QARMA.apply_sbox('S-boxinverse', bits, bits)
        else:
            for i in range(16):
                shuffle_bits_first_sbox = ["s{}".format(8*i + j) for j in [4, 2, 1]]
                shuffle_bits_secnd_sbox = ["s{}".format(8*i + j) for j in [5, 6, 3]]
                QARMA.apply_permutation(shuffle_bits_first_sbox)
                QARMA.apply_permutation(shuffle_bits_secnd_sbox)
            for i in range(32):
                bits = []
                for j in range(4):
                    bits.append("s{}".format(4*i + 3 - j))
                QARMA.apply_sbox('S-boxinverse', bits, bits)



    if wordsize == 4:
        MC = [[0,2,4,2],
              [2,0,2,4],
              [4,2,0,2],
              [2,4,2,0]]
    else:
        MC = [[0,2,16,32],
              [32,0,2,16],
              [16,32,0,2],
              [2,16,32,0]]

    shuffle = [0,10,5,15, 14,4,11,1, 9,3,12,6, 7,13,2,8]
    shufflei = [0,7,14,9, 5,2,11,12, 15,8,1,6, 10,13,4,3]

    def R():
        QARMA.shufflewords(shuffle,wordsize,1)
        QARMA.apply_MC(wordsize,MC,IP,4,4)
        S()

    def Ri():
        Si()
        QARMA.apply_MC(wordsize,MC,IP,4,4)
        QARMA.shufflewords(shufflei,wordsize,1)

    #Forward rounds
    S()
    for r in range(f):
        R()
    #Middle round
    QARMA.shufflewords(shuffle,wordsize,1)
    QARMA.apply_MC(wordsize,MC,IP,4,4)
    QARMA.shufflewords(shufflei,wordsize,1)
    #Backwards rounds
    for r in range(b):
        Ri()
    Si()

    return QARMA
示例#23
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)
示例#24
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)