]> git.ipfire.org Git - thirdparty/coreutils.git/commitdiff
digest: add support for sm3
authorPádraig Brady <P@draigBrady.com>
Tue, 7 Sep 2021 14:45:01 +0000 (15:45 +0100)
committerPádraig Brady <P@draigBrady.com>
Wed, 15 Sep 2021 19:44:18 +0000 (20:44 +0100)
Add message digest sm3, which uses the OSCCA SM3 secure
hash (OSCCA GM/T 0004-2012 SM3) generic hash transformation.

* bootstrap.conf: Add the sm3 module.
* doc/coreutils.texi: Mention the cksum -a option.
* src/digest.c: Provide support for --algorithm='sm3'.
* tests/misc/sm3sum.pl: Add a new test (from Tianjia Zhang)
* tests/local.mk: Reference the new test.
* NEWS: Mention the new feature.

Tested-by: Tianjia Zhang <tianjia.zhang@linux.alibaba.com>
NEWS
THANKS.in
bootstrap.conf
doc/coreutils.texi
src/digest.c
tests/local.mk
tests/misc/sm3sum.pl [new file with mode: 0755]

diff --git a/NEWS b/NEWS
index 1db2bd070759ea89818752987c8203c9d047d584..217cf987c03904c2ea7ae23d620cd812264b4917 100644 (file)
--- a/NEWS
+++ b/NEWS
@@ -93,6 +93,8 @@ GNU coreutils NEWS                                    -*- outline -*-
   cksum now subsumes all of these programs, and coreutils
   will introduce no future standalone checksum utility.
 
+  cksum -a now supports the 'sm3' argument, to use the SM3 digest algorithm.
+
   expr and factor now support bignums on all platforms.
 
   ls --classify now supports the "always", "auto", or "never" flags,
index 0424c22fb86411542a7b62b58f4d42f72f0389f7..edf93d78797045052fe76e7a30f18704d29d17bc 100644 (file)
--- a/THANKS.in
+++ b/THANKS.in
@@ -611,6 +611,7 @@ Thomas M.Ott                        thmo-13@gmx.de
 Thomas Quinot                       thomas@Cuivre.FR.EU.ORG
 Thomas Schwinge                     tschwinge@gnu.org
 Thomas Wolff                        mined@towo.net
+Tianjia Zhang                       tianjia.zhang@linux.alibaba.com
 Tim J. Robbins                      tjr@FreeBSD.org
 Tim Mooney                          mooney@dogbert.cc.ndsu.NoDak.edu
 Tim Ryan                            Tim_Ryan@bnz.co.nz
index e7ed7e5ffa722055c7d53ee7b9c059c9fb281106..481a37e9c3aeec4d6385fbfe5084a7f637ac7bb4 100644 (file)
@@ -60,6 +60,7 @@ gnulib_modules="
   crypto/sha1
   crypto/sha256
   crypto/sha512
+  crypto/sm3
   cycle-check
   d-ino
   d-type
index 0cb478964e69c8924913b66919309062f25fab28..183dde12ccf3f6155402ae695cefc5bac4243133 100644 (file)
@@ -3980,6 +3980,7 @@ Supported more modern digest algorithms are:
 @samp{sha384}    equivalent to @command{sha384sum}
 @samp{sha512}    equivalent to @command{sha512sum}
 @samp{blake2b}   equivalent to @command{b2sum}
+@samp{sm3}       only available through @command{cksum}
 @end example
 
 @item --debug
index 8039c671e77d8b30784e227344a4f5f572e128d4..fe1706d6f8103506a3c3d1a9b882c70827c6b5e0 100644 (file)
@@ -48,6 +48,9 @@
 #if HASH_ALGO_SHA512 || HASH_ALGO_SHA384 || HASH_ALGO_CKSUM
 # include "sha512.h"
 #endif
+#if HASH_ALGO_CKSUM
+# include "sm3.h"
+#endif
 #include "die.h"
 #include "error.h"
 #include "fadvise.h"
@@ -280,6 +283,11 @@ blake2b_sum_stream (FILE *stream, void *resstream, uintmax_t *length)
 {
   return blake2b_stream (stream, resstream, *length);
 }
+static int
+sm3_sum_stream (FILE *stream, void *resstream, uintmax_t *length)
+{
+  return sm3_stream (stream, resstream);
+}
 
 enum Algorithm
 {
@@ -293,29 +301,30 @@ enum Algorithm
   sha384,
   sha512,
   blake2b,
+  sm3,
 };
 
 static char const *const algorithm_args[] =
 {
   "bsd", "sysv", "crc", "md5", "sha1", "sha224",
-  "sha256", "sha384", "sha512", "blake2b", NULL
+  "sha256", "sha384", "sha512", "blake2b", "sm3", NULL
 };
 static enum Algorithm const algorithm_types[] =
 {
   bsd, sysv, crc, md5, sha1, sha224,
-  sha256, sha384, sha512, blake2b,
+  sha256, sha384, sha512, blake2b, sm3,
 };
 ARGMATCH_VERIFY (algorithm_args, algorithm_types);
 
 static char const *const algorithm_tags[] =
 {
   "BSD", "SYSV", "CRC", "MD5", "SHA1", "SHA224",
-  "SHA256", "SHA384", "SHA512", "BLAKE2b", NULL
+  "SHA256", "SHA384", "SHA512", "BLAKE2b", "SM3", NULL
 };
 static int const algorithm_bits[] =
 {
   16, 16, 32, 128, 160, 224,
-  256, 384, 512, 512, 0
+  256, 384, 512, 512, 256, 0
 };
 
 verify (ARRAY_CARDINALITY (algorithm_bits)
@@ -334,6 +343,7 @@ static sumfn cksumfns[]=
   sha384_sum_stream,
   sha512_sum_stream,
   blake2b_sum_stream,
+  sm3_sum_stream,
 };
 static digest_output_fn cksum_output_fns[]=
 {
@@ -347,6 +357,7 @@ static digest_output_fn cksum_output_fns[]=
   output_file,
   output_file,
   output_file,
+  output_file,
 };
 bool cksum_debug;
 #endif
@@ -497,6 +508,7 @@ DIGEST determines the digest algorithm and default output format:\n\
   'sha384'    (equivalent to sha384sum)\n\
   'sha512'    (equivalent to sha512sum)\n\
   'blake2b'   (equivalent to b2sum)\n\
+  'sm3'       (only available through cksum)\n\
 \n"), stdout);
 #endif
 #if !HASH_ALGO_SUM && !HASH_ALGO_CKSUM
index 3ddd6f1bb8eaad5ab66555af306f742c64a395b8..192c0d31c94aebc6b87b0dfe6e4164f0691e0dda 100644 (file)
@@ -362,6 +362,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 (executable)
index 0000000..667dac5
--- /dev/null
@@ -0,0 +1,57 @@
+#!/usr/bin/perl
+# Test "cksum -a sm3".
+
+# Copyright (C) 2021 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/;
+    splice @$t, 1, 0, '-a sm3'
+  }
+
+my $save_temps = $ENV{DEBUG};
+my $verbose = $ENV{VERBOSE};
+
+my $prog = 'cksum';
+my $fail = run_tests ($program_name, $prog, \@Tests, $save_temps, $verbose);
+exit $fail;