summaryrefslogtreecommitdiff
path: root/src/ref/lilliput-i.c
diff options
context:
space:
mode:
authorKévin Le Gouguec <kevin.legouguec@airbus.com>2019-06-12 17:34:41 +0200
committerKévin Le Gouguec <kevin.legouguec@airbus.com>2019-07-05 11:13:26 +0200
commit390b7a8ac7c14f73bf216561416f444109f24ec4 (patch)
tree77bdd81f6338625b71d0df1dd1534b6404b84fe6 /src/ref/lilliput-i.c
parent3d4608aac686498fa6a84f71dee05dadfd057dc9 (diff)
downloadlilliput-ae-implem-390b7a8ac7c14f73bf216561416f444109f24ec4.tar.xz
Changement de la concaténation des chaînes de bits
L'implémentation précédente n'était pas cohérente. Étant données deux chaînes X et Y de longueur x et y, et Z=X∥Y de longueur z=x+y, - pad10* et la construction des tweaks fonctionnaient selon la logique "indices faibles = LSB", donc Z[0] = Y[0] Z[z-1] = X[x-1] - le découpage de M, C et A en blocs fonctionnait selon la logique "indices faibles = premiers blocs", donc Z[0] = X[0] Z[z-1] = Y[y-1] En conséquence, la façon dont M, C et A étaient paddés n'avait aucun sens, e.g. pour un message M de taille 35, pad10*(M*) donnait : { M[34], M[33], M[32], 0b10000000, 0, … } Les deux seules façons logiques de padder M* sont { M[32], M[33], M[34], 0b10000000, 0, … } ou { M[2], M[1], M[0], 0b10000000, 0, … } Après revue d'autres implémentations de ΘCB3 et SCT-2, j'ai choisi de suivre la convention MSB. En conséquence, quand la spécification dit Z = X∥Y L'implémentation traduira : Z[] = { X[0], … X[x-1], Y[0], … Y[y-1] } Dans la même logique, les compteurs de blocs seront insérés MSB d'abord et paddés en conséquence, e.g. j=0x01020304 ≡ J[] = { 0, …, 0x01, 0x02, 0x03, 0x04 }
Diffstat (limited to 'src/ref/lilliput-i.c')
-rw-r--r--src/ref/lilliput-i.c67
1 files changed, 38 insertions, 29 deletions
diff --git a/src/ref/lilliput-i.c b/src/ref/lilliput-i.c
index 6f869c3..fb06237 100644
--- a/src/ref/lilliput-i.c
+++ b/src/ref/lilliput-i.c
@@ -32,58 +32,67 @@ static const uint8_t _0n[BLOCK_BYTES] = {
};
+static uint8_t _upper_nibble(uint8_t i)
+{
+ return i >> 4;
+}
+
+static uint8_t _lower_nibble(uint8_t i)
+{
+ return i & 0x0f;
+}
+
static void _init_msg_tweak(const uint8_t N[NONCE_BYTES], uint8_t tweak[TWEAK_BYTES])
{
/* With an s-bit block index, the t-bit tweak is filled as follows:
*
- * - bits [ 1, t-|N|-4]: block index
- * [ 1, s]: actual block index
- * [ s+1, t-|N|-4]: 0-padding
- * - bits [t-|N|-3, t-4]: nonce
- * - bits [ t-3, t]: 4-bit prefix
+ * [ 1, 4]: 4-bit prefix
+ * [ 5, |N|+4]: nonce
+ * [ |N|+5, t]: block index
+ * [|N|+5, t-s]: 0-padding
+ * [t-s+1, t]: actual block index, from MSB to LSB
*
- * This function sets bits s+1 to t-4 once and for all.
+ * This function sets bits 5 to t-s once and for all.
*/
- size_t N_start = TWEAK_BYTES - NONCE_BYTES - 1;
-
- for (size_t i=sizeof(size_t); i<N_start; i++)
- {
- tweak[i] = 0;
- }
-
- tweak[N_start] = lower_nibble(N[0]) << 4;
+ tweak[0] = _upper_nibble(N[0]);
for (size_t i=1; i<NONCE_BYTES; i++)
{
- tweak[N_start+i] = lower_nibble(N[i]) << 4 ^ upper_nibble(N[i-1]);
+ tweak[i] = _lower_nibble(N[i-1]) << 4 ^ _upper_nibble(N[i]);
}
- tweak[TWEAK_BYTES-1] = upper_nibble(N[NONCE_BYTES-1]);
+ tweak[NONCE_BYTES] = _lower_nibble(N[NONCE_BYTES-1]) << 4;
+
+ /* The number of bits we need to zero out is:
+ * t - |N| - s - 4 - 4
+ * (prefix) (zeroed out by previous assignment)
+ */
+ memset(&tweak[NONCE_BYTES+1], 0, TWEAK_BYTES-NONCE_BYTES-sizeof(size_t)-1);
}
static void _fill_msg_tweak(
- uint8_t prefix,
- size_t block_index,
- uint8_t tweak[TWEAK_BYTES]
+ uint8_t prefix,
+ size_t block_index,
+ uint8_t tweak[TWEAK_BYTES]
)
{
/* With an s-bit block index, the t-bit tweak is filled as follows:
*
- * - bits [ 1, t-|N|-4]: block index
- * [ 1, s]: actual block index
- * [ s+1, t-|N|-4]: 0-padding
- * - bits [t-|N|-3, t-4]: nonce
- * - bits [ t-3, t]: 4-bit prefix
+ * [ 1, 4]: 4-bit prefix
+ * [ 5, |N|+4]: nonce
+ * [ |N|+5, t]: block index
+ * [|N|+5, t-s]: 0-padding
+ * [t-s+1, t]: actual block index, from MSB to LSB
*
- * This function assumes bits s+1 to t-3 have already been set,
- * and only sets bits 1 to s and t-3 to t.
+ * This function assumes bits 5 to t-s have already been set, and
+ * only sets bits 1 to 4 and t-s+1 to t.
*/
- copy_block_index(block_index, tweak);
+ uint8_t *msb = &tweak[0];
+ *msb = prefix<<4 ^ _lower_nibble(*msb);
- uint8_t *msb = &tweak[TWEAK_BYTES-1];
- *msb = prefix<<4 ^ lower_nibble(*msb);
+ copy_block_index(block_index, tweak);
}
static void _encrypt_message(