From 6a49c8adb8165bb53d7a6cdd9c044b8b528b8872 Mon Sep 17 00:00:00 2001 From: Hanno Becker Date: Sun, 22 Dec 2024 06:31:19 +0000 Subject: [PATCH] Add META.sh file to extract data from META.json This commit adds a basic script META.sh to query the META.json. If jq is present, it's used for the query. Otherwise, some basic fiddling with cat, grep, cut, and tr is used. This is used to conduct a bash-based KAT test in `make quickcheck`, rather then running the test scripts, which already require a Python setup. Signed-off-by: Hanno Becker --- META.sh | 44 ++++++++++++++++++++++++++++++++++++++++++++ Makefile | 7 ++++--- 2 files changed, 48 insertions(+), 3 deletions(-) create mode 100755 META.sh diff --git a/META.sh b/META.sh new file mode 100755 index 000000000..fc1c0f307 --- /dev/null +++ b/META.sh @@ -0,0 +1,44 @@ +#!/bin/bash + +# Helper script to query META.json +# +# Arguments +# - Scheme to query: ML-KEM-512, ML-KEM-768, ML-KEM-1024 +# - Field to query, e.g. "kat-sha256" +# +# Optional: +# - Value to compare against + +META=META.json + +# Manual extraction of metadata with basic cmd line tools +VAL=$(cat $META | + grep "name\|\"$2\"" | + grep $1 -A 1 | + grep $2 | + cut -d ":" -f 2 | + tr -d '", ') + +# More robust extraction using jq +if (which jq 2>&1 >/dev/null); then + QUERY=".implementations | .[] | select(.name==\"$1\") | .\"$2\"" + VAL_JQ=$(cat $META | jq "$QUERY" -r) + + if [[ $VAL_JQ != $VAL ]]; then + echo "ERROR parsing metadata file $META" + exit 1 + fi +fi + +INPUT=$3 +if [[ "$INPUT" != "" ]]; then + if [[ "$INPUT" != "$VAL" ]]; then + echo "$META $1 $2: FAIL ($VAL != $INPUT)" + exit 1 + else + echo "$META $1 $2: OK" + exit 0 + fi +else + echo $VAL +fi diff --git a/Makefile b/Makefile index a24c40245..2f0da929a 100644 --- a/Makefile +++ b/Makefile @@ -14,12 +14,13 @@ buildall: mlkem nistkat kat acvp $(Q)echo " Everything builds fine!" quickcheck: buildall - # Run basic functionality checks $(MLKEM512_DIR)/bin/test_mlkem512 $(MLKEM768_DIR)/bin/test_mlkem768 $(MLKEM1024_DIR)/bin/test_mlkem1024 - ./scripts/acvp - $(Q)echo " Functionality and ACVP tests passed!" + + $(MLKEM512_DIR)/bin/gen_KAT512 | sha256sum | cut -d " " -f 1 | xargs ./META.sh ML-KEM-512 kat-sha256 + $(MLKEM768_DIR)/bin/gen_KAT768 | sha256sum | cut -d " " -f 1 | xargs ./META.sh ML-KEM-768 kat-sha256 + $(MLKEM1024_DIR)/bin/gen_KAT1024 | sha256sum | cut -d " " -f 1 | xargs ./META.sh ML-KEM-1024 kat-sha256 lib: $(BUILD_DIR)/libmlkem.a