示例#1
0
def AesEncrypt(rk, Nr, pt, verbose=1):
    assert is_key_schedule(rk)
    assert is_block(pt)
    st = AddRoundKey(Block2State(pt, verbose), rk[0], rk[1], rk[2], rk[3],
                     verbose)
    # assert st = encrypt_round(Block2State(pt), rk, Nr, 0)

    for r in range(1, Nr):

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

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

        # assert 1 <= r and r <= Nr-1 and Nr = Nr% and
        #        st = encrypt_round(Block2State(pt), rk, Nr, r)

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

    ct = State2Block(st, verbose)
    assert is_block(ct)
    return ct
示例#2
0
def AesDecrypt(rk, Nr, ct, verbose=1):
    assert is_key_schedule(rk)
    assert is_block(ct)
    assert Nr in nr_vals

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

    for r in range(1, Nr):
        st = AddRoundKey(InvMixColumns(InvShiftRows(InvSubBytes(st, verbose=0),
                                                    verbose=0),
                                       verbose=0),
                         rk[4 * r],
                         rk[4 * r + 1],
                         rk[4 * r + 2],
                         rk[4 * r + 3],
                         verbose=0)

    st = AddRoundKey(InvShiftRows(InvSubBytes(st, verbose=0), verbose=0),
                     rk[4 * Nr],
                     rk[4 * Nr + 1],
                     rk[4 * Nr + 2],
                     rk[4 * Nr + 3],
                     verbose=0)
    # assert st = decrypt_round(Block2State(ct), rk, Nr, Nr)

    # assert 1 <= r and r <= Nr-1 and Nr = Nr% and
    #        st = decrypt_round(Block2State(ct), rk, Nr, r)

    pt = State2Block(st, verbose=0)

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

    assert is_block(pt)
    return pt
示例#3
0
def Block2State(t, verbose=1):
    """
    tvn: multidim array inv
    r[i][j] = t[4i+j]
    """
    assert is_block(t)
    #inv : should get this:  r00 = t0 , r01 = t1  ....
    r = [[t[0], t[1], t[2], t[3]], [t[4], t[5], t[6], t[7]],
         [t[8], t[9], t[10], t[11]], [t[12], t[13], t[14], t[15]]]

    #tvn traces
    if verbose >= 1:
        print 'r t'
        print r, t

    assert is_state(r)
    return r
示例#4
0
def State2Block(st, verbose=1):
    """
    tvn: multidim array inv
    r[4*i+j] = st[i][j]
    """

    #inv: should get this:r0 = st00 ...
    r = [
        st[0][0], st[0][1], st[0][2], st[0][3], st[1][0], st[1][1], st[1][2],
        st[1][3], st[2][0], st[2][1], st[2][2], st[2][3], st[3][0], st[3][1],
        st[3][2], st[3][3]
    ]

    #tvn traces
    if verbose >= 1:
        print 'r st'
        print r, st

    assert is_block(r)
    return r