From b128a3efc39c6e9307ef31e3e8a6b2e0bcd98079 Mon Sep 17 00:00:00 2001 From: makejian Date: Thu, 20 Apr 2023 20:34:47 +0800 Subject: [PATCH] Porting NIST-Statistical-Test-Suite in nuttx to test quality of rng (1)add nist-sts init nuttx (2)Fix possible memory bug (3)Fix double free memory Signed-off-by: makejian --- .gitignore | 1 + ...-memory-out-of-bounds-problem-in-sts.patch | 27 ++++++++ ...sh-in-running-a-single-test-after-ru.patch | 37 +++++++++++ testing/nist-sts/Kconfig | 30 +++++++++ testing/nist-sts/Make.defs | 23 +++++++ testing/nist-sts/Makefile | 63 +++++++++++++++++++ 6 files changed, 181 insertions(+) create mode 100644 testing/nist-sts/0001-Solve-the-memory-out-of-bounds-problem-in-sts.patch create mode 100644 testing/nist-sts/0002-Fix-bug-that-crash-in-running-a-single-test-after-ru.patch create mode 100644 testing/nist-sts/Kconfig create mode 100644 testing/nist-sts/Make.defs create mode 100644 testing/nist-sts/Makefile diff --git a/.gitignore b/.gitignore index 794f52e36c..93d90cc3de 100644 --- a/.gitignore +++ b/.gitignore @@ -37,3 +37,4 @@ Make.dep .dirlinks .vscode .DS_Store +testing/nist-sts/* diff --git a/testing/nist-sts/0001-Solve-the-memory-out-of-bounds-problem-in-sts.patch b/testing/nist-sts/0001-Solve-the-memory-out-of-bounds-problem-in-sts.patch new file mode 100644 index 0000000000..89e113d03d --- /dev/null +++ b/testing/nist-sts/0001-Solve-the-memory-out-of-bounds-problem-in-sts.patch @@ -0,0 +1,27 @@ +From 530b8e0d0f9ea68f1dd977caddc76fbe4a2c66b8 Mon Sep 17 00:00:00 2001 +From: makejian +Date: Wed, 19 Apr 2023 22:57:44 +0800 +Subject: [PATCH 1/2] Solve the memory out of bounds problem in sts + +Signed-off-by: makejian +Change-Id: I28a132a793603205e25e5925de3e51ff2c44873b +--- + sts/src/discreteFourierTransform.c | 2 +- + 1 file changed, 1 insertion(+), 1 deletion(-) + +diff --git nist-sts/sts/src/discreteFourierTransform.c nist-sts/sts/src/discreteFourierTransform.c +index 61b52c2..96b4bb7 100644 +--- nist-sts/sts/src/discreteFourierTransform.c ++++ nist-sts/sts/src/discreteFourierTransform.c +@@ -38,7 +38,7 @@ DiscreteFourierTransform(int n) + + m[0] = sqrt(X[0]*X[0]); /* COMPUTE MAGNITUDE */ + +- for ( i=0; i +Date: Tue, 16 May 2023 21:23:20 +0800 +Subject: [PATCH 2/2] Fix bug that crash in running a single test after running all + tests + +double free same memory +Signed-off-by: makejian +Change-Id: Ie56dabd6d6eae93c0b066ced6148ea362f1c6085 +--- + sts/src/assess.c | 8 ++++++-- + 1 file changed, 6 insertions(+), 2 deletions(-) + +diff --git nist-sts/sts/src/assess.c nist-sts/sts/src/assess.c +index cf41d4f..3aa5800 100644 +--- nist-sts/sts/src/assess.c ++++ nist-sts/sts/src/assess.c +@@ -84,10 +84,14 @@ main(int argc, char *argv[]) + invokeTestSuite(option, streamFile); + fclose(freqfp); + for( i=1; i<=NUMOFTESTS; i++ ) { +- if ( stats[i] != NULL ) ++ if ( stats[i] != NULL ) { + fclose(stats[i]); +- if ( results[i] != NULL ) ++ stats[i] = NULL; ++ } ++ if ( results[i] != NULL ) { + fclose(results[i]); ++ results[i] = NULL; ++ } + } + if ( (testVector[0] == 1) || (testVector[TEST_CUSUM] == 1) ) + partitionResultFile(2, tp.numOfBitStreams, option, TEST_CUSUM); +-- +2.40.0 + diff --git a/testing/nist-sts/Kconfig b/testing/nist-sts/Kconfig new file mode 100644 index 0000000000..1d28cbddff --- /dev/null +++ b/testing/nist-sts/Kconfig @@ -0,0 +1,30 @@ +# +# For a description of the syntax of this configuration file, +# see the file kconfig-language.txt in the NuttX tools repository. +# + +config TESTING_NIST_STS + tristate "NIST Statistical Test Suite" + default n + ---help--- + https://csrc.nist.gov/projects/random-bit-generation/documentation-and-software + +if TESTING_NIST_STS + +config TESTING_NIST_STS_PROGNAME + string "Program name" + default "nist_sts" + +config TESTING_NIST_STS_PRIORITY + int "nist_sts task priority" + default 100 + +config TESTING_NIST_STS_STACKSIZE + int "nist_sts stack size" + default 8192 + +config TESTING_NIST_STS_VERSION + string "nist_sts version" + default "2_1_2" + +endif diff --git a/testing/nist-sts/Make.defs b/testing/nist-sts/Make.defs new file mode 100644 index 0000000000..ba3894344e --- /dev/null +++ b/testing/nist-sts/Make.defs @@ -0,0 +1,23 @@ +############################################################################ +# apps/testing/nist-sts/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +ifneq ($(CONFIG_TESTING_NIST_STS),) +CONFIGURED_APPS += $(APPDIR)/testing/nist-sts +endif diff --git a/testing/nist-sts/Makefile b/testing/nist-sts/Makefile new file mode 100644 index 0000000000..d4dd405a14 --- /dev/null +++ b/testing/nist-sts/Makefile @@ -0,0 +1,63 @@ +############################################################################ +# apps/testing/nist-sts/Make.defs +# +# Licensed to the Apache Software Foundation (ASF) under one or more +# contributor license agreements. See the NOTICE file distributed with +# this work for additional information regarding copyright ownership. The +# ASF licenses this file to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance with the +# License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, WITHOUT +# WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the +# License for the specific language governing permissions and limitations +# under the License. +# +############################################################################ + +include $(APPDIR)/Make.defs + +PROGNAME = $(CONFIG_TESTING_NIST_STS_PROGNAME) +PRIORITY = $(CONFIG_TESTING_NIST_STS_PRIORITY) +STACKSIZE = $(CONFIG_TESTING_NIST_STS_STACKSIZE) +MODULE = $(CONFIG_TESTING_NIST_STS) + +NIST_URL ?= "https://csrc.nist.gov/CSRC/media/Projects/Random-Bit-Generation/documents/" + +NIST_UNPACKNAME = nist-sts +NIST_PACKNAME = sts-2.1.2 +NIST_ZIP = sts-2_1_2.zip +UNPACK ?= unzip -q -o + +$(NIST_ZIP): + @echo "Downloading: $(NIST_ZIP)" + $(Q) $(call DOWNLOAD,$(NIST_URL),$(NIST_ZIP)) + +$(NIST_UNPACKNAME): $(NIST_ZIP) + @echo "Unpacking: $(NIST_ZIP) -> $(NIST_UNPACKNAME)" + $(Q) $(UNPACK) $(NIST_ZIP) + $(Q) mv $(NIST_PACKNAME) $(NIST_UNPACKNAME) + $(Q) mv $(NIST_UNPACKNAME)/$(NIST_PACKNAME) $(NIST_UNPACKNAME)/sts + $(Q) echo "Patching $(NIST_UNPACKNAME)" + $(Q) patch -p1 -d $(NIST_UNPACKNAME) < 0001-Solve-the-memory-out-of-bounds-problem-in-sts.patch + $(Q) patch -p1 -d $(NIST_UNPACKNAME) < 0002-Fix-bug-that-crash-in-running-a-single-test-after-ru.patch + $(Q) touch $(NIST_UNPACKNAME) + +MAINSRC = nist-sts/sts/src/assess.c +CSRCS = $(shell find nist-sts/sts/src -name "*.c" ! -name "assess.c") +CFLAGS += -Wno-misleading-indentation -Wno-unused-but-set-variable \ + -Wno-strict-prototypes -Wno-undef -Wno-shadow -Wno-unused-variable + +# Download and unpack tarball if no git repo found +ifeq ($(wildcard $(NIST_UNPACKNAME)/.git),) +context:: $(NIST_UNPACKNAME) + +distclean:: + $(call DELDIR, $(NIST_UNPACKNAME)) + $(call DELFILE, $(NIST_ZIP)) +endif + +include $(APPDIR)/Application.mk