coreutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[PATCH] maint: add sm3sum based on OSCCA SM3 secure hash


From: Tianjia Zhang
Subject: [PATCH] maint: add sm3sum based on OSCCA SM3 secure hash
Date: Tue, 9 Jun 2020 14:23:56 +0800

Add message digest program sm3sum, it use OSCCA SM3 secure
hash (OSCCA GM/T 0004-2012 SM3) generic hash transformation.

* AUTHORS: Add sm3sum authors.
* README: Add sm3sum to programs list.
* bootstrap.conf: Add gnulib module 'crypto/sm3' used by sm3sum.
* build-aux/gen-lists-of-programs.sh: Add sm3sum to normal_progs.
* doc/coreutils.texi: Document sm3sum.
* man/.gitignore: Ignore sm3sum.1.
* man/local.mk: Generate man/sm3sum.1.
* man/sm3sum.x: sm3sum manual.
* src/.gitignore: Ignore sm3sum binary.
* src/local.mk: Build for sm3sum.
* src/md5sum.c: Add sm3sum algorithm.
* tests/local.mk: Add sm3sum.pl test.
* tests/misc/sm3sum.pl: sm3sum testcase.
* tests/misc/tty-eof.pl: Add sm3sum to algorithm list.
---
 AUTHORS                            |  1 +
 README                             |  6 ++--
 bootstrap.conf                     |  1 +
 build-aux/gen-lists-of-programs.sh |  1 +
 doc/coreutils.texi                 | 20 ++++++++++-
 man/.gitignore                     |  1 +
 man/local.mk                       |  1 +
 man/sm3sum.x                       |  4 +++
 src/.gitignore                     |  1 +
 src/local.mk                       |  4 +++
 src/md5sum.c                       | 10 ++++++
 tests/local.mk                     |  1 +
 tests/misc/sm3sum.pl               | 55 ++++++++++++++++++++++++++++++
 tests/misc/tty-eof.pl              |  1 +
 14 files changed, 103 insertions(+), 4 deletions(-)
 create mode 100644 man/sm3sum.x
 create mode 100755 tests/misc/sm3sum.pl

diff --git a/AUTHORS b/AUTHORS
index 46948d5fd..967b9de06 100644
--- a/AUTHORS
+++ b/AUTHORS
@@ -80,6 +80,7 @@ sha512sum: Ulrich Drepper, Scott Miller, David Madore
 shred: Colin Plumb
 shuf: Paul Eggert
 sleep: Jim Meyering, Paul Eggert
+sm3sum: Ulrich Drepper, Scott Miller, David Madore
 sort: Mike Haertel, Paul Eggert
 split: Torbjorn Granlund, Richard M. Stallman
 stat: Michael Meskes
diff --git a/README b/README
index 32496b2c5..7400a1029 100644
--- a/README
+++ b/README
@@ -13,9 +13,9 @@ The programs that can be built with this package are:
   id install 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 unlink uptime users vdir wc who whoami yes
+  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
 
 See the file NEWS for a list of major changes in the current release.
 
diff --git a/bootstrap.conf b/bootstrap.conf
index 12e2d831a..d445f8427 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -58,6 +58,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 3ec9a6dd1..7cf656b54 100755
--- a/build-aux/gen-lists-of-programs.sh
+++ b/build-aux/gen-lists-of-programs.sh
@@ -113,6 +113,7 @@ normal_progs='
     shred
     shuf
     sleep
+    sm3sum
     sort
     split
     stat
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 3432fb294..560859d24 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -110,6 +110,7 @@
 * shred: (coreutils)shred invocation.           Remove files more securely.
 * shuf: (coreutils)shuf invocation.             Shuffling text files.
 * sleep: (coreutils)sleep invocation.           Delay for a specified time.
+* sm3sum: (coreutils)sm3sum invocation.         Print or check SM3 digests.
 * sort: (coreutils)sort invocation.             Sort text files.
 * split: (coreutils)split invocation.           Split into pieces.
 * stat: (coreutils)stat invocation.             Report file(system) status.
@@ -188,7 +189,7 @@ Free Documentation License''.
 * Output of entire files::       cat tac nl od base32 base64 basenc
 * Formatting file contents::     fmt pr fold
 * Output of parts of files::     head tail split csplit
-* Summarizing files::            wc sum cksum b2sum md5sum sha1sum sha2
+* Summarizing files::            wc sum cksum b2sum md5sum sha1sum sha2 sm3sum
 * Operating on sorted files::    sort shuf uniq comm ptx tsort
 * Operating on fields::          cut paste join
 * Operating on characters::      tr expand unexpand
@@ -268,6 +269,7 @@ Summarizing files
 * md5sum invocation::            Print or check MD5 digests
 * sha1sum invocation::           Print or check SHA-1 digests
 * sha2 utilities::               Print or check SHA-2 digests
+* sm3sum invocation::            Print or check SM3 digests
 
 Operating on sorted files
 
@@ -3736,6 +3738,7 @@ contents of files.
 * md5sum invocation::           Print or check MD5 digests.
 * sha1sum invocation::          Print or check SHA-1 digests.
 * sha2 utilities::              Print or check SHA-2 digests.
+* sm3sum invocation::           Print or check SM3 digests.
 @end menu
 
 
@@ -4189,6 +4192,21 @@ and @command{sha1sum}.
 @xref{md5sum invocation}.
 
 
+@node sm3sum invocation
+@section @command{sm3sum}: Print or check SM3 digests
+
+@pindex sm3sum
+@cindex SM3
+@cindex 256-bit checksum
+@cindex checksum, 256-bit
+@cindex fingerprint, 256-bit
+@cindex message-digest, 256-bit
+
+@command{sm3sum} computes a 256-bit checksum for each specified
+@var{file}.  The usage and options of this command are precisely the
+same as for @command{md5sum}.  @xref{md5sum invocation}.
+
+
 @node Operating on sorted files
 @chapter Operating on sorted files
 
diff --git a/man/.gitignore b/man/.gitignore
index 4eecb7833..d47a94d2d 100644
--- a/man/.gitignore
+++ b/man/.gitignore
@@ -75,6 +75,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 3df879cc3..28846349c 100644
--- a/man/local.mk
+++ b/man/local.mk
@@ -142,6 +142,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/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/src/.gitignore b/src/.gitignore
index 86d82ad4b..d9e3c73da 100644
--- a/src/.gitignore
+++ b/src/.gitignore
@@ -87,6 +87,7 @@ sha512sum
 shred
 shuf
 sleep
+sm3sum
 sort
 split
 stat
diff --git a/src/local.mk b/src/local.mk
index 72db9c704..5b9433140 100644
--- a/src/local.mk
+++ b/src/local.mk
@@ -182,6 +182,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)
@@ -302,6 +303,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)
@@ -400,6 +402,8 @@ 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)
 src_b2sum_CPPFLAGS = -DHASH_ALGO_BLAKE2=1 -DHAVE_CONFIG_H $(AM_CPPFLAGS)
 src_b2sum_SOURCES = src/md5sum.c \
                    src/blake2/blake2.h src/blake2/blake2-impl.h \
diff --git a/src/md5sum.c b/src/md5sum.c
index 447a005a1..ca5c08ab0 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 "OSCCA"
+# define DIGEST_ALIGN 4
 #else
 # error "Can't decide which hash algorithm to compile."
 #endif
diff --git a/tests/local.mk b/tests/local.mk
index 799200393..e2ec2d61f 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -358,6 +358,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..b2fe2252d
--- /dev/null
+++ b/tests/misc/sm3sum.pl
@@ -0,0 +1,55 @@
+#!/usr/bin/perl
+# Test "sm3sum".
+
+# Copyright (C) 2005-2020 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 <https://www.gnu.org/licenses/>.
+
+use strict;
+
+(my $program_name = $0) =~ s|.*/||;
+
+# Turn off localization of executable's output.
+@ENV{qw(LANGUAGE LANG LC_ALL)} = ('C') x 3;
+
+my $sha_degenerate = 
"1ab21d8355cfa17f8e61194831e81a8f22bec8c728fefb747ed035eb5082aa2b";
+
+my @Tests =
+    (
+     ['s1', {IN=> {f=> ''}},
+                        {OUT=>"$sha_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;
diff --git a/tests/misc/tty-eof.pl b/tests/misc/tty-eof.pl
index 04bcb2e00..34c3dca17 100755
--- a/tests/misc/tty-eof.pl
+++ b/tests/misc/tty-eof.pl
@@ -52,6 +52,7 @@ $@
     sha384sum
     sha512sum
     shuf
+    sm3sum
     sort
     sum
     tac
-- 
2.17.1




reply via email to

[Prev in Thread] Current Thread [Next in Thread]