lilliput-ae-reference-implementation

Implementations of Lilliput-AE submitted to the NIST LWC standardization process
git clone https://git.kevinlegouguec.net/lilliput-ae-reference-implementation
Log | Files | Refs | README

__init__.py (1739B)


      1 # Implementation of the Lilliput-AE tweakable block cipher.
      2 #
      3 # Authors, hereby denoted as "the implementer":
      4 #     Kévin Le Gouguec,
      5 #     Léo Reynaud
      6 #     2019.
      7 #
      8 # For more information, feedback or questions, refer to our website:
      9 # https://paclido.fr/lilliput-ae
     10 #
     11 # To the extent possible under law, the implementer has waived all copyright
     12 # and related or neighboring rights to the source code in this file.
     13 # http://creativecommons.org/publicdomain/zero/1.0/
     14 
     15 """Lilliput-AE tweakable block cipher.
     16 
     17 This module provides the high-level functions for authenticated encryption and
     18 decryption.  Both functions take and return bytestring values.
     19 
     20 The "mode" argument can be either of the following integers:
     21 
     22 - 1, for the ΘCB3 nonce-respecting mode,
     23 - 2, for the SCT-2 nonce-misuse-resistant mode.
     24 """
     25 
     26 
     27 from . import ae_mode_1
     28 from . import ae_mode_2
     29 from .constants import NONCE_BITS
     30 
     31 
     32 _AE_MODES = {
     33     1: ae_mode_1,
     34     2: ae_mode_2
     35 }
     36 
     37 
     38 def _check_inputs(key, mode, nonce):
     39     valid_key_lengths = (128, 192, 256)
     40     if len(key)*8 not in valid_key_lengths:
     41         raise ValueError('invalid key size: {} not in {}'.format(len(key)*8, valid_key_lengths))
     42 
     43     if mode not in _AE_MODES:
     44         raise ValueError('invalid mode: {} not in {}'.format(mode, tuple(_AE_MODES)))
     45 
     46     if len(nonce)*8 != NONCE_BITS:
     47         raise ValueError('invalid nonce size: expecting {}, have {}'.format(NONCE_BITS, len(nonce)*8))
     48 
     49 
     50 def encrypt(plaintext, adata, key, nonce, mode):
     51     _check_inputs(key, mode, nonce)
     52     return _AE_MODES[mode].encrypt(adata, plaintext, nonce, key)
     53 
     54 
     55 def decrypt(ciphertext, tag, adata, key, nonce, mode):
     56     _check_inputs(key, mode, nonce)
     57     return _AE_MODES[mode].decrypt(adata, ciphertext, nonce, tag, key)