diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/cipher.c | 91 | ||||
| -rw-r--r-- | src/tweakey.c | 28 | ||||
| -rw-r--r-- | src/tweakey.h | 18 |
3 files changed, 60 insertions, 77 deletions
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; i<ROUNDS; i++) { - tweakey_state_update(&TK); - tweakey_state_extract(&TK, RTK[i], i); + tweakey_state_update(TK); + tweakey_state_extract(TK, i, RTK[i]); } } -static void _nonlinear_layer(cipher_state *X, const uint8_t RTK[ROUND_TWEAKEY_BYTES]) +static void _nonlinear_layer(uint8_t X[BLOCK_BYTES], const uint8_t RTK[ROUND_TWEAKEY_BYTES]) { uint8_t F[ROUND_TWEAKEY_BYTES]; - for (size_t j=0; j<sizeof(F); j++) + for (size_t j=0; j<ROUND_TWEAKEY_BYTES; j++) { - F[j] = X->X[j] ^ RTK[j]; + F[j] = X[j] ^ RTK[j]; } - for (size_t j=0; j<sizeof(F); j++) + for (size_t j=0; j<ROUND_TWEAKEY_BYTES; j++) { F[j] = S[F[j]]; } @@ -75,29 +66,29 @@ static void _nonlinear_layer(cipher_state *X, const uint8_t RTK[ROUND_TWEAKEY_BY for (size_t j=0; j<8; j++) { size_t dest_j = 15-j; - X->X[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; j<BLOCK_BYTES; j++) { - X->X[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<ROUNDS-1; i++) { - _one_round_egfn(&X, RTK[i], PERMUTATION_ENCRYPTION); + _one_round_egfn(X, RTK[i], PERMUTATION_ENCRYPTION); } - _one_round_egfn(&X, RTK[ROUNDS-1], PERMUTATION_NONE); + _one_round_egfn(X, RTK[ROUNDS-1], PERMUTATION_NONE); - memcpy(ciphertext, X.X, BLOCK_BYTES); + memcpy(ciphertext, X, BLOCK_BYTES); } void lilliput_tbc_decrypt( @@ -153,18 +144,18 @@ void lilliput_tbc_decrypt( uint8_t message[BLOCK_BYTES] ) { - cipher_state X; - _state_init(&X, ciphertext); + uint8_t X[BLOCK_BYTES]; + _state_init(X, ciphertext); uint8_t RTK[ROUNDS][ROUND_TWEAKEY_BYTES]; _compute_round_tweakeys(key, tweak, RTK); for (uint8_t i=0; i<ROUNDS-1; i++) { - _one_round_egfn(&X, RTK[ROUNDS-1-i], PERMUTATION_DECRYPTION); + _one_round_egfn(X, RTK[ROUNDS-1-i], PERMUTATION_DECRYPTION); } - _one_round_egfn(&X, RTK[0], PERMUTATION_NONE); + _one_round_egfn(X, RTK[0], PERMUTATION_NONE); - memcpy(message, X.X, BLOCK_BYTES); + memcpy(message, X, BLOCK_BYTES); } diff --git a/src/tweakey.c b/src/tweakey.c index 7c66ee9..761ec53 100644 --- a/src/tweakey.c +++ b/src/tweakey.c @@ -12,25 +12,25 @@ void tweakey_state_init( - tweakey_state *TK, + uint8_t TK[TWEAKEY_BYTES], const uint8_t key[KEY_BYTES], const uint8_t tweak[TWEAK_BYTES] ) { - memcpy(TK->TK, tweak, TWEAK_BYTES); - memcpy(TK->TK+TWEAK_BYTES, key, KEY_BYTES); + memcpy(TK, tweak, TWEAK_BYTES); + memcpy(TK+TWEAK_BYTES, key, KEY_BYTES); } void tweakey_state_extract( - const tweakey_state *TK, - uint8_t round_tweakey[ROUND_TWEAKEY_BYTES], /* output */ - uint8_t i /* round constant */ + const uint8_t TK[TWEAKEY_BYTES], + uint8_t round_constant, + uint8_t round_tweakey[ROUND_TWEAKEY_BYTES] ) { memset(round_tweakey, 0, ROUND_TWEAKEY_BYTES); - for (const uint8_t *lane=TK->TK; lane<TK->TK+TWEAKEY_BYTES; lane+=LANE_BYTES) + for (const uint8_t *lane=TK; lane<TK+TWEAKEY_BYTES; lane+=LANE_BYTES) { for (size_t j=0; j<LANE_BYTES; j++) { @@ -38,25 +38,25 @@ void tweakey_state_extract( } } - round_tweakey[0] ^= i; + round_tweakey[0] ^= round_constant; } -static void _permute_state(tweakey_state *TK) +static void _permute_state(uint8_t TK[TWEAKEY_BYTES]) { uint8_t TK_old[TWEAKEY_BYTES]; - memcpy(TK_old, TK->TK, sizeof(TK_old)); + memcpy(TK_old, TK, TWEAKEY_BYTES); for (size_t j=0; j<TWEAKEY_BYTES; j+=LANE_BYTES) { for (size_t k=0; k<LANE_BYTES; k++) { - TK->TK[j+h[k]] = TK_old[j+k]; + TK[j+h[k]] = TK_old[j+k]; } } } -static void _multiply_state(tweakey_state *TK) +static void _multiply_state(uint8_t TK[TWEAKEY_BYTES]) { /* Lane 0 is multiplied by Id; lane 1 by P_0, lane 2 by P_1... */ @@ -67,12 +67,12 @@ static void _multiply_state(tweakey_state *TK) for (size_t k=0; k<LANE_BYTES; k++) { size_t offset = j*LANE_BYTES + k; - TK->TK[offset] = P_lane[TK->TK[offset]]; + TK[offset] = P_lane[TK[offset]]; } } } -void tweakey_state_update(tweakey_state *TK) +void tweakey_state_update(uint8_t TK[TWEAKEY_BYTES]) { _permute_state(TK); _multiply_state(TK); diff --git a/src/tweakey.h b/src/tweakey.h index 0642724..5470bc8 100644 --- a/src/tweakey.h +++ b/src/tweakey.h @@ -6,26 +6,18 @@ #include "parameters.h" -struct tweakey_state -{ - uint8_t TK[TWEAKEY_BYTES]; -}; - -typedef struct tweakey_state tweakey_state; - - void tweakey_state_init( - tweakey_state *TK, + uint8_t TK[TWEAKEY_BYTES], const uint8_t key[KEY_BYTES], const uint8_t tweak[TWEAK_BYTES] ); void tweakey_state_extract( - const tweakey_state *TK, - uint8_t round_tweakey[ROUND_TWEAKEY_BYTES], /* output */ - uint8_t i /* round constant */ + const uint8_t TK[TWEAKEY_BYTES], + uint8_t round_constant, + uint8_t round_tweakey[ROUND_TWEAKEY_BYTES] /* output */ ); -void tweakey_state_update(tweakey_state *TK); +void tweakey_state_update(uint8_t TK[TWEAKEY_BYTES]); #endif /* TWEAKEY_H */ |
