[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[PATCH 08/12] digest: add support for sm3
From: |
Pádraig Brady |
Subject: |
[PATCH 08/12] digest: add support for sm3 |
Date: |
Sun, 12 Sep 2021 19:14:02 +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 | 2 ++
THANKS.in | 1 +
bootstrap.conf | 1 +
doc/coreutils.texi | 1 +
src/digest.c | 20 ++++++++++++----
tests/local.mk | 1 +
tests/misc/sm3sum.pl | 57 ++++++++++++++++++++++++++++++++++++++++++++
7 files changed, 79 insertions(+), 4 deletions(-)
create mode 100755 tests/misc/sm3sum.pl
diff --git a/NEWS b/NEWS
index 1fb9e341a..8936ccbe0 100644
--- 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,
diff --git a/THANKS.in b/THANKS.in
index 0424c22fb..edf93d787 100644
--- 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
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 a6db6f55b..578273746 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 86ef89257..07cc7c921 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 cksum_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
- [PATCH V2] Add support for cksum --algorithm [sm3], Pádraig Brady, 2021/09/12
- [PATCH 01/12] maint: rename md5sum.c to more general digest.c, Pádraig Brady, 2021/09/12
- [PATCH 02/12] digest: add LENGTH parameter to digest to support cksum, Pádraig Brady, 2021/09/12
- [PATCH 05/12] cksum: add --debug to --help output and man page, Pádraig Brady, 2021/09/12
- [PATCH 03/12] digest: refactor sum(1) into digest.c, Pádraig Brady, 2021/09/12
- [PATCH 04/12] sum: handle EOVERFLOW for too large inputs, Pádraig Brady, 2021/09/12
- [PATCH 09/12] maint: simplify b2sum to only handle BLAKE2b, Pádraig Brady, 2021/09/12
- [PATCH 08/12] digest: add support for sm3,
Pádraig Brady <=
- [PATCH 06/12] digest: refactor cksum(1) into digest.c, Pádraig Brady, 2021/09/12
- [PATCH 07/12] cksum: add --algorithm option to select digest mode, Pádraig Brady, 2021/09/12
- [PATCH 12/12] cksum: support --zero in default mode, Pádraig Brady, 2021/09/12
- [PATCH 10/12] cksum: support digest detection for tagged format, Pádraig Brady, 2021/09/12
- [PATCH 11/12] digest: support -length specifiers on all digest tags, Pádraig Brady, 2021/09/12
- Re: [PATCH V2] Add support for cksum --algorithm [sm3], Pádraig Brady, 2021/09/12