From c63728b59f5916bb62e0053d8211f4d07dbaa771 Mon Sep 17 00:00:00 2001 From: Bruno Haible Date: Sun, 19 Sep 2021 01:43:02 +0200 Subject: sm3-buffer tests: New module. * tests/test-sm3-buffer.c: Renamed from tests/test-sm3.c. * modules/crypto/sm3-buffer-tests: Renamed from modules/crypto/sm3-tests. Test tests/test-sm3-buffer.c instead of tests/test-sm3.c. --- ChangeLog | 6 ++++ modules/crypto/sm3-buffer-tests | 11 +++++++ modules/crypto/sm3-tests | 11 ------- tests/test-sm3-buffer.c | 72 +++++++++++++++++++++++++++++++++++++++++ tests/test-sm3.c | 72 ----------------------------------------- 5 files changed, 89 insertions(+), 83 deletions(-) create mode 100644 modules/crypto/sm3-buffer-tests delete mode 100644 modules/crypto/sm3-tests create mode 100644 tests/test-sm3-buffer.c delete mode 100644 tests/test-sm3.c diff --git a/ChangeLog b/ChangeLog index 074bd3caf7..154678293e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,5 +1,11 @@ 2021-09-18 Bruno Haible + sm3-buffer tests: New module. + * tests/test-sm3-buffer.c: Renamed from tests/test-sm3.c. + * modules/crypto/sm3-buffer-tests: Renamed from + modules/crypto/sm3-tests. Test tests/test-sm3-buffer.c instead of + tests/test-sm3.c. + sm3-buffer: New module. * lib/sm3-stream.c: New file, extracted from lib/sm3.c. * lib/sm3.c: Don't include stdlib.h, unlocked-io.h. diff --git a/modules/crypto/sm3-buffer-tests b/modules/crypto/sm3-buffer-tests new file mode 100644 index 0000000000..983a22d46d --- /dev/null +++ b/modules/crypto/sm3-buffer-tests @@ -0,0 +1,11 @@ +Files: +tests/test-sm3-buffer.c + +Depends-on: + +configure.ac: + +Makefile.am: +TESTS += test-sm3-buffer +check_PROGRAMS += test-sm3-buffer +test_sm3_buffer_LDADD = $(LDADD) @LIB_CRYPTO@ diff --git a/modules/crypto/sm3-tests b/modules/crypto/sm3-tests deleted file mode 100644 index fac6ca6c21..0000000000 --- a/modules/crypto/sm3-tests +++ /dev/null @@ -1,11 +0,0 @@ -Files: -tests/test-sm3.c - -Depends-on: - -configure.ac: - -Makefile.am: -TESTS += test-sm3 -check_PROGRAMS += test-sm3 -test_sm3_LDADD = $(LDADD) @LIB_CRYPTO@ diff --git a/tests/test-sm3-buffer.c b/tests/test-sm3-buffer.c new file mode 100644 index 0000000000..df4476f150 --- /dev/null +++ b/tests/test-sm3-buffer.c @@ -0,0 +1,72 @@ +/* + * Copyright (C) 2017-2021 Free Software Foundation, Inc. + * Written by Jia Zhang + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . */ + +#include + +#include "sm3.h" + +#include +#include + +static int +test (const char *in, const char *out) +{ + char buf[SM3_DIGEST_SIZE]; + + if (memcmp (sm3_buffer (in, strlen (in), buf), + out, SM3_DIGEST_SIZE) != 0) + { + size_t i; + printf ("expected:\n"); + for (i = 0; i < SM3_DIGEST_SIZE; i++) + printf ("%02x ", out[i] & 0xFFu); + printf ("\ncomputed:\n"); + for (i = 0; i < SM3_DIGEST_SIZE; i++) + printf ("%02x ", buf[i] & 0xFFu); + printf ("\n"); + return 1; + } + + return 0; +} + +int +main (void) +{ + /* Test vectors from GM/T 004-2012 */ + const char *in[] = + { + "abc", + "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", + }; + const char *out[] = + { + "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2" + "\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0", + "\xde\xbe\x9f\xf9\x22\x75\xb8\xa1\x38\x60\x48\x89\xc1\x8e\x5a\x4d" + "\x6f\xdb\x70\xe5\x38\x7e\x57\x65\x29\x3d\xcb\xa3\x9c\x0c\x57\x32", + }; + size_t i; + + for (i = 0; i < sizeof (in) / sizeof (in[0]); i++) + { + if (test (in[i], out[i])) + return 1; + } + + return 0; +} diff --git a/tests/test-sm3.c b/tests/test-sm3.c deleted file mode 100644 index df4476f150..0000000000 --- a/tests/test-sm3.c +++ /dev/null @@ -1,72 +0,0 @@ -/* - * Copyright (C) 2017-2021 Free Software Foundation, Inc. - * Written by Jia Zhang - * - * This program is free software: you can redistribute it and/or modify - * it under the terms of the GNU General Public License as published by - * the Free Software Foundation; either version 3 of the License, or - * (at your option) any later version. - * - * This program is distributed in the hope that it will be useful, - * but WITHOUT ANY WARRANTY; without even the implied warranty of - * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the - * GNU General Public License for more details. - * - * You should have received a copy of the GNU General Public License - * along with this program. If not, see . */ - -#include - -#include "sm3.h" - -#include -#include - -static int -test (const char *in, const char *out) -{ - char buf[SM3_DIGEST_SIZE]; - - if (memcmp (sm3_buffer (in, strlen (in), buf), - out, SM3_DIGEST_SIZE) != 0) - { - size_t i; - printf ("expected:\n"); - for (i = 0; i < SM3_DIGEST_SIZE; i++) - printf ("%02x ", out[i] & 0xFFu); - printf ("\ncomputed:\n"); - for (i = 0; i < SM3_DIGEST_SIZE; i++) - printf ("%02x ", buf[i] & 0xFFu); - printf ("\n"); - return 1; - } - - return 0; -} - -int -main (void) -{ - /* Test vectors from GM/T 004-2012 */ - const char *in[] = - { - "abc", - "abcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcdabcd", - }; - const char *out[] = - { - "\x66\xc7\xf0\xf4\x62\xee\xed\xd9\xd1\xf2\xd4\x6b\xdc\x10\xe4\xe2" - "\x41\x67\xc4\x87\x5c\xf2\xf7\xa2\x29\x7d\xa0\x2b\x8f\x4b\xa8\xe0", - "\xde\xbe\x9f\xf9\x22\x75\xb8\xa1\x38\x60\x48\x89\xc1\x8e\x5a\x4d" - "\x6f\xdb\x70\xe5\x38\x7e\x57\x65\x29\x3d\xcb\xa3\x9c\x0c\x57\x32", - }; - size_t i; - - for (i = 0; i < sizeof (in) / sizeof (in[0]); i++) - { - if (test (in[i], out[i])) - return 1; - } - - return 0; -} -- cgit v1.2.1