summaryrefslogtreecommitdiff
path: root/python/lilliput.py
blob: 21feb6032959a323551045be64788f8dd84befc1 (plain)
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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
import lilliput_ae_1
import lilliput_ae_2

N_BYTES = 15

def _getParameters(mode=1, key_length=128) :
    rounds = {
        128: 32,
        192: 36,
        256: 42
    }

    tweak_lengths = {
        1: 192,
        2: 128
    }

    return tweak_lengths[mode], rounds[key_length]


############################################

def _checkInputs(key, nonce):
    valid_key_lengths = (128, 192, 256)

    if len(key)*8 not in valid_key_lengths:
        raise ValueError('invalid key size: {} not in {}'.format(len(key)*8, valid_key_lengths))

    if len(nonce) != N_BYTES:
        raise ValueError('nonce must be {}-byte long'.format(N_BYTES))


def mainEnc(plaintext, adata, key, nonce, mode):
    _checkInputs(key, nonce)

    tweak_bits, rounds = _getParameters(mode, len(key)*8)

    A = adata
    M = plaintext
    N = nonce

    if(mode == 1) :
        return lilliput_ae_1.OCB3Enc(A, M, N, key, tweak_bits, rounds)
    if(mode == 2) :
        return lilliput_ae_2.SCT2Enc(A, M, N, key, tweak_bits, rounds)


def mainDec(ciphertext, tag, adata, key, nonce, mode):
    _checkInputs(key, nonce)

    tweak_bits, rounds = _getParameters(mode, len(key)*8)

    A = adata
    C = ciphertext
    N = nonce
    tag = list(tag)

    if(mode == 1) :
        return lilliput_ae_1.OCB3Dec(A, C, N, tag, key, tweak_bits, rounds)
    if(mode == 2) :
        return lilliput_ae_2.SCT2Dec(A, C, N, tag, key, tweak_bits, rounds)