eBPF-based security platform
bpm: write every sha256: digest with sha256_digest_encode()
Daniel Borkmann585e93401b2b
diff --git a/testing/unit/bpm_oci.c b/testing/unit/bpm_oci.cindex 8fb1c18c..b5bfa363 100644--- a/testing/unit/bpm_oci.c+++ b/testing/unit/bpm_oci.c@@ -2,6 +2,7 @@/* Copyright (c) 2026 Authors of shirudo */#include <errno.h>+#include <string.h>#include <shirudo/string.h>@@ -50,8 +51,23 @@ static void manifest_layer_is_a_whole_descriptor(void)-EINVAL);}+/* SHA-256 of "abc", FIPS 180-2 appendix B.1. */+#define ABC_DIGEST \+ "sha256:ba7816bf8f01cfea414140de5dae2223b00361a396177a9cb410ff61f20015ad"++static void digest_names_a_blob_as_a_registry_does(void)+{+ char out[80];++ memset(out, 'x', sizeof(out));+ ASSERT_EQ(bpm_sha256_digest("abc", 3, out, 72), 0);+ EXPECT_STREQ(out, ABC_DIGEST);+ EXPECT_EQ(bpm_sha256_digest("abc", 3, out, 71), -ENAMETOOLONG);+}+static const struct unit_case cases[] = {UNIT_CASE(manifest_layer_is_a_whole_descriptor),+ UNIT_CASE(digest_names_a_blob_as_a_registry_does),{}};diff --git a/tools/shirudo-build/bpm-publish.c b/tools/shirudo-build/bpm-publish.cindex 4a0f8e46..fc5ba6a3 100644--- a/tools/shirudo-build/bpm-publish.c+++ b/tools/shirudo-build/bpm-publish.c@@ -358,9 +358,10 @@ static int push_package(const char *package_path, const char *raw_repository,struct json_emit e = {};__u8 *archive = NULL, *manifest = NULL;const char *base;- char repository[BPM_MAX_ENDPOINT], package_digest[72], key_digest[72];+ char repository[BPM_MAX_ENDPOINT];+ char package_digest[SHA256_DIGEST_STR_SIZE];+ char key_digest[SHA256_DIGEST_STR_SIZE];char index_repository[BPM_MAX_ENDPOINT] = {};- char hex[65];char reference[PATH_MAX];char config_body[] = "{\"mediaType\":\"" BPM_CONFIG_MEDIA "\"}";char empty_config[] = "{}";@@ -439,8 +440,8 @@ static int push_package(const char *package_path, const char *raw_repository,}manifest_len = e.len;manifest = (__u8 *)manifest_body;- if (bpm_sha256_hex(manifest, manifest_len, hex, sizeof(hex)) ||- strsprintf(package_digest, sizeof(package_digest), "sha256:%s", hex) < 0) {+ if (bpm_sha256_digest(manifest, manifest_len, package_digest,+ sizeof(package_digest))) {ret = -EINVAL;goto out;}@@ -496,9 +497,8 @@ static int push_package(const char *package_path, const char *raw_repository,goto out;}key_manifest_len = e.len;- if (bpm_sha256_hex(key_manifest_body, key_manifest_len, hex,- sizeof(hex)) ||- strsprintf(key_digest, sizeof(key_digest), "sha256:%s", hex) < 0) {+ if (bpm_sha256_digest(key_manifest_body, key_manifest_len, key_digest,+ sizeof(key_digest))) {ret = -EINVAL;goto out;}@@ -559,10 +559,7 @@ struct tree_upload {static int tree_describe_blob(const void *body, size_t len, const char *media,struct bpm_descriptor *out){- char hex[65];-- if (bpm_sha256_hex(body, len, hex, sizeof(hex)) ||- strsprintf(out->digest, sizeof(out->digest), "sha256:%s", hex) < 0)+ if (bpm_sha256_digest(body, len, out->digest, sizeof(out->digest)))return -EINVAL;bpm_copy_string(out->media, sizeof(out->media), media);out->size = (__s64)len;@@ -694,8 +691,9 @@ static int tree_describe_artifact(const char *raw_repository,char name[UPATCH_NAME_MAX + 1], title[UPATCH_NAME_MAX + 16];char temporary[] = "/tmp/shirudo-bpm-tree-XXXXXX";struct bpm_descriptor layer = {}, config = {};- char hex[65], manifest_hex[65];+ __u8 payload[UPATCH_PAYLOAD_HASH_LEN];struct json_emit e;+ char hex[65];struct upatch u;int fd, ret;@@ -712,7 +710,8 @@ static int tree_describe_artifact(const char *raw_repository,return ret;}upatch_header_name(&u, name);- hex_encode(u.hdr.payload_sha256, 32, hex);+ memcpy(payload, u.hdr.payload_sha256, sizeof(payload));+ hex_encode(payload, sizeof(payload), hex);if (strcmp(name, id) || upatch_is_unload(&u) != unload)ret = -EKEYREJECTED;upatch_close(&u);@@ -752,13 +751,11 @@ static int tree_describe_artifact(const char *raw_repository,if (e.err)return e.err;up->manifest_len = e.len;- if (bpm_sha256_hex(up->manifest, e.len, manifest_hex,- sizeof(manifest_hex)) ||- strsprintf(out->manifest_digest, sizeof(out->manifest_digest),- "sha256:%s", manifest_hex) < 0 ||- strsprintf(out->payload_sha256, sizeof(out->payload_sha256),- "sha256:%s", hex) < 0)- return -E2BIG;+ ret = bpm_sha256_digest(up->manifest, e.len, out->manifest_digest,+ sizeof(out->manifest_digest));+ if (ret)+ return ret;+ sha256_digest_encode(payload, out->payload_sha256);bpm_copy_string(out->repository, sizeof(out->repository), raw_repository);bpm_copy_string(out->layer_digest, sizeof(out->layer_digest), layer.digest);return 0;diff --git a/util/bpm_oci.c b/util/bpm_oci.cindex a7f072a9..30de7a23 100644--- a/util/bpm_oci.c+++ b/util/bpm_oci.c@@ -1189,6 +1189,18 @@ int bpm_sha256_hex(const void *data, size_t len,return 0;}+int bpm_sha256_digest(const void *data, size_t len, char *out, size_t out_sz)+{+ __u8 digest[CRYPTO_SHA256_LEN];++ if (out_sz < SHA256_DIGEST_STR_SIZE)+ return -ENAMETOOLONG;+ if (sha256_buffer(data, len, digest))+ return -EINVAL;+ sha256_digest_encode(digest, out);+ return 0;+}+bool bpm_digest_matches(const char *want, const char *hex){return want && !strncmp(want, "sha256:", 7) &&@@ -1220,16 +1232,15 @@ int bpm_oci_push_blob(struct bpm_oci_client *c, const char *repository,const void *body, size_t body_len, const char *media,struct bpm_descriptor *out){+ char digest[SHA256_DIGEST_STR_SIZE], url[PATH_MAX], upload[PATH_MAX];struct bpm_http_response r = {};- char hex[65], digest[72], url[PATH_MAX], upload[PATH_MAX];char location[PATH_MAX];const char *sep;int ret;- if (bpm_sha256_hex(body, body_len, hex, sizeof(hex)))- return -EINVAL;- if (strsprintf(digest, sizeof(digest), "sha256:%s", hex) < 0)- return -ENAMETOOLONG;+ ret = bpm_sha256_digest(body, body_len, digest, sizeof(digest));+ if (ret)+ return ret;ret = bpm_oci_url(c, repository, "blobs", digest, url, sizeof(url));if (ret)return ret;@@ -1302,12 +1313,12 @@ int bpm_oci_put_manifest(struct bpm_oci_client *c, const char *repository,size_t body_len, char *digest, size_t digest_sz){struct bpm_http_response r = {};- char hex[65], url[PATH_MAX];+ char url[PATH_MAX];int ret;- if (bpm_sha256_hex(body, body_len, hex, sizeof(hex)) ||- strsprintf(digest, digest_sz, "sha256:%s", hex) < 0)- return -ENAMETOOLONG;+ ret = bpm_sha256_digest(body, body_len, digest, digest_sz);+ if (ret)+ return ret;ret = bpm_oci_url(c, repository, "manifests", reference, url,sizeof(url));if (ret)diff --git a/util/bpm_oci.h b/util/bpm_oci.hindex 1b6c020b..9bc4dc8c 100644--- a/util/bpm_oci.h+++ b/util/bpm_oci.h@@ -93,6 +93,9 @@ bool bpm_name_ok(const char *s);bool bpm_version_ok(const char *s);bool bpm_package_name_ok(const char *s);int bpm_sha256_hex(const void *data, size_t len, char *hex, size_t hex_sz);+/* "sha256:<hex>" of the data, the form a registry names a blob by. */+int bpm_sha256_digest(const void *data, size_t len, char *out,+ size_t out_sz) __must_check;/* want is "sha256:<hex>" or bare hex; hex is the computed digest. */bool bpm_digest_matches(const char *want, const char *hex);void bpm_buffer_free(struct bpm_buffer *b);diff --git a/util/bpm_package.c b/util/bpm_package.cindex c1915898..d8f3eb95 100644--- a/util/bpm_package.c+++ b/util/bpm_package.c@@ -901,7 +901,7 @@ int bpm_tree_fetch_document(struct bpm_oci_client *c,{struct bpm_buffer manifest = {}, sig = {};struct bpm_descriptor *layers = NULL;- char url[PATH_MAX], hex[65];+ char url[PATH_MAX];int nl = 0, ret;if (!reference[0] || strchr(reference, '/') || strchr(reference, '?') ||@@ -914,14 +914,10 @@ int bpm_tree_fetch_document(struct bpm_oci_client *c,ret = bpm_oci_get_memory(c, url, BPM_MANIFEST_MEDIA, &manifest);if (ret)goto out;- if (bpm_sha256_hex(manifest.data, manifest.len, hex, sizeof(hex))) {- ret = -EKEYREJECTED;- goto out;- }- if (strsprintf(manifest_digest, digest_sz, "sha256:%s", hex) < 0) {- ret = -ENAMETOOLONG;+ ret = bpm_sha256_digest(manifest.data, manifest.len, manifest_digest,+ digest_sz);+ if (ret)goto out;- }layers = xcalloc(BPM_MAX_ENTRIES, sizeof(*layers));if (!layers) {ret = -ENOMEM;diff --git a/util/bpm_tree.c b/util/bpm_tree.cindex fe163aed..086a0ef2 100644--- a/util/bpm_tree.c+++ b/util/bpm_tree.c@@ -537,8 +537,8 @@ int tree_operation_digest(const struct tree_operation *op,{size_t body_cap = 1024u * 1024u;struct json_emit e;- char *body, hex[65];__u8 digest[32];+ char *body;int ret;body = xmalloc(body_cap);@@ -552,11 +552,8 @@ int tree_operation_digest(const struct tree_operation *op,ret = e.err;if (!ret && sha256_buffer(body, e.len, digest))ret = -EIO;- if (!ret) {- hex_encode(digest, sizeof(digest), hex);- memcpy(out, "sha256:", 7);- memcpy(out + 7, hex, sizeof(hex));- }+ if (!ret)+ sha256_digest_encode(digest, out);xfree(body, body_cap);return ret;}diff --git a/util/hex.c b/util/hex.cindex 5759b0ec..0ddd4bf7 100644--- a/util/hex.c+++ b/util/hex.c@@ -90,7 +90,8 @@ int sha256_digest_decode(const char *digest, __u8 out[32])return hex_decode(digest + 7, out, 32);}-void sha256_digest_encode(const __u8 in[32], char out[72])+void sha256_digest_encode(const __u8 in[32],+ char out[SHA256_DIGEST_STR_SIZE]){memcpy(out, "sha256:", 7);hex_encode(in, 32, out + 7);diff --git a/util/hex.h b/util/hex.hindex dda54094..cd676ba1 100644--- a/util/hex.h+++ b/util/hex.h@@ -39,7 +39,9 @@ int hex_decode_max(const char *hex, __u8 *out, size_t cap, size_t *len);* bytes. 0 or -EINVAL. sha256_digest_encode() writes the 32 bytes at in* as one, in lowercase and terminated.*/+#define SHA256_DIGEST_STR_SIZE 72int sha256_digest_decode(const char *digest, __u8 out[32]);-void sha256_digest_encode(const __u8 in[32], char out[72]);+void sha256_digest_encode(const __u8 in[32],+ char out[SHA256_DIGEST_STR_SIZE]);#endif /* SHIRUDO_HEX_H */