digest: add support for sm3

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>
This commit is contained in:
Pádraig Brady 2021-09-07 15:45:01 +01:00
parent ad6c8e1181
commit 8362f2552a
7 changed files with 79 additions and 4 deletions

2
NEWS
View File

@ -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,

View File

@ -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

View File

@ -60,6 +60,7 @@ gnulib_modules="
crypto/sha1
crypto/sha256
crypto/sha512
crypto/sm3
cycle-check
d-ino
d-type

View 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

View 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

View 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 \

57
tests/misc/sm3sum.pl Executable file
View File

@ -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;