Beispiel #1
0
def KeySetupDec(cipherKey, Nk, verbose=1):  # return key_schedule
    assert is_key(cipherKey)
    r = KeyScheduleMod2(
        KeyScheduleMod1(KeySetupEnc(cipherKey, Nk, verbose), Nk + 6, verbose),
        Nk + 6, verbose)
    assert is_key_schedule(r)
    return r
Beispiel #2
0
def KeySetupEnc4(cipherKey, verbose=1):  # return key_schedule
    """
    tvn: *multidim* array
    rk[i][j] = cipherKey[4*i+j]
    """
    print len(cipherKey)

    assert is_key(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]
        ]

    # #tvn: traces
    # if verbose >= 1:
    #     print 'rk cipherKey'
    #     print rk[:4], 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)

    #tvn: traces
    # 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 'rk cipherKey'
        print rk, cipherKey

    assert is_key_schedule(rk)

    return rk
Beispiel #3
0
def AesKeySetupEnc(cipherKey, keyBits, verbose=1):
    assert is_key(cipherKey)
    Nr = keyBits / 32 + 6
    rk = KeySetupEnc(cipherKey, keyBits / 32, verbose=0)

    assert is_key_schedule(rk)

    if verbose >= 1:  #tvntraces
        print 'Nr keyBits'
        print Nr, keyBits

    return Nr, rk
Beispiel #4
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
Beispiel #5
0
def KeySetupEnc(cipherKey, Nk, verbose=1):  #return key_schedule
    assert is_key(cipherKey)
    rk = [[0, 0, 0, 0]
          ] * roundkey_index  #create an array of roundkey_index empty words
    if (Nk == 4):
        rk = KeySetupEnc4(cipherKey, verbose)
    if (Nk == 6):
        rk = KeySetupEnc6(cipherKey, verbose)
    if (Nk == 8):
        rk = KeySetupEnc8(cipherKey, verbose)

    assert is_key_schedule(rk)
    return rk
Beispiel #6
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))))

    #tvn: traces
    # if verbose >= 1:
    #     print 'rk cipherKey'
    #     print rk[:6], 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)))))

    #tvn: traces
    if verbose >= 1:
        print 'rk cipherKey'
        print rk, cipherKey

    assert is_key_schedule(rk)
    return rk
Beispiel #7
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
Beispiel #8
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
Beispiel #9
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