From 01ab41f6676540713c5f3ad3ce51ea1780d647e4 Mon Sep 17 00:00:00 2001 From: Dan Page Date: Thu, 16 Jan 2020 08:24:25 +0000 Subject: [PATCH 1/4] tweak Makefile targets --- Makefile | 8 +++++--- README.md | 4 ++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/Makefile b/Makefile index 8505660..34536e1 100644 --- a/Makefile +++ b/Makefile @@ -55,13 +55,15 @@ ifeq "${CONTEXT}" "native" %-test : @make --directory="${REPO_HOME}/src/test" ${*} -venv : ${REPO_HOME}/requirements.txt +# ----------------------------------------------------------------------------- + +venv : ${REPO_HOME}/requirements.txt @${REPO_HOME}/bin/venv.sh -doc : ${REPO_HOME}/Doxyfile +doxygen : ${REPO_HOME}/Doxyfile @doxygen ${<} -clean : +spotless : @rm --force --recursive ${REPO_HOME}/build/* endif diff --git a/README.md b/README.md index 0952103..cee2fff 100644 --- a/README.md +++ b/README.md @@ -174,13 +174,13 @@ benchmark.* | Command | Description | :----------------------- | :----------------------------------------------------------------------------------- | - | `make doc` | build the [Doxygen](http://www.doxygen.nl)-based documentation | + | `make doxygen` | build the [Doxygen](http://www.doxygen.nl)-based documentation | | `make venv` | build the Python [virtual environment](https://docs.python.org/library/venv.html) | | `make build-libscarv` | build the `libscarv` library | | `make build-test` | build the `libscarv` test suite | | `make generate-test` | generate the `libscarv` test suite (i.e., produce meta-program from test executable) | | `make generate-test` | validate the `libscarv` test suite (i.e., execute meta-program) | - | `make clean` | clean-up (e.g., remove everything built in `${REPO_HOME}/build`) | + | `make spotless` | remove *everything* built in `${REPO_HOME}/build`) | From d52eb71098aa3f5a424fc6dd04bfd77ddeac4e62 Mon Sep 17 00:00:00 2001 From: Dan Page Date: Thu, 16 Jan 2020 08:28:05 +0000 Subject: [PATCH 2/4] bump --- Makefile | 6 ++++-- README.md | 4 ++-- 2 files changed, 6 insertions(+), 4 deletions(-) diff --git a/Makefile b/Makefile index 34536e1..6daa64e 100644 --- a/Makefile +++ b/Makefile @@ -42,11 +42,15 @@ include ${REPO_HOME}/conf/${ARCH}/conf.mk_docker # - deal with various specific, global targets (e.g., documentation), or # - defer to the appropriate sub-Makefile for everything else. +# ----------------------------------------------------------------------------- + ifeq "${CONTEXT}" "docker" % : @docker run --rm --volume "${REPO_HOME}:/mnt/scarv/libscarv" --env DOCKER_GID="$(shell id --group)" --env DOCKER_UID="$(shell id --user)" --env REPO_HOME="/mnt/scarv/libscarv" --env CONTEXT="native" --env ARCH="${ARCH}" --env KERNELS="${KERNELS}" ${DOCKER_FLAGS} ${DOCKER_REPO}:${DOCKER_TAG} ${*} endif +# ----------------------------------------------------------------------------- + ifeq "${CONTEXT}" "native" %-docker : @make --directory="${REPO_HOME}/src/docker" ${*} @@ -55,8 +59,6 @@ ifeq "${CONTEXT}" "native" %-test : @make --directory="${REPO_HOME}/src/test" ${*} -# ----------------------------------------------------------------------------- - venv : ${REPO_HOME}/requirements.txt @${REPO_HOME}/bin/venv.sh diff --git a/README.md b/README.md index cee2fff..2830492 100644 --- a/README.md +++ b/README.md @@ -174,13 +174,13 @@ benchmark.* | Command | Description | :----------------------- | :----------------------------------------------------------------------------------- | - | `make doxygen` | build the [Doxygen](http://www.doxygen.nl)-based documentation | | `make venv` | build the Python [virtual environment](https://docs.python.org/library/venv.html) | | `make build-libscarv` | build the `libscarv` library | | `make build-test` | build the `libscarv` test suite | | `make generate-test` | generate the `libscarv` test suite (i.e., produce meta-program from test executable) | | `make generate-test` | validate the `libscarv` test suite (i.e., execute meta-program) | - | `make spotless` | remove *everything* built in `${REPO_HOME}/build`) | + | `make doxygen` | build the [Doxygen](http://www.doxygen.nl)-based documentation | + | `make spotless` | remove *everything* built in `${REPO_HOME}/build` | From 75e7af14f97108b8c56a4b753f77fbd1f510fcb4 Mon Sep 17 00:00:00 2001 From: Dan Page Date: Thu, 16 Jan 2020 08:31:56 +0000 Subject: [PATCH 3/4] bump --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 2830492..c2a8db8 100644 --- a/README.md +++ b/README.md @@ -174,11 +174,11 @@ benchmark.* | Command | Description | :----------------------- | :----------------------------------------------------------------------------------- | - | `make venv` | build the Python [virtual environment](https://docs.python.org/library/venv.html) | | `make build-libscarv` | build the `libscarv` library | | `make build-test` | build the `libscarv` test suite | | `make generate-test` | generate the `libscarv` test suite (i.e., produce meta-program from test executable) | | `make generate-test` | validate the `libscarv` test suite (i.e., execute meta-program) | + | `make venv` | build the Python [virtual environment](https://docs.python.org/library/venv.html) | | `make doxygen` | build the [Doxygen](http://www.doxygen.nl)-based documentation | | `make spotless` | remove *everything* built in `${REPO_HOME}/build` | From 81db291f4ed1c469128cf9650925998800134183 Mon Sep 17 00:00:00 2001 From: Dan Page Date: Fri, 24 Jan 2020 15:55:36 +0000 Subject: [PATCH 4/4] initial, somewhat draft implementation (native only so far, plus KAT-based test) --- conf/native/conf.mk_kernel | 34 +++-- conf/riscv-xcrypto/conf.mk_kernel | 34 +++-- conf/riscv/conf.mk_kernel | 34 +++-- src/libscarv/block/prince/native/.gitignore | 0 .../block/prince/riscv-xcrypto/.gitignore | 0 src/libscarv/block/prince/riscv/.gitignore | 0 src/libscarv/block/sm4/Makefile.in | 17 +++ src/libscarv/block/sm4/native/.gitignore | 0 .../block/sm4/riscv-xcrypto/.gitignore | 0 src/libscarv/block/sm4/riscv/.gitignore | 0 src/libscarv/block/sm4/sm4_dec.c | 128 ++++++++++++++++++ src/libscarv/block/sm4/sm4_dec.h | 25 ++++ src/libscarv/block/sm4/sm4_enc.c | 104 ++++++++++++++ src/libscarv/block/sm4/sm4_enc.h | 25 ++++ src/libscarv/block/sm4/sm4_share.c | 42 ++++++ src/libscarv/block/sm4/sm4_share.h | 19 +++ src/libscarv/scarv.h | 14 +- src/libscarv/share/conf.h | 76 +++++++---- src/test/block/sm4/Makefile.in | 16 +++ src/test/block/sm4/test_sm4.c | 119 ++++++++++++++++ src/test/block/sm4/test_sm4.h | 22 +++ 21 files changed, 648 insertions(+), 61 deletions(-) create mode 100644 src/libscarv/block/prince/native/.gitignore create mode 100644 src/libscarv/block/prince/riscv-xcrypto/.gitignore create mode 100644 src/libscarv/block/prince/riscv/.gitignore create mode 100644 src/libscarv/block/sm4/Makefile.in create mode 100644 src/libscarv/block/sm4/native/.gitignore create mode 100644 src/libscarv/block/sm4/riscv-xcrypto/.gitignore create mode 100644 src/libscarv/block/sm4/riscv/.gitignore create mode 100644 src/libscarv/block/sm4/sm4_dec.c create mode 100644 src/libscarv/block/sm4/sm4_dec.h create mode 100644 src/libscarv/block/sm4/sm4_enc.c create mode 100644 src/libscarv/block/sm4/sm4_enc.h create mode 100644 src/libscarv/block/sm4/sm4_share.c create mode 100644 src/libscarv/block/sm4/sm4_share.h create mode 100644 src/test/block/sm4/Makefile.in create mode 100644 src/test/block/sm4/test_sm4.c create mode 100644 src/test/block/sm4/test_sm4.h diff --git a/conf/native/conf.mk_kernel b/conf/native/conf.mk_kernel index 2ebbe74..74741b8 100644 --- a/conf/native/conf.mk_kernel +++ b/conf/native/conf.mk_kernel @@ -28,6 +28,29 @@ export LIBSCARV_CONF_AES_RND_UNROLL = 1 # ----------------------------------------------------------------------------- +export LIBSCARV_CONF_SM4_ENC_EXTERN = 0 +export LIBSCARV_CONF_SM4_DEC_EXTERN = 0 + +export LIBSCARV_CONF_SM4_ENC_EXP_STEP_EXTERN = 0 +export LIBSCARV_CONF_SM4_DEC_EXP_STEP_EXTERN = 0 + +export LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD = 1 +export LIBSCARV_CONF_SM4_KEY_EVOLVE_REV = 0 +export LIBSCARV_CONF_SM4_KEY_PRECOMP = 1 + +# ----------------------------------------------------------------------------- + +export LIBSCARV_CONF_PRINCE_SBOX_EXTERN = 0 +export LIBSCARV_CONF_PRINCE_ISBOX_EXTERN = 0 +export LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN = 0 +export LIBSCARV_CONF_PRINCE_MPRIME_EXTERN = 0 + +# ----------------------------------------------------------------------------- + +export LIBSCARV_CONF_SPARX_L_EXTERN = 0 + +# ----------------------------------------------------------------------------- + export LIBSCARV_CONF_SHA1_COMP_EXTERN = 0 # ----------------------------------------------------------------------------- @@ -70,15 +93,4 @@ export LIBSCARV_CONF_MRZ_MUL_CIOS = 1 export LIBSCARV_CONF_CHACHA20_PROCESS_EXTERN = 0 export LIBSCARV_CONF_CHACHA20_BLOCK_EXTERN = 0 -# ----------------------------------------------------------------------------- - -export LIBSCARV_CONF_PRINCE_SBOX_EXTERN = 0 -export LIBSCARV_CONF_PRINCE_ISBOX_EXTERN = 0 -export LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN = 0 -export LIBSCARV_CONF_PRINCE_MPRIME_EXTERN = 0 - -# ----------------------------------------------------------------------------- - -export LIBSCARV_CONF_SPARX_L_EXTERN = 0 - # ============================================================================= diff --git a/conf/riscv-xcrypto/conf.mk_kernel b/conf/riscv-xcrypto/conf.mk_kernel index 6bcc859..2da2eee 100644 --- a/conf/riscv-xcrypto/conf.mk_kernel +++ b/conf/riscv-xcrypto/conf.mk_kernel @@ -28,6 +28,29 @@ export LIBSCARV_CONF_AES_RND_UNROLL = 1 # ----------------------------------------------------------------------------- +export LIBSCARV_CONF_SM4_ENC_EXTERN = 0 +export LIBSCARV_CONF_SM4_DEC_EXTERN = 0 + +export LIBSCARV_CONF_SM4_ENC_EXP_STEP_EXTERN = 0 +export LIBSCARV_CONF_SM4_DEC_EXP_STEP_EXTERN = 0 + +export LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD = 1 +export LIBSCARV_CONF_SM4_KEY_EVOLVE_REV = 0 +export LIBSCARV_CONF_SM4_KEY_PRECOMP = 1 + +# ----------------------------------------------------------------------------- + +export LIBSCARV_CONF_PRINCE_SBOX_EXTERN = 1 +export LIBSCARV_CONF_PRINCE_ISBOX_EXTERN = 1 +export LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN = 1 +export LIBSCARV_CONF_PRINCE_MPRIME_EXTERN = 0 + +# ----------------------------------------------------------------------------- + +export LIBSCARV_CONF_SPARX_L_EXTERN = 1 + +# ----------------------------------------------------------------------------- + export LIBSCARV_CONF_SHA1_COMP_EXTERN = 0 # ----------------------------------------------------------------------------- @@ -70,17 +93,6 @@ export LIBSCARV_CONF_MRZ_MUL_CIOS = 1 export LIBSCARV_CONF_CHACHA20_PROCESS_EXTERN = 1 export LIBSCARV_CONF_CHACHA20_BLOCK_EXTERN = 1 -# ----------------------------------------------------------------------------- - -export LIBSCARV_CONF_PRINCE_SBOX_EXTERN = 1 -export LIBSCARV_CONF_PRINCE_ISBOX_EXTERN = 1 -export LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN = 1 -export LIBSCARV_CONF_PRINCE_MPRIME_EXTERN = 0 - -# ----------------------------------------------------------------------------- - -export LIBSCARV_CONF_SPARX_L_EXTERN = 1 - # ============================================================================= export LIBSCARV_CONF_XCRYPTO_CLASS_1 = 1 diff --git a/conf/riscv/conf.mk_kernel b/conf/riscv/conf.mk_kernel index 7738708..3c30ad9 100644 --- a/conf/riscv/conf.mk_kernel +++ b/conf/riscv/conf.mk_kernel @@ -28,6 +28,29 @@ export LIBSCARV_CONF_AES_RND_UNROLL = 1 # ----------------------------------------------------------------------------- +export LIBSCARV_CONF_SM4_ENC_EXTERN = 0 +export LIBSCARV_CONF_SM4_DEC_EXTERN = 0 + +export LIBSCARV_CONF_SM4_ENC_EXP_STEP_EXTERN = 0 +export LIBSCARV_CONF_SM4_DEC_EXP_STEP_EXTERN = 0 + +export LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD = 1 +export LIBSCARV_CONF_SM4_KEY_EVOLVE_REV = 0 +export LIBSCARV_CONF_SM4_KEY_PRECOMP = 1 + +# ----------------------------------------------------------------------------- + +export LIBSCARV_CONF_PRINCE_SBOX_EXTERN = 0 +export LIBSCARV_CONF_PRINCE_ISBOX_EXTERN = 0 +export LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN = 0 +export LIBSCARV_CONF_PRINCE_MPRIME_EXTERN = 0 + +# ----------------------------------------------------------------------------- + +export LIBSCARV_CONF_SPARX_L_EXTERN = 0 + +# ----------------------------------------------------------------------------- + export LIBSCARV_CONF_SHA1_COMP_EXTERN = 0 # ----------------------------------------------------------------------------- @@ -70,15 +93,4 @@ export LIBSCARV_CONF_MRZ_MUL_CIOS = 1 export LIBSCARV_CONF_CHACHA20_PROCESS_EXTERN = 0 export LIBSCARV_CONF_CHACHA20_BLOCK_EXTERN = 0 -# ----------------------------------------------------------------------------- - -export LIBSCARV_CONF_PRINCE_SBOX_EXTERN = 0 -export LIBSCARV_CONF_PRINCE_ISBOX_EXTERN = 0 -export LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN = 0 -export LIBSCARV_CONF_PRINCE_MPRIME_EXTERN = 0 - -# ----------------------------------------------------------------------------- - -export LIBSCARV_CONF_SPARX_L_EXTERN = 0 - # ============================================================================= diff --git a/src/libscarv/block/prince/native/.gitignore b/src/libscarv/block/prince/native/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/libscarv/block/prince/riscv-xcrypto/.gitignore b/src/libscarv/block/prince/riscv-xcrypto/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/libscarv/block/prince/riscv/.gitignore b/src/libscarv/block/prince/riscv/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/libscarv/block/sm4/Makefile.in b/src/libscarv/block/sm4/Makefile.in new file mode 100644 index 0000000..7a76d0d --- /dev/null +++ b/src/libscarv/block/sm4/Makefile.in @@ -0,0 +1,17 @@ +# Copyright (C) 2019 SCARV project +# +# Use of this source code is restricted per the MIT license, a copy of which +# can be found at https://opensource.org/licenses/MIT (or should be included +# as LICENSE.txt within the associated archive or repository). + +ID = block/sm4 + +PATHS = ${REPO_HOME}/src/libscarv/${ID} +PATHS += ${REPO_HOME}/src/libscarv/${ID}/${ARCH} + +HEADERS = $(foreach PATH,${PATHS},$(wildcard ${PATH}/*.h)) +SOURCES = $(foreach PATH,${PATHS},$(wildcard ${PATH}/*.c)) +SOURCES += $(foreach PATH,${PATHS},$(wildcard ${PATH}/*.S)) + +$(eval $(call build,${ID},${PATHS},${HEADERS},${SOURCES})) + diff --git a/src/libscarv/block/sm4/native/.gitignore b/src/libscarv/block/sm4/native/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/libscarv/block/sm4/riscv-xcrypto/.gitignore b/src/libscarv/block/sm4/riscv-xcrypto/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/libscarv/block/sm4/riscv/.gitignore b/src/libscarv/block/sm4/riscv/.gitignore new file mode 100644 index 0000000..e69de29 diff --git a/src/libscarv/block/sm4/sm4_dec.c b/src/libscarv/block/sm4/sm4_dec.c new file mode 100644 index 0000000..a80d507 --- /dev/null +++ b/src/libscarv/block/sm4/sm4_dec.c @@ -0,0 +1,128 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#include + +#if ( !LIBSCARV_CONF_SM4_DEC_EXP_STEP_EXTERN ) +uint32_t sm4_dec_exp_step( uint32_t* r, const uint32_t* k, uint32_t ck ) { + uint32_t t_0 = k[ 0 ]; + uint32_t t_1 = k[ 1 ]; + uint32_t t_2 = k[ 2 ]; + uint32_t t_3 = k[ 3 ]; + + uint32_t t_4 = t_0 ^ t_1 ^ t_2 ^ ck; + + t_4 = ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 0 ) & 0xFF ] ) << 0 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 8 ) & 0xFF ] ) << 8 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 16 ) & 0xFF ] ) << 16 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 24 ) & 0xFF ] ) << 24 ) ; + + t_4 ^= U32_RTL( t_4, 13 ) ^ + U32_RTL( t_4, 23 ) ; + + t_4 ^= t_3; + + r[ 0 ] = t_4; + r[ 1 ] = t_0; + r[ 2 ] = t_1; + r[ 3 ] = t_2; + + return t_3; +} +#endif + +#if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) +void sm4_dec_exp( uint32_t* r, const uint8_t* k ) { + uint32_t t[ 4 ]; + + U8_TO_U32_BE( t[ 0 ], k, 0 ); + U8_TO_U32_BE( t[ 1 ], k, 4 ); + U8_TO_U32_BE( t[ 2 ], k, 8 ); + U8_TO_U32_BE( t[ 3 ], k, 12 ); + + #if ( LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD ) + t[ 0 ] ^= SM4_FK[ 0 ]; + t[ 1 ] ^= SM4_FK[ 1 ]; + t[ 2 ] ^= SM4_FK[ 2 ]; + t[ 3 ] ^= SM4_FK[ 3 ]; + + for( int i = 0; i < 32; i++ ) { + r[ i ] = sm4_enc_exp_step( t, t, SM4_CK[ i ] ); + } + #endif + + #if ( LIBSCARV_CONF_SM4_KEY_EVOLVE_REV ) + for( int i = 0; i < 32; i++ ) { + r[ 32 - i - 1 ] = sm4_dec_exp_step( t, t, SM4_CK[ 32 - i - 1 ] ); + } + #endif +} +#endif + +#if ( !LIBSCARV_CONF_SM4_DEC_EXTERN ) +void sm4_dec( uint8_t* r, uint8_t* c, uint8_t* k ) { + #if ( !LIBSCARV_CONF_SM4_KEY_PRECOMP ) + uint32_t rk[ 4 ]; + + U8_TO_U32_BE( rk[ 0 ], k, 0 ); + U8_TO_U32_BE( rk[ 1 ], k, 4 ); + U8_TO_U32_BE( rk[ 2 ], k, 8 ); + U8_TO_U32_BE( rk[ 3 ], k, 12 ); + #else + uint32_t* rk = ( uint32_t* )( k ); + #endif + + uint32_t t_0, t_1, t_2, t_3, t_4; + + U8_TO_U32_BE( t_0, c, 0 ); + U8_TO_U32_BE( t_1, c, 4 ); + U8_TO_U32_BE( t_2, c, 8 ); + U8_TO_U32_BE( t_3, c, 12 ); + + #if ( !LIBSCARV_CONF_SM4_KEY_PRECOMP ) && ( LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD ) + rk[ 0 ] ^= SM4_FK[ 0 ]; + rk[ 1 ] ^= SM4_FK[ 1 ]; + rk[ 2 ] ^= SM4_FK[ 2 ]; + rk[ 3 ] ^= SM4_FK[ 3 ]; + + for( int i = 0; i < 32; i++ ) { + sm4_enc_exp_step( rk, rk, SM4_CK[ i ] ); + } + #endif + + for( int i = 0; i < 32; i++ ) { + t_4 = t_1 ^ t_2 ^ t_3; + #if ( !LIBSCARV_CONF_SM4_KEY_PRECOMP ) + t_4 ^= sm4_dec_exp_step( rk, rk, SM4_CK[ 32 - i - 1 ] ); + #else + t_4 ^= rk[ 32 - i - 1 ]; + #endif + + t_4 = ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 0 ) & 0xFF ] ) << 0 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 8 ) & 0xFF ] ) << 8 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 16 ) & 0xFF ] ) << 16 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 24 ) & 0xFF ] ) << 24 ) ; + + t_4 ^= U32_RTL( t_4, 2 ) ^ + U32_RTL( t_4, 10 ) ^ + U32_RTL( t_4, 18 ) ^ + U32_RTL( t_4, 24 ) ; + + t_4 ^= t_0; + + t_0 = t_1; + t_1 = t_2; + t_2 = t_3; + t_3 = t_4; + } + + U32_TO_U8_BE( r, t_3, 0 ); + U32_TO_U8_BE( r, t_2, 4 ); + U32_TO_U8_BE( r, t_1, 8 ); + U32_TO_U8_BE( r, t_0, 12 ); +} +#endif diff --git a/src/libscarv/block/sm4/sm4_dec.h b/src/libscarv/block/sm4/sm4_dec.h new file mode 100644 index 0000000..0ac0064 --- /dev/null +++ b/src/libscarv/block/sm4/sm4_dec.h @@ -0,0 +1,25 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#ifndef __LIBSCARV_SM4_DEC_H +#define __LIBSCARV_SM4_DEC_H + +#include +#include + +#include + +#include + +#if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) +extern void sm4_dec_exp( uint32_t* r, const uint8_t* k ); +#endif +extern uint32_t sm4_dec_exp_step( uint32_t* r, const uint32_t* k, uint32_t ck ); + +extern void sm4_dec( uint8_t* r, uint8_t* m, uint8_t* k ); + +#endif diff --git a/src/libscarv/block/sm4/sm4_enc.c b/src/libscarv/block/sm4/sm4_enc.c new file mode 100644 index 0000000..c265aa3 --- /dev/null +++ b/src/libscarv/block/sm4/sm4_enc.c @@ -0,0 +1,104 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#include + +#if ( !LIBSCARV_CONF_SM4_ENC_EXP_STEP_EXTERN ) +uint32_t sm4_enc_exp_step( uint32_t* r, const uint32_t* k, uint32_t ck ) { + uint32_t t_0 = k[ 0 ]; + uint32_t t_1 = k[ 1 ]; + uint32_t t_2 = k[ 2 ]; + uint32_t t_3 = k[ 3 ]; + + uint32_t t_4 = t_1 ^ t_2 ^ t_3 ^ ck; + + t_4 = ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 0 ) & 0xFF ] ) << 0 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 8 ) & 0xFF ] ) << 8 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 16 ) & 0xFF ] ) << 16 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 24 ) & 0xFF ] ) << 24 ) ; + + t_4 ^= U32_RTL( t_4, 13 ) ^ + U32_RTL( t_4, 23 ) ; + + t_4 ^= t_0; + + r[ 0 ] = t_1; + r[ 1 ] = t_2; + r[ 2 ] = t_3; + r[ 3 ] = t_4; + + return t_4; +} +#endif + +#if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) +void sm4_enc_exp( uint32_t* r, const uint8_t* k ) { + uint32_t t[ 4 ]; + + U8_TO_U32_BE( t[ 0 ], k, 0 ); t[ 0 ] ^= SM4_FK[ 0 ]; + U8_TO_U32_BE( t[ 1 ], k, 4 ); t[ 1 ] ^= SM4_FK[ 1 ]; + U8_TO_U32_BE( t[ 2 ], k, 8 ); t[ 2 ] ^= SM4_FK[ 2 ]; + U8_TO_U32_BE( t[ 3 ], k, 12 ); t[ 3 ] ^= SM4_FK[ 3 ]; + + for( int i = 0; i < 32; i++ ) { + r[ i ] = sm4_enc_exp_step( t, t, SM4_CK[ i ] ); + } +} +#endif + +#if ( !LIBSCARV_CONF_SM4_ENC_EXTERN ) +void sm4_enc( uint8_t* r, uint8_t* m, uint8_t* k ) { + #if ( !LIBSCARV_CONF_SM4_KEY_PRECOMP ) + uint32_t rk[ 4 ]; + + U8_TO_U32_BE( rk[ 0 ], k, 0 ); rk[ 0 ] ^= SM4_FK[ 0 ]; + U8_TO_U32_BE( rk[ 1 ], k, 4 ); rk[ 1 ] ^= SM4_FK[ 1 ]; + U8_TO_U32_BE( rk[ 2 ], k, 8 ); rk[ 2 ] ^= SM4_FK[ 2 ]; + U8_TO_U32_BE( rk[ 3 ], k, 12 ); rk[ 3 ] ^= SM4_FK[ 3 ]; + #else + uint32_t* rk = ( uint32_t* )( k ); + #endif + + uint32_t t_0, t_1, t_2, t_3, t_4; + + U8_TO_U32_BE( t_0, m, 0 ); + U8_TO_U32_BE( t_1, m, 4 ); + U8_TO_U32_BE( t_2, m, 8 ); + U8_TO_U32_BE( t_3, m, 12 ); + + for( int i = 0; i < 32; i++ ) { + t_4 = t_1 ^ t_2 ^ t_3; + #if ( !LIBSCARV_CONF_SM4_KEY_PRECOMP ) + t_4 ^= sm4_enc_exp_step( rk, rk, SM4_CK[ i ] ); + #else + t_4 ^= rk[ i ]; + #endif + + t_4 = ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 0 ) & 0xFF ] ) << 0 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 8 ) & 0xFF ] ) << 8 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 16 ) & 0xFF ] ) << 16 ) | + ( ( uint32_t )( SM4_SBOX[ ( t_4 >> 24 ) & 0xFF ] ) << 24 ) ; + + t_4 ^= U32_RTL( t_4, 2 ) ^ + U32_RTL( t_4, 10 ) ^ + U32_RTL( t_4, 18 ) ^ + U32_RTL( t_4, 24 ) ; + + t_4 ^= t_0; + + t_0 = t_1; + t_1 = t_2; + t_2 = t_3; + t_3 = t_4; + } + + U32_TO_U8_BE( r, t_3, 0 ); + U32_TO_U8_BE( r, t_2, 4 ); + U32_TO_U8_BE( r, t_1, 8 ); + U32_TO_U8_BE( r, t_0, 12 ); +} +#endif diff --git a/src/libscarv/block/sm4/sm4_enc.h b/src/libscarv/block/sm4/sm4_enc.h new file mode 100644 index 0000000..8a06106 --- /dev/null +++ b/src/libscarv/block/sm4/sm4_enc.h @@ -0,0 +1,25 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#ifndef __LIBSCARV_SM4_ENC_H +#define __LIBSCARV_SM4_ENC_H + +#include +#include + +#include + +#include + +#if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) +extern void sm4_enc_exp( uint32_t* r, const uint8_t* k ); +#endif +extern uint32_t sm4_enc_exp_step( uint32_t* r, const uint32_t* k, uint32_t ck ); + +extern void sm4_enc( uint8_t* r, uint8_t* m, uint8_t* k ); + +#endif diff --git a/src/libscarv/block/sm4/sm4_share.c b/src/libscarv/block/sm4/sm4_share.c new file mode 100644 index 0000000..55fb570 --- /dev/null +++ b/src/libscarv/block/sm4/sm4_share.c @@ -0,0 +1,42 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#include + +uint8_t SM4_SBOX[] = { + 0xD6, 0x90, 0xE9, 0xFE, 0xCC, 0xE1, 0x3D, 0xB7, 0x16, 0xB6, 0x14, 0xC2, 0x28, 0xFB, 0x2C, 0x05, + 0x2B, 0x67, 0x9A, 0x76, 0x2A, 0xBE, 0x04, 0xC3, 0xAA, 0x44, 0x13, 0x26, 0x49, 0x86, 0x06, 0x99, + 0x9C, 0x42, 0x50, 0xF4, 0x91, 0xEF, 0x98, 0x7A, 0x33, 0x54, 0x0B, 0x43, 0xED, 0xCF, 0xAC, 0x62, + 0xE4, 0xB3, 0x1C, 0xA9, 0xC9, 0x08, 0xE8, 0x95, 0x80, 0xDF, 0x94, 0xFA, 0x75, 0x8F, 0x3F, 0xA6, + 0x47, 0x07, 0xA7, 0xFC, 0xF3, 0x73, 0x17, 0xBA, 0x83, 0x59, 0x3C, 0x19, 0xE6, 0x85, 0x4F, 0xA8, + 0x68, 0x6B, 0x81, 0xB2, 0x71, 0x64, 0xDA, 0x8B, 0xF8, 0xEB, 0x0F, 0x4B, 0x70, 0x56, 0x9D, 0x35, + 0x1E, 0x24, 0x0E, 0x5E, 0x63, 0x58, 0xD1, 0xA2, 0x25, 0x22, 0x7C, 0x3B, 0x01, 0x21, 0x78, 0x87, + 0xD4, 0x00, 0x46, 0x57, 0x9F, 0xD3, 0x27, 0x52, 0x4C, 0x36, 0x02, 0xE7, 0xA0, 0xC4, 0xC8, 0x9E, + 0xEA, 0xBF, 0x8A, 0xD2, 0x40, 0xC7, 0x38, 0xB5, 0xA3, 0xF7, 0xF2, 0xCE, 0xF9, 0x61, 0x15, 0xA1, + 0xE0, 0xAE, 0x5D, 0xA4, 0x9B, 0x34, 0x1A, 0x55, 0xAD, 0x93, 0x32, 0x30, 0xF5, 0x8C, 0xB1, 0xE3, + 0x1D, 0xF6, 0xE2, 0x2E, 0x82, 0x66, 0xCA, 0x60, 0xC0, 0x29, 0x23, 0xAB, 0x0D, 0x53, 0x4E, 0x6F, + 0xD5, 0xDB, 0x37, 0x45, 0xDE, 0xFD, 0x8E, 0x2F, 0x03, 0xFF, 0x6A, 0x72, 0x6D, 0x6C, 0x5B, 0x51, + 0x8D, 0x1B, 0xAF, 0x92, 0xBB, 0xDD, 0xBC, 0x7F, 0x11, 0xD9, 0x5C, 0x41, 0x1F, 0x10, 0x5A, 0xD8, + 0x0A, 0xC1, 0x31, 0x88, 0xA5, 0xCD, 0x7B, 0xBD, 0x2D, 0x74, 0xD0, 0x12, 0xB8, 0xE5, 0xB4, 0xB0, + 0x89, 0x69, 0x97, 0x4A, 0x0C, 0x96, 0x77, 0x7E, 0x65, 0xB9, 0xF1, 0x09, 0xC5, 0x6E, 0xC6, 0x84, + 0x18, 0xF0, 0x7D, 0xEC, 0x3A, 0xDC, 0x4D, 0x20, 0x79, 0xEE, 0x5F, 0x3E, 0xD7, 0xCB, 0x39, 0x48 +}; + +uint32_t SM4_FK[] = { + 0xA3B1BAC6, 0x56AA3350, 0x677D9197, 0xB27022DC +}; + +uint32_t SM4_CK[] = { + 0x00070E15, 0x1C232A31, 0x383F464D, 0x545B6269, + 0x70777E85, 0x8C939AA1, 0xA8AFB6BD, 0xC4CBD2D9, + 0xE0E7EEF5, 0xFC030A11, 0x181F262D, 0x343B4249, + 0x50575E65, 0x6C737A81, 0x888F969D, 0xA4ABB2B9, + 0xC0C7CED5, 0xDCE3EAF1, 0xF8FF060D, 0x141B2229, + 0x30373E45, 0x4C535A61, 0x686F767D, 0x848B9299, + 0xA0A7AEB5, 0xBCC3CAD1, 0xD8DFE6ED, 0xF4FB0209, + 0x10171E25, 0x2C333A41, 0x484F565D, 0x646B7279 +}; diff --git a/src/libscarv/block/sm4/sm4_share.h b/src/libscarv/block/sm4/sm4_share.h new file mode 100644 index 0000000..a597c2f --- /dev/null +++ b/src/libscarv/block/sm4/sm4_share.h @@ -0,0 +1,19 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#ifndef __LIBSCARV_SM4_SHARE_H +#define __LIBSCARV_SM4_SHARE_H + +#include +#include + +extern uint8_t SM4_SBOX[]; + +extern uint32_t SM4_FK[]; +extern uint32_t SM4_CK[]; + +#endif diff --git a/src/libscarv/scarv.h b/src/libscarv/scarv.h index 1f3f5de..9675930 100644 --- a/src/libscarv/scarv.h +++ b/src/libscarv/scarv.h @@ -9,6 +9,16 @@ #include #include #endif +#if ( LIBSCARV_CONF_SM4_ENABLE ) +#include +#include +#endif +#if ( LIBSCARV_CONF_PRINCE_ENABLE ) +#include +#endif +#if ( LIBSCARV_CONF_SPARX_ENABLE ) +#include +#endif #if ( LIBSCARV_CONF_SHA1_ENABLE ) #include @@ -42,8 +52,4 @@ #include #endif -#if ( LIBSCARV_CONF_PRINCE_ENABLE ) -#include -#endif - #endif diff --git a/src/libscarv/share/conf.h b/src/libscarv/share/conf.h index 3079c01..68824bc 100644 --- a/src/libscarv/share/conf.h +++ b/src/libscarv/share/conf.h @@ -29,6 +29,15 @@ /** Capture whether or not block/aes kernel is enabled (i.e., supported). */ #define LIBSCARV_CONF_AES_ENABLE ${LIBSCARV_CONF_AES_ENABLE} +/** Capture whether or not block/sm4 kernel is enabled (i.e., supported). + */ +#define LIBSCARV_CONF_SM4_ENABLE ${LIBSCARV_CONF_SM4_ENABLE} +/** Capture whether or not block/sm4 kernel is enabled (i.e., supported). + */ +#define LIBSCARV_CONF_PRINCE_ENABLE ${LIBSCARV_CONF_PRINCE_ENABLE} +/** Capture whether or not block/prince kernel is enabled (i.e., supported). + */ +#define LIBSCARV_CONF_SPARX_ENABLE ${LIBSCARV_CONF_SPARX_ENABLE} /** Capture whether or not hash/sha1 kernel is enabled (i.e., supported). */ @@ -126,6 +135,49 @@ /* ------------------------------------------------------------------------- */ +/** Use an external, architecture-specific implementation of @p sm4_enc. + */ +#define LIBSCARV_CONF_SM4_ENC_EXTERN ${LIBSCARV_CONF_SM4_ENC_EXTERN} +/** Use an external, architecture-specific implementation of @p sm4_dec. + */ +#define LIBSCARV_CONF_SM4_DEC_EXTERN ${LIBSCARV_CONF_SM4_DEC_EXTERN} + +/** Evolve round key in a forward direction (i.e., the starting point is the first round key). + */ +#define LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD ${LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD} +/** Evolve round key in a reverse direction (i.e., the starting point is the last round key). + */ +#define LIBSCARV_CONF_SM4_KEY_EVOLVE_REV ${LIBSCARV_CONF_SM4_KEY_EVOLVE_REV} +/** Pre-compute round keys (rather than evolve from starting point). + */ +#define LIBSCARV_CONF_SM4_KEY_PRECOMP ${LIBSCARV_CONF_SM4_KEY_PRECOMP} + +/* ------------------------------------------------------------------------- */ + +/** Use an external, architecture-specific implementation of @p prince_sbox. + */ +#define LIBSCARV_CONF_PRINCE_SBOX_EXTERN ${LIBSCARV_CONF_PRINCE_SBOX_EXTERN} + +/** Use an external, architecture-specific implementation of @p prince_isbox. + */ +#define LIBSCARV_CONF_PRINCE_ISBOX_EXTERN ${LIBSCARV_CONF_PRINCE_ISBOX_EXTERN} + +/** Use an external, architecture-specific implementation of @p prince_gf_mul. + */ +#define LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN ${LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN} + +/** Use an external, architecture-specific implementation of @p prince_m_prime_layer. + */ +#define LIBSCARV_CONF_PRINCE_MPRIME_EXTERN ${LIBSCARV_CONF_PRINCE_MPRIME_EXTERN} + +/* ------------------------------------------------------------------------- */ + +/** Use an external, architecture-specific implementation of the sparx L* functions. + */ +#define LIBSCARV_CONF_SPARX_L_EXTERN ${LIBSCARV_CONF_SPARX_L_EXTERN} + +/* ------------------------------------------------------------------------- */ + /** Use an external, architecture-specific implementation of @p sha1_comp. */ #define LIBSCARV_CONF_SHA1_COMP_EXTERN ${LIBSCARV_CONF_SHA1_COMP_EXTERN} @@ -217,30 +269,6 @@ */ #define LIBSCARV_CONF_CHACHA20_BLOCK_EXTERN ${LIBSCARV_CONF_CHACHA20_BLOCK_EXTERN} -/* ------------------------------------------------------------------------- */ - -/** Use an external, architecture-specific implementation of @p prince_sbox. - */ -#define LIBSCARV_CONF_PRINCE_SBOX_EXTERN ${LIBSCARV_CONF_PRINCE_SBOX_EXTERN} - -/** Use an external, architecture-specific implementation of @p prince_isbox. - */ -#define LIBSCARV_CONF_PRINCE_ISBOX_EXTERN ${LIBSCARV_CONF_PRINCE_ISBOX_EXTERN} - -/** Use an external, architecture-specific implementation of @p prince_gf_mul. - */ -#define LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN ${LIBSCARV_CONF_PRINCE_GF_MUL_EXTERN} - -/** Use an external, architecture-specific implementation of @p prince_m_prime_layer. - */ -#define LIBSCARV_CONF_PRINCE_MPRIME_EXTERN ${LIBSCARV_CONF_PRINCE_MPRIME_EXTERN} - -/* ------------------------------------------------------------------------- */ - -/** Use an external, architecture-specific implementation of the sparx L* functions. - */ -#define LIBSCARV_CONF_SPARX_L_EXTERN ${LIBSCARV_CONF_SPARX_L_EXTERN} - /* ========================================================================= */ /** Capture whether or not XCrypto implementation supports class 1 instructions. diff --git a/src/test/block/sm4/Makefile.in b/src/test/block/sm4/Makefile.in new file mode 100644 index 0000000..bbea6f3 --- /dev/null +++ b/src/test/block/sm4/Makefile.in @@ -0,0 +1,16 @@ +# Copyright (C) 2019 SCARV project +# +# Use of this source code is restricted per the MIT license, a copy of which +# can be found at https://opensource.org/licenses/MIT (or should be included +# as LICENSE.txt within the associated archive or repository). + +KERNEL = block/sm4 + +KERNEL_PATHS = ${REPO_HOME}/src/test/share +KERNEL_PATHS += ${REPO_HOME}/src/test/${KERNEL} + +KERNEL_HEADERS = $(foreach PATH,${KERNEL_PATHS},$(wildcard ${PATH}/*.h)) +KERNEL_SOURCES = $(foreach PATH,${KERNEL_PATHS},$(wildcard ${PATH}/*.c)) +KERNEL_SOURCES += $(foreach PATH,${KERNEL_PATHS},$(wildcard ${PATH}/*.S)) + +$(eval $(call build,${KERNEL},${KERNEL_PATHS},${KERNEL_HEADERS},${KERNEL_SOURCES})) diff --git a/src/test/block/sm4/test_sm4.c b/src/test/block/sm4/test_sm4.c new file mode 100644 index 0000000..6735a12 --- /dev/null +++ b/src/test/block/sm4/test_sm4.c @@ -0,0 +1,119 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#include "test_sm4.h" + +// ============================================================================ + +void test_sm4_dump( char* id, uint8_t* x, int l_x ) { + printf( "%s = binascii.a2b_hex( '", id ); test_dump_seq( x, l_x, DUMP_LSB ); printf( "' )\n" ); +} + +// ============================================================================ + +test_sm4_kat_t test_sm4_kat[] = { { + .m = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, + .c = { 0x68, 0x1e, 0xdf, 0x34, 0xd2, 0x06, 0x96, 0x5e, 0x86, 0xb3, 0xe9, 0x4f, 0x53, 0x6e, 0x42, 0x46 }, + #if ( LIBSCARV_CONF_SM4_KEY_EVOLVE_FWD ) + .k_enc = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, + .k_dec = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 } + #endif + #if ( LIBSCARV_CONF_SM4_KEY_EVOLVE_REV ) + .k_enc = { 0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef, 0xfe, 0xdc, 0xba, 0x98, 0x76, 0x54, 0x32, 0x10 }, + .k_dec = { 0x42, 0x8D, 0x36, 0x54, 0x62, 0x29, 0x34, 0x96, 0x01, 0xCF, 0x72, 0xE5, 0x91, 0x24, 0xA0, 0x12 } + #endif + } +}; + +void test_sm4_enc( int trials ) { + uint8_t t[ 16 ], c[ 16 ], m[ 16 ], k[ 16 ]; + + #if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) + uint32_t rk[ 32 ]; + #endif + + for( int i = 1; i <= trials; i++ ) { + test_id( "test_sm4", "enc", i, trials ); + + memcpy( m, test_sm4_kat[ i - 1 ].m, 16 * SIZEOF( uint8_t ) ); + memcpy( c, test_sm4_kat[ i - 1 ].c, 16 * SIZEOF( uint8_t ) ); + memcpy( k, test_sm4_kat[ i - 1 ].k_enc, 16 * SIZEOF( uint8_t ) ); + + test_sm4_dump( "m", m, 16 ); + test_sm4_dump( "k", k, 16 ); + + #if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) + sm4_enc_exp( rk, k ); MEASURE( sm4_enc( t, m, ( uint8_t* )( rk ) ) ); + #else + MEASURE( sm4_enc( t, m, k ) ); + #endif + + test_sm4_dump( "t", t, 16 ); + test_sm4_dump( "c", c, 16 ); + + printf( "if ( c != t ) : " "\n" ); + printf( " print( 'fail %%s' %% ( id ) )" "\n" ); + printf( " print( 'm == %%s' %% ( binascii.b2a_hex( m ) ) )" "\n" ); + printf( " print( 'k == %%s' %% ( binascii.b2a_hex( k ) ) )" "\n" ); + printf( " print( 'c == %%s' %% ( binascii.b2a_hex( c ) ) )" "\n" ); + printf( " print( ' != %%s' %% ( binascii.b2a_hex( t ) ) )" "\n" ); + + printf( " sys.exit( 1 ) " "\n\n" ); + } +} + +void test_sm4_dec( int trials ) { + uint8_t m[ 16 ], c[ 16 ], k[ 16 ], t[ 16 ]; + + #if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) + uint32_t rk[ 32 ]; + #endif + + for( int i = 1; i <= trials; i++ ) { + test_id( "test_sm4", "dec", i, trials ); + + memcpy( m, test_sm4_kat[ i - 1 ].m, 16 * SIZEOF( uint8_t ) ); + memcpy( c, test_sm4_kat[ i - 1 ].c, 16 * SIZEOF( uint8_t ) ); + memcpy( k, test_sm4_kat[ i - 1 ].k_dec, 16 * SIZEOF( uint8_t ) ); + + test_sm4_dump( "c", c, 16 ); + test_sm4_dump( "k", k, 16 ); + + #if ( LIBSCARV_CONF_SM4_KEY_PRECOMP ) + sm4_dec_exp( rk, k ); MEASURE( sm4_dec( t, c, ( uint8_t* )( rk ) ) ); + #else + MEASURE( sm4_dec( t, c, k ) ); + #endif + + test_sm4_dump( "t", t, 16 ); + test_sm4_dump( "m", m, 16 ); + + printf( "if ( m != t ) : " "\n" ); + printf( " print( 'fail %%s' %% ( id ) )" "\n" ); + printf( " print( 'c == %%s' %% ( binascii.b2a_hex( c ) ) )" "\n" ); + printf( " print( 'k == %%s' %% ( binascii.b2a_hex( k ) ) )" "\n" ); + printf( " print( 'm == %%s' %% ( binascii.b2a_hex( m ) ) )" "\n" ); + printf( " print( ' != %%s' %% ( binascii.b2a_hex( t ) ) )" "\n" ); + + printf( " sys.exit( 1 ) " "\n\n" ); + } +} + +// ============================================================================ + +int main( int argc, char* argv[] ) { + test_init( argc, argv, "sys, binascii" ); + + test_sm4_enc( TEST_SM4_KAT_COUNT ); + test_sm4_dec( TEST_SM4_KAT_COUNT ); + + test_fini(); + + return 0; +} + +// ============================================================================ diff --git a/src/test/block/sm4/test_sm4.h b/src/test/block/sm4/test_sm4.h new file mode 100644 index 0000000..d357e4d --- /dev/null +++ b/src/test/block/sm4/test_sm4.h @@ -0,0 +1,22 @@ +/* Copyright (C) 2019 SCARV project + * + * Use of this source code is restricted per the MIT license, a copy of which + * can be found at https://opensource.org/licenses/MIT (or should be included + * as LICENSE.txt within the associated archive or repository). + */ + +#ifndef __TEST_SM4_H +#define __TEST_SM4_H + +#include "util.h" + +#include "scarv/block/sm4/sm4_enc.h" +#include "scarv/block/sm4/sm4_dec.h" + +#define TEST_SM4_KAT_COUNT ( 1 ) + +typedef struct { + uint8_t m[ 16 ], c[ 16 ], k_enc[ 16 ], k_dec[ 16 ]; +} test_sm4_kat_t; + +#endif