From 9bc87082cec1dc078a6c9c1d62098aced5ea8331 Mon Sep 17 00:00:00 2001 From: Kévin Le Gouguec Date: Tue, 21 May 2019 13:53:07 +0200 Subject: Réutilisation des multiplications dans l'implémentation FELICS MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Plutôt que d'ajouter -Wno-unused-functin, on pourrait aussi déclarer les multiplications "static inline", ce qui par convention esquive les avertissements sur la non-utilisation. --- src/add_felicsref/implem.mk | 41 ++++++++++++- src/add_felicsref/multiplications.h | 1 + src/add_felicsref/tweakey.c | 111 +++--------------------------------- src/add_threshold/implem.mk | 3 +- 4 files changed, 50 insertions(+), 106 deletions(-) mode change 120000 => 100644 src/add_felicsref/implem.mk create mode 120000 src/add_felicsref/multiplications.h diff --git a/src/add_felicsref/implem.mk b/src/add_felicsref/implem.mk deleted file mode 120000 index eb789fb..0000000 --- a/src/add_felicsref/implem.mk +++ /dev/null @@ -1 +0,0 @@ -../ref/implem.mk \ No newline at end of file diff --git a/src/add_felicsref/implem.mk b/src/add_felicsref/implem.mk new file mode 100644 index 0000000..86b4048 --- /dev/null +++ b/src/add_felicsref/implem.mk @@ -0,0 +1,40 @@ +# This file sets some implementation-specific variables and defines +# build dependencies. + +tests = $(basename $(wildcard test-*.c)) +traces = $(basename $(wildcard traces-*.c)) + + +# Don't trigger warnings for unused tweakey multiplications. +$(results_dir)/src/tweakey.o: CFLAGS += -Wno-unused-function + + +# Program => additional objects dependencies + +$(results_dir)/test-tbc-decrypt $(results_dir)/test-tbc-encrypt $(results_dir)/traces-tbc: \ +$(results_dir)/src/cipher.o $(results_dir)/src/tweakey.o + +$(results_dir)/test-ae-decrypt $(results_dir)/test-ae-encrypt $(results_dir)/test-ae-roundtrip $(results_dir)/traces-ae: \ +$(results_dir)/src/lilliput-$(mode).o $(results_dir)/src/cipher.o \ +$(results_dir)/src/tweakey.o + +$(results_dir)/test-tweakey: $(results_dir)/src/tweakey.o + +# Object => headers dependencies + +$(results_dir)/src/cipher.o: $(src_dir)/cipher.h \ +$(src_dir)/tweakey.h $(variant_dir)/parameters.h + +$(results_dir)/src/lilliput-i.o $(results_dir)/src/lilliput-ii.o: \ +$(src_dir)/lilliput-ae.h $(src_dir)/cipher.h $(src_dir)/constants.h \ +$(variant_dir)/parameters.h + +$(results_dir)/src/tweakey.o: $(src_dir)/tweakey.h \ +$(src_dir)/constants.h $(src_dir)/multiplications.h \ +$(variant_dir)/parameters.h + +$(results_dir)/test/test-tbc-encrypt.o $(results_dir)/test/test-tbc-decrypt.o $(results_dir)/test/traces-tbc.o: \ +$(src_dir)/cipher.h + +$(results_dir)/test/test-ae-encrypt.o $(results_dir)/test/test-ae-decrypt.o $(results_dir)/test/test-ae-roundtrip.o $(results_dir)/test/traces-ae.o: \ +$(src_dir)/lilliput-ae.h diff --git a/src/add_felicsref/multiplications.h b/src/add_felicsref/multiplications.h new file mode 120000 index 0000000..df7f6a6 --- /dev/null +++ b/src/add_felicsref/multiplications.h @@ -0,0 +1 @@ +../ref/multiplications.h \ No newline at end of file diff --git a/src/add_felicsref/tweakey.c b/src/add_felicsref/tweakey.c index 635c179..c5532f8 100644 --- a/src/add_felicsref/tweakey.c +++ b/src/add_felicsref/tweakey.c @@ -15,30 +15,25 @@ http://creativecommons.org/publicdomain/zero/1.0/ --- This file provides an implementation of Lilliput-TBC's tweakey schedule, -similar to the reference implementation save for a few manual optimizations: +similar to the reference implementation save for one manual optimization: +the loop over an array of function pointers was unrolled. -- unused multiplication functions were removed using preprocessor - conditionals based on the number of lanes; - -- the loop over an array of function pointers was unrolled. - -These handmade optimizations have been found to significantly decrease code +This handmade optimization has been found to significantly decrease code size and execution time on GCC versions used in the FELICS framework. -This suggests that the compiler does not detect dead code nor does it -recognize unrolling opportunities, despite the multiplication functions -being static and thus limited in scope to the compilation unit. +This suggests that the compiler does not recognize inlining opportunities, +despite the multiplication functions being static and thus limited in scope +to the compilation unit. */ #include #include #include "constants.h" +#include "multiplications.h" #include "tweakey.h" -#define LANE_BITS 64 -#define LANE_BYTES (LANE_BITS/8) #define LANES_NB (TWEAKEY_BYTES/LANE_BYTES) @@ -75,98 +70,6 @@ void tweakey_state_extract( } -static void _multiply_M(const uint8_t x[LANE_BYTES], uint8_t y[LANE_BYTES]) -{ - y[7] = x[6]; - y[6] = x[5]; - y[5] = x[5]<<3 ^ x[4]; - y[4] = x[4]>>3 ^ x[3]; - y[3] = x[2]; - y[2] = x[6]<<2 ^ x[1]; - y[1] = x[0]; - y[0] = x[7]; -} - -static void _multiply_M2(const uint8_t x[LANE_BYTES], uint8_t y[LANE_BYTES]) -{ - uint8_t x_M_5 = x[5]<<3 ^ x[4]; - uint8_t x_M_4 = x[4]>>3 ^ x[3]; - - y[7] = x[5]; - y[6] = x_M_5; - y[5] = x_M_5<<3 ^ x_M_4; - y[4] = x_M_4>>3 ^ x[2]; - y[3] = x[6]<<2 ^ x[1]; - y[2] = x[5]<<2 ^ x[0]; - y[1] = x[7]; - y[0] = x[6]; -} - -static void _multiply_M3(const uint8_t x[LANE_BYTES], uint8_t y[LANE_BYTES]) -{ - uint8_t x_M_5 = x[5]<<3 ^ x[4]; - uint8_t x_M_4 = x[4]>>3 ^ x[3]; - uint8_t x_M2_5 = x_M_5<<3 ^ x_M_4; - uint8_t x_M2_4 = x_M_4>>3 ^ x[2]; - - y[7] = x_M_5; - y[6] = x_M2_5; - y[5] = x_M2_5<<3 ^ x_M2_4; - y[4] = x_M2_4>>3 ^ x[6]<<2 ^ x[1]; - y[3] = x[5]<<2 ^ x[0]; - y[2] = x_M_5<<2 ^ x[7]; - y[1] = x[6]; - y[0] = x[5]; -} - -#if LANES_NB >= 5 -static void _multiply_MR(const uint8_t x[LANE_BYTES], uint8_t y[LANE_BYTES]) -{ - y[0] = x[1]; - y[1] = x[2]; - y[2] = x[3] ^ x[4]>>3; - y[3] = x[4]; - y[4] = x[5] ^ x[6]<<3; - y[5] = x[3]<<2 ^ x[6]; - y[6] = x[7]; - y[7] = x[0]; -} - -#if LANES_NB >= 6 -static void _multiply_MR2(const uint8_t x[LANE_BYTES], uint8_t y[LANE_BYTES]) -{ - uint8_t x_MR_4 = x[5] ^ x[6]<<3; - - y[0] = x[2]; - y[1] = x[3] ^ x[4]>>3; - y[2] = x[4] ^ x_MR_4>>3; - y[3] = x_MR_4; - y[4] = x[3]<<2 ^ x[6] ^ x[7]<<3; - y[5] = x[4]<<2 ^ x[7]; - y[6] = x[0]; - y[7] = x[1]; -} - -#if LANES_NB >= 7 -static void _multiply_MR3(const uint8_t x[LANE_BYTES], uint8_t y[LANE_BYTES]) -{ - uint8_t x_MR_4 = x[5] ^ x[6]<<3; - uint8_t x_MR2_4 = x[3]<<2 ^ x[6] ^ x[7]<<3; - - y[0] = x[3] ^ x[4]>>3; - y[1] = x[4] ^ x_MR_4>>3; - y[2] = x_MR_4 ^ x_MR2_4>>3; - y[3] = x_MR2_4; - y[4] = x[0]<<3 ^ x[4]<<2 ^ x[7]; - y[5] = x_MR_4<<2 ^ x[0]; - y[6] = x[1]; - y[7] = x[2]; -} -#endif -#endif -#endif - - void tweakey_state_update(uint8_t TK[TWEAKEY_BYTES]) { /* Skip lane 0, as it is multiplied by the identity matrix. */ diff --git a/src/add_threshold/implem.mk b/src/add_threshold/implem.mk index 7e3ac8e..8d4d4a1 100644 --- a/src/add_threshold/implem.mk +++ b/src/add_threshold/implem.mk @@ -8,8 +8,9 @@ tests = $(filter-out test-tweakey,$(basename $(wildcard test-*.c))) # differ significantly. traces = $(basename $(wildcard traces-*.c)) + # Don't trigger warnings for "a&b ^ c". -CFLAGS += -Wno-parentheses +$(results_dir)/src/cipher.o: CFLAGS += -Wno-parentheses # Build dependencies: add random module; remove unused tests/traces. -- cgit v1.2.3