summaryrefslogtreecommitdiff
path: root/python/lilliput/__init__.py
diff options
context:
space:
mode:
Diffstat (limited to 'python/lilliput/__init__.py')
-rw-r--r--python/lilliput/__init__.py33
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)