coreutils
[Top][All Lists]
Advanced

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

[PATCH 8/8] digest: add support for sm3


From: Pádraig Brady
Subject: [PATCH 8/8] digest: add support for sm3
Date: Tue, 7 Sep 2021 16:45:31 +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.
* tests/local.mk: Reference the new test.
---
 bootstrap.conf       |  1 +
 doc/coreutils.texi   |  1 +
 src/digest.c         | 20 ++++++++++++----
 tests/local.mk       |  1 +
 tests/misc/sm3sum.pl | 57 ++++++++++++++++++++++++++++++++++++++++++++
 5 files changed, 76 insertions(+), 4 deletions(-)
 create mode 100755 tests/misc/sm3sum.pl

diff --git a/bootstrap.conf b/bootstrap.conf
index e7ed7e5ff..481a37e9c 100644
--- a/bootstrap.conf
+++ b/bootstrap.conf
@@ -60,6 +60,7 @@ gnulib_modules="
   crypto/sha1
   crypto/sha256
   crypto/sha512
+  crypto/sm3
   cycle-check
   d-ino
   d-type
diff --git a/doc/coreutils.texi b/doc/coreutils.texi
index 39eea77b0..50fc3dea7 100644
--- a/doc/coreutils.texi
+++ b/doc/coreutils.texi
@@ -3976,6 +3976,7 @@ Supported algorithms are:
 @samp{sha384}    (equivalent to sha384sum)
 @samp{sha512}    (equivalent to sha512sum)
 @samp{blake2b}   (equivalent to b2sum)
+@samp{sm3}       (only available through cksum)
 @end example
 @end table
 
diff --git a/src/digest.c b/src/digest.c
index fd4a963a4..7e82166c2 100644
--- a/src/digest.c
+++ b/src/digest.c
@@ -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 debug;
 #endif
@@ -496,6 +507,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
diff --git a/tests/local.mk b/tests/local.mk
index 3ddd6f1bb..192c0d31c 100644
--- a/tests/local.mk
+++ b/tests/local.mk
@@ -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
index 000000000..667dac52e
--- /dev/null
+++ b/tests/misc/sm3sum.pl
@@ -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;
-- 
2.26.2




reply via email to

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