/* 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 uint8_t upper_nibble(uint8_t i) { return i >> 4; } static inline uint8_t lower_nibble(uint8_t i) { return i & 0x0f; } 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*i & 0xff; } /* Assume padding bytes have already been set to 0. */ tweak[TWEAK_BYTES-1] |= prefix << 4; } 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