diff --git a/tests/prng_sha1prng/Makefile b/tests/prng_sha1prng/Makefile new file mode 100644 index 0000000000..c1bab91de0 --- /dev/null +++ b/tests/prng_sha1prng/Makefile @@ -0,0 +1,6 @@ +include ../Makefile.tests_common + +USEMODULE += random +USEMODULE += prng_sha1prng + +include $(RIOTBASE)/Makefile.include diff --git a/tests/prng_sha1prng/main.c b/tests/prng_sha1prng/main.c new file mode 100644 index 0000000000..8bd4828f4d --- /dev/null +++ b/tests/prng_sha1prng/main.c @@ -0,0 +1,82 @@ +/* + * Copyright (C) 2020 HAW Hamburg + * + * This file is subject to the terms and conditions of the GNU Lesser + * General Public License v2.1. See the file LICENSE in the top level + * directory for more details. + */ + +/** + * + * @file + * @brief Test cases for the SHA1PRNG pseudo random number generator + * + * @author Peter Kietzmann + * + */ + +#include +#include + +#include "kernel_defines.h" +#include "random.h" + +/** + * @brief expected sequence for seed=1. This sequence was generated running the + * following java program (openjdk 11.0.7) as a reference. + * + *~~~~ + * import java.security.SecureRandom; + * + * public class SHA1PRNGTEST { + * public static void main(String args[]) throws Exception { + * SecureRandom random = SecureRandom.getInstance("SHA1PRNG"); + * random.setSeed(1); + * int number = 0; + * for (int i = 0; i < 20; i++) { + * number = random.nextInt(); + * System.out.print(Integer.toUnsignedString(number) + " "); + * } + * System.out.println(""); + * } + * } + *~~~~ + */ +static const uint32_t seq_seed1[] = + {2529905901, 3336014406, 1714755920, 3709666991, 1432426612, 554064022, + 1614405352, 861636861, 3689098857, 3893737371, 3138964692, 506954022, + 3469584855, 4144207589, 2031557795, 3248917850, 2384338299, 3341545824, + 2454801916, 3985646079}; + +static void test_prng_sha1prng_java_u32(void) +{ + uint32_t seed[2] = {1, 0}; + + uint32_t test32[ARRAY_SIZE(seq_seed1)]; + + /* seed the generator with 8 bytes similar to the java reference + * implementation + */ + random_init_by_array(seed, sizeof(seed)); + + /* request random samples */ + for (unsigned i = 0; i < ARRAY_SIZE(seq_seed1); i++) { + test32[i] = random_uint32(); + } + + /* compare generator output and reference */ + if (!(memcmp(test32, seq_seed1, sizeof(seq_seed1)))) { + printf("%s:SUCCESS\n", __func__); + } + else { + printf("%s:FAILURE\n", __func__); + } +} + +int main(void) +{ + + test_prng_sha1prng_java_u32(); + + return 0; +} diff --git a/tests/prng_sha1prng/tests/01-run.py b/tests/prng_sha1prng/tests/01-run.py new file mode 100755 index 0000000000..c497b3f3df --- /dev/null +++ b/tests/prng_sha1prng/tests/01-run.py @@ -0,0 +1,18 @@ +#!/usr/bin/env python3 + +# Copyright (C) 2020 HAW Hamburg +# +# This file is subject to the terms and conditions of the GNU Lesser +# General Public License v2.1. See the file LICENSE in the top level +# directory for more details. + +import sys +from testrunner import run + + +def testfunc(child): + child.expect("test_prng_sha1prng_java_u32:SUCCESS\r\n") + + +if __name__ == "__main__": + sys.exit(run(testfunc))