viff

changeset 1200:5da110d6e5b2

Expand the key successively instead of at once.
author Marcel Keller <mkeller@cs.au.dk>
date Thu, 16 Jul 2009 12:25:22 +0200
parents f640ea5f0920
children 0fb5d4da2f1e
files viff/aes.py
diffstat 1 files changed, 16 insertions(+), 8 deletions(-) [+]
line diff
     1.1 --- a/viff/aes.py	Fri Jul 10 13:27:42 2009 +0200
     1.2 +++ b/viff/aes.py	Thu Jul 16 12:25:22 2009 +0200
     1.3 @@ -274,17 +274,22 @@
     1.4  
     1.5          state[:] = (Matrix(state) + Matrix(zip(*round_key))).rows
     1.6  
     1.7 -    def key_expansion(self, key):
     1.8 +    def key_expansion(self, key, new_length=None):
     1.9          """Rijndael key expansion.
    1.10  
    1.11 -        Input and output are lists of 4-byte columns (words)."""
    1.12 +        Input and output are lists of 4-byte columns (words).
    1.13 +        *new_length* is the round for which the key should be expanded.
    1.14 +        If ommitted, the key is expanded for all rounds."""
    1.15  
    1.16 -        assert len(key) == self.n_k, "Wrong key size."
    1.17 +        assert len(key) >= self.n_k, "Wrong key size."
    1.18          assert len(key[0]) == 4, "Key must consist of 4-byte words."
    1.19  
    1.20 -        expanded_key = list(key)
    1.21 +        expanded_key = key
    1.22  
    1.23 -        for i in xrange(self.n_k, self.n_b * (self.rounds + 1)):
    1.24 +        if new_length == None:
    1.25 +            new_length = self.rounds
    1.26 +
    1.27 +        for i in xrange(len(key), self.n_b * (new_length + 1)):
    1.28              temp = list(expanded_key[i - 1])
    1.29  
    1.30              if (i % self.n_k == 0):
    1.31 @@ -355,8 +360,7 @@
    1.32              progress = lambda x, i, start_round: x
    1.33              prep_progress = lambda i, start_round: None
    1.34  
    1.35 -        expanded_key = self.key_expansion(key)
    1.36 -
    1.37 +        expanded_key = self.key_expansion(key[:], 0)
    1.38          self.add_round_key(state, expanded_key[0:self.n_b])
    1.39  
    1.40          prep_progress(0, start)
    1.41 @@ -366,7 +370,9 @@
    1.42  
    1.43          def round(_, state, i):
    1.44              start_round = time.time()
    1.45 -            
    1.46 +
    1.47 +            self.key_expansion(expanded_key, i)
    1.48 +
    1.49              self.byte_sub(state)
    1.50              self.shift_row(state)
    1.51              self.mix_column(state)
    1.52 @@ -388,6 +394,8 @@
    1.53          def final_round(_, state):
    1.54              start_round = time.time()
    1.55  
    1.56 +            self.key_expansion(expanded_key, self.rounds)
    1.57 +
    1.58              self.byte_sub(state)
    1.59              self.shift_row(state)
    1.60              self.add_round_key(state, expanded_key[self.rounds*self.n_b:])