From 1b7b955fb449b987d564c9bcf65c51d4dd2eba16 Mon Sep 17 00:00:00 2001 From: cmomin Date: Mon, 30 Sep 2024 17:48:51 +0200 Subject: [PATCH 1/4] Add top level Makefile --- .gitignore | 2 +- Makefile | 58 ++++++++++++++++++++++ func_tests/Makefile | 11 ++-- func_tests/requirements.txt | 2 + hdl/gather_sources.sh => gather_sources.sh | 0 hdl/Makefile | 39 --------------- sboxes-compress/Makefile | 22 ++++++-- sboxes-compress/compress | 2 +- 8 files changed, 85 insertions(+), 51 deletions(-) create mode 100644 Makefile rename hdl/gather_sources.sh => gather_sources.sh (100%) delete mode 100644 hdl/Makefile diff --git a/.gitignore b/.gitignore index cdf1e92..370e0be 100644 --- a/.gitignore +++ b/.gitignore @@ -5,7 +5,7 @@ beh_simu/tvs/generated docs/SMAesH.pdf - +work # Created by https://www.toptal.com/developers/gitignore/api/latex # Edit at https://www.toptal.com/developers/gitignore?templates=latex diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..76a50c9 --- /dev/null +++ b/Makefile @@ -0,0 +1,58 @@ +# Use in order to use the compress-optimized sbox. +NSHARES?=2 +WORK?=work/d$(NSHARES) +WORKDIR=$(abspath $(WORK)) + +# Python Venv +SHELL=/bin/bash +VE=$(abspath $(WORKDIR)/ve) +VE_INSTALLED=$(VE)/installed +PYTHON_VE=source $(VE)/bin/activate + +# Directory created containing all the HDL files +DIR_HDL?=$(WORKDIR)/hdl +HDL_DONE = $(DIR_HDL)/.gather + +# path where the COMPRESS tool is located (root of the git) +DIR_COMPRESS_ROOT?=./sboxes-compress/compress + +##################" +COMPRESS_WORKDIR=$(WORKDIR)/sbox +SBOX_FILE=$(COMPRESS_WORKDIR)/circuits/canright_aes_sbox_dual_d$(NSHARES)_l4/design.v + +DIR_COMPRESS_GADGETS=$(DIR_COMPRESS_ROOT)/gadget_library +DIR_SMAESH_HDL=hdl/smaesh_hpc + +.PHONY: sbox hdl + +$(VE)/pyvenv.cfg: + mkdir -p $(WORKDIR) + python3 -m venv $(VE) + +$(VE_INSTALLED): $(VE)/pyvenv.cfg + ${PYTHON_VE}; python -m pip install -r func_tests/requirements.txt + touch $(VE_INSTALLED) + +$(SBOX_FILE): + make -C sboxes-compress WORK=$(COMPRESS_WORKDIR) DS=$(NSHARES) + +sbox: $(SBOX_FILE) + +$(HDL_DONE): $(SBOX_FILE) + OUT_DIR=${DIR_HDL} ./gather_sources.sh $(DIR_SMAESH_HDL) $(DIR_COMPRESS_GADGETS)/BIN $(DIR_COMPRESS_GADGETS)/MSK + cp $(SBOX_FILE) $(DIR_HDL)/canright_aes_sbox_dual.v + cp $(dir $(SBOX_FILE))/design.vh $(DIR_HDL) + echo "\`define DEFAULTSHARES ${NSHARES}" > $(DIR_HDL)/architecture_default.vh + touch $(HDL_DONE) + +hdl: $(HDL_DONE) + +FUNC_RESULTS=$(WORKDIR)/functests/results.xml +$(FUNC_RESULTS): $(VE_INSTALLED) $(HDL_DONE) + $(PYTHON_VE); make -C func_tests NSHARES=$(NSHARES) WORK_CASE=$(WORKDIR)/functests RTL_DIR_HDL=$(DIR_HDL) simu + +func-tests: $(FUNC_RESULTS) + + +clean: + if [ -d $(DIR_HDL) ]; then rm -r $(DIR_HDL); fi diff --git a/func_tests/Makefile b/func_tests/Makefile index 6413ab3..9bfac59 100644 --- a/func_tests/Makefile +++ b/func_tests/Makefile @@ -8,7 +8,7 @@ RTL_DIR_HDL?=$(WORK_CASE)/smaesh_hdl_d$(NSHARES) ####### Verilator related VERILATOR_MDIR=$(WORK_CASE)/verilator -VERILATOR_BUILD_PROC?=8 +VERILATOR_BUILD_PROC?=1 ####### COCOTB related # defaults @@ -19,21 +19,20 @@ export TOPLEVEL_LANG ?= verilog export SIM_BUILD ?= $(WORK_CASE) # Sources related export VERILOG_INCLUDE_DIRS?= $(RTL_DIR_HDL) -export VERILOG_SOURCES ?= $(shell ls $(RTL_DIR_HDL)/*.v) +export VERILOG_SOURCES ?= $(RTL_DIR_HDL)/*.v # TOPLEVEL is the name of the toplevel module in your Verilog or VHDL file export TOPLEVEL ?= smaesh_hpc # MODULE is the basename of the Python test file export MODULE ?= functionnal_tests # Logging level export COCOTB_LOG_LEVEL ?= INFO +# Results +export COCOTB_RESULTS_FILE = $(WORK_CASE)/result.xml # include cocotb's make rules to take care of the simulator setup COCOTB_DIR_MAKEFILES=$(shell cocotb-config --makefiles) -$(RTL_DIR_HDL): - NSHARES=$(NSHARES) DIR_HDL=$(RTL_DIR_HDL) make -C ../hdl all - -simu: $(RTL_DIR_HDL) +simu: make -f $(COCOTB_DIR_MAKEFILES)/Makefile.sim clean: diff --git a/func_tests/requirements.txt b/func_tests/requirements.txt index 9de64d2..3da3675 100644 --- a/func_tests/requirements.txt +++ b/func_tests/requirements.txt @@ -1 +1,3 @@ cocotb +numpy +cryptography diff --git a/hdl/gather_sources.sh b/gather_sources.sh similarity index 100% rename from hdl/gather_sources.sh rename to gather_sources.sh diff --git a/hdl/Makefile b/hdl/Makefile deleted file mode 100644 index af5594d..0000000 --- a/hdl/Makefile +++ /dev/null @@ -1,39 +0,0 @@ -# Use in order to use the compress-optimized sbox. -NSHARES?=2 - -# Directory created containing all the HDL files -DIR_HDL?=smaesh-hdl-d${NSHARES} - -# path where the COMPRESS tool is located (root of the git) -DIR_COMPRESS_ROOT?=../sboxes-compress/compress - -##################" -DIR_COMPRESS_GADGETS=$(DIR_COMPRESS_ROOT)/gadget_library -DIR_SBOX=sbox-d${NSHARES} -DIR_SBOX_COMPRESS=../sboxes-compress/work/circuits/canright_aes_sbox_dual_d${NSHARES}_l4 - -DIR_SMAESH_HDL=./smaesh_hpc - -.PHONY: all - -help: - @echo "Generation of a single directory containing the synthetisable SMAesH HDL files." - @echo "Variables:" - @echo " NSHARES: Amount of shares for optimal Sbox architecture." - @echo " DIR_HDL: Path to the generated directory" - @echo " DIR_COMPRESS_ROOT: Path to the COMPRESS repository." - @echo "Targets:" - @echo " all: Generate the dir" - -all: $(DIR_HDL) gather - -$(DIR_HDL): - mkdir -p $(DIR_HDL) - -gather: - OUT_DIR=${DIR_HDL} ./gather_sources.sh $(DIR_SMAESH_HDL) $(DIR_SBOX) $(DIR_COMPRESS_GADGETS)/BIN $(DIR_COMPRESS_GADGETS)/MSK - cp $(DIR_SBOX_COMPRESS)/canright_aes_sbox_dual.v $(DIR_HDL) - cp $(DIR_SBOX_COMPRESS)/design.vh $(DIR_HDL) - -clean: - if [ -d $(DIR_HDL) ]; then rm -r $(DIR_HDL); fi diff --git a/sboxes-compress/Makefile b/sboxes-compress/Makefile index 7f5d8c1..ea0e88c 100644 --- a/sboxes-compress/Makefile +++ b/sboxes-compress/Makefile @@ -6,6 +6,12 @@ CIRCUIT=$(WORK)/$(MODULE_NAME).txt LATS ?= 4 DS ?= 2 3 4 +# Python Venv +SHELL=/bin/bash +VE=$(abspath $(WORK)/ve) +VE_INSTALLED=$(VE)/installed +PYTHON_VE=source $(VE)/bin/activate + AREA_REPORT = $(WORK)/$(MODULE_NAME)_area.csv BEH_SIMU_TB ?= tb_sbox_dual.v @@ -15,6 +21,14 @@ BEH_SIMU_DIR ?= $(WORK)/beh-simu all: $(AREA_REPORT) +$(VE)/pyvenv.cfg: + mkdir -p work + python3 -m venv $(VE) + +$(VE_INSTALLED): $(VE)/pyvenv.cfg + ${PYTHON_VE}; python -m pip install -r compress/requirements.txt + touch $(VE_INSTALLED) + clean: -rm -r $(WORK) / 2> /dev/null @@ -23,14 +37,14 @@ $(YOSYS_NETLIST): $(FILE_IN) build-json-yosys.tcl mkdir -p $(WORK) FILE_IN=$(FILE_IN) YOSYS_NETLIST=$(YOSYS_NETLIST) yosys -c build-json-yosys.tcl -$(CIRCUIT): $(YOSYS_NETLIST) - python compress/scripts/yosys2compress.py \ +$(CIRCUIT): $(VE_INSTALLED) $(YOSYS_NETLIST) + ${PYTHON_VE}; python compress/scripts/yosys2compress.py \ --netlist-file $(YOSYS_NETLIST) \ --top canright_aes_sbox_dual \ --compress-file $(CIRCUIT) -$(AREA_REPORT): $(CIRCUIT) - make -C compress CIRCUIT="$(abspath $(CIRCUIT))" LATS="$(LATS)" DS="$(DS)" WORK=$(abspath $(WORK)) area +$(AREA_REPORT): $(VE_INSTALLED) $(CIRCUIT) + ${PYTHON_VE}; make -C compress CIRCUIT="$(abspath $(CIRCUIT))" LATS="$(LATS)" DS="$(DS)" WORK=$(abspath $(WORK)) area $(BEH_SIMU_DIR): mkdir -p $(BEH_SIMU_DIR) diff --git a/sboxes-compress/compress b/sboxes-compress/compress index 08529a7..85647a8 160000 --- a/sboxes-compress/compress +++ b/sboxes-compress/compress @@ -1 +1 @@ -Subproject commit 08529a78fa5af70c6a35161d68f8fbbb332abcc6 +Subproject commit 85647a8998271988e6b01b5ea729ddea065d8568 From 9e56ba00bd587914068f46f4aa87e82a64cd224e Mon Sep 17 00:00:00 2001 From: cmomin Date: Mon, 30 Sep 2024 18:04:22 +0200 Subject: [PATCH 2/4] Add CI for functionnal test Also ensure that test failures with cocotb result in error at top level Makefile --- .github/workflows/ci.yml | 14 ++++++++++++++ Makefile | 14 ++++++++++---- 2 files changed, 24 insertions(+), 4 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ac00f1e..ddc65aa 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -44,3 +44,17 @@ jobs: release/* env: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} + + functest: + runs-on: ubuntu-latest + steps: + - name: Check out repository code + uses: actions/checkout@v4 + with: + submodules: true + - run: sudo apt-get install -y iverilog yosys + - run: make func-tests + + + + diff --git a/Makefile b/Makefile index 76a50c9..32002ca 100644 --- a/Makefile +++ b/Makefile @@ -47,11 +47,17 @@ $(HDL_DONE): $(SBOX_FILE) hdl: $(HDL_DONE) -FUNC_RESULTS=$(WORKDIR)/functests/results.xml -$(FUNC_RESULTS): $(VE_INSTALLED) $(HDL_DONE) - $(PYTHON_VE); make -C func_tests NSHARES=$(NSHARES) WORK_CASE=$(WORKDIR)/functests RTL_DIR_HDL=$(DIR_HDL) simu +# Functionnal testing +FUNC_LOG=$(WORKDIR)/functests/simu.log +FUNC_SUCCESS=$(WORKDIR)/functests/success +$(FUNC_LOG): $(VE_INSTALLED) $(HDL_DONE) + $(PYTHON_VE); make -C func_tests NSHARES=$(NSHARES) WORK_CASE=$(WORKDIR)/functests RTL_DIR_HDL=$(DIR_HDL) simu | tee $@ -func-tests: $(FUNC_RESULTS) +# Mark simulation success (simulation always return a zero exit code). +%/success: %/simu.log + grep -q -s FAIL=0 $< && touch $@ || exit 1 + +func-tests: $(FUNC_SUCCESS) clean: From 79d796fce8295495db810aaf9af8e6985530b739 Mon Sep 17 00:00:00 2001 From: cmomin Date: Tue, 1 Oct 2024 09:35:38 +0200 Subject: [PATCH 3/4] Fix HDL + move to verilator for functests + fix top-Level Makefile for func tests verification --- Makefile | 3 +- func_tests/Makefile | 4 +- func_tests/functionnal_tests.py | 84 ++++++++++----------- hdl/smaesh_hpc/MSKaes_32bits_key_datapath.v | 2 +- hdl/smaesh_hpc/MSKkey_holder.v | 4 +- 5 files changed, 48 insertions(+), 49 deletions(-) diff --git a/Makefile b/Makefile index 32002ca..351b31d 100644 --- a/Makefile +++ b/Makefile @@ -51,7 +51,8 @@ hdl: $(HDL_DONE) FUNC_LOG=$(WORKDIR)/functests/simu.log FUNC_SUCCESS=$(WORKDIR)/functests/success $(FUNC_LOG): $(VE_INSTALLED) $(HDL_DONE) - $(PYTHON_VE); make -C func_tests NSHARES=$(NSHARES) WORK_CASE=$(WORKDIR)/functests RTL_DIR_HDL=$(DIR_HDL) simu | tee $@ + mkdir $(dir $(FUNC_LOG)) + $(PYTHON_VE); make -C func_tests NSHARES=$(NSHARES) WORK_CASE=$(WORKDIR)/functests RTL_DIR_HDL=$(DIR_HDL) simu | tee $@ # Mark simulation success (simulation always return a zero exit code). %/success: %/simu.log diff --git a/func_tests/Makefile b/func_tests/Makefile index 9bfac59..e8cd727 100644 --- a/func_tests/Makefile +++ b/func_tests/Makefile @@ -12,8 +12,8 @@ VERILATOR_BUILD_PROC?=1 ####### COCOTB related # defaults -export SIM ?= icarus -#export EXTRA_ARGS ?= -Wno-NULLPORT -Wno-PINMISSING -Wno-WIDTH -j $(VERILATOR_BUILD_PROC) +export SIM ?= verilator +export EXTRA_ARGS ?= -Wno-NULLPORT -Wno-PINMISSING -Wno-WIDTH -j $(VERILATOR_BUILD_PROC) #--trace --trace-structs export TOPLEVEL_LANG ?= verilog # Scratch directory export SIM_BUILD ?= $(WORK_CASE) diff --git a/func_tests/functionnal_tests.py b/func_tests/functionnal_tests.py index f53bf22..7719f29 100644 --- a/func_tests/functionnal_tests.py +++ b/func_tests/functionnal_tests.py @@ -368,48 +368,48 @@ async def run(self): ############################################################ #### TESTS ################################################# -## Simple tests for KATs verif first -### Test for the AES-128 encryption KAT -#@cocotb.test() -#async def AES_128_ENC_KAT(dut): -# # Load the test cases -# list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_128_FILES) -# await AES_BC_ENC_TEMPLATE(dut, list_cases, 50000, NSHARES) -# -## Test for the AES-128 execution -#@cocotb.test() -#async def AES_128_DEC_KAT(dut): -# # Load the test cases -# list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_128_FILES) -# await AES_BC_DEC_TEMPLATE(dut, list_cases, 150000, NSHARES) -# -## Test for the AES-92 encryption KAT -#@cocotb.test() -#async def AES_192_ENC_KAT(dut): -# # Load the test cases -# list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_192_FILES) -# await AES_BC_ENC_TEMPLATE(dut, list_cases, 100000, NSHARES) -# -## Test for the AES-128 execution -#@cocotb.test() -#async def AES_192_DEC_KAT(dut): -# # Load the test cases -# list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_192_FILES) -# await AES_BC_DEC_TEMPLATE(dut, list_cases, 300000, NSHARES) -# -## Test for the AES-92 encryption KAT -#@cocotb.test() -#async def AES_256_ENC_KAT(dut): -# # Load the test cases -# list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_256_FILES) -# await AES_BC_ENC_TEMPLATE(dut, list_cases, 100000, NSHARES) -# -## Test for the AES-128 execution -#@cocotb.test() -#async def AES_256_DEC_KAT(dut): -# # Load the test cases -# list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_256_FILES) -# await AES_BC_DEC_TEMPLATE(dut, list_cases, 300000, NSHARES) +# Simple tests for KATs verif first +## Test for the AES-128 encryption KAT +@cocotb.test() +async def AES_128_ENC_KAT(dut): + # Load the test cases + list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_128_FILES) + await AES_BC_ENC_TEMPLATE(dut, list_cases, 50000, NSHARES) + +# Test for the AES-128 execution +@cocotb.test() +async def AES_128_DEC_KAT(dut): + # Load the test cases + list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_128_FILES) + await AES_BC_DEC_TEMPLATE(dut, list_cases, 150000, NSHARES) + +# Test for the AES-92 encryption KAT +@cocotb.test() +async def AES_192_ENC_KAT(dut): + # Load the test cases + list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_192_FILES) + await AES_BC_ENC_TEMPLATE(dut, list_cases, 100000, NSHARES) + +# Test for the AES-128 execution +@cocotb.test() +async def AES_192_DEC_KAT(dut): + # Load the test cases + list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_192_FILES) + await AES_BC_DEC_TEMPLATE(dut, list_cases, 300000, NSHARES) + +# Test for the AES-92 encryption KAT +@cocotb.test() +async def AES_256_ENC_KAT(dut): + # Load the test cases + list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_256_FILES) + await AES_BC_ENC_TEMPLATE(dut, list_cases, 100000, NSHARES) + +# Test for the AES-128 execution +@cocotb.test() +async def AES_256_DEC_KAT(dut): + # Load the test cases + list_cases = utils_KAT.load_AES_BC_KAT_files(utils_KAT.KAT_AES_BC_256_FILES) + await AES_BC_DEC_TEMPLATE(dut, list_cases, 300000, NSHARES) ########## Test to validate basic functionality of the KSU diff --git a/hdl/smaesh_hpc/MSKaes_32bits_key_datapath.v b/hdl/smaesh_hpc/MSKaes_32bits_key_datapath.v index 946bd36..a5a4921 100644 --- a/hdl/smaesh_hpc/MSKaes_32bits_key_datapath.v +++ b/hdl/smaesh_hpc/MSKaes_32bits_key_datapath.v @@ -42,7 +42,7 @@ module MSKaes_32bits_key_datapath sh_4bytes_rot_to_SB, sh_4bytes_from_SB, sh_4bytes_to_AK, - sh_3bytes_to_AK_inverse, + sh_3bytes_to_AK_inverse ); input clk; diff --git a/hdl/smaesh_hpc/MSKkey_holder.v b/hdl/smaesh_hpc/MSKkey_holder.v index 7820037..3e58cd0 100644 --- a/hdl/smaesh_hpc/MSKkey_holder.v +++ b/hdl/smaesh_hpc/MSKkey_holder.v @@ -387,7 +387,6 @@ always@(*) begin end IN_REFRESH: begin in_refresh = 1; - ll_enable_from_ksched = 1; if(rnd_rfrsh_in_valid) begin inc_count_words = 1; ll_enable_mask = 1; @@ -408,7 +407,7 @@ always@(*) begin // LL data holder ll_fetch_in = 0; // Mux taking data from input instead of refresh shift - ll_enable_from_ksched = 0; // data at the input comes from the key scheduling + ll_enable_from_ksched = in_fetch_last_key | in_refresh; // data at the input comes from the key scheduling enforce_data_in_zero = 0; // Logic for branch_compute_last @@ -418,7 +417,6 @@ always@(*) begin branch_compute_last = IDLE; end - ll_enable_from_ksched = in_fetch_last_key; data_in_from_buffer = in_fetch_from_buffer; fetch_key_lcol_high = word_idx[0]; if (in_padding) begin From af66e7953f36c60262dd10c1a3d18084042eca7f Mon Sep 17 00:00:00 2001 From: cmomin Date: Tue, 1 Oct 2024 09:37:43 +0200 Subject: [PATCH 4/4] Use ubuntu-24.04 and add verilator support for func tests CI --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index ddc65aa..bfdbd55 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -46,13 +46,13 @@ jobs: GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} functest: - runs-on: ubuntu-latest + runs-on: ubuntu-24.04 steps: - name: Check out repository code uses: actions/checkout@v4 with: submodules: true - - run: sudo apt-get install -y iverilog yosys + - run: sudo apt-get install -y yosys verilator iverilog - run: make func-tests