viff

changeset 1089:992c7eee2b3f

Added application demonstrating AES.
author Marcel Keller <mkeller@cs.au.dk>
date Mon, 26 Jan 2009 10:02:48 +0100
parents 3da9bd2661a3
children a45e34e4ebf0
files apps/aes.py
diffstat 1 files changed, 87 insertions(+), 0 deletions(-) [+]
line diff
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/apps/aes.py	Mon Jan 26 10:02:48 2009 +0100
     1.3 @@ -0,0 +1,87 @@
     1.4 +#!/usr/bin/python
     1.5 +
     1.6 +# Copyright 2009 VIFF Development Team.
     1.7 +#
     1.8 +# This file is part of VIFF, the Virtual Ideal Functionality Framework.
     1.9 +#
    1.10 +# VIFF is free software: you can redistribute it and/or modify it
    1.11 +# under the terms of the GNU Lesser General Public License (LGPL) as
    1.12 +# published by the Free Software Foundation, either version 3 of the
    1.13 +# License, or (at your option) any later version.
    1.14 +#
    1.15 +# VIFF is distributed in the hope that it will be useful, but WITHOUT
    1.16 +# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
    1.17 +# or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
    1.18 +# Public License for more details.
    1.19 +#
    1.20 +# You should have received a copy of the GNU Lesser General Public
    1.21 +# License along with VIFF. If not, see <http://www.gnu.org/licenses/>.
    1.22 +
    1.23 +# This example shows how to use multi-party AES encryption.
    1.24 +
    1.25 +
    1.26 +import sys
    1.27 +import time
    1.28 +from optparse import OptionParser
    1.29 +
    1.30 +from twisted.internet import reactor
    1.31 +
    1.32 +from viff.runtime import BasicRuntime, create_runtime, gather_shares
    1.33 +from viff.config import load_config
    1.34 +
    1.35 +from viff.aes import bit_decompose,AES
    1.36 +
    1.37 +
    1.38 +parser = OptionParser(usage="Usage: %prog [options] config_file")
    1.39 +parser.add_option("-e", "--exponentiation", action="store_true",
    1.40 +                  help="Use exponentiation to invert bytes (default).")
    1.41 +parser.add_option("-m", "--masking", action="store_false", 
    1.42 +                  dest="exponentiation", 
    1.43 +                  help="Use masking to invert bytes.")
    1.44 +parser.set_defaults(exponentiation=True)
    1.45 +
    1.46 +# Add standard VIFF options.
    1.47 +BasicRuntime.add_options(parser)
    1.48 +
    1.49 +(options, args) = parser.parse_args()
    1.50 +
    1.51 +if len(args) == 0:
    1.52 +    parser.error("You must specify a config file.")
    1.53 +
    1.54 +id, players = load_config(args[0])
    1.55 +
    1.56 +def encrypt(_, rt):
    1.57 +    start = time.time()
    1.58 +    print "Started at %f." % start
    1.59 +
    1.60 +    aes = AES(rt, 192, use_exponentiation=options.exponentiation)
    1.61 +    ciphertext = aes.encrypt("a" * 16, "b" * 24, True)
    1.62 +
    1.63 +    opened = []
    1.64 +    result = [0] * 16
    1.65 +
    1.66 +    for i, c in enumerate(ciphertext):
    1.67 +        o = rt.open(c)
    1.68 +        
    1.69 +        def res(x, i):
    1.70 +            result[i] = hex(x.value)
    1.71 +
    1.72 +        o.addCallback(res, i)
    1.73 +        opened.append(o)
    1.74 +
    1.75 +    def fin(g, result):
    1.76 +        print "Finished after %f sec." % (time.time() - start)
    1.77 +        print result
    1.78 +        rt.shutdown()
    1.79 +
    1.80 +    g = gather_shares(opened)
    1.81 +    g.addCallback(fin, result)
    1.82 +
    1.83 +def sync(rt):
    1.84 +    s = rt.synchronize()
    1.85 +    s.addCallback(encrypt, rt)
    1.86 +
    1.87 +rt = create_runtime(id, players, 1, options)
    1.88 +rt.addCallback(sync)
    1.89 +
    1.90 +reactor.run()