diff --git a/CMakeLists.txt b/CMakeLists.txt index bc6752b..8a51376 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -17,6 +17,7 @@ option(ENABLE_STATIC "Build a static version of the libraries?" OFF) option(DISABLE_SHARED "Do not build a shared version of the libraries?" OFF) option(ENABLE_TESTING "Enable ctest?" OFF) option(ENABLE_BENCHMARK "Enable benchmark?" OFF) +option(LITTLE_ENDIAN_CTR "little-endian ctr mode" OFF) # simon speck build options @@ -133,6 +134,11 @@ if(ENABLE_AVX2) endif() endif() +# flags +if(LITTLE_ENDIAN_CTR) + set(CMAKE_C_FLAGS "${CMAKE_C_FLAGS} -DLITTLE_ENDIAN_CTR") + set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DLITTLE_ENDIAN_CTR") +endif() # source set(libspeck_SRCS ${libspeck_SRCS} src/speck/speck_common.c) diff --git a/src/speck/speck_ctr.c b/src/speck/speck_ctr.c index e2523c8..178c65a 100644 --- a/src/speck/speck_ctr.c +++ b/src/speck/speck_ctr.c @@ -42,11 +42,21 @@ int speck_ctr_encrypt(speck_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len uint64_t crypted_iv_block[2]; uint64_t plain_block[2]; uint64_t iv_block[2]; +#ifdef LITTLE_ENDIAN_CTR + uint64_t iv_tmp[2]; + iv_tmp[0] = ((uint64_t*)iv)[0]; + iv_tmp[1] = ((uint64_t*)iv)[1]; +#endif for (i = 0; i < count; i++) { +#ifdef LITTLE_ENDIAN_CTR + iv_block[0] = iv_tmp[0]; + iv_block[1] = iv_tmp[1]; + iv_tmp[0]++; +#else cast_uint8_array_to_uint64(&iv_block[0], iv + (WORDS * 0)); cast_uint8_array_to_uint64(&iv_block[1], iv + (WORDS * 1)); ctr128_inc(iv); - +#endif speck_encrypt(ctx, iv_block, crypted_iv_block); int array_idx = (i * (BLOCK_SIZE * LANE_NUM)); @@ -60,9 +70,15 @@ int speck_ctr_encrypt(speck_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len cast_uint64_to_uint8_array(cur_crypted + (WORDS * 1), crypted_iv_block[1] ^ plain_block[1]); } if (remain_bytes != 0) { +#ifdef LITTLE_ENDIAN_CTR + iv_block[0] = iv_tmp[0]; + iv_block[1] = iv_tmp[1]; + iv_tmp[0]++; +#else cast_uint8_array_to_uint64(&iv_block[0], iv + (WORDS * 0)); cast_uint8_array_to_uint64(&iv_block[1], iv + (WORDS * 1)); ctr128_inc(iv); +#endif speck_encrypt(ctx, iv_block, crypted_iv_block); @@ -84,6 +100,11 @@ int speck_ctr_encrypt(speck_ctx_t *ctx, const uint8_t *in, uint8_t *out, int len } } +#ifdef LITTLE_ENDIAN_CTR + cast_uint64_to_uint8_array(iv + (WORDS * 0), iv_tmp[0]); + cast_uint64_to_uint8_array(iv + (WORDS * 1), iv_tmp[1]); +#endif + return 0; }