summaryrefslogtreecommitdiffstats
path: root/src/basic/sha256.c
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 03:50:45 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-06-12 03:50:45 +0000
commitefeb864cb547a2cbf96dc0053a8bdb4d9190b364 (patch)
treec0b83368f18be983fcc763200c4c24d633244588 /src/basic/sha256.c
parentReleasing progress-linux version 255.5-1~progress7.99u1. (diff)
downloadsystemd-efeb864cb547a2cbf96dc0053a8bdb4d9190b364.tar.xz
systemd-efeb864cb547a2cbf96dc0053a8bdb4d9190b364.zip
Merging upstream version 256.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/basic/sha256.c')
-rw-r--r--src/basic/sha256.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/basic/sha256.c b/src/basic/sha256.c
new file mode 100644
index 0000000..f011695
--- /dev/null
+++ b/src/basic/sha256.c
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: LGPL-2.1-or-later */
+
+#include <unistd.h>
+
+#include "hexdecoct.h"
+#include "macro.h"
+#include "sha256.h"
+
+int sha256_fd(int fd, uint64_t max_size, uint8_t ret[static SHA256_DIGEST_SIZE]) {
+ struct sha256_ctx ctx;
+ uint64_t total_size = 0;
+
+ sha256_init_ctx(&ctx);
+
+ for (;;) {
+ uint8_t buffer[64 * 1024];
+ ssize_t n;
+
+ n = read(fd, buffer, sizeof(buffer));
+ if (n < 0)
+ return -errno;
+ if (n == 0)
+ break;
+
+ if (!INC_SAFE(&total_size, n) || total_size > max_size)
+ return -EFBIG;
+
+ sha256_process_bytes(buffer, n, &ctx);
+ }
+
+ sha256_finish_ctx(&ctx, ret);
+ return 0;
+}
+
+int parse_sha256(const char *s, uint8_t ret[static SHA256_DIGEST_SIZE]) {
+ _cleanup_free_ uint8_t *data = NULL;
+ size_t size = 0;
+ int r;
+
+ if (!sha256_is_valid(s))
+ return -EINVAL;
+
+ r = unhexmem_full(s, SHA256_DIGEST_SIZE * 2, false, (void**) &data, &size);
+ if (r < 0)
+ return r;
+ assert(size == SHA256_DIGEST_SIZE);
+
+ memcpy(ret, data, size);
+ return 0;
+}