diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-22 10:38:19 +0100 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-03-22 10:38:19 +0100 |
| commit | bac28f498c5fee10720c8ed71988434e05d9197f (patch) | |
| tree | 017de97abfb609163669a89c754bb813c50ec891 /python/lilliput/__init__.py | |
| parent | dd934c63386c8fa22a5b0944e0256c435d55938c (diff) | |
| download | lilliput-ae-implem-bac28f498c5fee10720c8ed71988434e05d9197f.tar.xz | |
[implem-python] Création d'un paquet "lilliput"
Diffstat (limited to 'python/lilliput/__init__.py')
| -rw-r--r-- | python/lilliput/__init__.py | 33 |
1 files changed, 33 insertions, 0 deletions
diff --git a/python/lilliput/__init__.py b/python/lilliput/__init__.py new file mode 100644 index 0000000..43179f8 --- /dev/null +++ b/python/lilliput/__init__.py @@ -0,0 +1,33 @@ +from enum import Enum + +from . import lilliput_ae_1 +from . import lilliput_ae_2 +from .constants import NONCE_BYTES + + +class LilliputAeMode(Enum): + lilliput_1 = lilliput_ae_1 + lilliput_2 = lilliput_ae_2 + + +def _checkInputs(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.name not in LilliputAeMode.__members__: + raise ValueError('invalid mode: use a member of the LilliputAeMode enumeration') + + if len(nonce) != NONCE_BYTES: + raise ValueError('nonce must be {}-byte long'.format(NONCE_BYTES)) + + +def encrypt(plaintext, adata, key, nonce, mode): + _checkInputs(key, mode, nonce) + return mode.value.encrypt(adata, plaintext, nonce, key) + + +def decrypt(ciphertext, tag, adata, key, nonce, mode): + _checkInputs(key, mode, nonce) + return mode.value.decrypt(adata, ciphertext, nonce, tag, key) |
