viff

changeset 1077:3be465a9f016

AES encryption added.
author Marcel Keller <mkeller@cs.au.dk>
date Mon, 12 Jan 2009 15:26:47 +0100
parents e77c20718de2
children 395273e73571
files viff/aes.py viff/test/test_aes.py
diffstat 2 files changed, 55 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- a/viff/aes.py	Mon Jan 12 15:23:53 2009 +0100
     1.2 +++ b/viff/aes.py	Mon Jan 12 15:26:47 2009 +0100
     1.3 @@ -199,3 +199,45 @@
     1.4              expanded_key.append(new_word)
     1.5  
     1.6          return expanded_key
     1.7 +
     1.8 +    def preprocess(self, input):
     1.9 +        if (isinstance(input, str)):
    1.10 +            return [Share(self.runtime, GF256, GF256(ord(c))) 
    1.11 +                    for c in input]
    1.12 +        else:
    1.13 +            for byte in input:
    1.14 +                assert byte.field == GF256, \
    1.15 +                    "Input must be a list of GF256 elements " \
    1.16 +                    "or of shares thereof."
    1.17 +            return input
    1.18 +
    1.19 +    def encrypt(self, cleartext, key):
    1.20 +        """Rijndael encryption.
    1.21 +
    1.22 +        Cleartext and key should be either a string or a list of bytes 
    1.23 +        (possibly shared as elements of GF256)."""
    1.24 +
    1.25 +        assert len(cleartext) == 4 * self.n_b, "Wrong length of cleartext."
    1.26 +        assert len(key) == 4 * self.n_k, "Wrong length of key."
    1.27 +
    1.28 +        cleartext = self.preprocess(cleartext)
    1.29 +        key = self.preprocess(key)
    1.30 +
    1.31 +        state = [cleartext[i::4] for i in xrange(4)]
    1.32 +        key = [key[4*i:4*i+4] for i in xrange(self.n_k)]
    1.33 +
    1.34 +        expanded_key = self.key_expansion(key)
    1.35 +
    1.36 +        self.add_round_key(state, expanded_key[0:self.n_b])
    1.37 +
    1.38 +        for i in xrange(1, self.rounds):
    1.39 +            self.byte_sub(state)
    1.40 +            self.shift_row(state)
    1.41 +            self.mix_column(state)
    1.42 +            self.add_round_key(state, expanded_key[i*self.n_b:(i+1)*self.n_b])
    1.43 +
    1.44 +        self.byte_sub(state)
    1.45 +        self.shift_row(state)
    1.46 +        self.add_round_key(state, expanded_key[self.rounds*self.n_b:])
    1.47 +
    1.48 +        return [byte for word in zip(*state) for byte in word]
     2.1 --- a/viff/test/test_aes.py	Mon Jan 12 15:23:53 2009 +0100
     2.2 +++ b/viff/test/test_aes.py	Mon Jan 12 15:26:47 2009 +0100
     2.3 @@ -116,3 +116,16 @@
     2.4                      word /= 256
     2.5  
     2.6          self.verify(runtime, result, expected_result)
     2.7 +
     2.8 +    @protocol
     2.9 +    def test_encrypt(self, runtime):
    2.10 +        cleartext = "Encrypt this!!!!"
    2.11 +        key = "Supposed to be secret!?!"
    2.12 +
    2.13 +        aes = AES(runtime, 192)
    2.14 +        r = rijndael(key)
    2.15 +
    2.16 +        result = aes.encrypt(cleartext, key)
    2.17 +        expected = [ord(c) for c in r.encrypt(cleartext)]
    2.18 +
    2.19 +        return self.verify(runtime, [result], [expected])