summaryrefslogtreecommitdiff
path: root/src/ref/lilliput-ae-utils.h
diff options
context:
space:
mode:
authorGaetan Leplus <gaetan.leplus@airbus.com>2019-07-05 16:16:19 +0200
committerGaetan Leplus <gaetan.leplus@airbus.com>2019-07-05 16:16:19 +0200
commit62433c71e25f157dd79ba10d81631ee4c67f8eb1 (patch)
tree3ac352a3598fa444d45695dbb2b4cee63698ac57 /src/ref/lilliput-ae-utils.h
parentde5f3445b5d382237afc39869907957e65c8a91e (diff)
parent75d7f59658539c699cdf9c7a3abdbead15aac199 (diff)
downloadlilliput-ae-implem-62433c71e25f157dd79ba10d81631ee4c67f8eb1.tar.xz
Merge remote-tracking branch 'origin/master' into fix-vhdltbc
Diffstat (limited to 'src/ref/lilliput-ae-utils.h')
-rw-r--r--src/ref/lilliput-ae-utils.h67
1 files changed, 33 insertions, 34 deletions
diff --git a/src/ref/lilliput-ae-utils.h b/src/ref/lilliput-ae-utils.h
index 0efb776..19b4623 100644
--- a/src/ref/lilliput-ae-utils.h
+++ b/src/ref/lilliput-ae-utils.h
@@ -28,16 +28,6 @@ This file provides functions used by both authenticated encryption modes.
#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],
@@ -68,35 +58,41 @@ static inline void xor_arrays(size_t len, uint8_t out[len], const uint8_t a[len]
static inline void pad10(size_t X_len, const uint8_t X[X_len], uint8_t padded[BLOCK_BYTES])
{
- /* pad10*(X) = X || 1 || 0^{n-|X|-1} */
-
- /* For example, with uint8_t X[3] = { [0]=0x01, [1]=0x02, [2]=0x03 }
+ /* Assuming 0 < |X| < n:
+ *
+ * pad10*(X) = X || 1 || 0^{n-|X|-1}
+ *
+ * For example, with uint8_t X[3] = { [0]=0x01, [1]=0x02, [2]=0x03 }
*
* pad10*(X) =
- * X[2] X[1] X[0] 1 0*
- * 00000011 00000010 00000001 1 0000000 00000000...
+ * X[0] X[1] X[2] 1 0*
+ * 00000001 00000010 00000011 1 0000000 00000000...
*
- * - padded[0, 11]: zeroes
- * - padded[12]: 10000000
- * - padded[13, 15]: X[0, 2]
+ * - padded[0, 2]: X[0, 2]
+ * - padded[3]: 10000000
+ * - padded[4, 15]: zeroes
*/
- /* Assume that X_len<BLOCK_BYTES. */
+ memcpy(padded, X, X_len);
+ padded[X_len] = 0x80;
- size_t pad_len = BLOCK_BYTES-X_len;
+ /* memset(&padded[BLOCK_BYTES], 0, 0) may or may not constitute
+ * undefined behaviour; use a straight loop instead. */
- memset(padded, 0, pad_len-1);
- padded[pad_len-1] = 0x80;
- memcpy(padded+pad_len, X, X_len);
+ for (size_t i=X_len+1; i<BLOCK_BYTES; i++)
+ {
+ padded[i] = 0;
+ }
}
static inline void copy_block_index(size_t index, uint8_t tweak[TWEAK_BYTES])
{
- /* NB: little-endian architectures can simply use:
- * memcpy(tweak, &index, sizeof(index)); */
- for (size_t i=0; i<sizeof(index); i++)
+ size_t s = sizeof(index);
+ uint8_t *dest = &tweak[TWEAK_BYTES-s];
+
+ for (size_t i=0; i<s; i++)
{
- tweak[i] = index >> 8*i & 0xff;
+ dest[i] = index >> 8*(s-1-i);
}
}
@@ -106,19 +102,22 @@ static inline void fill_index_tweak(
uint8_t tweak[TWEAK_BYTES]
)
{
- /* With an s-bit block index, the t-bit tweak is filled as follows:
+ /* The t-bit tweak is filled as follows:
*
- * - bits [ 1, t-4]: block index
- * [ 1, s]: actual block index
- * [s+1, t-4]: 0-padding
- * - bits [t-3, t]: 4-bit prefix
+ * 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 ]
*/
- copy_block_index(block_index, tweak);
+ tweak[0] = prefix<<4;
/* Assume padding bytes have already been set to 0. */
- tweak[TWEAK_BYTES-1] |= prefix << 4;
+ copy_block_index(block_index, tweak);
}
static void process_associated_data(