Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
23 changes: 22 additions & 1 deletion src/speck/speck_ctr.c
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand All @@ -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);

Expand All @@ -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;
}

Expand Down