diff options
-rw-r--r-- | pypride.py | 197 | ||||
-rw-r--r-- | test-vectors.py | 33 |
2 files changed, 129 insertions, 101 deletions
@@ -1,6 +1,6 @@ # Python PRIDE implementation # Version: 1.0 -# Date: 22/04/2015 +# Date: 23/04/2015 # # ============================================================================= # @@ -82,102 +82,104 @@ PBox_inv = [PBox.index(x) for x in xrange(64)] """ Matrices for permutation in the L layer """ L0 = [[0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0], - [0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0], - [0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0], - [0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1], - [1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0], - [0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0], - [0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0], - [0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1], - [1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0], - [0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0], - [0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0], - [0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1], - [1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0], - [0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0], - [0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0], - [0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0]] + [0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0], + [0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0], + [0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1], + [1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0], + [0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0], + [0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0], + [0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1], + [1,0,0,0,1,0,0,0,0,0,0,0,1,0,0,0], +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,0,0,1]] + [0,1,0,0,0,1,0,0,0,0,0,0,0,1,0,0], + [0,0,1,0,0,0,1,0,0,0,0,0,0,0,1,0], + [0,0,0,1,0,0,0,1,0,0,0,0,0,0,0,1], + [1,0,0,0,1,0,0,0,1,0,0,0,0,0,0,0], + [0,1,0,0,0,1,0,0,0,1,0,0,0,0,0,0], + [0,0,1,0,0,0,1,0,0,0,1,0,0,0,0,0], + [0,0,0,1,0,0,0,1,0,0,0,1,0,0,0,0], + [0,0,0,0,1,0,0,0,1,0,0,0,1,0,0,0], + [0,0,0,0,0,1,0,0,0,1,0,0,0,1,0,0], + [0,0,0,0,0,0,1,0,0,0,1,0,0,0,1,0], + [0,0,0,0,0,0,0,1,0,0,0,1,0,0,0,1], + [1,0,0,0,0,0,0,0,1,0,0,0,1,0,0,0], + [0,1,0,0,0,0,0,0,0,1,0,0,0,1,0,0], + [0,0,1,0,0,0,0,0,0,0,1,0,0,0,1,0], + [0,0,0,1,0,0,0,0,0,0,0,1,0,0,0,1]] L3_inv = L3 def mapXor(xs): @@ -295,11 +297,4 @@ def number2string_N(i, N): Output: string (big-endian) """ s = '%0*x' % (N*2, i) - return s.decode('hex') - -if __name__ == "__main__": - cipher = Pride("0000000000000000fedcba9876543210".decode('hex')) - encryption = cipher.encrypt("0123456789abcdef".decode('hex')) - print encryption.encode('hex') - decryption = cipher.decrypt(encryption) - print decryption.encode('hex')
\ No newline at end of file + return s.decode('hex')
\ No newline at end of file diff --git a/test-vectors.py b/test-vectors.py new file mode 100644 index 0000000..2cb46d6 --- /dev/null +++ b/test-vectors.py @@ -0,0 +1,33 @@ +# Python PRIDE implementation test vectors +# Version: 1.0 +# Date: 23/04/2015 +# +# ============================================================================= +# +# Python implementation of the PRIDE cipher; test vectors +# Copyright (C) 2015 Camil Staps (info@camilstaps.nl) +# +# ============================================================================= +# +# These are the test vectors from appendix J of Block Ciphers -- Focus On The +# Linear Layer (feat. PRIDE); Martin R. Albrecht, Benedikt Driessen, Elif Bilge +# Kavun, Gregor Leander, Christof Paar, Tolga Yalçın: +# https://eprint.iacr.org/2014/453 +# +# ============================================================================= + +from pypride import Pride + +test_vectors = [ + {'key': "00000000000000000000000000000000", 'plaintext': "0000000000000000", 'ciphertext': "82b4109fcc70bd1f"}, + {'key': "00000000000000000000000000000000", 'plaintext': "ffffffffffffffff", 'ciphertext': "d70e60680a17b956"}, + {'key': "ffffffffffffffff0000000000000000", 'plaintext': "0000000000000000", 'ciphertext': "28f19f97f5e846a9"}, + {'key': "0000000000000000ffffffffffffffff", 'plaintext': "0000000000000000", 'ciphertext': "d123ebaf368fce62"}, + {'key': "0000000000000000fedcba9876543210", 'plaintext': "0123456789abcdef", 'ciphertext': "d1372929712d336e"} +] + +for vector in test_vectors: + cipher = Pride(vector['key'].decode('hex')) + encryption = cipher.encrypt(vector['plaintext'].decode('hex')) + decryption = cipher.decrypt(vector['ciphertext'].decode('hex')) + print encryption == vector['ciphertext'].decode('hex'), decryption == vector['plaintext'].decode('hex')
\ No newline at end of file |