aead.py 1.23 KB
Newer Older
lwc-tester committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34

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

    if (len(sys.argv) > 7) | (len(sys.argv) < 6):
        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
    if len(sys.argv) >= 7 :
        if len(sys.argv[6]) > 1:
            staticData = binascii.unhexlify(sys.argv[6])
        else:
            verbosityIdx = 6
    if len(sys.argv) == 8 :
        assert(verbosityIdx is None)
        verbosityIdx = 7
    if verbosityIdx is not None:
        impl.Verbose(int(verbosityIdx))
    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)