Skip to content

eBPF-based security platform

hex: write a sha256: digest with one helper

John Fastabend34b90fd2dc5a
diff --git a/builtin-bpm.c b/builtin-bpm.c
index 6d23cf78..776cf2fb 100644
--- a/builtin-bpm.c
+++ b/builtin-bpm.c
@@ -2437,8 +2437,7 @@ static int tree_walk_forward(struct tree_source *src, struct tree_sync *sync,
for (size_t i = w->n; i-- > 1;) {
struct tree_publication seg = {};
- memcpy(want, "sha256:", 7);
- hex_encode(w->segments[i].manifest_digest, 32, want + 7);
+ sha256_digest_encode(w->segments[i].manifest_digest, want);
ret = tree_fetch_checked(src, want, &seg, digest,
sizeof(digest));
if (!ret && (sha256_digest_decode(digest, got) ||
diff --git a/testing/proofs/cbmc/bpm-tree-sync-model.c b/testing/proofs/cbmc/bpm-tree-sync-model.c
index 0601f890..53231fb0 100644
--- a/testing/proofs/cbmc/bpm-tree-sync-model.c
+++ b/testing/proofs/cbmc/bpm-tree-sync-model.c
@@ -410,6 +410,13 @@ void hex_encode(const __u8 *in, size_t n, char *out)
out[2 * n] = '\0';
}
+/* As util/hex.c has it, over the hex_encode() above. */
+void sha256_digest_encode(const __u8 in[32], char out[72])
+{
+ memcpy(out, "sha256:", 7);
+ hex_encode(in, 32, out + 7);
+}
+
int sha256_2(const void *a, size_t a_len, const void *b, size_t b_len,
__u8 out[CHAIN_DIGEST_LEN])
{
diff --git a/testing/unit/hex.c b/testing/unit/hex.c
index ecf13ad8..7c009345 100644
--- a/testing/unit/hex.c
+++ b/testing/unit/hex.c
@@ -2,6 +2,7 @@
/* Copyright (c) 2026 Authors of shirudo */
#include <errno.h>
+#include <string.h>
#include "hex.h"
#include "unit.h"
@@ -77,6 +78,7 @@ static void decode_max_takes_an_even_count_up_to_cap(void)
}
#define HEX64 "00112233445566778899aabbccddeeffFFEEDDCCBBAA99887766554433221100"
+#define HEX64_LOWER "00112233445566778899aabbccddeeffffeeddccbbaa99887766554433221100"
#define NOTHEX64 "g0112233445566778899aabbccddeeffFFEEDDCCBBAA99887766554433221100"
static void sha256_digest_is_prefix_and_64_digits(void)
@@ -97,6 +99,20 @@ static void sha256_digest_is_prefix_and_64_digits(void)
EXPECT_EQ(sha256_digest_decode("sha256:" NOTHEX64, out), -EINVAL);
}
+/* The digest written reads back, in lowercase whatever case it came in. */
+static void sha256_digest_encodes_in_lowercase(void)
+{
+ __u8 in[32], back[32];
+ char digest[72];
+
+ ASSERT_EQ(sha256_digest_decode("sha256:" HEX64, in), 0);
+ memset(digest, 'X', sizeof(digest));
+ sha256_digest_encode(in, digest);
+ EXPECT_STREQ(digest, "sha256:" HEX64_LOWER);
+ ASSERT_EQ(sha256_digest_decode(digest, back), 0);
+ EXPECT_EQ(memcmp(back, in, sizeof(in)), 0);
+}
+
static const struct unit_case cases[] = {
UNIT_CASE(encode_is_lower_case),
UNIT_CASE(decode_takes_either_case),
@@ -105,6 +121,7 @@ static const struct unit_case cases[] = {
UNIT_CASE(is_lower_takes_lowercase_digits_only),
UNIT_CASE(decode_max_takes_an_even_count_up_to_cap),
UNIT_CASE(sha256_digest_is_prefix_and_64_digits),
+ UNIT_CASE(sha256_digest_encodes_in_lowercase),
{}
};
diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.c
index 0f2b06fd..4a0f8e46 100644
--- a/tools/shirudo-build/bpm-publish.c
+++ b/tools/shirudo-build/bpm-publish.c
@@ -1028,10 +1028,11 @@ static int tree_segment_tag(__u64 k, char *tag, size_t tag_sz)
/* The digest of the manifest segment-<k> tags, -ENOENT without the tag. */
static int tree_segment_tag_digest(struct bpm_oci_client *c,
const char *repository, __u64 k,
- char *digest, size_t digest_sz)
+ char digest[72])
{
struct bpm_buffer manifest = {};
- char tag[32], url[PATH_MAX], hex[65];
+ __u8 sum[CRYPTO_SHA256_LEN];
+ char tag[32], url[PATH_MAX];
int ret;
ret = tree_segment_tag(k, tag, sizeof(tag));
@@ -1040,11 +1041,10 @@ static int tree_segment_tag_digest(struct bpm_oci_client *c,
sizeof(url));
if (!ret)
ret = bpm_oci_get_memory(c, url, BPM_MANIFEST_MEDIA, &manifest);
- if (!ret && bpm_sha256_hex(manifest.data, manifest.len, hex,
- sizeof(hex)))
+ if (!ret && sha256_buffer(manifest.data, manifest.len, sum))
ret = -EINVAL;
- if (!ret && strsprintf(digest, digest_sz, "sha256:%s", hex) < 0)
- ret = -ENAMETOOLONG;
+ if (!ret)
+ sha256_digest_encode(sum, digest);
bpm_buffer_free(&manifest);
return ret;
}
@@ -1066,8 +1066,7 @@ static int tree_next_segment_tag(struct bpm_oci_client *c,
while (doubling || absent - present > 1) {
probe = doubling ? absent : present + (absent - present) / 2;
- ret = tree_segment_tag_digest(c, repository, probe, digest,
- sizeof(digest));
+ ret = tree_segment_tag_digest(c, repository, probe, digest);
if (ret == -ENOENT) {
absent = probe;
doubling = false;
diff --git a/tools/shirudo-build/bpm-show.c b/tools/shirudo-build/bpm-show.c
index 5e638ca2..17875497 100644
--- a/tools/shirudo-build/bpm-show.c
+++ b/tools/shirudo-build/bpm-show.c
@@ -185,14 +185,12 @@ static int tree_show_sealed(struct bpm_oci_client *c, const char *repository,
const struct trusted_set *trusted,
const __u8 manifest_digest[32])
{
- char hex[65], reference[BPM_MAX_DIGEST], digest[BPM_MAX_DIGEST];
+ char reference[BPM_MAX_DIGEST], digest[BPM_MAX_DIGEST];
struct tree_publication pub = {};
struct bpm_buffer document = {};
int ret;
- hex_encode(manifest_digest, 32, hex);
- if (strsprintf(reference, sizeof(reference), "sha256:%s", hex) < 0)
- return -E2BIG;
+ sha256_digest_encode(manifest_digest, reference);
ret = build_tree_fetch_checked(c, repository, reference, NULL, trusted,
&document, &pub, digest,
sizeof(digest));
diff --git a/util/hex.c b/util/hex.c
index 5ae407f4..5759b0ec 100644
--- a/util/hex.c
+++ b/util/hex.c
@@ -89,3 +89,9 @@ int sha256_digest_decode(const char *digest, __u8 out[32])
return -EINVAL;
return hex_decode(digest + 7, out, 32);
}
+
+void sha256_digest_encode(const __u8 in[32], char out[72])
+{
+ memcpy(out, "sha256:", 7);
+ hex_encode(in, 32, out + 7);
+}
diff --git a/util/hex.h b/util/hex.h
index e776b9c8..dda54094 100644
--- a/util/hex.h
+++ b/util/hex.h
@@ -36,8 +36,10 @@ int hex_decode_max(const char *hex, __u8 *out, size_t cap, size_t *len);
/*
* An OCI digest, "sha256:" and 64 hex digits of either case, into its 32
- * bytes. 0 or -EINVAL.
+ * bytes. 0 or -EINVAL. sha256_digest_encode() writes the 32 bytes at in
+ * as one, in lowercase and terminated.
*/
int sha256_digest_decode(const char *digest, __u8 out[32]);
+void sha256_digest_encode(const __u8 in[32], char out[72]);
#endif /* SHIRUDO_HEX_H */