From e13590a378d947527da943c3f7876af5b1bd81b1 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Mon, 3 Dec 2018 10:47:49 +0100 Subject: Suppression des structures (tweakey|cipher)_state MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Pas l'impression que l'utilisation de structures dans les codes de référence soit très idiomatique. --- src/cipher.c | 91 +++++++++++++++++++++++++++--------------------------------- 1 file changed, 41 insertions(+), 50 deletions(-) (limited to 'src/cipher.c') diff --git a/src/cipher.c b/src/cipher.c index 7f1152a..4190359 100644 --- a/src/cipher.c +++ b/src/cipher.c @@ -26,18 +26,9 @@ const uint8_t PERMUTATIONS[2][BLOCK_BYTES] = { }; -struct cipher_state +static void _state_init(uint8_t X[BLOCK_BYTES], const uint8_t message[BLOCK_BYTES]) { - uint8_t X[BLOCK_BYTES]; -}; - - -typedef struct cipher_state cipher_state; - - -static void _state_init(cipher_state *X, const uint8_t message[BLOCK_BYTES]) -{ - memcpy(X->X, message, sizeof(X->X)); + memcpy(X, message, BLOCK_BYTES); } @@ -47,27 +38,27 @@ static void _compute_round_tweakeys( uint8_t RTK[ROUNDS][ROUND_TWEAKEY_BYTES] ) { - tweakey_state TK; - tweakey_state_init(&TK, key, tweak); - tweakey_state_extract(&TK, RTK[0], 0); + uint8_t TK[TWEAKEY_BYTES]; + tweakey_state_init(TK, key, tweak); + tweakey_state_extract(TK, 0, RTK[0]); for (uint8_t i=1; iX[j] ^ RTK[j]; + F[j] = X[j] ^ RTK[j]; } - for (size_t j=0; jX[dest_j] ^= F[j]; + X[dest_j] ^= F[j]; } } -static void _linear_layer(cipher_state *X) +static void _linear_layer(uint8_t X[BLOCK_BYTES]) { - X->X[15] ^= X->X[1]; - X->X[15] ^= X->X[2]; - X->X[15] ^= X->X[3]; - X->X[15] ^= X->X[4]; - X->X[15] ^= X->X[5]; - X->X[15] ^= X->X[6]; - X->X[15] ^= X->X[7]; - - X->X[14] ^= X->X[7]; - X->X[13] ^= X->X[7]; - X->X[12] ^= X->X[7]; - X->X[11] ^= X->X[7]; - X->X[10] ^= X->X[7]; - X->X[9] ^= X->X[7]; + X[15] ^= X[1]; + X[15] ^= X[2]; + X[15] ^= X[3]; + X[15] ^= X[4]; + X[15] ^= X[5]; + X[15] ^= X[6]; + X[15] ^= X[7]; + + X[14] ^= X[7]; + X[13] ^= X[7]; + X[12] ^= X[7]; + X[11] ^= X[7]; + X[10] ^= X[7]; + X[9] ^= X[7]; } -static void _permutation_layer(cipher_state *X, permutation p) +static void _permutation_layer(uint8_t X[BLOCK_BYTES], permutation p) { if (p == PERMUTATION_NONE) { @@ -105,17 +96,17 @@ static void _permutation_layer(cipher_state *X, permutation p) } uint8_t X_old[BLOCK_BYTES]; - memcpy(X_old, X, sizeof(X_old)); + memcpy(X_old, X, BLOCK_BYTES); const uint8_t *pi = PERMUTATIONS[p]; for (size_t j=0; jX[pi[j]] = X_old[j]; + X[pi[j]] = X_old[j]; } } -static void _one_round_egfn(cipher_state *X, const uint8_t RTK[ROUND_TWEAKEY_BYTES], permutation p) +static void _one_round_egfn(uint8_t X[BLOCK_BYTES], const uint8_t RTK[ROUND_TWEAKEY_BYTES], permutation p) { _nonlinear_layer(X, RTK); _linear_layer(X); @@ -130,20 +121,20 @@ void lilliput_tbc_encrypt( uint8_t ciphertext[BLOCK_BYTES] ) { - cipher_state X; - _state_init(&X, message); + uint8_t X[BLOCK_BYTES]; + _state_init(X, message); uint8_t RTK[ROUNDS][ROUND_TWEAKEY_BYTES]; _compute_round_tweakeys(key, tweak, RTK); for (uint8_t i=0; i