summaryrefslogtreecommitdiff
path: root/python/lilliput/__init__.py
blob: 5fbc0deaa2e1cee774ec5fbe8bf41eee471be28f (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
from . import lilliput_ae_1
from . import lilliput_ae_2
from .constants import NONCE_BYTES


_AE_MODES = {
    1: lilliput_ae_1,
    2: lilliput_ae_2
}


def _check_inputs(key, mode, 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 mode not in _AE_MODES:
        raise ValueError('invalid mode: {} not in {}'.format(mode, tuple(_AE_MODES)))

    if len(nonce) != NONCE_BYTES:
        raise ValueError('invalid nonce size: expecting {}, have {}'.format(NONCE_BYTES, len(nonce)))


def encrypt(plaintext, adata, key, nonce, mode):
    _check_inputs(key, mode, nonce)
    return _AE_MODES[mode].encrypt(adata, plaintext, nonce, key)


def decrypt(ciphertext, tag, adata, key, nonce, mode):
    _check_inputs(key, mode, nonce)
    return _AE_MODES[mode].decrypt(adata, ciphertext, nonce, tag, key)