diff options
author | Camil Staps | 2015-04-23 12:51:25 +0200 |
---|---|---|
committer | Camil Staps | 2015-04-23 12:51:25 +0200 |
commit | 09560a14c64f9a5e418334d3a5e77447e32ae85f (patch) | |
tree | 44e8794a2eaad4614b397e06f40a46ef6791bc34 | |
parent | Attribution for pypresent ideas (diff) |
Timing; generate roundkeys on initialisation
-rw-r--r-- | pypride.py | 25 | ||||
-rwxr-xr-x | test-vectors.py | 29 |
2 files changed, 37 insertions, 17 deletions
@@ -61,13 +61,12 @@ class Pride: def __init__(self,key): """Create a PRIDE cipher object - key: the key as a 128-bit raw string""" + Input: the key as a 128-bit raw string""" - if len(key) * 8 == 128: - self.key_whitening = string2number(key[:8]) - self.key_1 = key[8:] - else: + if len(key) != 16: raise ValueError, "Key must be a 128-bit raw string" + self.whitening_key = string2number(key[:8]) + self.roundkeys = [roundKey(key[8:], i) for i in xrange(0,21)] def encrypt(self,block): """Encrypt 1 block (8 bytes) @@ -79,17 +78,17 @@ class Pride: # Initial permutation & pre-whitening state = pLayer_dec(state) - state = addRoundKey(state, self.key_whitening) + state = addRoundKey(state, self.whitening_key) # 19 rounds R for i in xrange (1,20): - state = addRoundKey(state, roundKey(self.key_1, i)) + state = addRoundKey(state, self.roundkeys[i]) state = sBoxLayer(state) state = lLayer(state) # Last round R' - state = addRoundKey(state, roundKey(self.key_1, 20)) + state = addRoundKey(state, self.roundkeys[20]) state = sBoxLayer(state) # Post-whitening & final permutation - state = addRoundKey(state, self.key_whitening) + state = addRoundKey(state, self.whitening_key) state = pLayer(state) return number2string_N(state,8) @@ -104,17 +103,17 @@ class Pride: # Final permutation & post-whitening state = pLayer_dec(state) - state = addRoundKey(state, self.key_whitening) + state = addRoundKey(state, self.whitening_key) # Last round R' state = sBoxLayer_dec(state) - state = addRoundKey(state, roundKey(self.key_1, 20)) + state = addRoundKey(state, self.roundkeys[20]) # 19 rounds R for i in xrange(19,0,-1): state = lLayer_dec(state) state = sBoxLayer_dec(state) - state = addRoundKey(state, roundKey(self.key_1, i)) + state = addRoundKey(state, self.roundkeys[i]) # Pre-whitening & initial permutation - state = addRoundKey(state, self.key_whitening) + state = addRoundKey(state, self.whitening_key) state = pLayer(state) return number2string_N(state,8) diff --git a/test-vectors.py b/test-vectors.py index 7deb8d4..2cd75f5 100755 --- a/test-vectors.py +++ b/test-vectors.py @@ -21,6 +21,7 @@ # ============================================================================= from pypride import Pride +import time test_vectors = [ {'key': "00000000000000000000000000000000", 'plaintext': "0000000000000000", 'ciphertext': "82b4109fcc70bd1f"}, @@ -30,8 +31,28 @@ test_vectors = [ {'key': "0000000000000000fedcba9876543210", 'plaintext': "0123456789abcdef", 'ciphertext': "d1372929712d336e"} ] +time_cipher = 0 +time_encrypt = 0 +time_decrypt = 0 + 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 + key = vector['key'].decode('hex') + start = time.time() + cipher = Pride(key) + time_cipher += time.time() - start + + plaintext = vector['plaintext'].decode('hex') + start = time.time() + encryption = cipher.encrypt(plaintext) + time_encrypt += time.time() - start + + ciphertext = vector['ciphertext'].decode('hex') + start = time.time() + decryption = cipher.decrypt(ciphertext) + time_decrypt += time.time() - start + + print encryption == vector['ciphertext'].decode('hex'), decryption == vector['plaintext'].decode('hex') + +print "Generating round keys:", str(time_cipher) + "s" +print "Encryption:", str(time_encrypt) + "s" +print "Decryption:", str(time_decrypt) + "s"
\ No newline at end of file |