commit: 644abc326a993a7727dace5a2e12b5752f09505f
parent e7d5081d54838a38b45ce5931da6f1612c08cc95
Author: Haelwenn (lanodan) Monnier <contact@hacktivis.me>
Date: Sat, 14 Sep 2024 05:23:48 +0200
cmd/sha256sum: new
Diffstat:
6 files changed, 374 insertions(+), 1 deletion(-)
diff --git a/Makefile b/Makefile
@@ -189,5 +189,8 @@ cmd/mv: cmd/mv.c lib/consent.c lib/consent.h lib/fs.c lib/fs.h Makefile config.m
cmd/sha1sum: cmd/sha1sum.c lib/sha1.c lib/sha1.h lib/bytes2hex.c lib/strconv.h Makefile
$(CC) -std=c99 $(CFLAGS) -o $@ cmd/sha1sum.c lib/sha1.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC)
+cmd/sha256sum: cmd/sha256sum.c lib/sha256.c lib/sha256.h lib/bytes2hex.c lib/strconv.h Makefile
+ $(CC) -std=c99 $(CFLAGS) -o $@ cmd/sha256sum.c lib/sha256.c lib/bytes2hex.c $(LDFLAGS) $(LDSTATIC)
+
test-cmd/pathchk-getlimits: test-cmd/pathchk-getlimits.c Makefile
$(CC) -std=c99 $(CFLAGS) -o $@ test-cmd/pathchk-getlimits.c $(LDFLAGS) $(LDSTATIC)
diff --git a/cmd/sha1sum.1 b/cmd/sha1sum.1
@@ -28,6 +28,8 @@ and finally a filename.
.El
.Sh EXIT STATUS
.Ex -std
+.Sh SEE ALSO
+.Xr sha256sum 1
.Sh STANDARDS
SHA1 is standardized both in FIPS 180-4 and RFC 3174.
.Sh AUTHORS
@@ -37,3 +39,6 @@ Due to it's known collissions SHA-1 is deprecated, the
.Nm
utility is only provided in the interest of compatibility and
verification of legacy checksums.
+For non-legacy usage,
+.Xr sha256sum 1
+should be used instead.
diff --git a/cmd/sha256sum.1 b/cmd/sha256sum.1
@@ -0,0 +1,34 @@
+.\" utils-std: Collection of commonly available Unix tools
+.\" Copyright 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+.\" SPDX-License-Identifier: MPL-2.0
+.Dd 2024-08-15
+.Dt SHA256SUM 1
+.Os
+.Sh NAME
+.Nm sha256sum
+.Nd write and verify sha256 checksums
+.Sh SYNOPSIS
+.Nm
+.Op Fl c
+.Op Ar file...
+.Sh DESCRIPTION
+.Nm
+reads each
+.Ar file ,
+or standard input if none were specified,
+and prints each of their SHA256 checksum.
+.Sh OPTIONS
+.Bl -tag -width _c
+.It Fl c
+Verify checksums contained in each
+.Ar file .
+Currently supported format being checksum, whitespace, an optional asterisk
+.Ql *
+and finally a filename.
+.El
+.Sh EXIT STATUS
+.Ex -std
+.Sh STANDARDS
+SHA256 is standardized both in FIPS 180-4 and RFC 6234.
+.Sh AUTHORS
+.An Haelwenn (lanodan) Monnier Aq Mt contact+utils@hacktivis.me
diff --git a/cmd/sha256sum.c b/cmd/sha256sum.c
@@ -0,0 +1,279 @@
+// utils-std: Collection of commonly available Unix tools
+// SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+// SPDX-License-Identifier: MPL-2.0
+
+#define _POSIX_C_SOURCE 200809L
+#include "../lib/sha256.h" // sha256_*
+#include "../lib/strconv.h" // bytes2hex
+
+#include <assert.h>
+#include <ctype.h> // isxdigit
+#include <errno.h>
+#include <fcntl.h> // open, O_*
+#include <stdbool.h>
+#include <stdio.h> // fprintf
+#include <stdlib.h> // free
+#include <string.h> // strerror
+#include <unistd.h> // read, write, close, getopt
+
+#define SHA256SUM_LEN SHA256_DIGEST_LENGTH * 2 + 1
+
+static int
+sha256sum(int fd, const char *fdname, char sum[SHA256SUM_LEN])
+{
+ struct sha256 ctx;
+
+ sha256_init(&ctx);
+
+ uint8_t buf[BUFSIZ];
+ ssize_t nread = -1;
+ while((nread = read(fd, buf, BUFSIZ)) > 0)
+ {
+ sha256_update(&ctx, buf, nread);
+ }
+ if(nread < 0)
+ {
+ fprintf(stderr,
+ "sha256sum: I/O Error while reading file '%s': %s\n",
+ fdname ? fdname : "<stdin>",
+ strerror(errno));
+ return -1;
+ }
+
+ uint8_t res[SHA256_DIGEST_LENGTH] = "";
+ sha256_sum(&ctx, res);
+
+ bytes2hex(res, SHA256_DIGEST_LENGTH, sum, SHA256SUM_LEN);
+
+ return 0;
+}
+
+#define STR(s) #s
+#define XSTR(s) STR(s)
+
+static int
+check(FILE *file, const char *filename)
+{
+ int err = 0;
+
+ ssize_t nread = -1;
+ char *line = NULL;
+ size_t len = 0;
+ errno = 0;
+ while((nread = getline(&line, &len, file)) > 0)
+ {
+ assert(errno == 0);
+ if(line[nread - 1] == '\n') line[nread - 1] = '\0';
+
+ ssize_t i = 0;
+ for(; i < nread; i++)
+ {
+ if(isxdigit(line[i])) continue;
+
+ if(line[i] == ' ')
+ {
+ line[i] = '\0';
+ break;
+ }
+
+ fprintf(stderr,
+ "sha256sum: Error: Invalid character '%c' while reading hash in line: %s\n",
+ line[i],
+ line);
+ if(len > 0) free(line);
+ return -1;
+ }
+ if(line[i++] != '\0')
+ {
+ fprintf(stderr, "sha256sum: Error: Invalid line: %s\n", line);
+ if(len > 0) free(line);
+ return -1;
+ }
+
+ if(i != SHA256SUM_LEN)
+ {
+ fprintf(stderr,
+ "sha256sum: Error: Got %zd hexadecimal digits while expected %d for a SHA256\n",
+ i,
+ SHA256SUM_LEN);
+ if(len > 0) free(line);
+ return -1;
+ }
+
+ while(i < nread && line[i] == ' ')
+ i++;
+
+ if(i < nread && line[i] == '*') i++;
+
+ char *target = line + i;
+
+ int fd = open(target, O_RDONLY | O_NOCTTY);
+ if(fd < 0)
+ {
+ fprintf(stderr, "sha256sum: Error: Failed opening file '%s': %s\n", target, strerror(errno));
+
+ if(len > 0) free(line);
+ return -1;
+ }
+
+ int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
+ if(ret != 0)
+ fprintf(stderr,
+ "sha256sum: Warning: posix_fadvise failed on file '%s': %s\n",
+ target,
+ strerror(ret));
+
+ char got[SHA256SUM_LEN] = "";
+ if(sha256sum(fd, target, got) < 0) err = 1;
+ if(memcmp(line, got, SHA256SUM_LEN) == 0)
+ {
+ printf("%s: OK\n", target);
+ }
+ else
+ {
+ err = 1;
+ printf("%s: FAILED\n", target);
+ }
+
+ if(close(fd) < 0)
+ {
+ fprintf(stderr, "sha256sum: Failed closing file '%s': %s\n", filename, strerror(errno));
+
+ if(len > 0) free(line);
+ return -1;
+ }
+ }
+ if(nread < 0 && errno != 0)
+ {
+ err = 1;
+ fprintf(
+ stderr, "sha256sum: Failed reading line from file '%s': %s\n", filename, strerror(errno));
+ }
+ if(len > 0) free(line);
+
+ return err;
+}
+
+int
+main(int argc, char *argv[])
+{
+ bool opt_c = false;
+
+ int c = -1;
+ while((c = getopt(argc, argv, "c")) != -1)
+ {
+ switch(c)
+ {
+ case 'c':
+ opt_c = true;
+ break;
+ default:
+ fprintf(stderr, "sha256sum: Unhandled option '-%c'\n", c);
+ return 1;
+ }
+ }
+
+ argc -= optind;
+ argv += optind;
+
+ if(opt_c)
+ {
+ if(argc == 0)
+ {
+ if(check(stdin, "<stdin>") != 0) return 1;
+
+ return 0;
+ }
+
+ int err = 0;
+
+ for(int i = 0; i < argc; i++)
+ {
+ FILE *file = NULL;
+ const char *filename = argv[i];
+
+ if(filename[0] == '-' && filename[1] == '\0')
+ {
+ filename = "<stdin>";
+ file = stdin;
+ }
+ else
+ {
+ file = fopen(filename, "rb");
+ if(file == NULL)
+ {
+ fprintf(stderr,
+ "sha256sum: Error: Failed opening file '%s': %s\n",
+ filename,
+ strerror(errno));
+ return 1;
+ }
+ }
+
+ if(check(file, filename) != 0) err = 1;
+
+ if(fclose(file) < 0)
+ {
+ fprintf(stderr, "sha256sum: Failed closing file '%s': %s\n", filename, strerror(errno));
+ return 1;
+ }
+ }
+
+ return err;
+ }
+
+ if(argc == 0)
+ {
+
+ char sum[SHA256SUM_LEN] = "";
+ if(sha256sum(STDIN_FILENO, NULL, sum) < 0) return 1;
+
+ puts(sum);
+
+ return 0;
+ }
+
+ for(int i = 0; i < argc; i++)
+ {
+ int fd = -1;
+ const char *filename = argv[i];
+ if(filename[0] == '-' && filename[1] == '\0')
+ {
+ filename = "<stdin>";
+ fd = STDIN_FILENO;
+ }
+ else
+ {
+ fd = open(filename, O_RDONLY | O_NOCTTY);
+ if(fd < 0)
+ {
+ fprintf(
+ stderr, "sha256sum: Error: Failed opening file '%s': %s\n", filename, strerror(errno));
+ return 1;
+ }
+
+ int ret = posix_fadvise(fd, 0, 0, POSIX_FADV_SEQUENTIAL);
+ if(ret != 0)
+ fprintf(stderr,
+ "sha256sum: Warning: posix_fadvise failed on file '%s': %s\n",
+ filename,
+ strerror(ret));
+ }
+
+ int err = 0;
+
+ char sum[SHA256SUM_LEN] = "";
+ if(sha256sum(fd, filename, sum) < 0) err = 1;
+ printf("%s %s\n", sum, filename);
+
+ if(close(fd) < 0)
+ {
+ fprintf(stderr, "sha256sum: Failed closing file '%s': %s\n", filename, strerror(errno));
+ return 1;
+ }
+
+ if(err == 1) return 1;
+ }
+
+ return 0;
+}
diff --git a/coreutils.txt b/coreutils.txt
@@ -70,7 +70,7 @@ runcon: No. (SELinux-specific util, wtf)
seq: Done
sha1sum: Done
sha224sum: No
-sha256sum: No
+sha256sum: Done
sha384sum: No
sha512sum: No
shred: ?
diff --git a/test-cmd/sha256sum.sh b/test-cmd/sha256sum.sh
@@ -0,0 +1,52 @@
+#!/bin/sh
+# SPDX-FileCopyrightText: 2017 Haelwenn (lanodan) Monnier <contact+utils@hacktivis.me>
+# SPDX-License-Identifier: MPL-2.0
+
+WD="$(dirname "$0")"
+target="${WD}/../cmd/sha256sum"
+plans=16
+. "$(dirname "$0")/tap.sh"
+
+if test "$(uname -s)" = "FreeBSD"
+then
+ skip devnull 'FreeBSD treats posix_fadvise on /dev/null as invalid'
+else
+ t devnull '/dev/null' 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /dev/null
+'
+fi
+t empty "$WD/inputs/empty" "e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 $WD/inputs/empty
+"
+t --input='' 'empty_stdin' '' 'e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855
+'
+
+t --input='abc' 'abc' '' 'ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad
+'
+
+t --exit=1 'enoent' '/var/empty/e/no/ent' "sha256sum: Error: Failed opening file '/var/empty/e/no/ent': No such file or directory
+"
+
+t --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 $WD/inputs/empty" 'check:empty' '-c' "$WD/inputs/empty: OK
+"
+t --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *$WD/inputs/empty" 'bin_check:empty' '-c' "$WD/inputs/empty: OK
+"
+t --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 *$WD/inputs/empty" 'bin_check:empty:dash' '-c -' "$WD/inputs/empty: OK
+"
+
+t --exit=1 --input="ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad $WD/inputs/empty" 'xfail_check:empty' '-c' "$WD/inputs/empty: FAILED
+"
+t --exit=1 --input="ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad *$WD/inputs/empty" 'xfail_bin_check:empty' '-c' "$WD/inputs/empty: FAILED
+"
+
+t --exit=1 --input='e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 /var/empty/e/no/ent' 'xfail_check:enoent' '-c' "sha256sum: Error: Failed opening file '/var/empty/e/no/ent': No such file or directory
+"
+t --exit=1 --input='e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855 */var/empty/e/no/ent' 'xfail_bin_check:enoent' '-c' "sha256sum: Error: Failed opening file '/var/empty/e/no/ent': No such file or directory
+"
+
+t --exit=1 --input="# $WD/inputs/empty" 'invalid_chars:#' '-c' "sha256sum: Error: Invalid character '#' while reading hash in line: # $WD/inputs/empty
+"
+t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855" 'missing_file' '-c' "sha256sum: Error: Failed opening file '': No such file or directory
+"
+t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b85 $WD/inputs/empty" 'truncated_hash' '-c' 'sha256sum: Error: Got 64 hexadecimal digits while expected 65 for a SHA256
+'
+t --exit=1 --input="e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855d $WD/inputs/empty" 'elongated_hash' '-c' 'sha256sum: Error: Got 66 hexadecimal digits while expected 65 for a SHA256
+'