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)
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)
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)
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)
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)
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)
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)
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)
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)
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) + ')'
def testFFXInteger4(self): X = FFXInteger('000') self.assertEquals(X.to_bytes(), '\x00')
def testFFXInteger3(self): for blocksize in range(1, 129): X = FFXInteger('0', radix=2, blocksize=blocksize) self.assertEquals(len(X), blocksize)
def testFFXInteger6(self): X = FFXInteger('FF', radix=16) self.assertEquals(X.to_bytes(), '\xFF')
def testFFXInteger5(self): X = FFXInteger('11111111') self.assertEquals(X.to_bytes(), '\xFF')
def testFFXInteger3(self): X = FFXInteger('000') Y = FFXInteger('111') self.assertEquals(str(X) + str(Y), '000111')
def testFFXInteger2(self): X = FFXInteger('000') Y = FFXInteger('111') self.assertEquals(X + Y, 7) self.assertEquals(X + Y, FFXInteger('111'))
def testFFXInteger1(self): X = FFXInteger('1') Y = FFXInteger('1') self.assertEquals(X + Y, 2) self.assertEquals(X + Y, FFXInteger('10'))