From 28337843b925af0c8867d4d72e29485333f6281c Mon Sep 17 00:00:00 2001 From: Peter Schwabe Date: Wed, 10 Apr 2019 16:02:45 +0200 Subject: [PATCH] Added python test running all unit tests of common crypto functions --- test/Makefile | 10 +++------- test/common/aes.c | 9 ++++++++- test/test_common.py | 29 +++++++++++++++++++++++++++++ 3 files changed, 40 insertions(+), 8 deletions(-) create mode 100644 test/test_common.py diff --git a/test/Makefile b/test/Makefile index 8b938246..51da5b8b 100644 --- a/test/Makefile +++ b/test/Makefile @@ -10,8 +10,8 @@ SCHEME_UPPERCASE=$(shell echo $(SCHEME) | tr a-z A-Z | sed 's/-//g') IMPLEMENTATION_UPPERCASE=$(shell echo $(IMPLEMENTATION) | tr a-z A-Z | sed 's/-//g') COMMON_DIR=../common -COMMON_FILES=$(COMMON_DIR)/fips202.c $(COMMON_DIR)/sha2.c -COMMON_HEADERS=$(COMMON_DIR)/fips202.h $(COMMON_DIR)/randombytes.h $(COMMON_DIR)/sha2.h +COMMON_FILES=$(COMMON_DIR)/aes.c $(COMMON_DIR)/sha2.c $(COMMON_DIR)/fips202.c +COMMON_HEADERS=$(COMMON_DIR)/*.h DEST_DIR=../bin # This -Wall was supported by the European Commission through the ERC Starting Grant 805031 (EPOQUE) @@ -38,11 +38,7 @@ testvectors: $(DEST_DIR)/testvectors_$(SCHEME)_$(IMPLEMENTATION) .PHONY: printparams printparams: $(DEST_DIR)/printparams_$(SCHEME)_$(IMPLEMENTATION) -$(DEST_DIR)/test_fips202: common/fips202.c $(COMMON_FILES) - mkdir -p $(DEST_DIR) - $(CC) $(CFLAGS) $< $(COMMON_FILES) -o $@ - -$(DEST_DIR)/test_sha2: common/sha2.c $(COMMON_FILES) +$(DEST_DIR)/test_%: common/%.c $(COMMON_FILES) mkdir -p $(DEST_DIR) $(CC) $(CFLAGS) $< $(COMMON_FILES) -o $@ diff --git a/test/common/aes.c b/test/common/aes.c index b7fc4cb1..dcfe8029 100644 --- a/test/common/aes.c +++ b/test/common/aes.c @@ -50,6 +50,7 @@ const unsigned char nonce[AESCTR_NONCEBYTES] = { int main(void) { unsigned char ct[67]; + int r = 0; aes128ctx ctx128; aes192ctx ctx192; aes256ctx ctx256; @@ -61,33 +62,39 @@ int main(void) aes128_ctr(ct, 67, nonce, &ctx128); if(memcmp(ct, stream128, 67)) { printf("ERROR AES128CTR output does not match test vector.\n"); + r = 1; } aes192_ctr(ct, 67, nonce, &ctx192); if(memcmp(ct, stream192, 67)) { printf("ERROR AES192CTR output does not match test vector.\n"); + r = 1; } aes256_ctr(ct, 67, nonce, &ctx256); if(memcmp(ct, stream256, 67)) { printf("ERROR AES256CTR output does not match test vector.\n"); + r = 1; } aes128_ecb(ct, msg, 3, &ctx128); if(memcmp(ct, ct128, 48)) { printf("ERROR AES128ECB output does not match test vector.\n"); + r = 1; } aes192_ecb(ct, msg, 3, &ctx192); if(memcmp(ct, ct192, 48)) { printf("ERROR AES192ECB output does not match test vector.\n"); + r = 1; } aes256_ecb(ct, msg, 3, &ctx256); if(memcmp(ct, ct256, 48)) { printf("ERROR AES256ECB output does not match test vector.\n"); + r = 1; } - return 0; + return r; } diff --git a/test/test_common.py b/test/test_common.py new file mode 100644 index 00000000..65dc50a6 --- /dev/null +++ b/test/test_common.py @@ -0,0 +1,29 @@ +""" +Runs functional tests for common crypto functions (e.g., fips202, sha2, aes). +""" + +import os +import re +import platform +import unittest + +import pqclean +import helpers + + +def test_common(): + if os.name != 'nt': # Cannot build on Windows at the moment + for d in os.listdir('common'): + primitive = re.sub("\.c$", "", d) + binname = os.path.join('..', 'bin', 'test_'+primitive) + helpers.make(binname) + helpers.run_subprocess(binname) + + +if __name__ == '__main__': + try: + import nose2 + nose2.main() + except ImportError: + import nose + nose.runmodule()