Beispiel #1
0
    def testFFXEncrypt1(self):
        radix = 2
        K = FFXInteger('0' * 8, radix=radix, blocksize=128)
        T = FFXInteger('0' * 8, radix=radix, blocksize=8)
        M1 = FFXInteger('0' * 8, radix=radix, blocksize=8)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)
Beispiel #2
0
    def testFFXEncrypt1(self):
        radix = 2
        K = FFXInteger('0' * 8, radix=radix, blocksize=128)
        T = FFXInteger('0' * 8, radix=radix, blocksize=8)
        M1 = FFXInteger('0' * 8, radix=radix, blocksize=8)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)
Beispiel #3
0
    def testVector2(self):
        # see aes-ffx-vectors.txt

        radix = 10
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16, blocksize=32)
        T = 0  # FFXInteger(0, radix=radix, blocksize=2)
        M1 = FFXInteger('0123456789', radix=radix, blocksize=10)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(C, '2433477484')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #2: radix=' + str(radix) + ', input=' + str(M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #4
0
    def testVector3(self):
        # see aes-ffx-vectors.txt

        radix = 10
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16, blocksize=32)
        T = FFXInteger('2718281828', radix=radix, blocksize=10)
        M1 = FFXInteger('314159', radix=radix, blocksize=6)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(C, '535005')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #3: radix=' + str(radix) + ', input=' + str(M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #5
0
    def testVector5(self):
        # see aes-ffx-vectors.txt

        radix = 36
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16, blocksize=32)
        T = FFXInteger('TQF9J5QDAGSCSPB1', radix=radix, blocksize=16)
        M1 = FFXInteger('C4XPWULBM3M863JH', radix=radix, blocksize=16)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(str(C).upper(), 'C8AQ3U846ZWH6QZP')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #5: radix=' + str(radix) + ', input=' + str(M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #6
0
    def testVector4(self):
        # see aes-ffx-vectors.txt

        radix = 10
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16, blocksize=32)
        T = FFXInteger('7777777', radix=radix, blocksize=7)
        M1 = FFXInteger('999999999', radix=radix, blocksize=9)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(C, '658229573')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #4: radix=' + str(radix) + ', input=' + str(M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #7
0
    def testVector3(self):
        # see aes-ffx-vectors.txt

        radix = 10
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16,
                       blocksize=32)
        T = FFXInteger('2718281828', radix=radix, blocksize=10)
        M1 = FFXInteger('314159', radix=radix, blocksize=6)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(C, '535005')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #3: radix=' + str(radix) + ', input=' + str(
            M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #8
0
    def testVector2(self):
        # see aes-ffx-vectors.txt

        radix = 10
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16,
                       blocksize=32)
        T = 0  # FFXInteger(0, radix=radix, blocksize=2)
        M1 = FFXInteger('0123456789', radix=radix, blocksize=10)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(C, '2433477484')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #2: radix=' + str(radix) + ', input=' + str(
            M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #9
0
    def testVector5(self):
        # see aes-ffx-vectors.txt

        radix = 36
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16,
                       blocksize=32)
        T = FFXInteger('TQF9J5QDAGSCSPB1', radix=radix, blocksize=16)
        M1 = FFXInteger('C4XPWULBM3M863JH', radix=radix, blocksize=16)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(str(C).upper(), 'C8AQ3U846ZWH6QZP')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #5: radix=' + str(radix) + ', input=' + str(
            M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #10
0
    def testVector4(self):
        # see aes-ffx-vectors.txt

        radix = 10
        K = FFXInteger('2b7e151628aed2a6abf7158809cf4f3c',
                       radix=16,
                       blocksize=32)
        T = FFXInteger('7777777', radix=radix, blocksize=7)
        M1 = FFXInteger('999999999', radix=radix, blocksize=9)

        ffx = FFX.new(K.to_bytes(16), radix)
        C = ffx.encrypt(T, M1)
        self.assertEquals(C, '658229573')
        M2 = ffx.decrypt(T, C)

        self.assertEquals(M1, M2)

        print ''
        print 'TEST VECTOR #4: radix=' + str(radix) + ', input=' + str(
            M1) + ', tweak=' + str(T) + ', encrypted=' + str(C)
Beispiel #11
0
def main():
    parser = argparse.ArgumentParser()
    parser.add_argument("--radix", type=int, default=2)
    parser.add_argument("--tweaksize", type=int, default=8)
    parser.add_argument("--messagesize", type=int, default=32)
    parser.add_argument("--trials", type=int, default=10, required=False)
    args = parser.parse_args()

    radix = args.radix
    tweaksize = args.tweaksize
    messagesize = args.messagesize
    trials = args.trials

    keysize = 128
    K = random.randint(0, 2 ** keysize - 1)
    K = FFXInteger(K, radix=2, blocksize=keysize)

    banner = ['RADIX=' + str(radix),
              'TWEAKSIZE=' + str(tweaksize),
              'MESSAGESIZE=' + str(messagesize),
              'KEY=' + hex(K.to_int())
              ]
    print ', '.join(banner)

    ffx = FFX.new(K.to_bytes(), radix)
    for i in range(1, trials):
        T = random.randint(0, radix ** tweaksize - 1)
        T = FFXInteger(T, radix=radix, blocksize=tweaksize)

        M1 = random.randint(0, radix ** messagesize - 1)
        M1 = FFXInteger(M1, radix=radix, blocksize=messagesize) 

        start = time.time()
        C = ffx.encrypt(T, M1)
        encrypt_cost = time.time() - start
        encrypt_cost *= 1000.0

        start = time.time()
        M2 = ffx.decrypt(T, C)
        decrypt_cost = time.time() - start
        decrypt_cost *= 1000.0

        assert M1 == M2

        to_print = ['encrypt_cost=' + str(round(encrypt_cost, 1)) + 'ms',
                    'decrypt_cost=' + str(round(decrypt_cost, 1)) + 'ms',
                    'tweak=' + str(T),
                    'plaintext=' + str(M1),
                    'ciphertext=' + str(C),
                    ]
        print 'test #' + string.rjust(str(i), len(str(trials - 1)), '0') + ' SUCCESS: (' + ', '.join(to_print) + ')'
Beispiel #12
0
    def testFFXInteger4(self):
        X = FFXInteger('000')

        self.assertEquals(X.to_bytes(), '\x00')
Beispiel #13
0
 def testFFXInteger3(self):
     for blocksize in range(1, 129):
         X = FFXInteger('0', radix=2, blocksize=blocksize)
         self.assertEquals(len(X), blocksize)
Beispiel #14
0
    def testFFXInteger6(self):
        X = FFXInteger('FF', radix=16)

        self.assertEquals(X.to_bytes(), '\xFF')
Beispiel #15
0
    def testFFXInteger5(self):
        X = FFXInteger('11111111')

        self.assertEquals(X.to_bytes(), '\xFF')
Beispiel #16
0
    def testFFXInteger4(self):
        X = FFXInteger('000')

        self.assertEquals(X.to_bytes(), '\x00')
Beispiel #17
0
    def testFFXInteger3(self):
        X = FFXInteger('000')
        Y = FFXInteger('111')

        self.assertEquals(str(X) + str(Y), '000111')
Beispiel #18
0
    def testFFXInteger2(self):
        X = FFXInteger('000')
        Y = FFXInteger('111')

        self.assertEquals(X + Y, 7)
        self.assertEquals(X + Y, FFXInteger('111'))
Beispiel #19
0
    def testFFXInteger5(self):
        X = FFXInteger('11111111')

        self.assertEquals(X.to_bytes(), '\xFF')
Beispiel #20
0
    def testFFXInteger1(self):
        X = FFXInteger('1')
        Y = FFXInteger('1')

        self.assertEquals(X + Y, 2)
        self.assertEquals(X + Y, FFXInteger('10'))
Beispiel #21
0
    def testFFXInteger6(self):
        X = FFXInteger('FF', radix=16)

        self.assertEquals(X.to_bytes(), '\xFF')