From f713968fcc4cf3aa5f34077488c8989ec71fcb70 Mon Sep 17 00:00:00 2001 From: Jia Zhang Date: Sun, 8 Oct 2017 00:11:11 -0400 Subject: [PATCH] md5sum: implement SM3 hash utility This new hash computing tool is provided for SM3 hash algorithm. * AUTHORS: Add author info. * README: Add sm3sum to the program list. * bootstrap.conf: Add sm3 module to the gnulib module list. * build-aux/gen-lists-of-programs.sh: Add sm3sum to the buildable list. * man/.gitignore: Ignore the man page file sm3sum.1. * man/local.mk: Define the generation rule of sm3sum.1. * man/md5sum.x: Add the advice of using sm3sum. * man/sha1sum.x: Likewise. * man/sm3sum.x: The man page added. * scripts/git-hooks/commit-msg: Add sm3sum to the valid keyword list. * src/.gitignore: Ignore the program sm3sum. * src/local.mk: Define the rules of compiling sm3sum. * src/md5sum.c: Define HASH_ALGO_SM3 and associated contents. * src/system.h: Add sm3sum to infomap. * tests/local.mk: Add sm3sum.pl to the testcase list. * tests/misc/sm3sum.pl: The test case added. --- AUTHORS | 1 + README | 4 +-- bootstrap.conf | 1 + build-aux/gen-lists-of-programs.sh | 1 + man/.gitignore | 1 + man/local.mk | 1 + man/md5sum.x | 3 ++- man/sha1sum.x | 3 ++- man/sm3sum.x | 4 +++ scripts/git-hooks/commit-msg | 2 +- src/.gitignore | 1 + src/local.mk | 5 ++++ src/md5sum.c | 14 ++++++++++ src/system.h | 1 + tests/local.mk | 1 + tests/misc/sm3sum.pl | 55 ++++++++++++++++++++++++++++++++++++++ 16 files changed, 93 insertions(+), 5 deletions(-) create mode 100644 man/sm3sum.x create mode 100755 tests/misc/sm3sum.pl diff --git a/AUTHORS b/AUTHORS index 93d547b2d..a9e91b115 100644 --- a/AUTHORS +++ b/AUTHORS @@ -79,6 +79,7 @@ sha512sum: Ulrich Drepper, Scott Miller, David Madore shred: Colin Plumb shuf: Paul Eggert sleep: Jim Meyering, Paul Eggert +sm3sum: Jia Zhang sort: Mike Haertel, Paul Eggert split: Torbjorn Granlund, Richard M. Stallman stat: Michael Meskes diff --git a/README b/README index 9284e7c0a..9b3e02884 100644 --- a/README +++ b/README @@ -13,8 +13,8 @@ The programs that can be built with this package are: join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum sha384sum - sha512sum shred shuf sleep sort split stat stdbuf stty sum sync tac tail - tee test timeout touch tr true truncate tsort tty uname unexpand uniq + sha512sum shred shuf sleep sm3sum sort split stat stdbuf stty sum sync tac + tail tee test timeout touch tr true truncate tsort tty uname unexpand uniq unlink uptime users vdir wc who whoami yes See the file NEWS for a list of major changes in the current release. diff --git a/bootstrap.conf b/bootstrap.conf index ce1689a7e..e521ecd6a 100644 --- a/bootstrap.conf +++ b/bootstrap.conf @@ -55,6 +55,7 @@ gnulib_modules=" crypto/sha1 crypto/sha256 crypto/sha512 + crypto/sm3 cycle-check d-ino d-type diff --git a/build-aux/gen-lists-of-programs.sh b/build-aux/gen-lists-of-programs.sh index cdbcd0a9e..5687d7a55 100755 --- a/build-aux/gen-lists-of-programs.sh +++ b/build-aux/gen-lists-of-programs.sh @@ -112,6 +112,7 @@ normal_progs=' shred shuf sleep + sm3sum sort split stat diff --git a/man/.gitignore b/man/.gitignore index 9ef048a4c..e45ad4451 100644 --- a/man/.gitignore +++ b/man/.gitignore @@ -74,6 +74,7 @@ sha512sum.1 shred.1 shuf.1 sleep.1 +sm3sum.1 sort.1 split.1 stat.1 diff --git a/man/local.mk b/man/local.mk index 5f3f2152d..d146c8629 100644 --- a/man/local.mk +++ b/man/local.mk @@ -141,6 +141,7 @@ man/sha512sum.1: src/sha512sum$(EXEEXT) man/shred.1: src/shred$(EXEEXT) man/shuf.1: src/shuf$(EXEEXT) man/sleep.1: src/sleep$(EXEEXT) +man/sm3sum.1: src/sm3sum$(EXEEXT) man/sort.1: src/sort$(EXEEXT) man/split.1: src/split$(EXEEXT) man/stat.1: src/stat$(EXEEXT) diff --git a/man/md5sum.x b/man/md5sum.x index bdb6cc765..ad5488c4b 100644 --- a/man/md5sum.x +++ b/man/md5sum.x @@ -6,4 +6,5 @@ md5sum \- compute and check MD5 message digest Do not use the MD5 algorithm for security related purposes. Instead, use an SHA\-2 algorithm, implemented in the programs sha224sum(1), sha256sum(1), sha384sum(1), sha512sum(1), -or the BLAKE2 algorithm, implemented in b2sum(1) +or the BLAKE2 algorithm, implemented in b2sum(1), or the SM3 +algorithm, implemented in sm3sum(1) diff --git a/man/sha1sum.x b/man/sha1sum.x index ef0852f4d..10d5bd687 100644 --- a/man/sha1sum.x +++ b/man/sha1sum.x @@ -6,4 +6,5 @@ sha1sum \- compute and check SHA1 message digest Do not use the SHA-1 algorithm for security related purposes. Instead, use an SHA\-2 algorithm, implemented in the programs sha224sum(1), sha256sum(1), sha384sum(1), sha512sum(1), -or the BLAKE2 algorithm, implemented in b2sum(1) +or the BLAKE2 algorithm, implemented in b2sum(1), or the SM3 +algorithm, implemented in sm3sum(1) diff --git a/man/sm3sum.x b/man/sm3sum.x new file mode 100644 index 000000000..dc28c5074 --- /dev/null +++ b/man/sm3sum.x @@ -0,0 +1,4 @@ +[NAME] +sm3sum \- compute and check SM3 message digest +[DESCRIPTION] +.\" Add any additional description here diff --git a/scripts/git-hooks/commit-msg b/scripts/git-hooks/commit-msg index e6b38bb21..0148211bf 100755 --- a/scripts/git-hooks/commit-msg +++ b/scripts/git-hooks/commit-msg @@ -20,7 +20,7 @@ my @valid = qw( join kill link ln logname ls md5sum mkdir mkfifo mknod mktemp mv nice nl nohup nproc numfmt od paste pathchk pinky pr printenv printf ptx pwd readlink realpath rm rmdir runcon seq sha1sum sha224sum sha256sum - sha384sum sha512sum shred shuf sleep sort split stat stdbuf stty + sha384sum sha512sum shred shuf sleep sm3sum sort split stat stdbuf stty sum sync tac tail tee test timeout touch tr true truncate tsort tty uname unexpand uniq unlink uptime users vdir wc who whoami yes diff --git a/src/.gitignore b/src/.gitignore index 70ab2cac8..fe9700467 100644 --- a/src/.gitignore +++ b/src/.gitignore @@ -86,6 +86,7 @@ sha512sum shred shuf sleep +sm3sum sort split stat diff --git a/src/local.mk b/src/local.mk index 38b4bd449..4042b3134 100644 --- a/src/local.mk +++ b/src/local.mk @@ -180,6 +180,7 @@ src_sha512sum_LDADD = $(LDADD) src_shred_LDADD = $(LDADD) src_shuf_LDADD = $(LDADD) src_sleep_LDADD = $(LDADD) +src_sm3sum_LDADD = $(LDADD) src_sort_LDADD = $(LDADD) src_split_LDADD = $(LDADD) src_stat_LDADD = $(LDADD) @@ -300,6 +301,7 @@ src_sha224sum_LDADD += $(LIB_CRYPTO) src_sha256sum_LDADD += $(LIB_CRYPTO) src_sha384sum_LDADD += $(LIB_CRYPTO) src_sha512sum_LDADD += $(LIB_CRYPTO) +src_sm3sum_LDADD += $(LIB_CRYPTO) # for canon_host src_pinky_LDADD += $(GETADDRINFO_LIB) @@ -397,6 +399,9 @@ src_sha384sum_SOURCES = src/md5sum.c src_sha384sum_CPPFLAGS = -DHASH_ALGO_SHA384=1 $(AM_CPPFLAGS) src_sha512sum_SOURCES = src/md5sum.c src_sha512sum_CPPFLAGS = -DHASH_ALGO_SHA512=1 $(AM_CPPFLAGS) +src_sm3sum_SOURCES = src/md5sum.c +src_sm3sum_CPPFLAGS = -DHASH_ALGO_SM3=1 $(AM_CPPFLAGS) + # Include the file on the command line to avoid modifying # the blake2 upstream source if USE_XLC_INCLUDE diff --git a/src/md5sum.c b/src/md5sum.c index c80023178..27828f6d2 100644 --- a/src/md5sum.c +++ b/src/md5sum.c @@ -42,6 +42,9 @@ #if HASH_ALGO_SHA512 || HASH_ALGO_SHA384 # include "sha512.h" #endif +#if HASH_ALGO_SM3 +# include "sm3.h" +#endif #include "die.h" #include "error.h" #include "fadvise.h" @@ -98,6 +101,13 @@ # define DIGEST_BITS 384 # define DIGEST_REFERENCE "FIPS-180-2" # define DIGEST_ALIGN 8 +#elif HASH_ALGO_SM3 +# define PROGRAM_NAME "sm3sum" +# define DIGEST_TYPE_STRING "SM3" +# define DIGEST_STREAM sm3_stream +# define DIGEST_BITS 256 +# define DIGEST_REFERENCE "GB/T 32905-2016" +# define DIGEST_ALIGN 4 #else # error "Can't decide which hash algorithm to compile." #endif @@ -106,6 +116,10 @@ # define AUTHORS \ proper_name ("Padraig Brady"), \ proper_name ("Samuel Neves") +#elif HASH_ALGO_SM3 +# define AUTHORS \ + proper_name ("Jia Zhang") +# define DIGEST_HEX_BYTES (DIGEST_BITS / 4) #else # define AUTHORS \ proper_name ("Ulrich Drepper"), \ diff --git a/src/system.h b/src/system.h index 1c7fee9e9..e725f1ea0 100644 --- a/src/system.h +++ b/src/system.h @@ -636,6 +636,7 @@ emit_ancillary_info (char const *program) { "sha256sum", "sha2 utilities" }, { "sha384sum", "sha2 utilities" }, { "sha512sum", "sha2 utilities" }, + { "sm3sum", "sm3 utility" }, { NULL, NULL } }; diff --git a/tests/local.mk b/tests/local.mk index 74426f608..b22cf35ff 100644 --- a/tests/local.mk +++ b/tests/local.mk @@ -346,6 +346,7 @@ all_tests = \ tests/misc/shuf.sh \ tests/misc/shuf-reservoir.sh \ tests/misc/sleep.sh \ + tests/misc/sm3sum.pl \ tests/misc/sort.pl \ tests/misc/sort-benchmark-random.sh \ tests/misc/sort-compress.sh \ diff --git a/tests/misc/sm3sum.pl b/tests/misc/sm3sum.pl new file mode 100755 index 000000000..70d586f86 --- /dev/null +++ b/tests/misc/sm3sum.pl @@ -0,0 +1,55 @@ +#!/usr/bin/perl +# Test "sm3sum". + +# Copyright (C) 2017 Free Software Foundation, Inc. + +# 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 . + +use strict; + +(my $program_name = $0) =~ s|.*/||; + +# Turn off localization of executable's output. +@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3; + +my $sm3_degenerate = "1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b"; + +my @Tests = + ( + ['s1', {IN=> {f=> ''}}, + {OUT=>"$sm3_degenerate f\n"}], + ['s2', {IN=> {f=> 'a'}}, + {OUT=>"623476ac18f65a2909e43c7fec61b49c7e764a91a18ccb82f1917a29c86c5e88 f\n"}], + ['s3', {IN=> {f=> 'abc'}}, + {OUT=>"66c7f0f462eeedd9d1f2d46bdc10e4e24167c4875cf2f7a2297da02b8f4ba8e0 f\n"}], + ['s4', + {IN=> {f=> 'abcdbcdecdefdefgefghfghighijhijkijkljklmklmnlmnomnopnopq'}}, + {OUT=>"639b6cc5e64d9e37a390b192df4fa1ea0720ab747ff692b9f38c4e66ad7b8c05 f\n"}], + ['s8', {IN=> {f=> 'a' x 1000000}}, + {OUT=>"c8aaf89429554029e231941a2acc0ad61ff2a5acd8fadd25847a3a732b3b02c3 f\n"}], + ); + +# Insert the '--text' argument for each test. +my $t; +foreach $t (@Tests) + { + splice @$t, 1, 0, '--text' unless @$t[1] =~ /--check/; + } + +my $save_temps = $ENV{DEBUG}; +my $verbose = $ENV{VERBOSE}; + +my $prog = 'sm3sum'; +my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose); +exit $fail; -- 2.14.1