diff options
| author | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-07-05 14:28:17 +0200 |
|---|---|---|
| committer | Kévin Le Gouguec <kevin.legouguec@airbus.com> | 2019-07-05 14:28:17 +0200 |
| commit | 4f58d99e11e1c412a600f39f32a8d181765f0246 (patch) | |
| tree | 7bd7b860ab8e60105e37c873d134b2d59842b21e /src/ref/lilliput-ae-utils.h | |
| parent | 904b99b495a419fefc666e489c31893f86d5080e (diff) | |
| parent | da895e90ec0db658ce9ebbfe60591c7e88f9d6a3 (diff) | |
| download | lilliput-ae-implem-4f58d99e11e1c412a600f39f32a8d181765f0246.tar.xz | |
Merge branch 'fix-concatenation'
Diffstat (limited to 'src/ref/lilliput-ae-utils.h')
| -rw-r--r-- | src/ref/lilliput-ae-utils.h | 67 |
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( |
