/* Implementation of the Lilliput-AE tweakable block cipher. Authors, hereby denoted as "the implementer": Kévin Le Gouguec, 2019. For more information, feedback or questions, refer to our website: https://paclido.fr/lilliput-ae To the extent possible under law, the implementer has waived all copyright and related or neighboring rights to the source code in this file. http://creativecommons.org/publicdomain/zero/1.0/ --- This file provides functions used by both authenticated encryption modes. */ #ifndef LILLIPUT_AE_UTILS_H #define LILLIPUT_AE_UTILS_H #include #include #include #include "cipher.h" #include "constants.h" static inline void encrypt(const uint8_t K[KEY_BYTES], const uint8_t T[TWEAK_BYTES], const uint8_t M[BLOCK_BYTES], uint8_t C[BLOCK_BYTES]) { lilliput_tbc_encrypt(K, T, M, C); } static inline void decrypt(const uint8_t K[KEY_BYTES], const uint8_t T[TWEAK_BYTES], const uint8_t C[BLOCK_BYTES], uint8_t M[BLOCK_BYTES]) { lilliput_tbc_decrypt(K, T, C, M); } static inline void xor_into(uint8_t dest[BLOCK_BYTES], const uint8_t src[BLOCK_BYTES]) { for (size_t i=0; i> 8*(s-1-i); } } static inline void fill_index_tweak( uint8_t prefix, size_t block_index, uint8_t tweak[TWEAK_BYTES] ) { /* The t-bit tweak is filled as follows: * * 1 4 5 t * [ prefix || block index ] * * The s-bit block index is encoded as follows: * * 5 t-s t-s+1 t * [ zero padding || block index, MSB first ] */ tweak[0] = prefix<<4; /* Assume padding bytes have already been set to 0. */ copy_block_index(block_index, tweak); } static void process_associated_data( const uint8_t key[KEY_BYTES], size_t A_len, const uint8_t A[A_len], uint8_t Auth[BLOCK_BYTES] ) { uint8_t Ek_Ai[BLOCK_BYTES]; uint8_t tweak[TWEAK_BYTES]; memset(tweak, 0, TWEAK_BYTES); memset(Auth, 0, BLOCK_BYTES); size_t l_a = A_len / BLOCK_BYTES; size_t rest = A_len % BLOCK_BYTES; for (size_t i=0; i