예제 #1
0
def KeyScheduleMod1_(W, Nr, verbose=1):
    assert is_key_schedule(W)
    rk = deepcopy(W)
    for i in range(Nr + 1):
        for j in range(len(rk[0])):
            rk[4 * i + j] = W[4 * (Nr - i) + j]
        # assert 0 <= i and i <= Nr and Nr = Nr% and
        #        (for all p in Integer range 0 .. i =>
        #          (rk(4*p  ) = W(4*(Nr-p)  ) and
        #           rk(4*p+1) = W(4*(Nr-p)+1) and
        #           rk(4*p+2) = W(4*(Nr-p)+2) and
        #           rk(4*p+3) = W(4*(Nr-p)+3))) and
        #        (for all j in Integer range 4*(Nr+1) .. 4*(MAXNR+1)-1 =>
        #          (rk(j) = W(j)))


#
#     if verbose >= 1:
#         vs1 = ['RK_%d_%d'%(i,j) for i in range(len(rk)) for j in range(len(rk[0]))]
#         ns1 = [rk[i][j]  for i in range(len(rk)) for j in range(len(rk[0]))]
#
#         vs2 = ['W_%d_%d'%(i,j) for i in range(len(W)) for j in range(len(W[0]))]
#         ns2 = [W[i][j]  for i in range(len(W)) for j in range(len(W[0]))]
#
#
#         print 'l0: ', ' '.join(vs1+vs2)
#         print 'l0: ', ' '.join(map(str,ns1+ns2))

    assert is_key_schedule(rk)
    return rk
예제 #2
0
def KeyScheduleMod2(W, Nr, verbose=1):  # return key_schedule
    assert is_key_schedule(W)
    assert Nr in nr_vals

    rk = deepcopy(W)
    for i in range(1, Nr):
        st = [W[4 * i], W[4 * i + 1], W[4 * i + 2], W[4 * i + 3]]
        assert is_state(st)
        st = InvMixColumns(st, verbose=0)
        rk[4 * i] = st[0]
        rk[4 * i + 1] = st[1]
        rk[4 * i + 2] = st[2]
        rk[4 * i + 3] = st[3]
        # assert 1 <= i and i <= Nr-1 and Nr = Nr% and
        #        (for all p in Integer range 1 .. i =>
        #          (rk(4*p  ) = InvMixColumns([W(4*p), W(4*p+1), W(4*p+2), W(4*p+3)))(0) and
        #           rk(4*p+1) = InvMixColumns([W(4*p), W(4*p+1), W(4*p+2), W(4*p+3)))(1) and
        #           rk(4*p+2) = InvMixColumns([W(4*p), W(4*p+1), W(4*p+2), W(4*p+3)))(2) and
        #           rk(4*p+3) = InvMixColumns([W(4*p), W(4*p+1), W(4*p+2), W(4*p+3)))(3))) and
        #        (for all j in Integer range 0 .. 3 =>
        #          (rk(j) = W(j))) and
        #        (for all j in Integer range 4*Nr .. 4*(MAXNR+1)-1 =>
        #          (rk(j) = W(j)))

    assert is_key_schedule(rk)

    if verbose >= 1:
        print 'l0: W rk'
        print 'l0: ', W, rk

    return rk
예제 #3
0
def KeyScheduleMod1__(W, Nr, verbose=1):
    assert is_key_schedule(W)
    rk = deepcopy(W)
    for i in range(4 * (Nr + 1)):
        #print i, 4*(Nr-i//4)+i%4
        #print i, (4*Nr- 4*(i//4))+i%4
        rk[i] = W[4 * (Nr - i // 4) + i % 4]

        #rk[i] = W[add(mul4(sub(Nr,div4(i))),mod4(i))]

        # assert 0 <= i and i <= Nr and Nr = Nr% and
        #        (for all p in Integer range 0 .. i =>
        #          (rk(4*p  ) = W(4*(Nr-p)  ) and
        #           rk(4*p+1) = W(4*(Nr-p)+1) and
        #           rk(4*p+2) = W(4*(Nr-p)+2) and
        #           rk(4*p+3) = W(4*(Nr-p)+3))) and
        #        (for all j in Integer range 4*(Nr+1) .. 4*(MAXNR+1)-1 =>
        #          (rk(j) = W(j)))

    if verbose >= 1:
        # vs1 = ['RK_%d_%d'%(i,j) for i in range(len(rk)) for j in range(len(rk[0]))]
        # ns1 = [rk[i][j]  for i in range(len(rk)) for j in range(len(rk[0]))]

        # vs2 = ['W_%d_%d'%(i,j) for i in range(len(W)) for j in range(len(W[0]))]
        # ns2 = [W[i][j]  for i in range(len(W)) for j in range(len(W[0]))]

        # print ' '.join(vs1+vs2)
        # print ' '.join(map(str,ns1+ns2))

        print 'l0: rk, W Nr'
        print 'l0: ', rk, W, [Nr]

    assert is_key_schedule(rk)
    return rk
예제 #4
0
def KeySetupEnc4(cipherKey, verbose=1):  # return key_schedule
    """
    tvn: *multidim* array
    rk[i][j] = cipherKey[4*i+j]
    """

    if __debug__:
        assert is_key(cipherKey), cipherKey

    #rk = key_schedule'(others => [0, 0, 0, 0])
    rk = [[0, 0, 0, 0]
          ] * roundkey_index  #create an array of roundkey_index empty words
    for i in range(4):
        rk[i] = [
            cipherKey[4 * i], cipherKey[4 * i + 1], cipherKey[4 * i + 2],
            cipherKey[4 * i + 3]
        ]

    if verbose >= 1:
        print 'l1: rk cipherKey'
        print 'l1: ', rk[:6], cipherKey

    #--# assert 0 <= i and i <= 3 and
    #--#        (for all p in Integer range 0 .. i =>
    #--#          (rk(p) = [cipherKey(4*p), cipherKey(4*p+1), cipherKey(4*p+2), cipherKey(4*p+3))))

    for i in range(4, 44):
        if i % 4 == 0:
            rk[i] = word_xor_xor(rk[i - 4],
                                 SubWord(RotWord(rk[i - 1], verbose=0),
                                         verbose=0),
                                 rcon[i / 4 - 1],
                                 verbose=0)
        else:
            rk[i] = word_xor(rk[i - 4], rk[i - 1], verbose=0)

    # if verbose >= 1:
    #     print 'rk_else cipherKey'
    #     print [rk[i] for i in range(4,44)], cipherKey

    # assert 4 <= i and i <= 43 and
    # (for all p in Integer range 0 .. 3 =>
    #          (rk(p) = [cipherKey(4*p), cipherKey(4*p+1), cipherKey(4*p+2), cipherKey(4*p+3)))) and
    #        (for all p in Integer range 4 .. i =>
    #          ((p mod 4 = 0 and rk(p) = word_xor_xor(rk(p-4), SubWord(RotWord(rk(p-1))), rcon(p/4-1))) or
    #          (p mod 4 /= 0 and rk(p) = word_xor(rk(p-4), rk(p-1)))))

    #disj
    if verbose >= 1:
        print 'l0: rk cipherKey'
        print 'l0: ', rk, cipherKey

    assert is_key_schedule(rk)

    return rk
예제 #5
0
def KeySetupEnc8(cipherKey, verbose=1):  # return key_schedule
    """
    tvn: *multidim* array
    rk[i][j] = cipherKey[4*i+j]
    """
    assert is_key(cipherKey)

    rk = [[0, 0, 0, 0]
          ] * roundkey_index  #create an array of roundkey_index empty words

    for i in range(8):
        rk[i] = [
            cipherKey[4 * i], cipherKey[4 * i + 1], cipherKey[4 * i + 2],
            cipherKey[4 * i + 3]
        ]

    if verbose >= 1:
        print 'l1: rk cipherKey'
        print 'l1: ', rk, cipherKey

        # assert 0 <= i and i <= 7 and
        #        (for all p in Integer range 0 .. i =>
        #          (rk(p) = [cipherKey(4*p), cipherKey(4*p+1), cipherKey(4*p+2), cipherKey(4*p+3))))

    for i in range(8, 60):
        if (i % 8 == 0):
            rk[i] = word_xor_xor(rk[i - 8],
                                 SubWord(RotWord(rk[i - 1], verbose=0),
                                         verbose=0),
                                 rcon[i / 8 - 1],
                                 verbose=0)
        elif (i % 4 == 0):
            rk[i] = word_xor(rk[i - 8],
                             SubWord(rk[i - 1], verbose=0),
                             verbose=0)
        else:
            rk[i] = word_xor(rk[i - 8], rk[i - 1], verbose=0)

        # assert 8 <= i and i <= 59 and
        #        (for all p in Integer range 0 .. 7 =>
        #          (rk(p) = [cipherKey(4*p), cipherKey(4*p+1), cipherKey(4*p+2), cipherKey(4*p+3)))) and
        #        (for all p in Integer range 8 .. i =>
        #          ((p % 8 = 0 and                  rk(p) = word_xor_xor(rk(p-8), SubWord(RotWord(rk(p-1))), rcon(p/8-1))) or
        #          (p % 8 /= 0 and p % 4 = 0 and  rk(p) = word_xor(rk(p-8), SubWord(rk(p-1)))) or
        #          (p % 8 /= 0 and p % 4 /= 0 and rk(p) = word_xor(rk(p-8), rk(p-1)))))

    if verbose >= 1:
        print 'l0: rk cipherKey'
        print 'l0: ', rk, cipherKey

    assert is_key_schedule(rk)
    return rk
예제 #6
0
def AesKeySetupDec(cipherKey, keyBits, verbose=1):

    assert is_key(cipherKey)
    assert keyBits in keybit_vals

    Nr = keyBits / 32 + 6
    rk = KeySetupDec(cipherKey, keyBits / 32, verbose=0)

    if verbose >= 1:  #tvntraces
        print 'l0: cipherKey rk'
        print 'l0: ', cipherKey, rk

    assert is_key_schedule(rk)
    assert Nr in nr_vals

    return Nr, rk
예제 #7
0
def KeySetupDec(cipherKey, Nk, verbose=1):  # return key_schedule
    assert is_key(cipherKey)
    assert Nk in nk_vals

    rk = KeyScheduleMod2(KeyScheduleMod1(KeySetupEnc(cipherKey, Nk, verbose=0),
                                         Nk + 6,
                                         verbose=0),
                         Nk + 6,
                         verbose=0)
    assert is_key_schedule(rk)

    if verbose >= 1:
        print 'l0: rk cipherKey'
        print 'l0: ', rk, cipherKey

    return rk
예제 #8
0
def KeySetupEnc6(cipherKey, verbose=1):  # return key_schedule
    """
    tvn: *multidim* array
    rk[i][j] = cipherKey[4*i+j]
    """

    assert is_key(cipherKey)

    rk = [[0, 0, 0, 0]
          ] * roundkey_index  #create an array of roundkey_index empty words

    for i in range(6):
        rk[i] = [
            cipherKey[4 * i], cipherKey[4 * i + 1], cipherKey[4 * i + 2],
            cipherKey[4 * i + 3]
        ]
        # assert 0 <= i and i <= 5 and
        #        (for all p in Integer range 0 .. i =>
        #          (rk(p) = [cipherKey(4*p), cipherKey(4*p+1), cipherKey(4*p+2), cipherKey(4*p+3))))

    if verbose >= 1:
        print 'l1: rk cipherKey'
        print 'l1: ', rk, cipherKey

    for i in range(6, 52):
        if (i % 6 == 0):
            rk[i] = word_xor_xor(rk[i - 6],
                                 SubWord(RotWord(rk[i - 1], verbose=0),
                                         verbose=0),
                                 rcon[i / 6 - 1],
                                 verbose=0)
        else:
            rk[i] = word_xor(rk[i - 6], rk[i - 1], verbose=0)
        # assert 6 <= i and i <= 51 and
        #        (for all p in Integer range 0 .. 5 =>
        #          (rk(p) = [cipherKey(4*p), cipherKey(4*p+1), cipherKey(4*p+2), cipherKey(4*p+3)))) and
        #        (for all p in Integer range 6 .. i =>
        #          ((p mod 6 = 0 and rk(p) = word_xor_xor(rk(p-6), SubWord(RotWord(rk(p-1))), rcon(p/6-1))) or
        #          (p mod 6 /= 0 and rk(p) = word_xor(rk(p-6), rk(p-1)))))

    if verbose >= 1:
        print 'l0: rk cipherKey'
        print 'l0: ', rk, cipherKey

    assert is_key_schedule(rk)
    return rk
예제 #9
0
def AesEncrypt(rk, Nr, pt, verbose=1):
    assert is_key_schedule(rk)
    assert is_block(pt)
    assert Nr in nr_vals

    st = AddRoundKey(Block2State(pt, verbose=0),
                     rk[0],
                     rk[1],
                     rk[2],
                     rk[3],
                     verbose=0)
    # assert st = encrypt_round(Block2State(pt), rk, Nr, 0)

    for r in range(1, Nr):

        sb = SubBytes(st, verbose=0)
        sr = ShiftRows(sb, verbose=0)

        st = AddRoundKey(MixColumns(sr, verbose=0),
                         rk[4 * r],
                         rk[4 * r + 1],
                         rk[4 * r + 2],
                         rk[4 * r + 3],
                         verbose=0)

    st = AddRoundKey(ShiftRows(SubBytes(st, verbose=0), verbose=0),
                     rk[4 * Nr],
                     rk[4 * Nr + 1],
                     rk[4 * Nr + 2],
                     rk[4 * Nr + 3],
                     verbose=0)
    # assert st = encrypt_round(Block2State(pt), rk, Nr, Nr)
    # assert 1 <= r and r <= Nr-1 and Nr = Nr% and
    #        st = encrypt_round(Block2State(pt), rk, Nr, r)

    ct = State2Block(st, verbose=0)

    if verbose >= 1:
        print 'l0: rk ct st pt'
        print 'l0: ', rk, ct, st, pt

    assert is_block(ct)
    return ct
예제 #10
0
def KeySetupEnc(cipherKey, Nk, verbose=1):
    assert is_key(cipherKey)
    assert Nk in nk_vals

    rk = [[0, 0, 0, 0]
          ] * roundkey_index  #create an array of roundkey_index empty words
    if (Nk == 4):
        rk = KeySetupEnc4(cipherKey, verbose=0)
    if (Nk == 6):
        rk = KeySetupEnc6(cipherKey, verbose=0)
    if (Nk == 8):
        rk = KeySetupEnc8(cipherKey, verbose=0)

    assert is_key_schedule(rk)

    if verbose >= 1:
        print 'l0: rk cipherKey'
        print 'l0: ', rk, cipherKey

    return rk