aead.py 1.27 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6

def aead(impl):
    from drysponge.base import DrySponge
    import sys
    import binascii

lwc-tester committed
7
    if (len(sys.argv) > 8) | (len(sys.argv) < 6):
lwc-tester committed
8 9 10 11 12 13 14 15 16
        print("ERROR: need at least 5 arguments: operation,key,nonce,message,associatedData,[staticData],[verbosity]")
        exit()
    op = sys.argv[1]
    key = binascii.unhexlify(sys.argv[2])
    nonce = binascii.unhexlify(sys.argv[3])
    message = binascii.unhexlify(sys.argv[4])
    associatedData = binascii.unhexlify(sys.argv[5])
    staticData = None
    verbosityIdx = None
lwc-tester committed
17
    if len(sys.argv) > 6 :
lwc-tester committed
18 19 20
        if len(sys.argv[6]) > 1:
            staticData = binascii.unhexlify(sys.argv[6])
        else:
lwc-tester committed
21
            assert(len(sys.argv) == 7)
lwc-tester committed
22 23 24 25 26
            verbosityIdx = 6
    if len(sys.argv) == 8 :
        assert(verbosityIdx is None)
        verbosityIdx = 7
    if verbosityIdx is not None:
lwc-tester committed
27
        impl.Verbose(int(sys.argv[verbosityIdx]))
lwc-tester committed
28 29 30 31 32 33 34 35
    if op in ['e','enc','encrypt']:
        out = impl.encrypt(key,nonce,message,associatedData,staticData)
    elif op in ['d','dec','decreypt']:
        out = impl.decrypt(key,nonce,message,associatedData,staticData)
    else:
        out = impl.encrypt(key,nonce,message,associatedData,staticData)
        impl.decrypt(key,nonce,out,associatedData,staticData)
    DrySponge.print_bytes_as_hex(out)